From e0b42e71aa78b495f164a5dea871c30dd930409f Mon Sep 17 00:00:00 2001 From: Palm Date: Wed, 17 Sep 2025 19:21:17 -0400 Subject: [PATCH 1/2] Update container.ts --- packages/core/src/container.ts | 99 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/packages/core/src/container.ts b/packages/core/src/container.ts index 7bc31b30a..9696b8483 100644 --- a/packages/core/src/container.ts +++ b/packages/core/src/container.ts @@ -123,70 +123,67 @@ class Container { * @returns Whether the item was successfully added into the container. */ public addItem(item: ItemStack): boolean { - // Find a slot that has the same item type and isn't full (x64) - // If there is no slot, find the next empty slot. - const slot = this.storage.findIndex((slot) => { - // Check if the slot is null. - if (!slot) return false; - - // Check if the item can be stacked. - if (slot.stackSize >= item.maxStackSize) return false; + // Non-stackable logic. + if (!item.isStackable) { + const emptySlot = this.storage.indexOf(null); + if (emptySlot > -1) { + this.setItem(emptySlot, item); + // Item was fully transferred already. + item.stackSize = 0; + return true; + } + // No empty slot was found. + return false; + } - // Check if the item is equal to the slot. - return item.equals(slot); - }); + // Loop as long as there are items left in the stack to be added. + while (item.stackSize > 0) { + const existingSlotIndex = this.storage.findIndex( + (slot) => + slot && + slot.stackSize < slot.maxStackSize && + item.equals(slot) + ); - // Check if the item is maxed. - const maxed = item.stackSize >= item.maxStackSize; + // If a suitable stack is found, add to it. + if (existingSlotIndex > -1) { + const existingItem = this.storage[existingSlotIndex] as ItemStack; - // Check if exists an available slot - if (slot > -1 && !maxed && item.isStackable) { - // Get the item if slot available - const existingItem = this.storage[slot] as ItemStack; + // Calculate how many items we can add to this stack. + const amountToAdd = Math.min( + existingItem.maxStackSize - existingItem.stackSize, + item.stackSize + ); - // Calculate the amount of items to add. - const amount = Math.min( - item.maxStackSize - existingItem.stackSize, - item.stackSize - ); + existingItem.incrementStack(amountToAdd); + item.decrementStack(amountToAdd); - // Add the amount to the existing item. - existingItem.incrementStack(amount); - - // Subtract the amount from the item. - item.decrementStack(amount); + continue; + } - // Return true as the item was successfully added. - return true; - } else { - // Find the next empty slot. - const emptySlot = this.storage.indexOf(null); + // If no stack was found, find the next empty slot. + const emptySlotIndex = this.storage.indexOf(null); - // Check if there is an empty slot, if not return false. - if (emptySlot === -1) return false; + // If there's an empty slot, put items there. + if (emptySlotIndex > -1) { + // Determine how many items to put in the new stack. + const amountToSet = Math.min(item.maxStackSize, item.stackSize); - // Check if the item is maxed. - if (item.stackSize > item.maxStackSize) { - // Create a full stack item for the empty slot - const newItem = new ItemStack(item.type, { - ...item, - stackSize: item.maxStackSize - }); + // Clone new itemstack for empty slot. + const newItem = new ItemStack(item.type, { ...item, stackSize: amountToSet }); - // Add the new Item and decrease it - this.setItem(emptySlot, newItem); - item.decrementStack(item.maxStackSize); + this.setItem(emptySlotIndex, newItem); + item.decrementStack(amountToSet); - // Because it is greater than 64 call the function to add the remaining items - return this.addItem(item); + continue; } - // Set the item in the empty slot. - this.setItem(emptySlot, item); - - // Return true as the item was successfully added. - return true; + // Inventory is full. + break; } + + // Whether or not all the items were successfully added. + return item.stackSize === 0; } /** From a45c71002f5cc39708bf899a01c4a89958569157 Mon Sep 17 00:00:00 2001 From: Palm Date: Fri, 19 Sep 2025 16:57:14 -0400 Subject: [PATCH 2/2] Fix item data getting cleared. --- packages/core/src/container.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/core/src/container.ts b/packages/core/src/container.ts index 9696b8483..c03953296 100644 --- a/packages/core/src/container.ts +++ b/packages/core/src/container.ts @@ -123,13 +123,14 @@ class Container { * @returns Whether the item was successfully added into the container. */ public addItem(item: ItemStack): boolean { + let amount = item.stackSize // Non-stackable logic. if (!item.isStackable) { const emptySlot = this.storage.indexOf(null); if (emptySlot > -1) { this.setItem(emptySlot, item); // Item was fully transferred already. - item.stackSize = 0; + amount = 0; return true; } // No empty slot was found. @@ -137,7 +138,7 @@ class Container { } // Loop as long as there are items left in the stack to be added. - while (item.stackSize > 0) { + while (amount > 0) { const existingSlotIndex = this.storage.findIndex( (slot) => slot && @@ -152,11 +153,11 @@ class Container { // Calculate how many items we can add to this stack. const amountToAdd = Math.min( existingItem.maxStackSize - existingItem.stackSize, - item.stackSize + amount ); existingItem.incrementStack(amountToAdd); - item.decrementStack(amountToAdd); + amount -= amountToAdd; continue; } @@ -167,13 +168,12 @@ class Container { // If there's an empty slot, put items there. if (emptySlotIndex > -1) { // Determine how many items to put in the new stack. - const amountToSet = Math.min(item.maxStackSize, item.stackSize); + const amountToSet = Math.min(item.maxStackSize, amount); - // Clone new itemstack for empty slot. - const newItem = new ItemStack(item.type, { ...item, stackSize: amountToSet }); + item.stackSize = amountToSet; - this.setItem(emptySlotIndex, newItem); - item.decrementStack(amountToSet); + this.setItem(emptySlotIndex, item); + amount -= amountToSet; continue; } @@ -183,7 +183,7 @@ class Container { } // Whether or not all the items were successfully added. - return item.stackSize === 0; + return amount === 0; } /**