From 42cef0a83858f4b826bb2d99128e3f84a891808d Mon Sep 17 00:00:00 2001 From: Holly Date: Tue, 11 Apr 2023 01:53:33 +0100 Subject: [PATCH] WIP: Inventory support --- server/blocks/Block.ts | 15 +++++----- server/containers/Container.ts | 13 +++++++++ server/containers/ItemStack.ts | 49 ++++++++++++++++++++++++++++++++ server/containers/Slot.ts | 11 +++++++ server/inventories/IInventory.ts | 7 +++++ server/items/Item.ts | 16 +++++++++++ 6 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 server/containers/Container.ts create mode 100644 server/containers/ItemStack.ts create mode 100644 server/containers/Slot.ts create mode 100644 server/inventories/IInventory.ts create mode 100644 server/items/Item.ts diff --git a/server/blocks/Block.ts b/server/blocks/Block.ts index 918929e..48badfe 100644 --- a/server/blocks/Block.ts +++ b/server/blocks/Block.ts @@ -5,15 +5,16 @@ export class Block { this.blockId = blockId; } - static stone = new Block(1); - static grass = new Block(2); - static dirt = new Block(3); + // Define statics here + static readonly stone = new Block(1); + static readonly grass = new Block(2); + static readonly dirt = new Block(3); - static bedrock = new Block(7); + static readonly bedrock = new Block(7); - static waterStill = new Block(9); + static readonly waterStill = new Block(9); @@ -21,6 +22,6 @@ export class Block { - static wood = new Block(17); - static leaves = new Block(18); + static readonly wood = new Block(17); + static readonly leaves = new Block(18); } \ No newline at end of file diff --git a/server/containers/Container.ts b/server/containers/Container.ts new file mode 100644 index 0000000..ed28e3f --- /dev/null +++ b/server/containers/Container.ts @@ -0,0 +1,13 @@ +import { FunkyArray } from "../../funkyArray"; +import { ItemStack } from "./ItemStack"; +import { ContainerSlot } from "./Slot"; + +export abstract class Container { + public itemSlots:Array; + public itemStacks:Array; + + public constructor() { + this.itemSlots = new Array(); + this.itemStacks = new Array(); + } +} \ No newline at end of file diff --git a/server/containers/ItemStack.ts b/server/containers/ItemStack.ts new file mode 100644 index 0000000..53f6be8 --- /dev/null +++ b/server/containers/ItemStack.ts @@ -0,0 +1,49 @@ +import { Block } from "../blocks/Block"; +import { Item } from "../items/Item"; + +export class ItemStack { + public readonly itemID:number; + public stackSize:number; + public damage:number; + + public constructor(blockOrItemOrItemID:Block|Item|number, stackSize?:number, damage?:number) { + if (blockOrItemOrItemID instanceof Block && stackSize === undefined && damage === undefined) { + this.itemID = blockOrItemOrItemID.blockId; + this.stackSize = 1; + this.damage = 0; + } else if (blockOrItemOrItemID instanceof Block && typeof(stackSize) === "number" && damage === undefined) { + this.itemID = blockOrItemOrItemID.blockId; + this.stackSize = stackSize; + this.damage = 0; + } else if (blockOrItemOrItemID instanceof Block && typeof(stackSize) === "number" && typeof(damage) === "number") { + this.itemID = blockOrItemOrItemID.blockId; + this.stackSize = stackSize; + this.damage = damage; + } else if (blockOrItemOrItemID instanceof Item && stackSize === undefined && damage === undefined) { + this.itemID = blockOrItemOrItemID.shiftedItemID; + this.stackSize = 1; + this.damage = 0; + } else if (blockOrItemOrItemID instanceof Item && typeof(stackSize) === "number" && damage === undefined) { + this.itemID = blockOrItemOrItemID.shiftedItemID; + this.stackSize = stackSize; + this.damage = 0; + } else if (blockOrItemOrItemID instanceof Item && typeof(stackSize) === "number" && typeof(damage) === "number") { + this.itemID = blockOrItemOrItemID.shiftedItemID; + this.stackSize = stackSize; + this.damage = damage; + } else if (typeof(blockOrItemOrItemID) === "number" && typeof(stackSize) === "number" && typeof(damage) === "number") { + this.itemID = blockOrItemOrItemID; + this.stackSize = stackSize; + this.damage = damage; + } else { + this.itemID = Number.MIN_VALUE; + this.stackSize = Number.MIN_VALUE; + this.damage = Number.MIN_VALUE; + } + } + + split(amount:number) { + this.stackSize -= amount; + return new ItemStack(this.itemID, amount, this.damage); + } +} \ No newline at end of file diff --git a/server/containers/Slot.ts b/server/containers/Slot.ts new file mode 100644 index 0000000..643b201 --- /dev/null +++ b/server/containers/Slot.ts @@ -0,0 +1,11 @@ +import { IInventory } from "../inventories/IInventory"; + +export class ContainerSlot { + public inventory:IInventory; + public index:number; + + public constructor(inventory:IInventory, index:number) { + this.inventory = inventory; + this.index = index; + } +} \ No newline at end of file diff --git a/server/inventories/IInventory.ts b/server/inventories/IInventory.ts new file mode 100644 index 0000000..aed63ff --- /dev/null +++ b/server/inventories/IInventory.ts @@ -0,0 +1,7 @@ +import { ItemStack } from "../containers/ItemStack"; + +export interface IInventory { + getInventoryName:() => string, + getInventorySize:() => number, + getSlotItemStack:(slotId:number) => ItemStack +} \ No newline at end of file diff --git a/server/items/Item.ts b/server/items/Item.ts new file mode 100644 index 0000000..85a3a69 --- /dev/null +++ b/server/items/Item.ts @@ -0,0 +1,16 @@ +export class Item { + public maxStackSize:number; + public shiftedItemID:number; + + public constructor(itemID:number) { + this.shiftedItemID = 256 + itemID; + this.maxStackSize = 64; + } + + public setMaxStackSize(stackSize:number) { + this.maxStackSize = stackSize; + return this; + } + + // Define statics here +} \ No newline at end of file