Skip to content

Commit

Permalink
feat: support non-networked dumpsters (#1875)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scullyy authored Jan 30, 2025
1 parent 1f0e10a commit b5b6912
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ shared = {
playerweight = GetConvarInt('inventory:weight', 30000),
target = GetConvarInt('inventory:target', 0) == 1,
police = json.decode(GetConvar('inventory:police', '["police", "sheriff"]')),
networkdumpsters = GetConvarInt('inventory:networkdumpsters', 0) == 1
}

shared.dropslots = GetConvarInt('inventory:dropslots', shared.playerslots)
Expand Down
22 changes: 22 additions & 0 deletions modules/inventory/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@ local Inventory = {}

Inventory.Dumpsters = {218085040, 666561306, -58485588, -206690185, 1511880420, 682791951}

if shared.networkdumpsters then
-- Make sure dumpsters are frozen to ensure persistent position across clients
SetInterval(function()
local objects = GetGamePool('CObject')

for i = 1, #objects do
local object = objects[i]
local model = GetEntityModel(object)

if lib.table.contains(Inventory.Dumpsters, model) and not IsEntityPositionFrozen(object) then
FreezeEntityPosition(object, true)
end
end
end, 3000)
end

function Inventory.OpenDumpster(entity)
if shared.networkdumpsters then
local coords = GetEntityCoords(entity)
client.openInventory('dumpster', coords)
return
end

local netId = NetworkGetEntityIsNetworked(entity) and NetworkGetNetworkIdFromEntity(entity)

if not netId then
Expand Down
44 changes: 36 additions & 8 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ end
exports('setPlayerInventory', server.setPlayerInventory)
AddEventHandler('ox_inventory:setPlayerInventory', server.setPlayerInventory)

local registeredDumpsters = {}

---@param coords vector3
---@return string?
local function getDumpsterFromCoords(coords)
local found

for i = 1, #registeredDumpsters do
local distance = #(coords - registeredDumpsters[i])

if distance < 0.1 then
found = i
break
end
end

return found
end

---@param playerPed number
---@param stash OxInventory
---@return vector3?
Expand Down Expand Up @@ -178,16 +197,25 @@ local function openInventory(source, invType, data, ignoreSecurityChecks)
right = Inventory(('evidence-%s'):format(data))
end
elseif invType == 'dumpster' then
---@cast data string
right = Inventory(data)
if shared.networkdumpsters then
local dumpsterId = getDumpsterFromCoords(data)
right = dumpsterId and Inventory(('dumpster-%s'):format(dumpsterId))

if not right then
local netid = tonumber(data:sub(9))
if not right then
dumpsterId = #registeredDumpsters + 1
right = Inventory.Create(('dumpster-%s'):format(dumpsterId), locale('dumpster'), invType, 15, 0, 100000, false)
registeredDumpsters[dumpsterId] = data
end
else
---@cast data string
right = Inventory(data)

-- dumpsters do not work with entity lockdown. need to rewrite, but having to do
-- distance checks to some ~7000 dumpsters and freeze the entities isn't ideal
if netid and NetworkGetEntityFromNetworkId(netid) > 0 then
right = Inventory.Create(data, locale('dumpster'), invType, 15, 0, 100000, false)
if not right then
local netid = tonumber(data:sub(9))

if netid and NetworkGetEntityFromNetworkId(netid) > 0 then
right = Inventory.Create(data, locale('dumpster'), invType, 15, 0, 100000, false)
end
end
end
elseif invType == 'container' then
Expand Down

0 comments on commit b5b6912

Please sign in to comment.