2024-11-26 19:24:08 +00:00
|
|
|
import InventoryType from "../enums/InventoryType";
|
|
|
|
import Inventory from "../inventories/Inventory";
|
2024-11-29 15:00:48 +00:00
|
|
|
import ItemStack from "../inventories/ItemStack";
|
2024-12-01 00:19:32 +00:00
|
|
|
import PlayerCombinedInventory from "../inventories/PlayerCombinedInventory";
|
2024-11-26 19:24:08 +00:00
|
|
|
import MPClient from "../MPClient";
|
|
|
|
import PacketOpenWindow from "../packets/OpenWindow";
|
2024-12-02 00:25:03 +00:00
|
|
|
import PacketSetSlot from "../packets/SetSlot";
|
2024-12-01 00:19:32 +00:00
|
|
|
import PacketWindowItems from "../packets/WindowItems";
|
2024-11-26 19:24:08 +00:00
|
|
|
|
|
|
|
export default abstract class Window {
|
|
|
|
public static WINDOW_GLOBAL_COUNTER = 1;
|
|
|
|
|
2024-12-01 00:19:32 +00:00
|
|
|
public readonly inventorySize: number;
|
|
|
|
|
2024-11-26 19:24:08 +00:00
|
|
|
public windowId = Window.WINDOW_GLOBAL_COUNTER++;
|
|
|
|
public inventoryType: InventoryType;
|
2024-12-01 00:19:32 +00:00
|
|
|
public inventory: PlayerCombinedInventory;
|
2024-12-02 00:25:03 +00:00
|
|
|
private readonly mpClient: MPClient;
|
|
|
|
|
|
|
|
private readonly inventoryUpdateHandle: number;
|
2024-12-01 00:19:32 +00:00
|
|
|
|
|
|
|
public cursorItemStack: ItemStack | null;
|
2024-11-26 19:24:08 +00:00
|
|
|
|
2024-12-02 00:25:03 +00:00
|
|
|
public constructor(inventoryType: InventoryType, inventory: PlayerCombinedInventory, mpClient: MPClient, inventorySize: number) {
|
2024-12-01 00:19:32 +00:00
|
|
|
this.inventorySize = inventorySize;
|
2024-11-29 15:00:48 +00:00
|
|
|
|
2024-11-26 19:24:08 +00:00
|
|
|
this.inventoryType = inventoryType;
|
|
|
|
this.inventory = inventory;
|
2024-12-02 00:25:03 +00:00
|
|
|
this.mpClient = mpClient;
|
|
|
|
|
|
|
|
this.inventoryUpdateHandle = this.inventory.registerChangeHandler((slotId) => {
|
|
|
|
const slotItem = this.inventory.getSlotItemStack(slotId);
|
|
|
|
if (slotItem == null) {
|
|
|
|
this.mpClient.send(new PacketSetSlot(this.windowId, slotId, -1).writeData());
|
|
|
|
} else {
|
|
|
|
this.mpClient.send(new PacketSetSlot(this.windowId, slotId, slotItem.itemID, slotItem.size, slotItem.damage).writeData());
|
|
|
|
}
|
|
|
|
});
|
2024-12-01 00:19:32 +00:00
|
|
|
|
|
|
|
this.cursorItemStack = null;
|
2024-11-26 19:24:08 +00:00
|
|
|
}
|
|
|
|
|
2024-12-02 00:25:03 +00:00
|
|
|
openWindow() {
|
2024-11-26 19:24:08 +00:00
|
|
|
const windowPacket = new PacketOpenWindow(this.windowId, this.inventoryType, this.inventory.getInventoryName(), this.inventory.getInventorySize()).writeData();
|
2024-12-01 00:19:32 +00:00
|
|
|
const windowItems = new PacketWindowItems(this.windowId, this.inventorySize, this.inventory.constructInventoryPayload()).writeData();
|
2024-12-02 00:25:03 +00:00
|
|
|
this.mpClient.send(Buffer.concat([ windowPacket, windowItems ], windowPacket.length + windowItems.length));
|
2024-12-01 00:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clickedWindow(slotId: number, rightClick: boolean) {
|
2024-12-02 00:25:03 +00:00
|
|
|
const slotItemStack = this.inventory.getSlotItemStack(slotId);
|
2024-12-01 00:19:32 +00:00
|
|
|
if (this.cursorItemStack) {
|
2024-12-02 00:25:03 +00:00
|
|
|
if (rightClick) {
|
|
|
|
if (slotItemStack) {
|
|
|
|
slotItemStack.insert(this.cursorItemStack.split(1));
|
|
|
|
this.inventory.sendSlotUpdate(slotId);
|
|
|
|
} else {
|
|
|
|
this.inventory.setSlotItemStack(slotId, this.cursorItemStack.split(1));
|
|
|
|
}
|
|
|
|
if (this.cursorItemStack.size === 0) {
|
|
|
|
this.cursorItemStack = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (slotItemStack) {
|
|
|
|
slotItemStack.insert(this.cursorItemStack);
|
|
|
|
this.inventory.sendSlotUpdate(slotId);
|
|
|
|
if (this.cursorItemStack.size === 0) {
|
|
|
|
this.cursorItemStack = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.inventory.setSlotItemStack(slotId, this.cursorItemStack);
|
|
|
|
this.cursorItemStack = null;
|
|
|
|
}
|
|
|
|
}
|
2024-12-01 00:19:32 +00:00
|
|
|
} else {
|
2024-12-02 00:25:03 +00:00
|
|
|
if (slotItemStack) {
|
|
|
|
if (rightClick) {
|
|
|
|
this.cursorItemStack = slotItemStack.split(Math.ceil(slotItemStack.size / 2));
|
|
|
|
this.inventory.sendSlotUpdate(slotId);
|
|
|
|
} else {
|
|
|
|
this.cursorItemStack = slotItemStack;
|
|
|
|
this.inventory.setSlotItemStack(slotId, null);
|
|
|
|
}
|
|
|
|
}
|
2024-12-01 00:19:32 +00:00
|
|
|
}
|
2024-11-26 19:24:08 +00:00
|
|
|
}
|
2024-12-02 00:25:03 +00:00
|
|
|
|
|
|
|
closeWindow() {
|
|
|
|
this.inventory.unregisterChangeHandler(this.inventoryUpdateHandle);
|
|
|
|
}
|
2024-11-26 19:24:08 +00:00
|
|
|
}
|