Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop items disappearing if overweight, drag handling. #614

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions html/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ const InventoryContainer = Vue.createApp({
const targetItem = targetInventory[targetSlotNumber];

if (targetItem) {
if (this.dragStartInventoryType == "other") {
const totalWeightAfterTransfer = (this.otherInventoryWeight-sourceItem.weight * amountToTransfer) + targetItem.weight * targetItem.amount;
if (totalWeightAfterTransfer > this.otherInventoryMaxWeight) {
throw new Error("Insufficient weight capacity in target inventory");
}
}
else if (this.dragStartInventoryType == "player") {
const totalWeightAfterTransfer = (this.playerWeight-sourceItem.weight * amountToTransfer) + targetItem.weight * targetItem.amount;
if (totalWeightAfterTransfer > this.maxWeight) {
throw new Error("Insufficient weight capacity in player inventory");
}
}
if (sourceItem.name === targetItem.name && targetItem.unique) {
this.inventoryError(this.currentlyDraggingSlot);
return;
Expand Down
3 changes: 3 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ function CanAddItem(identifier, item, amount)
elseif Inventories[identifier] then
inventory = Inventories[identifier]
items = Inventories[identifier].items
elseif Drops[identifier] then
inventory = Drops[identifier]
items = Drops[identifier].items
end

if not inventory then
Expand Down
26 changes: 22 additions & 4 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ RegisterNetEvent('qb-inventory:server:SetInventoryData', function(fromInventory,
local toId = getIdentifier(toInventory, src)

if toItem and fromItem.name == toItem.name then
if RemoveItem(fromId, fromItem.name, toAmount, fromSlot, 'stacked item') then
if CanAddItem(toId, toItem.name, toAmount) and RemoveItem(fromId, fromItem.name, toAmount, fromSlot, 'stacked item') then
AddItem(toId, toItem.name, toAmount, toSlot, toItem.info, 'stacked item')
end
elseif not toItem and toAmount < fromAmount then
Expand All @@ -521,9 +521,27 @@ RegisterNetEvent('qb-inventory:server:SetInventoryData', function(fromInventory,
local fromItemAmount = fromItem.amount
local toItemAmount = toItem.amount

if RemoveItem(fromId, fromItem.name, fromItemAmount, fromSlot, 'swapped item') and RemoveItem(toId, toItem.name, toItemAmount, toSlot, 'swapped item') then
AddItem(toId, fromItem.name, fromItemAmount, toSlot, fromItem.info, 'swapped item')
AddItem(fromId, toItem.name, toItemAmount, fromSlot, toItem.info, 'swapped item')
local didRemoveFromOurInventory = RemoveItem(fromId, fromItem.name, fromItemAmount, fromSlot, 'swapped item')
local didRemoveFromTheirInventory = RemoveItem(toId, toItem.name, toItemAmount, toSlot, 'swapped item')

if didRemoveFromOurInventory and didRemoveFromTheirInventory then
local didAddToTheirInventory = AddItem(toId, fromItem.name, fromItemAmount, toSlot, fromItem.info, 'swapped item')
local didAddToOurInventory = AddItem(fromId, toItem.name, toItemAmount, fromSlot, toItem.info, 'swapped item')

local function refundBoth()
AddItem(fromId, fromItem.name, fromItemAmount, fromSlot, fromItem.info, 'refund swapped item due to error')
AddItem(toId, toItem.name, toItemAmount, toSlot, toItem.info, 'refund swapped item due to error')
end

if (not didAddToTheirInventory) and (not didAddToOurInventory) then
refundBoth()
elseif (not didAddToTheirInventory) and (didAddToOurInventory) then
RemoveItem(fromId, toItem.name, toItemAmount, fromSlot, 'remove item due to being refunded')
refundBoth()
elseif (not didAddToOurInventory) and (didAddToTheirInventory) then
RemoveItem(toId, fromItem.name, fromItemAmount, toSlot, 'remove item due to being refunded')
refundBoth()
end
end
else
if RemoveItem(fromId, fromItem.name, toAmount, fromSlot, 'moved item') then
Expand Down
Loading