Skip to content

Commit

Permalink
refactor: require modules
Browse files Browse the repository at this point in the history
Start using the module system added to ox_lib.
Need to clean up and restyle a lot of code later (big lack of consistency).
  • Loading branch information
thelindat committed Mar 1, 2023
1 parent 3a3217e commit 397ef18
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 119 deletions.
22 changes: 14 additions & 8 deletions client.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
if not lib then return end

local Utils = client.utils
local Weapon = client.weapon
require 'modules.bridge.client'
require 'modules.interface.client'

local Utils = require 'modules.utils.client'
local Weapon = require 'modules.weapon.client'
local currentWeapon

exports('getCurrentWeapon', function()
Expand Down Expand Up @@ -93,6 +96,8 @@ local function closeTrunk()
end
end

local CraftingBenches = require 'modules.crafting.client'

---@param inv string?
---@param data any?
---@return boolean?
Expand Down Expand Up @@ -167,7 +172,7 @@ function client.openInventory(inv, data)
left = lib.callback.await('ox_inventory:openCraftingBench', 200, data.id, data.index)

if left then
right = client.craftingBenches[data.id]
right = CraftingBenches[data.id]
local coords = shared.target == 'ox_target' and right.zones[data.index].coords or right.points[data.index]

right = {
Expand Down Expand Up @@ -243,7 +248,7 @@ RegisterNetEvent('ox_inventory:openInventory', client.openInventory)
exports('openInventory', client.openInventory)

local Animations = data 'animations'
local Items = client.items
local Items = require 'modules.items.client'

lib.callback.register('ox_inventory:usingItem', function(data)
local item = Items[data.name]
Expand Down Expand Up @@ -546,7 +551,8 @@ exports('openNearbyInventory', openNearbyInventory)
local currentInstance
local playerCoords
local table = lib.table
local Inventory = client.inventory
local Inventory = require 'modules.inventory.client'
local Shops = require 'modules.shops.client'

---@todo remove or replace when the bridge module gets restructured
function OnPlayerData(key, val)
Expand All @@ -555,7 +561,7 @@ function OnPlayerData(key, val)
if key == 'groups' then
Inventory.Stashes()
Inventory.Evidence()
client.refreshShops()
Shops.refreshShops()
elseif key == 'dead' and val then
currentWeapon = Weapon.Disarm(currentWeapon)
client.closeInventory()
Expand Down Expand Up @@ -1229,7 +1235,7 @@ RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inven
PlayerData.loaded = true

lib.notify({ description = locale('inventory_setup') })
client.refreshShops()
Shops.refreshShops()
Inventory.Stashes()
Inventory.Evidence()
registerCommands()
Expand Down Expand Up @@ -1585,7 +1591,7 @@ RegisterNUICallback('exit', function(_, cb)
end)

lib.callback.register('ox_inventory:startCrafting', function(id, recipe)
recipe = client.craftingBenches[id].items[recipe]
recipe = CraftingBenches[id].items[recipe]

return lib.progressCircle({
label = locale('crafting_item', Items[recipe.name].label),
Expand Down
36 changes: 5 additions & 31 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,27 @@ dependencies {
'ox_lib',
}

shared_scripts {
'@ox_lib/init.lua',
'modules/init.lua'
}

server_scripts {
'@oxmysql/lib/MySQL.lua',
'modules/bridge/server.lua',
}

shared_scripts {
'modules/**/shared.lua'
}

client_scripts {
'modules/items/client.lua',
'modules/utils/client.lua',
'modules/weapon/client.lua',
'modules/bridge/client.lua',
'modules/interface/client.lua',
'modules/crafting/client.lua',
'modules/shops/client.lua',
'modules/inventory/client.lua',
'client.lua'
}

server_scripts {
'modules/hooks/server.lua',
'modules/utils/server.lua',
'modules/mysql/server.lua',
'modules/items/server.lua',
'modules/inventory/server.lua',
'modules/crafting/server.lua',
'modules/shops/server.lua',
'modules/pefcl/server.lua',
'server.lua',
'@ox_lib/init.lua',
'modules/init.lua'
}

ui_page 'web/build/index.html'

files {
'client.lua',
'server.lua',
'locales/*.json',
'web/build/index.html',
'web/build/assets/*.js',
'web/build/assets/*.css',
'web/images/*.png',
'modules/**/shared.lua',
'modules/**/client.lua',
'modules/bridge/**/client.lua',
'data/*.lua',
}
7 changes: 4 additions & 3 deletions modules/bridge/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ function client.hasGroup(group)
end
end

local Utils = client.utils
local Weapon = client.weapon
local Shops = require 'modules.shops.client'
local Utils = require 'modules.utils.client'
local Weapon = require 'modules.weapon.client'

function client.onLogout()
if not PlayerData.loaded then return end
Expand All @@ -47,7 +48,7 @@ function client.onLogout()
client.drops = nil

client.closeInventory()
client.wipeShops()
Shops.wipeShops()
ClearInterval(client.interval)
ClearInterval(client.tick)
Weapon.Disarm()
Expand Down
4 changes: 2 additions & 2 deletions modules/bridge/esx/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ local playerDropped = ...
local Inventory, Items

CreateThread(function()
Inventory = server.inventory
Items = server.items
Inventory = require 'modules.inventory.server'
Items = require 'modules.items.server'
end)

AddEventHandler('esx:playerDropped', playerDropped)
Expand Down
2 changes: 1 addition & 1 deletion modules/bridge/nd/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Inventory
local NDCore

CreateThread(function()
Inventory = server.inventory
Inventory = require 'modules.inventory.server'
end)

AddEventHandler("ND:characterUnloaded", playerDropped)
Expand Down
2 changes: 1 addition & 1 deletion modules/bridge/ox/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local playerDropped = ...
local Inventory

CreateThread(function()
Inventory = server.inventory
Inventory = require 'modules.inventory.server'
end)

AddEventHandler('ox:playerLogout', playerDropped)
Expand Down
4 changes: 2 additions & 2 deletions modules/bridge/qb/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ local playerDropped = ...
local Inventory, Items

CreateThread(function()
Inventory = server.inventory
Items = server.items
Inventory = require 'modules.inventory.server'
Items = require 'modules.items.server'
end)

local QBCore
Expand Down
2 changes: 1 addition & 1 deletion modules/bridge/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end
local Inventory

CreateThread(function()
Inventory = server.inventory
Inventory = require 'modules.inventory.server'
end)

local function playerDropped(source)
Expand Down
6 changes: 3 additions & 3 deletions modules/crafting/client.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
if not lib then return end

local CraftingBenches = {}
local Items = client.items
local Items = require 'modules.items.client'
local locations = shared.target == 'ox_target' and 'zones' or 'points'
local createBlip = client.utils.CreateBlip
local createBlip = require 'modules.utils.client'.CreateBlip

---@param id number
---@param data table
Expand Down Expand Up @@ -99,4 +99,4 @@ end

for id, data in pairs(data('crafting')) do createCraftingBench(id, data) end

client.craftingBenches = CraftingBenches
return CraftingBenches
6 changes: 4 additions & 2 deletions modules/crafting/server.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if not lib then return end

local CraftingBenches = {}
local Items = server.items
local Inventory = server.inventory
local Items = require 'modules.items.server'
local Inventory = require 'modules.inventory.server'

---@param id number
---@param data table
Expand Down Expand Up @@ -75,6 +75,8 @@ lib.callback.register('ox_inventory:openCraftingBench', function(source, id, ind
return { label = left.label, type = left.type, slots = left.slots, weight = left.weight, maxWeight = left.maxWeight }
end)

local TriggerEventHooks = require 'modules.hooks.server'

lib.callback.register('ox_inventory:craftItem', function(source, id, index, recipeId, toSlot)
local left, bench = Inventory(source), CraftingBenches[id]

Expand Down
4 changes: 3 additions & 1 deletion modules/hooks/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local function typeFilter(filter, type)
return filter[type] or false
end

function TriggerEventHooks(event, payload)
local function TriggerEventHooks(event, payload)
local hooks = eventHooks[event]

if hooks then
Expand Down Expand Up @@ -125,3 +125,5 @@ AddEventHandler('onResourceStop', removeResourceHooks)
exports('removeHooks', function(id)
removeResourceHooks(GetInvokingResource() or cache.resource, id)
end)

return TriggerEventHooks
7 changes: 6 additions & 1 deletion modules/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,9 @@ if shared.target then
shared.target = ox_target and 'ox_target' or 'qtarget'
end

if shared.server then shared.ready = false end
if lib.context == 'server' then
shared.ready = false
return require 'server'
end

require 'client'
2 changes: 1 addition & 1 deletion modules/inventory/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@ Inventory.Stashes = setmetatable(data('stashes'), {
end
})

client.inventory = Inventory
return Inventory
14 changes: 8 additions & 6 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ local Items

CreateThread(function()
TriggerEvent('ox_inventory:loadInventory', Inventory)
Items = server.items
Items = require 'modules.items.server'

-- Require "set inventory:weaponmismatch 1" to enable experimental weapon checks.
-- Maybe need some tweaks, and will definitely need more hashes added to the ignore list.
Expand Down Expand Up @@ -1281,6 +1281,8 @@ exports('CreateDropFromPlayer', function(playerId)
return dropId
end)

local TriggerEventHooks = require 'modules.hooks.server'

local function dropItem(source, data)
local playerInventory = Inventory(source)
local fromData = playerInventory.items[data.fromSlot]
Expand Down Expand Up @@ -1813,7 +1815,7 @@ SetInterval(function()
db.saveInventories(parameters[1], parameters[2], parameters[3], parameters[4])
end, 600000)

function server.saveInventories(lock)
function Inventory.SaveInventories(lock)
local parameters = { {}, {}, {}, {} }
local size = { 0, 0, 0, 0 }
Inventory.Lock = lock or nil
Expand All @@ -1836,17 +1838,17 @@ end

AddEventHandler('playerDropped', function()
if GetNumPlayerIndices() == 0 then
server.saveInventories()
Inventory.SaveInventories()
end
end)

AddEventHandler('txAdmin:events:serverShuttingDown', function()
server.saveInventories(true)
Inventory.SaveInventories(true)
end)

AddEventHandler('onResourceStop', function(resource)
if resource == shared.resource then
server.saveInventories(true)
Inventory.SaveInventories(true)
end
end)

Expand Down Expand Up @@ -2118,4 +2120,4 @@ end

exports('CreateTemporaryStash', Inventory.CreateTemporaryStash)

server.inventory = Inventory
return Inventory
6 changes: 3 additions & 3 deletions modules/items/client.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
if not lib then return end

---@type { [string]: OxClientItem }
local Items = shared.items
local Items = require 'modules.items.shared' --[[@as { [string]: OxClientItem }]]

local function displayMetadata(metadata, value)
local data = metadata
Expand Down Expand Up @@ -138,4 +137,5 @@ end)

exports('Items', GetItem)
exports('ItemList', GetItem)
client.items = Items

return Items
9 changes: 5 additions & 4 deletions modules/items/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ if not lib then return end

---@overload fun(name: string): OxServerItem
local Items = {}
local ItemList = shared.items
---@cast ItemList { [string]: OxServerItem }
local ItemList = require 'modules.items.shared' --[[@as { [string]: OxServerItem }]]

TriggerEvent('ox_inventory:itemList', ItemList)

Expand Down Expand Up @@ -65,7 +64,7 @@ exports('ItemList', function(item) return getItem(nil, item) end)
local Inventory

CreateThread(function()
Inventory = server.inventory
Inventory = require 'modules.inventory.server'

if shared.framework == 'esx' then
local success, items = pcall(MySQL.query.await, 'SELECT * FROM items')
Expand Down Expand Up @@ -255,6 +254,8 @@ local function setItemDurability(item, metadata)
return metadata
end

local TriggerEventHooks = require 'modules.hooks.server'

function Items.Metadata(inv, item, metadata, count)
if type(inv) ~= 'table' then inv = Inventory(inv) end
if not item.weapon then metadata = not metadata and {} or type(metadata) == 'string' and {type=metadata} or metadata end
Expand Down Expand Up @@ -411,4 +412,4 @@ end

-----------------------------------------------------------------------------------------------

server.items = Items
return Items
Loading

0 comments on commit 397ef18

Please sign in to comment.