diff --git a/es_extended.sql b/es_extended.sql index 9ea30491..c01587aa 100644 --- a/es_extended.sql +++ b/es_extended.sql @@ -1,3 +1,4 @@ +CREATE DATABASE IF NOT EXISTS `essentialmode`; USE `essentialmode`; CREATE TABLE `users` ( diff --git a/server/classes/player.lua b/server/classes/player.lua index 4df91ee8..52f661e2 100644 --- a/server/classes/player.lua +++ b/server/classes/player.lua @@ -1,18 +1,19 @@ -function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, job, loadout, name, coords) +function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, weight, job, loadout, name, coords) local self = {} self.accounts = accounts + self.coords = coords + self.group = group + self.identifier = identifier self.inventory = inventory self.job = job - self.group = group self.loadout = loadout self.name = name - self.maxWeight = Config.MaxWeight - self.coords = coords - self.variables = {} - self.source = playerId self.playerId = playerId - self.identifier = identifier + self.source = playerId + self.variables = {} + self.weight = weight + self.maxWeight = Config.MaxWeight ExecuteCommand(('add_principal identifier.license:%s group.%s'):format(self.identifier, self.group)) @@ -216,9 +217,8 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, if item then count = ESX.Math.Round(count) - - local newCount = item.count + count - item.count = newCount + item.count = item.count + count + self.weight = self.weight + (item.weight * count) TriggerEvent('esx:onAddInventoryItem', self.source, item.name, item.count) self.triggerEvent('esx:addInventoryItem', item.name, item.count) @@ -234,6 +234,7 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, if newCount >= 0 then item.count = newCount + self.weight = self.weight - (item.weight * count) TriggerEvent('esx:onRemoveInventoryItem', self.source, item.name, item.count) self.triggerEvent('esx:removeInventoryItem', item.name, item.count) @@ -256,13 +257,7 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end self.getWeight = function() - local inventoryWeight = 0 - - for k,v in ipairs(self.inventory) do - inventoryWeight = inventoryWeight + (v.count * v.weight) - end - - return inventoryWeight + return self.weight end self.getMaxWeight = function() @@ -270,7 +265,7 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end self.canCarryItem = function(name, count) - local currentWeight, itemWeight = self.getWeight(), ESX.Items[name].weight + local currentWeight, itemWeight = self.weight, ESX.Items[name].weight local newWeight = currentWeight + (itemWeight * count) return newWeight <= self.maxWeight @@ -281,7 +276,7 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, local testItemObject = self.getInventoryItem(testItem) if firstItemObject.count >= firstItemCount then - local weightWithoutFirstItem = ESX.Math.Round(self.getWeight() - (firstItemObject.weight * firstItemCount)) + local weightWithoutFirstItem = ESX.Math.Round(self.weight - (firstItemObject.weight * firstItemCount)) local weightWithTestItem = ESX.Math.Round(weightWithoutFirstItem + (testItemObject.weight * testItemCount)) return weightWithTestItem <= self.maxWeight diff --git a/server/main.lua b/server/main.lua index 4d89c235..5e6b7b61 100644 --- a/server/main.lua +++ b/server/main.lua @@ -47,7 +47,8 @@ function loadESXPlayer(identifier, playerId) inventory = {}, job = {}, loadout = {}, - playerName = GetPlayerName(playerId) + playerName = GetPlayerName(playerId), + weight = 0 } table.insert(tasks, function(cb) @@ -115,6 +116,7 @@ function loadESXPlayer(identifier, playerId) for name,item in pairs(ESX.Items) do local count = foundItems[name] or 0 + if count > 0 then userData.weight = userData.weight + (item.weight * count) end table.insert(userData.inventory, { name = name, @@ -173,7 +175,7 @@ function loadESXPlayer(identifier, playerId) end) Async.parallel(tasks, function(results) - local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.job, userData.loadout, userData.playerName, userData.coords) + local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.weight, userData.job, userData.loadout, userData.playerName, userData.coords) ESX.Players[playerId] = xPlayer TriggerEvent('esx:playerLoaded', playerId, xPlayer)