crafting table
All checks were successful
Node.js Build / build (20.x) (push) Successful in 5m16s

This commit is contained in:
Holly Stubbs 2024-11-29 15:00:48 +00:00
parent 6492f7c363
commit 65d31be4f9
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
11 changed files with 85 additions and 7 deletions

View file

@ -31,6 +31,8 @@ import FunkyArray from "funky-array";
import Window from "./windows/Window";
import WindowChest from "./windows/WindowChest";
import TileEntityChest from "./tileentities/TileEntityChest";
import WindowCrafting from "./windows/WindowCrafting";
import PacketWindowClick from "./packets/WindowClick";
export default class MPClient {
private readonly mcServer:MinecraftServer;
@ -95,8 +97,9 @@ export default class MPClient {
//case Packets.UseBed: break;
case Packet.Animation: this.handlePacketAnimation(new PacketAnimation().readData(reader)); break;
case Packet.EntityAction: this.handlePacketEntityAction(new PacketEntityAction().readData(reader)); break;
case Packet.WindowClick: this.handleWindowClick(new PacketWindowClick().readData(reader)); break;
case Packet.DisconnectKick: this.handleDisconnectKick(); break;
default: return Console.printWarn(`UNIMPLEMENTED PACKET: ${Packet[packetId]}`);
default: return Console.printWarn(`UNIMPLEMENTED PACKET: ${Packet[packetId]} 0x${packetId < 10 ? `0${packetId.toString(16).toUpperCase()}` : packetId.toString(16).toUpperCase()}`);
}
if (reader.readOffset < reader.length - 1) {
@ -281,6 +284,10 @@ export default class MPClient {
this.windows.set(window.windowId, window);
window.openWindow(this);
}
} else if (blockClicked.is(Block.craftingTable)) {
const window = new WindowCrafting(this);
this.windows.set(window.windowId, window);
window.openWindow(this);
}
return;
@ -350,6 +357,10 @@ export default class MPClient {
this.entity.forceUpdatePlayerChunks();
}
private handleWindowClick(windowClick: PacketWindowClick) {
console.log(windowClick);
}
private handleDisconnectKick() {
this.socket.end();
}

View file

@ -12,6 +12,7 @@ import BlockBehaviourTallGrass from "./BlockBehaviourTallGrass";
import IBlockBehaviour from "./IBlockBehaviour";
import World from "../World";
import BlockBehaviourChest from "./BlockBehaviourChest";
import BlockBehaviourCraftingTable from "./BlockBehaviourCraftingTable";
abstract class Behaviour {
public static base = new BlockBehaviour();
@ -26,6 +27,8 @@ abstract class Behaviour {
public static chest = new BlockBehaviourChest();
public static craftingTable = new BlockBehaviourCraftingTable();
public static redstoneOre = new BlockBehaviourRedstoneOre();
public static clay = new BlockBehaviourClay();
@ -222,7 +225,7 @@ export default class Block {
static readonly redstoneDust = new Block(55).setHardness(0).setBlockName("Redstone Dust"); // TODO: Behavior script
static readonly diamondOre = new Block(56).setHardness(3).setBlockName("Diamond Ore"); // TODO: Behavior script
static readonly diamondBlock = new Block(57).setHardness(5).setBlockName("Diamond Block"); // TODO: Behavior script
static readonly craftingTable = new Block(58).setHardness(2.5).setBlockName("Crafting Table"); // TODO: Behavior script
static readonly craftingTable = new Block(58).setHardness(2.5).setBehaviour(Behaviour.craftingTable).setBlockName("Crafting Table"); // TODO: Behavior script
static readonly wheatCrop = new Block(59).setHardness(0).setBlockName("Wheet Crop"); // TODO: Behavior script
static readonly farmland = new Block(60).setHardness(0.6).setBlockName("Farmland"); // TODO: Behavior script
static readonly furnaceIdle = new Block(61).setHardness(3.5).setBlockName("Furnace"); // TODO: Behavior script

View file

@ -0,0 +1,10 @@
import TileEntityChest from "../tileentities/TileEntityChest";
import Vec3 from "../Vec3";
import World from "../World";
import BlockBehaviour from "./BlockBehaviour";
export default class BlockBehaviourCraftingTable extends BlockBehaviour {
public interactable() {
return true;
}
}

View file

@ -42,6 +42,7 @@ export default class Player extends EntityLiving {
this.inventory.setSlotItemStack(37, new ItemStack(Item.ironPickaxe, 1));
this.inventory.setSlotItemStack(38, new ItemStack(Item.ironShovel, 1));
this.inventory.setSlotItemStack(39, new ItemStack(Item.ironAxe, 1));
this.inventory.setSlotItemStack(41, new ItemStack(Block.craftingTable, 1));
this.inventory.setSlotItemStack(42, new ItemStack(Block.chest, 32));
this.inventory.setSlotItemStack(43, new ItemStack(Block.dirt, 32));

View file

@ -1,6 +1,6 @@
enum InventoryType {
Chest = 0,
Workbench = 1,
CraftingTable = 1,
Furnace = 2,
Dispenser = 3
}

View file

@ -1,6 +1,7 @@
enum TileEntityType {
Unknown,
Chest
Chest,
CraftingTable
}
export default TileEntityType;

View file

@ -1,14 +1,19 @@
import { Endian, IReader, IWriter, createWriter } from "bufferstuff";
import FunkyArray from "funky-array";
import IInventory from "./IInventory";
import ItemStack from "./ItemStack";
export default class Inventory implements IInventory {
private static CHANGE_HANDLER_ROLLING_HANDLE_ID = 0;
public changeHandlers:FunkyArray<number, (itemStack: ItemStack) => void>;
public itemStacks:Array<ItemStack | null>;
private size:number;
private name:string;
public constructor(size:number, name:string) {
this.changeHandlers = new FunkyArray<number, (itemStack: ItemStack) => void>();
this.itemStacks = new Array<ItemStack | null>();
for (let i = 0; i < size; i++) {
this.itemStacks.push(null);
@ -18,6 +23,18 @@ export default class Inventory implements IInventory {
this.name = name;
}
registerChangeHandler(changeHandler: (itemStack: ItemStack) => void) {
const changeHandlerHandle = Inventory.CHANGE_HANDLER_ROLLING_HANDLE_ID++;
this.changeHandlers.set(changeHandlerHandle, changeHandler);
return changeHandlerHandle;
}
unregisterChangeHandler(changeHandlerHandle: number) {
if (this.changeHandlers.has(changeHandlerHandle)) {
this.changeHandlers.remove(changeHandlerHandle);
}
}
public fromSave(reader:IReader) {
const inventorySize = reader.readByte();
for (let i = 0; i < inventorySize; i++) {

View file

@ -0,0 +1,16 @@
import Inventory from "./Inventory";
import ItemStack from "./ItemStack";
export default class PlayerCombinedInventory extends Inventory {
private combinedInventoryChangeHandlerHandle: number;
public constructor(size: number, name: string) {
super(size, name);
this.combinedInventoryChangeHandlerHandle = this.registerChangeHandler(this.onInventoryChange);
}
private onInventoryChange(itemStack: ItemStack) {
}
}

View file

@ -42,13 +42,19 @@ export default class PacketWindowClick implements IPacket {
this.actionNumber = reader.readShort();
this.shift = reader.readBool();
this.itemId = reader.readShort();
this.itemCount = reader.readByte();
this.itemUses = reader.readShort();
if (this.itemId !== -1) {
this.itemCount = reader.readByte();
this.itemUses = reader.readShort();
}
return this;
}
public writeData() {
return createWriter(Endian.BE, 4).writeUByte(this.packetId).writeByte(this.windowId).writeShort(this.slot).writeBool(this.rightClick).writeShort(this.actionNumber).writeShort(this.itemId).writeByte(this.itemCount).writeShort(this.itemUses).toBuffer();
const writer = createWriter(Endian.BE, 4).writeUByte(this.packetId).writeByte(this.windowId).writeShort(this.slot).writeBool(this.rightClick).writeShort(this.actionNumber).writeShort(this.itemId);
if (this.itemId !== -1) {
writer.writeByte(this.itemCount).writeShort(this.itemUses);
}
return writer.toBuffer();
}
}

View file

@ -1,5 +1,6 @@
import InventoryType from "../enums/InventoryType";
import Inventory from "../inventories/Inventory";
import ItemStack from "../inventories/ItemStack";
import MPClient from "../MPClient";
import PacketOpenWindow from "../packets/OpenWindow";
@ -10,6 +11,8 @@ export default abstract class Window {
public inventoryType: InventoryType;
public inventory: Inventory;
public cursorItemStack?: ItemStack;
public constructor(inventoryType: InventoryType, inventory: Inventory) {
this.inventoryType = inventoryType;
this.inventory = inventory;

View file

@ -0,0 +1,10 @@
import InventoryType from "../enums/InventoryType";
import Inventory from "../inventories/Inventory";
import MPClient from "../MPClient";
import Window from "./Window";
export default class WindowCrafting extends Window {
public constructor(mpClient: MPClient) {
super(InventoryType.CraftingTable, new Inventory(45, "Crafting"));
}
}