From 823279816cfcf023453cc857c541e52b6c7c58ae Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Tue, 29 Mar 2022 21:06:17 -0500 Subject: [PATCH] More NPC properties and some fixes --- scripts/server/npc.js | 59 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/scripts/server/npc.js b/scripts/server/npc.js index daa2301e..20050614 100644 --- a/scripts/server/npc.js +++ b/scripts/server/npc.js @@ -17,12 +17,12 @@ function initNPCScript() { // =========================================================================== /** - * @param {Ped} ped - The data index of the NPC + * @param {Number} npcId - The data index of the NPC * @return {NPCData} The NPC's data (class instancee) */ -function getNPCData(ped) { - if(ped.getData("vrr.dataIndex")) { - return ped.getData("vrr.dataIndex"); +function getNPCData(npcId) { + if(typeof getServerData().npcs[npcId] != "undefined") { + return getServerData().npcs[npcId]; } return false; } @@ -52,6 +52,7 @@ function createNPCCommand(client, command, params) { let npcIndex = getServerData().npcs.push(tempNPCData); spawnNPC(npcIndex-1); + setAllNPCDataIndexes(); } // =========================================================================== @@ -196,9 +197,17 @@ function saveNPCToDatabase(npcDataId) { } } + let safeAnimationName = escapeDatabaseString(tempNPCData.animationName); + let safeFirstName = escapeDatabaseString(tempNPCData.firstName); + let safeLastName = escapeDatabaseString(tempNPCData.lastName); + let safeMiddleName = escapeDatabaseString(tempNPCData.middleName); + let data = [ ["npc_server", getServerId()], ["npc_skin", toInteger(tempNPCData.model)], + ["npc_name_first", safeFirstName], + ["npc_name_middle", safeMiddleName], + ["npc_name_last", safeLastName], ["npc_owner_type", toInteger(tempNPCData.ownerType)], ["npc_owner_id", toInteger(tempNPCData.ownerId)], ["npc_pos_x", toFloat(tempNPCData.position.x)], @@ -208,6 +217,14 @@ function saveNPCToDatabase(npcDataId) { ["npc_scale_x", toFloat(tempNPCData.scale.x)], ["npc_scale_y", toFloat(tempNPCData.scale.y)], ["npc_scale_z", toFloat(tempNPCData.scale.z)], + ["npc_animation", safeAnimationName], + ["npc_health", toInteger(tempNPCData.health)], + ["npc_armour", toInteger(tempNPCData.armour)], + ["npc_invincible", boolToInt(tempNPCData.invincible)], + ["npc_heedthreats", boolToInt(tempNPCData.heedThreats)], + ["npc_threats", toInteger(tempNPCData.threats)], + ["npc_stay", boolToInt(tempNPCData.stay)], + ["npc_type_flags", toInteger(tempNPCData.typeFlags)], ]; let dbQuery = null; @@ -272,4 +289,36 @@ function spawnAllNPCs() { } } -// =========================================================================== \ No newline at end of file +// =========================================================================== + +function deleteNPCCommand(command, params, client) { + if(areParamsEmpty(params)) { + messagePlayerSyntax(client, command); + return false; + } + + let closestNPC = getClosestNPC(client); + + if(!getNPCData(closestNPC)) { + messagePlayerError(client, getLocaleString(client, "InvalidNPC")); + return false; + } + + let npcName = getNPCData(closestNPC).name; + + deleteNPC(closestNPC); + messageAdmins(`${client.name} deleted NPC ${npcName}`) +} + +function deleteNPC(npcId) { + quickDatabaseQuery(`DELETE FROM npc_main WHERE npc_id=${getNPCData(npcId).databaseId}`); + + if(getNPCData(npcId)) { + if(getNPCData(npcId).ped != false) { + deleteEntity(getNPCData(npcId).ped); + } + delete getServerData().npcs[npcId]; + } + + setAllNPCDataIndexes(); +} \ No newline at end of file