2023-12-18 01:23:52 +00:00
|
|
|
import { Player } from "../entities/Player";
|
2023-11-02 08:31:43 +00:00
|
|
|
import { Inventory } from "./Inventory";
|
|
|
|
import { ItemStack } from "./ItemStack";
|
|
|
|
|
|
|
|
export default class PlayerInventory extends Inventory {
|
2023-12-18 01:23:52 +00:00
|
|
|
private player:Player;
|
|
|
|
|
|
|
|
public constructor(player:Player) {
|
2023-11-02 08:31:43 +00:00
|
|
|
super(44, "Player Inventory");
|
2023-12-18 01:23:52 +00:00
|
|
|
|
|
|
|
this.player = player;
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addItemStack(itemStack:ItemStack) {
|
|
|
|
const itemStacksOfSameType:Array<ItemStack> = new Array<ItemStack>();
|
|
|
|
|
|
|
|
// Check bottom inventory row (hotbar) first.
|
|
|
|
let workingItemStack:ItemStack | null;
|
|
|
|
for (let slotId = 36; slotId <= 44; slotId++) {
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) != null) {
|
|
|
|
itemStacksOfSameType.push(workingItemStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let slotId = 9; slotId <= 35; slotId++) {
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) != null) {
|
|
|
|
itemStacksOfSameType.push(workingItemStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert into existing stacks first.
|
|
|
|
for (const inventoryItemStack of itemStacksOfSameType) {
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inventoryItemStack.itemID !== itemStack.itemID || inventoryItemStack.damage !== itemStack.damage) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryItemStack.insert(itemStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let slotId = 36; slotId <= 44; slotId++) {
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) == null) {
|
|
|
|
const stack = this.itemStacks[slotId] = new ItemStack(itemStack.itemID, 0, itemStack.damage);
|
|
|
|
stack.insert(itemStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let slotId = 9; slotId <= 35; slotId++) {
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) == null) {
|
|
|
|
const stack = this.itemStacks[slotId] = new ItemStack(itemStack.itemID, 0, itemStack.damage);
|
|
|
|
stack.insert(itemStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|