From b83099236eac41cc8e4985f072e53bc6cf0386fa Mon Sep 17 00:00:00 2001 From: Fernando-A-Rocha Date: Mon, 2 Sep 2024 14:17:18 +0100 Subject: [PATCH] add getCustomModelFromElement --- newmodels_reborn/scripts/core/client_logic.lua | 14 +++++++------- newmodels_reborn/scripts/core/shared_exported.lua | 8 ++++++-- .../scripts/optional/debug/c_debug.lua | 2 +- .../scripts/optional/debug/s_debug.lua | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/newmodels_reborn/scripts/core/client_logic.lua b/newmodels_reborn/scripts/core/client_logic.lua index f9a8263..6c90bad 100644 --- a/newmodels_reborn/scripts/core/client_logic.lua +++ b/newmodels_reborn/scripts/core/client_logic.lua @@ -8,7 +8,7 @@ local currFreeIdDelay = 9500 -- ms local FREE_ID_DELAY_STEP = 500 -- ms local function applyElementCustomModel(element) - local customModel = tonumber(getElementData(element, getCustomModelDataKey(element))) + local customModel = getCustomModelFromElement(element) if not customModel then return end local loadedModel = loadedModels[customModel] if not loadedModel then return end @@ -142,8 +142,8 @@ end local function isCustomModelInUse(customModel, loadedModel) if loadedModel then for _, elementType in pairs(loadedModel.elementTypes) do - for _, v in pairs(getElementsByType(elementType, root, true)) do - if tonumber(getElementData(v, getCustomModelDataKey(elementType))) == customModel then + for _, element in pairs(getElementsByType(elementType, root, true)) do + if getCustomModelFromElement(element) == customModel then return true end end @@ -213,7 +213,7 @@ local function freeAllocatedModelIfUnused(customModel) end local function setElementCustomModel(element) - local customModel = tonumber(getElementData(element, getCustomModelDataKey(element))) + local customModel = getCustomModelFromElement(element) if not customModel then return end if not loadedModels[customModel] then loadCustomModel(customModel, element) @@ -224,7 +224,7 @@ end addEventHandler("onClientElementDataChange", root, function(key, prevCustomModel, newCustomModel) if not isValidElement(source) then return end - if key ~= getCustomModelDataKey(source) then return end + if key ~= getCustomModelFromElement(source) then return end prevCustomModel = tonumber(prevCustomModel) -- Get the base model of the previous custom model the element has @@ -255,14 +255,14 @@ end) addEventHandler("onClientElementStreamOut", root, function() if not isValidElement(source) then return end - local customModel = tonumber(getElementData(source, getCustomModelDataKey(source))) + local customModel = getCustomModelFromElement(source) if not customModel then return end freeAllocatedModelIfUnused(customModel) end) addEventHandler("onClientElementDestroy", root, function() if not isValidElement(source) then return end - local customModel = tonumber(getElementData(source, getCustomModelDataKey(source))) + local customModel = getCustomModelFromElement(source) if not customModel then return end freeAllocatedModelIfUnused(customModel) end) diff --git a/newmodels_reborn/scripts/core/shared_exported.lua b/newmodels_reborn/scripts/core/shared_exported.lua index ef4cf1b..c681289 100644 --- a/newmodels_reborn/scripts/core/shared_exported.lua +++ b/newmodels_reborn/scripts/core/shared_exported.lua @@ -20,6 +20,10 @@ setPickupTypeMTA = setPickupType getElementModelMTA = getElementModel setElementModelMTA = setElementModel +function getCustomModelFromElement(element) + return tonumber(getElementData(element, getCustomModelDataKey(element))) +end + function getSharedCustomModelsTable() if IS_IMPORTED then return exports["newmodels_reborn"]:getCustomModels() @@ -215,14 +219,14 @@ end function getElementModel(element) assert(isElement(element), "Invalid element passed: " .. tostring(element)) assert(isValidElement(element), "Invalid element type passed: " .. getElementType(element)) - return tonumber(getElementData(element, getCustomModelDataKey(element))) or getElementModelMTA(element) + return getCustomModelFromElement(element) or getElementModelMTA(element) end function getElementBaseModel(element) if not isClientsideScript then return getElementModelMTA(element) end - local customModel = tonumber(getElementData(element, getCustomModelDataKey(element))) + local customModel = getCustomModelFromElement(element) if not customModel then return getElementModelMTA(element) end diff --git a/newmodels_reborn/scripts/optional/debug/c_debug.lua b/newmodels_reborn/scripts/optional/debug/c_debug.lua index baf5412..0128a15 100644 --- a/newmodels_reborn/scripts/optional/debug/c_debug.lua +++ b/newmodels_reborn/scripts/optional/debug/c_debug.lua @@ -43,7 +43,7 @@ local function pairsByKeys(t) end local function getElementCustomModelString(element) - local customModel = tonumber(getElementData(element, getCustomModelDataKey(element))) + local customModel = getCustomModelFromElement(element) if customModel and customModels[customModel] then local name, baseModel = customModels[customModel].name, customModels[customModel].baseModel return {("%d \"%s\" (%d)"):format(customModel, name, baseModel), 0xffffa263} diff --git a/newmodels_reborn/scripts/optional/debug/s_debug.lua b/newmodels_reborn/scripts/optional/debug/s_debug.lua index a5f0457..c7df276 100644 --- a/newmodels_reborn/scripts/optional/debug/s_debug.lua +++ b/newmodels_reborn/scripts/optional/debug/s_debug.lua @@ -64,7 +64,7 @@ addCommandHandler("testspawn", function(thePlayer) local dataName = getCustomModelDataKey("player") local x,y,z = getElementPosition(thePlayer) local _, _, rz = getElementRotation(thePlayer) - local customModel = tonumber(getElementData(thePlayer, dataName)) + local customModel = getCustomModelFromElement(thePlayer) spawnPlayer(thePlayer, x,y,z, rz, getElementModelMTA(thePlayer), getElementInterior(thePlayer), getElementDimension(thePlayer)) setElementData(thePlayer, dataName, nil) if customModel then