-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chests work better i suppose, but it crashes now :c
- Loading branch information
Showing
8 changed files
with
128 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,93 @@ | ||
import { createWriter, Endian } from "bufferstuff"; | ||
import MPClient from "../MPClient"; | ||
import Inventory from "./Inventory"; | ||
import ItemStack from "./ItemStack"; | ||
|
||
export default class PlayerCombinedInventory extends Inventory { | ||
private combinedInventoryChangeHandlerHandle: number; | ||
private static PLAYER_INVENTORY_OFFSET = 10; | ||
|
||
public constructor(size: number, name: string) { | ||
private mpClient: MPClient; | ||
|
||
public constructor(mpClient:MPClient, size: number, name: string) { | ||
super(size, name); | ||
|
||
this.combinedInventoryChangeHandlerHandle = this.registerChangeHandler(this.onInventoryChange); | ||
this.mpClient = mpClient; | ||
} | ||
|
||
private getSlotId(slotId: number) { | ||
if (slotId > this.size - 1) { | ||
return slotId + PlayerCombinedInventory.PLAYER_INVENTORY_OFFSET; | ||
} else { | ||
return slotId; | ||
} | ||
} | ||
|
||
static FromExisting(mpClient: MPClient, inventory: Inventory, name: string) { | ||
const linkedInventory = new PlayerCombinedInventory(mpClient, inventory.size, name); | ||
linkedInventory.itemStacks = inventory.itemStacks; | ||
return linkedInventory; | ||
} | ||
|
||
addItemStack(itemStack:ItemStack) { | ||
for (let slotId = 0; slotId < this.itemStacks.length; slotId++) { | ||
if (itemStack.size === 0) { | ||
break; | ||
} | ||
|
||
this.itemStacks[slotId]?.insert(itemStack); | ||
} | ||
|
||
if (itemStack.size === 0) { | ||
return; | ||
} | ||
|
||
for (let slotId = 0; slotId < this.mpClient.entity.inventory.itemStacks.length; slotId++) { | ||
if (itemStack.size === 0) { | ||
break; | ||
} | ||
|
||
this.mpClient.entity.inventory.itemStacks[slotId]?.insert(itemStack); | ||
} | ||
} | ||
|
||
private onInventoryChange(itemStack: ItemStack) { | ||
|
||
// getInventorySize() { | ||
// return this.itemStacks.length + this.mpClient.entity.inventory.itemStacks.length; | ||
// } | ||
|
||
getSlotItemStack(slotId:number) { | ||
return this.itemStacks[slotId] ?? this.mpClient.entity.inventory.itemStacks[this.getSlotId(slotId)]; | ||
} | ||
|
||
dropEmptyItemStacks() { | ||
super.dropEmptyItemStacks(); | ||
this.mpClient.entity.inventory.dropEmptyItemStacks(); | ||
} | ||
|
||
setSlotItemStack(slotId:number, itemStack: ItemStack | null) { | ||
if (slotId > this.size - 1) { | ||
this.mpClient.entity.inventory.setSlotItemStack(this.getSlotId(slotId), itemStack); | ||
} else { | ||
super.setSlotItemStack(slotId, itemStack); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
constructInventoryPayload() { | ||
const thisInventoryPayload = super.constructInventoryPayload(0); | ||
const playerInventoryPayload = this.mpClient.entity.inventory.constructInventoryPayload(PlayerCombinedInventory.PLAYER_INVENTORY_OFFSET); | ||
return Buffer.concat([thisInventoryPayload, playerInventoryPayload], thisInventoryPayload.length + playerInventoryPayload.length); | ||
} | ||
|
||
constructInventorySinglePayload(slotId:number) { | ||
const stack = this.itemStacks[slotId] ?? this.mpClient.entity.inventory.itemStacks[this.getSlotId(slotId)]; | ||
const writer = createWriter(Endian.BE, stack == null ? 2 : 5); | ||
writer.writeShort(stack == null ? -1 : stack.itemID); | ||
if (stack != null) { | ||
writer.writeByte(stack.size); | ||
writer.writeShort(stack.damage); | ||
} | ||
|
||
return writer.toBuffer(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
import InventoryType from "../enums/InventoryType"; | ||
import Inventory from "../inventories/Inventory"; | ||
import ItemStack from "../inventories/ItemStack"; | ||
import PlayerCombinedInventory from "../inventories/PlayerCombinedInventory"; | ||
import MPClient from "../MPClient"; | ||
import PacketOpenWindow from "../packets/OpenWindow"; | ||
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: Inventory; | ||
public inventory: PlayerCombinedInventory; | ||
|
||
public cursorItemStack: ItemStack | null; | ||
|
||
public cursorItemStack?: ItemStack; | ||
public constructor(inventoryType: InventoryType, inventory: PlayerCombinedInventory, inventorySize: number) { | ||
this.inventorySize = inventorySize; | ||
|
||
public constructor(inventoryType: InventoryType, inventory: Inventory) { | ||
this.inventoryType = inventoryType; | ||
this.inventory = inventory; | ||
|
||
this.cursorItemStack = null; | ||
} | ||
|
||
openWindow(mpClient: MPClient) { | ||
const windowPacket = new PacketOpenWindow(this.windowId, this.inventoryType, this.inventory.getInventoryName(), this.inventory.getInventorySize()).writeData(); | ||
//const inventoryDataPayload = this.inventory.constructInventoryPayload(); | ||
//mpClient.send(Buffer.concat([ windowPacket, inventoryDataPayload ], windowPacket.length + inventoryDataPayload.length)); | ||
mpClient.send(windowPacket); | ||
const windowItems = new PacketWindowItems(this.windowId, this.inventorySize, this.inventory.constructInventoryPayload()).writeData(); | ||
mpClient.send(Buffer.concat([ windowPacket, windowItems ], windowPacket.length + windowItems.length)); | ||
//mpClient.send(windowPacket); | ||
//mpClient.send(inventoryDataPayload); | ||
} | ||
|
||
clickedWindow(slotId: number, rightClick: boolean) { | ||
if (this.cursorItemStack) { | ||
|
||
} else { | ||
this.cursorItemStack = this.inventory.getSlotItemStack(slotId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import InventoryType from "../enums/InventoryType"; | ||
import Inventory from "../inventories/Inventory"; | ||
import PlayerCombinedInventory from "../inventories/PlayerCombinedInventory"; | ||
import Window from "./Window"; | ||
|
||
export default class WindowChest extends Window { | ||
public constructor(inventory: Inventory) { | ||
super(InventoryType.Chest, inventory); | ||
public constructor(inventory: PlayerCombinedInventory) { | ||
super(InventoryType.Chest, inventory, 62); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import InventoryType from "../enums/InventoryType"; | ||
import Inventory from "../inventories/Inventory"; | ||
import PlayerCombinedInventory from "../inventories/PlayerCombinedInventory"; | ||
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")); | ||
public constructor(inventory: PlayerCombinedInventory) { | ||
super(InventoryType.CraftingTable, inventory, 45); | ||
} | ||
} |