2023-12-18 01:23:52 +00:00
|
|
|
import { Player } from "../entities/Player";
|
2023-12-18 11:18:15 +00:00
|
|
|
import { PacketSetSlot } from "../packets/SetSlot";
|
|
|
|
import { PacketWindowItems } from "../packets/WindowItems";
|
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
|
|
|
}
|
|
|
|
|
2023-12-18 11:18:15 +00:00
|
|
|
sendUpdatedStacks(stackIdsChanged:Array<number>) {
|
|
|
|
let updateBuffer = Buffer.alloc(0);
|
|
|
|
for (const slotId of stackIdsChanged) {
|
|
|
|
const slotItem = this.itemStacks[slotId];
|
|
|
|
let buffer:Buffer;
|
|
|
|
if (slotItem == null) {
|
|
|
|
buffer = new PacketSetSlot(0, slotId, -1).writeData();
|
|
|
|
} else {
|
|
|
|
buffer = new PacketSetSlot(0, slotId, slotItem.itemID, slotItem.size, slotItem.damage).writeData();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBuffer = Buffer.concat([updateBuffer, buffer], updateBuffer.length + buffer.length);
|
|
|
|
}
|
|
|
|
if (updateBuffer.length > 0) {
|
|
|
|
this.player.mpClient?.send(updateBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
addItemStack(itemStack:ItemStack) {
|
|
|
|
const itemStacksOfSameType:Array<ItemStack> = new Array<ItemStack>();
|
2023-12-18 11:18:15 +00:00
|
|
|
const itemStackIds:Array<number> = new Array<number>();
|
|
|
|
const stackIdsChanged:Array<number> = new Array<number>();
|
2023-11-02 08:31:43 +00:00
|
|
|
|
|
|
|
// 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);
|
2023-12-18 11:18:15 +00:00
|
|
|
itemStackIds.push(slotId);
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let slotId = 9; slotId <= 35; slotId++) {
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) != null) {
|
|
|
|
itemStacksOfSameType.push(workingItemStack);
|
2023-12-18 11:18:15 +00:00
|
|
|
itemStackIds.push(slotId);
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert into existing stacks first.
|
2023-12-18 11:18:15 +00:00
|
|
|
for (let i = 0; i < itemStacksOfSameType.length; i++) {
|
|
|
|
const inventoryItemStack = itemStacksOfSameType[i];
|
2023-11-02 08:31:43 +00:00
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
2023-12-18 11:18:15 +00:00
|
|
|
return this.sendUpdatedStacks(stackIdsChanged);
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inventoryItemStack.itemID !== itemStack.itemID || inventoryItemStack.damage !== itemStack.damage) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:18:15 +00:00
|
|
|
if (inventoryItemStack.insert(itemStack)) {
|
|
|
|
stackIdsChanged.push(itemStackIds[i]);
|
|
|
|
}
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
2023-12-18 11:18:15 +00:00
|
|
|
return this.sendUpdatedStacks(stackIdsChanged);
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let slotId = 36; slotId <= 44; slotId++) {
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
2023-12-18 11:18:15 +00:00
|
|
|
return this.sendUpdatedStacks(stackIdsChanged);
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) == null) {
|
|
|
|
const stack = this.itemStacks[slotId] = new ItemStack(itemStack.itemID, 0, itemStack.damage);
|
2023-12-18 11:18:15 +00:00
|
|
|
if (stack.insert(itemStack)) {
|
|
|
|
stackIdsChanged.push(slotId);
|
|
|
|
}
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let slotId = 9; slotId <= 35; slotId++) {
|
|
|
|
// Exit early if we have nothing left
|
|
|
|
if (itemStack.size === 0) {
|
2023-12-18 11:18:15 +00:00
|
|
|
return this.sendUpdatedStacks(stackIdsChanged);
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((workingItemStack = this.itemStacks[slotId]) == null) {
|
|
|
|
const stack = this.itemStacks[slotId] = new ItemStack(itemStack.itemID, 0, itemStack.damage);
|
2023-12-18 11:18:15 +00:00
|
|
|
if (stack.insert(itemStack)) {
|
|
|
|
stackIdsChanged.push(slotId);
|
|
|
|
}
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 11:18:15 +00:00
|
|
|
|
|
|
|
this.sendUpdatedStacks(stackIdsChanged)
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|