mc-beta-server/server/windows/Window.ts

90 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

import InventoryType from "../enums/InventoryType";
import Inventory from "../inventories/Inventory";
2024-11-29 15:00:48 +00:00
import ItemStack from "../inventories/ItemStack";
import PlayerCombinedInventory from "../inventories/PlayerCombinedInventory";
import MPClient from "../MPClient";
import PacketOpenWindow from "../packets/OpenWindow";
2024-12-02 00:25:03 +00:00
import PacketSetSlot from "../packets/SetSlot";
import PacketWindowItems from "../packets/WindowItems";
export default abstract class Window {
public static WINDOW_GLOBAL_COUNTER = 1;
public readonly inventorySize: number;
public windowId = Window.WINDOW_GLOBAL_COUNTER++;
public inventoryType: InventoryType;
public inventory: PlayerCombinedInventory;
2024-12-02 00:25:03 +00:00
private readonly mpClient: MPClient;
private readonly inventoryUpdateHandle: number;
public cursorItemStack: ItemStack | null;
2024-12-02 00:25:03 +00:00
public constructor(inventoryType: InventoryType, inventory: PlayerCombinedInventory, mpClient: MPClient, inventorySize: number) {
this.inventorySize = inventorySize;
2024-11-29 15:00:48 +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());
}
});
this.cursorItemStack = null;
}
2024-12-02 00:25:03 +00:00
openWindow() {
const windowPacket = new PacketOpenWindow(this.windowId, this.inventoryType, this.inventory.getInventoryName(), this.inventory.getInventorySize()).writeData();
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));
}
clickedWindow(slotId: number, rightClick: boolean) {
2024-12-02 00:25:03 +00:00
const slotItemStack = this.inventory.getSlotItemStack(slotId);
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;
}
}
} 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-02 00:25:03 +00:00
closeWindow() {
this.inventory.unregisterChangeHandler(this.inventoryUpdateHandle);
}
}