Skip to content

Commit

Permalink
add getCustomModelFromElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Sep 2, 2024
1 parent 600b26c commit b830992
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
14 changes: 7 additions & 7 deletions newmodels_reborn/scripts/core/client_logic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions newmodels_reborn/scripts/core/shared_exported.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion newmodels_reborn/scripts/optional/debug/c_debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion newmodels_reborn/scripts/optional/debug/s_debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b830992

Please sign in to comment.