Skip to content

Commit

Permalink
refactor: rename player module to bridge
Browse files Browse the repository at this point in the history
This module is used for more than player data, so set a more appropriate name.
This will be expanded on later to help provide compatibility with different frameworks.

Includes change to player accounts when syncing data.
Any additional accounts should be added to the bridge module to ensure sync between the item and the account.
  • Loading branch information
thelindat committed Jan 18, 2022
1 parent f8ce76e commit 521a63d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
20 changes: 10 additions & 10 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ local function updateInventory(items, weight)
PlayerData.inventory[slot] = v and v or nil
changes[slot] = v
end
client.SetPlayerData('weight', weight)
client.setPlayerData('weight', weight)
else
for i=1, #items do
local v = items[i].item
Expand All @@ -652,9 +652,9 @@ local function updateInventory(items, weight)
PlayerData.inventory[v.slot] = v.count and v or nil
changes[v.slot] = v.count and v or false
end
client.SetPlayerData('weight', weight.left)
client.setPlayerData('weight', weight.left)
end
client.SetPlayerData('inventory', PlayerData.inventory)
client.setPlayerData('inventory', PlayerData.inventory)
TriggerEvent('ox_inventory:updateInventory', changes)
end

Expand Down Expand Up @@ -686,16 +686,16 @@ RegisterNetEvent('ox_inventory:inventoryReturned', function(data)
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
TriggerEvent('ox_inventory:closeInventory')
PlayerData.inventory = data[1]
client.SetPlayerData('inventory', data[1])
client.SetPlayerData('weight', data[3])
client.setPlayerData('inventory', data[1])
client.setPlayerData('weight', data[3])
end)

RegisterNetEvent('ox_inventory:inventoryConfiscated', function(message)
if message then Utils.Notify({text = shared.locale('items_confiscated'), duration = 2500}) end
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
TriggerEvent('ox_inventory:closeInventory')
table.wipe(PlayerData.inventory)
client.SetPlayerData('weight', 0)
client.setPlayerData('weight', 0)
end)

RegisterNetEvent('ox_inventory:createDrop', function(data, owner, slot)
Expand Down Expand Up @@ -760,8 +760,8 @@ RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inven
exports.npwd:setPhoneDisabled(true)
end

client.SetPlayerData('inventory', inventory)
client.SetPlayerData('weight', weight)
client.setPlayerData('inventory', inventory)
client.setPlayerData('weight', weight)
currentWeapon = nil
drops = currentDrops
Utils.ClearWeapons()
Expand Down Expand Up @@ -1114,8 +1114,8 @@ RegisterNUICallback('buyItem', function(data, cb)
local response, data, message = ServerCallback.Await(shared.resource, 'buyItem', 100, data)
if data then
PlayerData.inventory[data[1]] = data[2]
client.SetPlayerData('inventory', PlayerData.inventory)
client.SetPlayerData('weight', data[3])
client.setPlayerData('inventory', PlayerData.inventory)
client.setPlayerData('weight', data[3])
SendNUIMessage({ action = 'refreshSlots', data = {item = data[2]} })
end
if message then Utils.Notify(message) end
Expand Down
2 changes: 1 addition & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ shared = loadConvar('ox_inventory')

shared = {
-- Enable support for es_extended (defaults to true, for now)
esx = shared.esx == false and false or true,
esx = shared.esx or false,

-- If vehicle plates are stored with a trailing space, set to false (i.e. `XXX 000 `)
trimplate = shared.trimplate or true,
Expand Down
4 changes: 2 additions & 2 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ shared_scripts {
'config.lua'
}

server_script 'modules/player/server.lua'
server_script 'modules/bridge/server.lua'

shared_scripts {
'modules/init.lua',
Expand All @@ -34,7 +34,7 @@ shared_scripts {
client_scripts {
'modules/items/client.lua',
'modules/utils/client.lua',
'modules/player/client.lua',
'modules/bridge/client.lua',
'modules/interface/client.lua',
'modules/shops/client.lua',
'modules/inventory/client.lua',
Expand Down
4 changes: 2 additions & 2 deletions modules/player/client.lua → modules/bridge/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PlayerData = {
loaded = false
}

function client.SetPlayerData(key, value)
function client.setPlayerData(key, value)
PlayerData[key] = value
OnPlayerData(key, value)
end
Expand All @@ -35,7 +35,7 @@ if shared.esx then
PlayerLoaded = ESX.PlayerLoaded
}

function client.SetPlayerData(key, value)
function client.setPlayerData(key, value)
PlayerData[key] = value
ESX.SetPlayerData(key, value)
end
Expand Down
24 changes: 24 additions & 0 deletions modules/player/server.lua → modules/bridge/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ function server.isPolice(inv)
return shared.police[inv.player.job.name] ~= nil
end

function server.setPlayerData(player)
return {
name = player.name,
job = player.job,
sex = player.sex,
dateofbirth = player.dateofbirth,
}
end

server.accounts = {
money = 0,
}

if shared.esx then
local ESX = exports['es_extended']:getSharedObject()

Expand All @@ -20,6 +33,17 @@ if shared.esx then
server.UsableItemsCallbacks = ESX.GetUsableItems
server.GetPlayerFromId = ESX.GetPlayerFromId

server.accounts.black_money = 0

function server.setPlayerData(player)
return {
name = player.name,
job = player.job,
sex = player.sex or player.variables.sex,
dateofbirth = player.dateofbirth or player.variables.dateofbirth,
}
end

RegisterServerEvent('ox_inventory:requestPlayerInventory', function()
local source = source
local player = server.GetPlayerFromId(source)
Expand Down
4 changes: 2 additions & 2 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ end
---@param inv table
--- Syncs inventory data with the xPlayer object for compatibility with shit resources
function Inventory.SyncInventory(inv)
local money = { money = 0, black_money = 0}
local money = table.clone(server.accounts)

for _, v in pairs(inv.items) do
if money[v.name] then
Expand Down Expand Up @@ -706,7 +706,7 @@ function Inventory.Return(source)
if data then
MySQL.query('DELETE FROM ox_inventory WHERE name = ?', { inv.owner })
data = json.decode(data)
local money, inventory, totalWeight = {money=0, black_money=0}, {}, 0
local money, inventory, totalWeight = table.clone(server.accounts), {}, 0

if data and next(data) then
for i=1, #data do
Expand Down
13 changes: 3 additions & 10 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ local Items = server.items
--- optionally, it should contain job, sex, and dateofbirth
local function setPlayerInventory(player, data)
while not shared.ready do Wait(0) end
local money = { money = 0, black_money = 0 }
local inventory = {}
local totalWeight = 0

Expand Down Expand Up @@ -35,19 +34,12 @@ local function setPlayerInventory(player, data)
end

inventory[v.slot] = {name = v.name, label = item.label, weight = weight, slot = v.slot, count = v.count, description = item.description, metadata = v.metadata, stack = item.stack, close = item.close}
if money[v.name] then money[v.name] = money[v.name] + v.count end
end
end
end

local inv = Inventory.Create(player.source, player.name, 'player', shared.playerslots, totalWeight, shared.playerweight, player.identifier, inventory)

inv.player = {
name = player.name,
job = player.job,
sex = player.sex or player.variables.sex,
dateofbirth = player.dateofbirth or player.variables.dateofbirth,
}
inv.player = server.setPlayerData(player)

if shared.esx then Inventory.SyncInventory(inv) end
TriggerClientEvent('ox_inventory:setPlayerInventory', player.source, Inventory.Drops, inventory, totalWeight, server.UsableItemsCallbacks, player)
Expand All @@ -58,6 +50,7 @@ AddEventHandler('ox_inventory:setPlayerInventory', setPlayerInventory)
local Stashes = data 'stashes'
local Vehicles = data 'vehicles'
local ServerCallback = import 'callbacks'
local table = import 'table'

ServerCallback.Register('openInventory', function(source, inv, data)
local left = Inventory(source)
Expand All @@ -74,6 +67,7 @@ ServerCallback.Register('openInventory', function(source, inv, data)
if inv == 'stash' then
local stash = Stashes[data.id]
if stash then

if not stash.jobs or (stash.jobs[left.player.job.name] and left.player.job.grade >= stash.jobs[left.player.job.name]) then
local owner = stash.owner and left.owner or stash.owner
right = Inventory(owner and stash.name..owner or stash.name)
Expand Down Expand Up @@ -162,7 +156,6 @@ ServerCallback.Register('openInventory', function(source, inv, data)
return {id=left.id, label=left.label, type=left.type, slots=left.slots, weight=left.weight, maxWeight=left.maxWeight}, right and {id=right.id, label=right.label, type=right.type, slots=right.slots, weight=right.weight, maxWeight=right.maxWeight, items=right.items, coords=right.coords}
end)

local table = import 'table'
local Log = server.logs

ServerCallback.Register('swapItems', function(source, data)
Expand Down

0 comments on commit 521a63d

Please sign in to comment.