This commit is contained in:
parent
6492f7c363
commit
65d31be4f9
11 changed files with 85 additions and 7 deletions
|
@ -31,6 +31,8 @@ import FunkyArray from "funky-array";
|
||||||
import Window from "./windows/Window";
|
import Window from "./windows/Window";
|
||||||
import WindowChest from "./windows/WindowChest";
|
import WindowChest from "./windows/WindowChest";
|
||||||
import TileEntityChest from "./tileentities/TileEntityChest";
|
import TileEntityChest from "./tileentities/TileEntityChest";
|
||||||
|
import WindowCrafting from "./windows/WindowCrafting";
|
||||||
|
import PacketWindowClick from "./packets/WindowClick";
|
||||||
|
|
||||||
export default class MPClient {
|
export default class MPClient {
|
||||||
private readonly mcServer:MinecraftServer;
|
private readonly mcServer:MinecraftServer;
|
||||||
|
@ -95,8 +97,9 @@ export default class MPClient {
|
||||||
//case Packets.UseBed: break;
|
//case Packets.UseBed: break;
|
||||||
case Packet.Animation: this.handlePacketAnimation(new PacketAnimation().readData(reader)); break;
|
case Packet.Animation: this.handlePacketAnimation(new PacketAnimation().readData(reader)); break;
|
||||||
case Packet.EntityAction: this.handlePacketEntityAction(new PacketEntityAction().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;
|
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) {
|
if (reader.readOffset < reader.length - 1) {
|
||||||
|
@ -281,6 +284,10 @@ export default class MPClient {
|
||||||
this.windows.set(window.windowId, window);
|
this.windows.set(window.windowId, window);
|
||||||
window.openWindow(this);
|
window.openWindow(this);
|
||||||
}
|
}
|
||||||
|
} else if (blockClicked.is(Block.craftingTable)) {
|
||||||
|
const window = new WindowCrafting(this);
|
||||||
|
this.windows.set(window.windowId, window);
|
||||||
|
window.openWindow(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -350,6 +357,10 @@ export default class MPClient {
|
||||||
this.entity.forceUpdatePlayerChunks();
|
this.entity.forceUpdatePlayerChunks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private handleWindowClick(windowClick: PacketWindowClick) {
|
||||||
|
console.log(windowClick);
|
||||||
|
}
|
||||||
|
|
||||||
private handleDisconnectKick() {
|
private handleDisconnectKick() {
|
||||||
this.socket.end();
|
this.socket.end();
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import BlockBehaviourTallGrass from "./BlockBehaviourTallGrass";
|
||||||
import IBlockBehaviour from "./IBlockBehaviour";
|
import IBlockBehaviour from "./IBlockBehaviour";
|
||||||
import World from "../World";
|
import World from "../World";
|
||||||
import BlockBehaviourChest from "./BlockBehaviourChest";
|
import BlockBehaviourChest from "./BlockBehaviourChest";
|
||||||
|
import BlockBehaviourCraftingTable from "./BlockBehaviourCraftingTable";
|
||||||
|
|
||||||
abstract class Behaviour {
|
abstract class Behaviour {
|
||||||
public static base = new BlockBehaviour();
|
public static base = new BlockBehaviour();
|
||||||
|
@ -26,6 +27,8 @@ abstract class Behaviour {
|
||||||
|
|
||||||
public static chest = new BlockBehaviourChest();
|
public static chest = new BlockBehaviourChest();
|
||||||
|
|
||||||
|
public static craftingTable = new BlockBehaviourCraftingTable();
|
||||||
|
|
||||||
public static redstoneOre = new BlockBehaviourRedstoneOre();
|
public static redstoneOre = new BlockBehaviourRedstoneOre();
|
||||||
|
|
||||||
public static clay = new BlockBehaviourClay();
|
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 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 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 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 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 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
|
static readonly furnaceIdle = new Block(61).setHardness(3.5).setBlockName("Furnace"); // TODO: Behavior script
|
||||||
|
|
10
server/blocks/BlockBehaviourCraftingTable.ts
Normal file
10
server/blocks/BlockBehaviourCraftingTable.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,6 +42,7 @@ export default class Player extends EntityLiving {
|
||||||
this.inventory.setSlotItemStack(37, new ItemStack(Item.ironPickaxe, 1));
|
this.inventory.setSlotItemStack(37, new ItemStack(Item.ironPickaxe, 1));
|
||||||
this.inventory.setSlotItemStack(38, new ItemStack(Item.ironShovel, 1));
|
this.inventory.setSlotItemStack(38, new ItemStack(Item.ironShovel, 1));
|
||||||
this.inventory.setSlotItemStack(39, new ItemStack(Item.ironAxe, 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(42, new ItemStack(Block.chest, 32));
|
||||||
this.inventory.setSlotItemStack(43, new ItemStack(Block.dirt, 32));
|
this.inventory.setSlotItemStack(43, new ItemStack(Block.dirt, 32));
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
enum InventoryType {
|
enum InventoryType {
|
||||||
Chest = 0,
|
Chest = 0,
|
||||||
Workbench = 1,
|
CraftingTable = 1,
|
||||||
Furnace = 2,
|
Furnace = 2,
|
||||||
Dispenser = 3
|
Dispenser = 3
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
enum TileEntityType {
|
enum TileEntityType {
|
||||||
Unknown,
|
Unknown,
|
||||||
Chest
|
Chest,
|
||||||
|
CraftingTable
|
||||||
}
|
}
|
||||||
|
|
||||||
export default TileEntityType;
|
export default TileEntityType;
|
|
@ -1,14 +1,19 @@
|
||||||
import { Endian, IReader, IWriter, createWriter } from "bufferstuff";
|
import { Endian, IReader, IWriter, createWriter } from "bufferstuff";
|
||||||
|
import FunkyArray from "funky-array";
|
||||||
import IInventory from "./IInventory";
|
import IInventory from "./IInventory";
|
||||||
import ItemStack from "./ItemStack";
|
import ItemStack from "./ItemStack";
|
||||||
|
|
||||||
export default class Inventory implements IInventory {
|
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>;
|
public itemStacks:Array<ItemStack | null>;
|
||||||
|
|
||||||
private size:number;
|
private size:number;
|
||||||
private name:string;
|
private name:string;
|
||||||
|
|
||||||
public constructor(size:number, name:string) {
|
public constructor(size:number, name:string) {
|
||||||
|
this.changeHandlers = new FunkyArray<number, (itemStack: ItemStack) => void>();
|
||||||
this.itemStacks = new Array<ItemStack | null>();
|
this.itemStacks = new Array<ItemStack | null>();
|
||||||
for (let i = 0; i < size; i++) {
|
for (let i = 0; i < size; i++) {
|
||||||
this.itemStacks.push(null);
|
this.itemStacks.push(null);
|
||||||
|
@ -18,6 +23,18 @@ export default class Inventory implements IInventory {
|
||||||
this.name = name;
|
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) {
|
public fromSave(reader:IReader) {
|
||||||
const inventorySize = reader.readByte();
|
const inventorySize = reader.readByte();
|
||||||
for (let i = 0; i < inventorySize; i++) {
|
for (let i = 0; i < inventorySize; i++) {
|
||||||
|
|
16
server/inventories/PlayerCombinedInventory.ts
Normal file
16
server/inventories/PlayerCombinedInventory.ts
Normal 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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,13 +42,19 @@ export default class PacketWindowClick implements IPacket {
|
||||||
this.actionNumber = reader.readShort();
|
this.actionNumber = reader.readShort();
|
||||||
this.shift = reader.readBool();
|
this.shift = reader.readBool();
|
||||||
this.itemId = reader.readShort();
|
this.itemId = reader.readShort();
|
||||||
this.itemCount = reader.readByte();
|
if (this.itemId !== -1) {
|
||||||
this.itemUses = reader.readShort();
|
this.itemCount = reader.readByte();
|
||||||
|
this.itemUses = reader.readShort();
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public writeData() {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
import InventoryType from "../enums/InventoryType";
|
import InventoryType from "../enums/InventoryType";
|
||||||
import Inventory from "../inventories/Inventory";
|
import Inventory from "../inventories/Inventory";
|
||||||
|
import ItemStack from "../inventories/ItemStack";
|
||||||
import MPClient from "../MPClient";
|
import MPClient from "../MPClient";
|
||||||
import PacketOpenWindow from "../packets/OpenWindow";
|
import PacketOpenWindow from "../packets/OpenWindow";
|
||||||
|
|
||||||
|
@ -10,6 +11,8 @@ export default abstract class Window {
|
||||||
public inventoryType: InventoryType;
|
public inventoryType: InventoryType;
|
||||||
public inventory: Inventory;
|
public inventory: Inventory;
|
||||||
|
|
||||||
|
public cursorItemStack?: ItemStack;
|
||||||
|
|
||||||
public constructor(inventoryType: InventoryType, inventory: Inventory) {
|
public constructor(inventoryType: InventoryType, inventory: Inventory) {
|
||||||
this.inventoryType = inventoryType;
|
this.inventoryType = inventoryType;
|
||||||
this.inventory = inventory;
|
this.inventory = inventory;
|
||||||
|
|
10
server/windows/WindowCrafting.ts
Normal file
10
server/windows/WindowCrafting.ts
Normal 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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue