Skip to content

Commit

Permalink
Merge pull request #582 from overextended/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat authored Mar 2, 2022
2 parents 2515b55 + 6a67dc0 commit 9e6d51d
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 253 deletions.
166 changes: 71 additions & 95 deletions client.lua

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion data/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ return {
label = 'Test Burger',
weight = 220,
degrade = 60,
limit = 3,
client = {
status = { hunger = 200000 },
anim = 'eating',
Expand Down
7 changes: 4 additions & 3 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ game 'gta5'
--[[ Resource Information ]]--
name 'ox_inventory'
author 'Overextended'
version '2.4.5'
version '2.5.0'
repository 'https://github.com/overextended/ox_inventory'
description 'Slot-based inventory with metadata'

Expand Down Expand Up @@ -41,15 +41,16 @@ client_scripts {

server_scripts {
'@oxmysql/lib/MySQL.lua',
'modules/mysql/server.lua',
'modules/logs/server.lua',
'modules/items/server.lua',
'modules/inventory/server.lua',
'modules/shops/server.lua',
'server.lua',
-- 'setup/convert.lua',
'setup/dbcleanup.lua',
}

--server_script 'setup/convert.lua'

ui_page 'web/build/index.html'

files {
Expand Down
12 changes: 1 addition & 11 deletions modules/bridge/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ function server.setPlayerData(player)
}
end

if shared.framework == 'ox' then
function server.getInventory(identifier)
local inventory = MySQL.prepare.await('SELECT inventory FROM characters WHERE charid = ?', { identifier })
return inventory and json.decode(inventory)
end
elseif shared.framework == 'esx' then
if shared.framework == 'esx' then
local ESX = exports['es_extended']:getSharedObject()

if ESX.CreatePickup then
Expand Down Expand Up @@ -68,11 +63,6 @@ elseif shared.framework == 'esx' then
}
end

function server.getInventory(identifier)
local inventory = MySQL.prepare.await('SELECT inventory FROM users WHERE identifier = ?', { identifier })
return inventory and json.decode(inventory)
end

RegisterServerEvent('ox_inventory:requestPlayerInventory', function()
local source = source
local player = server.GetPlayerFromId(source)
Expand Down
8 changes: 4 additions & 4 deletions modules/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ function data(name)
return func()
end

if not SetInterval or not import then
if not lib then
error('Ox Inventory requires the ox_lib resource, refer to the documentation.')
end

if not lib.checkDependency('oxmysql', '2.0.0') or not lib.checkDependency('ox_lib', '1.5.0') then error() end
if not lib.checkDependency('oxmysql', '2.0.0') or not lib.checkDependency('ox_lib', '2.0.0') then error() end

if not LoadResourceFile(shared.resource, 'web/build/index.html') then
error('Unable to locate ox_inventory/web/build, refer to the documentation or download a release build.')
error('UI has not been built, refer to the documentation or download a release build.')
end

-- Disable qtarget compatibility if it isn't running
if shared.qtarget and not GetResourceState('qtarget'):find('start') then
shared.qtarget = false
shared.warning(("qtarget compatibility has been disabled, resource state is '%s'"):format(GetResourceState('qtarget')))
shared.warning(("qtarget is '%s' - ensure it is starting before ox_inventory"):format(GetResourceState('qtarget')))
end

if shared.server then shared.ready = false end
Expand Down
18 changes: 8 additions & 10 deletions modules/interface/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ local progress = {

Interface.ProgressActive = false

local DisableControlActions = lib.controls

local function ResetPlayer()
if progress.anim or progress.scenario then
ClearPedTasks(PlayerData.ped)
ClearPedTasks(cache.ped)
end

for i = 1, 2 do
Expand All @@ -58,7 +56,7 @@ local function ResetPlayer()
end
end

if #progress.disable > 0 then DisableControlActions:Remove(progress.disable) end
if #progress.disable > 0 then lib.disableControls:Remove(progress.disable) end
table.wipe(progress.disable)
Interface.ProgressActive = false
progress.anim = false
Expand All @@ -81,7 +79,7 @@ local Animations = data 'animations'
function Interface.Progress(options, completed)
if Interface.ProgressActive == false then
progress.callback = completed
if not IsEntityDead(PlayerData.ped) or options.useWhileDead then
if not IsEntityDead(cache.ped) or options.useWhileDead then
if options.disable then
local count = 0
for key, disable in pairs(options.disable) do
Expand All @@ -93,7 +91,7 @@ function Interface.Progress(options, completed)
end
end
end
if count > 0 then DisableControlActions:Add(progress.disable) end
if count > 0 then disableControls:Add(progress.disable) end
end

Interface.ProgressActive = true
Expand All @@ -112,12 +110,12 @@ function Interface.Progress(options, completed)
if options.anim then
if options.anim.dict then
lib.requestAnimDict(options.anim.dict)
TaskPlayAnim(PlayerData.ped, options.anim.dict, options.anim.clip, 3.0, 1.0, -1, options.anim.flag or 49, 0, false, false, false)
TaskPlayAnim(cache.ped, options.anim.dict, options.anim.clip, 3.0, 1.0, -1, options.anim.flag or 49, 0, false, false, false)
progress.anim = true
end

if options.anim.scenario and not options.anim.dict then
TaskStartScenarioInPlace(PlayerData.ped, options.anim.scenario, 0, true)
TaskStartScenarioInPlace(cache.ped, options.anim.scenario, 0, true)
progress.scenario = true
end
end
Expand All @@ -132,15 +130,15 @@ function Interface.Progress(options, completed)

lib.requestModel(model)

local pCoords = GetOffsetFromEntityInWorldCoords(PlayerData.ped, 0.0, 0.0, 0.0)
local pCoords = GetOffsetFromEntityInWorldCoords(cache.ped, 0.0, 0.0, 0.0)
local modelSpawn = CreateObject(model, pCoords.x, pCoords.y, pCoords.z, true, true, true)

local netid = ObjToNet(modelSpawn)
SetNetworkIdExistsOnAllMachines(netid, true)
NetworkSetNetworkIdDynamic(netid, true)
SetNetworkIdCanMigrate(netid, false)

AttachEntityToEntity(modelSpawn, PlayerData.ped, GetPedBoneIndex(PlayerData.ped, prop.bone or 60309), prop.pos.x or 0.0, prop.pos.y or 0.0, prop.pos.z or 0.0, prop.rot.x or 0.0, prop.rot.y or 0.0, prop.rot.z or 0.0, 1, 1, 0, 1, 0, 1)
AttachEntityToEntity(modelSpawn, cache.ped, GetPedBoneIndex(cache.ped, prop.bone or 60309), prop.pos.x or 0.0, prop.pos.y or 0.0, prop.pos.z or 0.0, prop.rot.x or 0.0, prop.rot.y or 0.0, prop.rot.z or 0.0, 1, 1, 0, 1, 0, 1)
progress['prop'..i] = netid
SetModelAsNoLongerNeeded(model)
end
Expand Down
85 changes: 32 additions & 53 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,22 @@ function Inventory.Create(id, label, invType, slots, weight, maxWeight, owner, i
set = Inventory.Set,
get = Inventory.Get,
minimal = minimal,
time = os.time()
time = os.time(),
}

if self.type == 'drop' then
self.datastore = true
else
self.changed = false
self.dbId = self.id

if self.type ~= 'player' and self.owner and type(self.owner) ~= 'boolean' then
self.id = ('%s:%s'):format(self.id, self.owner)
end
end

if not self.items then
self.items, self.weight, self.datastore = Inventory.Load(self.id, self.type, self.owner)
self.items, self.weight, self.datastore = Inventory.Load(self.dbId, self.type, self.owner)
elseif self.weight == 0 and next(self.items) then
self.weight = Inventory.CalculateWeight(self.items)
end
Expand Down Expand Up @@ -204,16 +209,14 @@ function Inventory.Save(inv)
local inventory = json.encode(minimal(inv))

if inv.type == 'player' then
MySQL.prepare('UPDATE users SET inventory = ? WHERE identifier = ?', { inventory, inv.owner })
MySQL:savePlayer(inv.owner, inventory)
else
if inv.type == 'trunk' then
MySQL.prepare('UPDATE owned_vehicles SET trunk = ? WHERE plate = ?', { inventory, Inventory.GetPlateFromId(inv.id) })
MySQL:saveTrunk(Inventory.GetPlateFromId(inv.id), inventory)
elseif inv.type == 'glovebox' then
MySQL.prepare('UPDATE owned_vehicles SET glovebox = ? WHERE plate = ?', { inventory, Inventory.GetPlateFromId(inv.id) })
MySQL:saveGlovebox(Inventory.GetPlateFromId(inv.id), inventory)
else
MySQL.prepare('INSERT INTO ox_inventory (owner, name, data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)', {
inv.owner or '', inv.id, inventory,
})
MySQL:saveStash(inv.owner, inv.dbId, inventory)
end
inv.changed = false
end
Expand Down Expand Up @@ -289,7 +292,7 @@ function Inventory.Load(id, invType, owner)
datastore = true
end
elseif invType == 'trunk' or invType == 'glovebox' then
result = MySQL.prepare.await('SELECT plate, '..invType..' FROM owned_vehicles WHERE plate = ?', { Inventory.GetPlateFromId(id) })
result = invType == 'trunk' and MySQL:loadTrunk( Inventory.GetPlateFromId(id) ) or MySQL:loadGlovebox( Inventory.GetPlateFromId(id) )

if not result then
if server.randomloot then
Expand All @@ -299,7 +302,7 @@ function Inventory.Load(id, invType, owner)
end
else result = result[invType] end
else
result = MySQL.prepare.await('SELECT data FROM ox_inventory WHERE owner = ? AND name = ?', { owner or '', id })
result = MySQL:loadStash(owner or '', id)
end
end

Expand Down Expand Up @@ -581,7 +584,7 @@ function Inventory.GetItemSlots(inv, item, metadata)
if metadata and v.metadata == nil then
v.metadata = {}
end
if not metadata or item.limit or table.matches(v.metadata, metadata) then
if not metadata or table.matches(v.metadata, metadata) then
totalCount = totalCount + v.count
slots[k] = v.count
end
Expand Down Expand Up @@ -665,11 +668,6 @@ function Inventory.CanCarryItem(inv, item, count, metadata)
local itemSlots, totalCount, emptySlots = Inventory.GetItemSlots(inv, item, metadata == nil and {} or type(metadata) == 'string' and {type=metadata} or metadata)

if next(itemSlots) or emptySlots > 0 then
if inv.type == 'player' and item.limit and (totalCount + count) > item.limit then
TriggerClientEvent('ox_inventory:notify', inv.id, {type = 'error', text = shared.locale('cannot_carry_limit', item.limit, item.label)})
return false
end

if item.weight == 0 then return true end
if count == nil then count = 1 end
local newWeight = inv.weight + (item.weight * count)
Expand Down Expand Up @@ -755,19 +753,11 @@ exports('CustomDrop', CustomDrop)
function Inventory.Confiscate(source)
local inv = Inventories[source]
if inv?.player then
local inventory = json.encode(minimal(inv))
MySQL.update('INSERT INTO ox_inventory (owner, name, data) VALUES (:owner, :name, :data) ON DUPLICATE KEY UPDATE data = :data', {
owner = inv.owner,
name = inv.owner,
data = inventory,
}, function (result)
if result > 0 then
table.wipe(inv.items)
inv.weight = 0
TriggerClientEvent('ox_inventory:inventoryConfiscated', inv.id)
if shared.framework == 'esx' then Inventory.SyncInventory(inv) end
end
end)
MySQL:saveStash(inv.owner, inv.owner, json.encode(minimal(inv)))
table.wipe(inv.items)
inv.weight = 0
TriggerClientEvent('ox_inventory:inventoryConfiscated', inv.id)
if shared.framework == 'esx' then Inventory.SyncInventory(inv) end
end
end
exports('ConfiscateInventory', Inventory.Confiscate)
Expand Down Expand Up @@ -866,7 +856,7 @@ local function prepareSave(inv)
elseif inv.type == 'glovebox' then
return 2, { json.encode(minimal(inv)), Inventory.GetPlateFromId(inv.id) }
else
return 3, { inv.owner or '', inv.id, json.encode(minimal(inv)) }
return 3, { inv.owner or '', inv.dbId, json.encode(minimal(inv)) }
end
end

Expand All @@ -884,23 +874,12 @@ SetInterval(function()
end

if (inv.datastore or inv.owner) and time - inv.time >= 3000 then
Inventory.Remove(id, inv.type)
Inventory.Remove(inv.id, inv.type)
end
end
end

if #parameters[1] > 0 then
MySQL.prepare.await('UPDATE owned_vehicles SET trunk = ? WHERE plate = ?', parameters[1])
end

if #parameters[2] > 0 then
MySQL.prepare.await('UPDATE owned_vehicles SET glovebox = ? WHERE plate = ?', parameters[2])
end

if #parameters[3] > 0 then
MySQL.prepare.await('INSERT INTO ox_inventory (owner, name, data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)', parameters[3])
end

MySQL:saveInventories(parameters[1], parameters[2], parameters[3])
end, 600000)

local function saveInventories()
Expand Down Expand Up @@ -1048,7 +1027,7 @@ RegisterServerEvent('ox_inventory:updateWeapon', function(action, value, slot)
end
end)

import.commands('ox_inventory', {'additem', 'giveitem'}, function(source, args)
lib.addCommand('ox_inventory', {'additem', 'giveitem'}, function(source, args)
args.item = Items(args.item)
if args.item and args.count > 0 then
Inventory.AddItem(args.target, args.item.name, args.count, args.metatype)
Expand All @@ -1063,7 +1042,7 @@ import.commands('ox_inventory', {'additem', 'giveitem'}, function(source, args)
end
end, {'target:number', 'item:string', 'count:number', 'metatype:?string'})

import.commands('ox_inventory', 'removeitem', function(source, args)
lib.addCommand('ox_inventory', 'removeitem', function(source, args)
args.item = Items(args.item)
if args.item and args.count > 0 then
Inventory.RemoveItem(args.target, args.item.name, args.count, args.metaType)
Expand All @@ -1078,7 +1057,7 @@ import.commands('ox_inventory', 'removeitem', function(source, args)
end
end, {'target:number', 'item:string', 'count:number', 'metatype:?string'})

import.commands('ox_inventory', 'setitem', function(source, args)
lib.addCommand('ox_inventory', 'setitem', function(source, args)
args.item = Items(args.item)
if args.item and args.count >= 0 then
Inventory.SetItem(args.target, args.item.name, args.count, args.metaType)
Expand All @@ -1093,7 +1072,7 @@ import.commands('ox_inventory', 'setitem', function(source, args)
end
end, {'target:number', 'item:string', 'count:number', 'metatype:?string'})

import.commands(false, 'clearevidence', function(source, args)
lib.addCommand(false, 'clearevidence', function(source, args)
local inventory = Inventories[source]
local hasPermission = false

Expand All @@ -1109,28 +1088,28 @@ import.commands(false, 'clearevidence', function(source, args)
end
end, {'evidence:number'})

import.commands('ox_inventory', 'takeinv', function(source, args)
lib.addCommand('ox_inventory', 'takeinv', function(source, args)
Inventory.Confiscate(args.target)
end, {'target:number'})

import.commands('ox_inventory', 'returninv', function(source, args)
lib.addCommand('ox_inventory', 'returninv', function(source, args)
Inventory.Return(args.target)
end, {'target:number'})

import.commands('ox_inventory', 'clearinv', function(source, args)
lib.addCommand('ox_inventory', 'clearinv', function(source, args)
Inventory.Clear(args.target)
end, {'target:number'})

import.commands('ox_inventory', 'saveinv', function()
lib.addCommand('ox_inventory', 'saveinv', function()
saveInventories()
end)

import.commands('ox_inventory', 'viewinv', function(source, args)
lib.addCommand('ox_inventory', 'viewinv', function(source, args)
local inventory = Inventories[args.target] or Inventories[tonumber(args.target)]
TriggerClientEvent('ox_inventory:viewInventory', source, inventory)
end, {'target'})

import.commands = nil
lib.addCommand = nil
Inventory.accounts = server.accounts

TriggerEvent('ox_inventory:loadInventory', Inventory)
Expand Down
Loading

0 comments on commit 9e6d51d

Please sign in to comment.