Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/weapons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ return {
weapon_wrench = {
slot = 2,
},
}
}
5 changes: 2 additions & 3 deletions modules/carry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ local function carryLoop(itemConfig)

while carryingItem do
if dict and not overideanim and not IsEntityPlayingAnim(cache.ped, dict, anim, 3) then
TaskPlayAnim(cache.ped, dict, anim, 8.0, -8, -1, flag, 0, false, false, false)
TaskPlayAnim(cache.ped, dict, anim, 8.0, -8, -1, flag, 0, 0, 0, 0)
end

if amount > 0 then
Expand All @@ -135,7 +135,6 @@ local function carryLoop(itemConfig)
end
end

---@diagnostic disable-next-line: param-type-mismatch
AddStateBagChangeHandler('carry_items', nil, function(bagName, keyName, value, _, replicated)
if replicated then
return
Expand Down Expand Up @@ -205,7 +204,7 @@ local function updateState(inventory)
end

AddEventHandler('onResourceStop', function(resource)
if resource == cache.resource then
if resource == GetCurrentResourceName() then
for k, _ in pairs(Players) do
removePlayer(k)
end
Expand Down
69 changes: 63 additions & 6 deletions modules/inventory.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local weaponModule = require 'modules.weapons'
local carryModule = require 'modules.carry'
local weaponsConfig = require 'data.weapons'
local Utils = require 'modules.utils'

local Inventory = exports.ox_inventory:GetPlayerItems() or {}
local playerState = LocalPlayer.state
Expand Down Expand Up @@ -32,22 +33,66 @@ AddEventHandler('ox_inventory:currentWeapon', function(weapon)
end
end)

--- Updates the inventory with the changes
AddEventHandler('ox_inventory:updateInventory', function(changes)
if not changes then
return
end
if not changes then return end

local removedWeapons = {}
local currentWeaponsState = playerState.weapons_carry or {}

for slot, item in pairs(changes) do
Inventory[slot] = item
if item == false then
if Inventory[slot] and type(Inventory[slot]) == "table" and Inventory[slot].type == 'weapon' then
local wasAttached = false
for _, attachedWeapon in ipairs(currentWeaponsState) do
if attachedWeapon.name == Inventory[slot].name then
wasAttached = true
break
end
end

if wasAttached then
Utils.resetSlots()
playerState:set('weapons_carry', false, true)
Wait(0)
else
table.insert(removedWeapons, Inventory[slot].name)
end
end
Inventory[slot] = nil
elseif type(item) == "table" then
Inventory[slot] = item
end
end

if #removedWeapons > 0 then
for _, weaponName in ipairs(removedWeapons) do
weaponModule.removeWeaponAttachment(weaponName)
end
end

local hasWeapons = false
for _, item in pairs(Inventory) do
if item and type(item) == "table" and item.type == 'weapon' then
hasWeapons = true
break
end
end

if playerState.weapons_carry ~= hasWeapons then
playerState:set('weapons_carry', hasWeapons, true)
end

weaponModule.updateWeapons(Inventory, currentWeapon)
carryModule.updateCarryState(Inventory)
end)






AddEventHandler('onResourceStart', function(resource)
if resource == cache.resource then
if resource == GetCurrentResourceName() then
Wait(100)
if table.type(playerState.weapons_carry or {}) ~= 'empty' then
playerState:set('weapons_carry', false, true)
Expand Down Expand Up @@ -128,3 +173,15 @@ AddStateBagChangeHandler('instance', ('player:%s'):format(cache.serverId), funct
end
end
end)

local function updateState(inventory, currentWeapon)
while playerState.weapons_carry == nil do
Wait(0)
end

local items = formatPlayerInventory(inventory, currentWeapon)

playerState:set('weapons_carry', false, true)
Wait(0)
playerState:set('weapons_carry', items, true)
end
28 changes: 24 additions & 4 deletions modules/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ local playerSlots = {
{ bone = 24817, pos = vec3(-0.28, -0.14, 0.15), rot = vec3(0.0, 92.0, -13.0) },
{ bone = 24817, pos = vec3(-0.27, -0.14, 0.15), rot = vec3(0.0, 92.0, 13.0) },
},

{ -- Pistol Thigh
{ bone = 51826, pos = vec3(0.038041, 0.068614, 0.091040), rot = vec3(-124.852936, 5.241456, -2.311053) },
},
}

function Utils.resetSlots()
Expand Down Expand Up @@ -124,14 +128,17 @@ function Utils.findOpenSlot(tier)
end
end

return slotTier[slotAmount]
return nil
end
end

function Utils.formatData(itemData, itemConfig, ignoreSlot)
local isWeapon = itemData.name:find('WEAPON_')

local slot = not ignoreSlot and itemConfig.slot and Utils.findOpenSlot(itemConfig.slot)

if not ignoreSlot and itemConfig.slot and not slot then
return nil
end

return {
name = itemData.name,
Expand Down Expand Up @@ -218,7 +225,7 @@ local function createWeapon(item)

if Utils.checkFlashState(item) then
SetCreateWeaponObjectLightSource(weaponObject, true)
Wait(0) -- We need to skip a frame before attaching the object for the lightsource to be created
Wait(0)
end

if hasLuxeMod then
Expand All @@ -244,4 +251,17 @@ function Utils.getEntity(payload)
end


return Utils
return {
resetSlots = Utils.resetSlots,
removeEntities = Utils.removeEntities,
hasFlashLight = Utils.hasFlashLight,
checkFlashState = Utils.checkFlashState,
hasVarMod = Utils.hasVarMod,
getWeaponComponents = Utils.getWeaponComponents,
findOpenSlot = Utils.findOpenSlot,
formatData = Utils.formatData,
getEntityFromStateBag = Utils.getEntityFromStateBag,
AttachEntityToPlayer = Utils.AttachEntityToPlayer,
getEntity = Utils.getEntity,
playerSlots = playerSlots
}
4 changes: 2 additions & 2 deletions modules/vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ local function addVehicleStateBag(name)
end

AddEventHandler('onResourceStop', function(resource)
if resource == cache.resource then
if resource == GetCurrentResourceName() then

for bagname, _ in pairs(Vehicles) do
if Vehicles[bagname] and table.type(Vehicles[bagname]) ~= 'empty' then
Expand All @@ -97,4 +97,4 @@ end)

return {
addVehicleStateBag = addVehicleStateBag
}
}
39 changes: 27 additions & 12 deletions modules/weapons.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local Config = require 'data.weapons'
local Utils = require 'modules.utils'
local playerSlots = Utils.playerSlots

local Players = {}

SetFlashLightKeepOnWhileMoving(true)

---Removes the current player from the table and deletes the entities
---@param serverId number
local function removePlayer(serverId)
local Player = Players[serverId]
Expand All @@ -20,37 +20,55 @@ RegisterNetEvent('onPlayerDropped', function(serverId)
removePlayer(serverId)
end)

---Formats the players inventory to only have items that are in the config
---@param inventory table
---@param currentWeapon table | nil
---@return table
local function formatPlayerInventory(inventory, currentWeapon)
local items = {}
local amount = 0
local usedSlots = {}
local playerSlots = Utils.playerSlots

for _, itemData in pairs(inventory) do
local name = itemData and itemData.name:lower()
if name and Config[name] then
local slotType = Config[name].slot
usedSlots[slotType] = usedSlots[slotType] or 0
end
end

for _, itemData in pairs(inventory) do
local name = itemData and itemData.name:lower()

if currentWeapon and itemData and currentWeapon.name == itemData.name and lib.table.matches(itemData.metadata.components, currentWeapon.metadata.components) then
currentWeapon = nil
elseif name and Config[name] then
amount += 1
items[amount] = Utils.formatData(itemData, Config[name])
local slotType = Config[name].slot
if playerSlots[slotType] and usedSlots[slotType] < #playerSlots[slotType] then
amount += 1
items[amount] = Utils.formatData(itemData, Config[name])
usedSlots[slotType] += 1
end
end
end

Utils.resetSlots()

if amount > 1 then
table.sort(items, function(a, b)
return a.serial < b.serial
if not a or not b or not a.slot or not b.slot then return false end
return a.slot < b.slot
end)
end

return items
end

---Creates all the objects for the player





---@param pedHandle number
---@param addItems table
---@param currentTable table
Expand Down Expand Up @@ -109,7 +127,6 @@ end)

local playerState = LocalPlayer.state

---Updates the weapons_carry state for the player
---@param inventory table
---@param currentWeapon table
local function updateState(inventory, currentWeapon)
Expand All @@ -125,7 +142,7 @@ local function updateState(inventory, currentWeapon)
end

AddEventHandler('onResourceStop', function(resource)
if resource == cache.resource then
if resource == GetCurrentResourceName() then
for _, v in pairs(Players) do
if v then
Utils.removeEntities(v)
Expand All @@ -134,7 +151,6 @@ AddEventHandler('onResourceStop', function(resource)
end
end)

--- Refreshes the props for the player to make sure no clipping issues when going in/out of instances
---@param items table
---@param weapon table | nil
local function refreshProps(items, weapon)
Expand All @@ -149,10 +165,9 @@ local function refreshProps(items, weapon)
end
end

--- Loops the current flashlight to keep it enabled while the player is not aiming
---@param serial string
local function flashLightLoop(serial)
serial = serial or 'scratched' -- Adds support for Scratched serial numbers if they removed it
serial = serial or 'scratched'

local flashState = playerState.flashState?[serial]

Expand All @@ -179,4 +194,4 @@ return {
updateWeapons = updateState,
loopFlashlight = flashLightLoop,
refreshProps = refreshProps,
}
}
4 changes: 2 additions & 2 deletions server.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib.versionCheck('Renewed-Scripts/Renewed-Weaponscarry')
lib.versionCheck('Renewed-Scripts/Renewed-Lib')

-- Intiate the statebag for the player
AddEventHandler('Renewed-Lib:server:playerRemoved', function(source)
Expand All @@ -13,4 +13,4 @@ AddEventHandler('Renewed-Lib:server:playerLoaded', function(source)

playerState:set('weapons_carry', false, true)
playerState:set('carry_items', false, true)
end)
end)
Loading