Skip to content
Open
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
99 changes: 48 additions & 51 deletions packages/core/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,70 +123,67 @@
* @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;
let amount = item.stackSize

Check warning on line 126 in packages/core/src/container.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `;`
// Non-stackable logic.
if (!item.isStackable) {
const emptySlot = this.storage.indexOf(null);
if (emptySlot > -1) {
this.setItem(emptySlot, item);
// Item was fully transferred already.
amount = 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 (amount > 0) {
const existingSlotIndex = this.storage.findIndex(
(slot) =>
slot &&

Check warning on line 144 in packages/core/src/container.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎··········slot.stackSize·<·slot.maxStackSize·&&⏎·········` with `·slot.stackSize·<·slot.maxStackSize·&&`
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,
amount
);

// Calculate the amount of items to add.
const amount = Math.min(
item.maxStackSize - existingItem.stackSize,
item.stackSize
);
existingItem.incrementStack(amountToAdd);
amount -= 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, amount);

// 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
});
item.stackSize = amountToSet;

// Add the new Item and decrease it
this.setItem(emptySlot, newItem);
item.decrementStack(item.maxStackSize);
this.setItem(emptySlotIndex, item);
amount -= 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 amount === 0;
}

/**
Expand Down
Loading