2023-12-25 02:45:21 +00:00
|
|
|
import { IReader } from "bufferstuff";
|
2024-10-26 14:24:38 +01:00
|
|
|
import { Console } from "hsconsole";
|
2023-05-02 10:24:48 +01:00
|
|
|
import { Socket } from "net";
|
2023-12-24 17:47:20 +00:00
|
|
|
import AABB from "./AABB";
|
2024-10-26 14:24:38 +01:00
|
|
|
import Block from "./blocks/Block";
|
|
|
|
import EntityLiving from "./entities/EntityLiving";
|
|
|
|
import EntityItem from "./entities/EntityItem";
|
|
|
|
import ItemStack from "./inventories/ItemStack";
|
|
|
|
import MinecraftServer from "./MinecraftServer";
|
|
|
|
import Packet from "./enums/Packet";
|
|
|
|
import PacketAnimation from "./packets/Animation";
|
|
|
|
import PacketChat from "./packets/Chat"
|
|
|
|
import PacketEntityAction from "./packets/EntityAction";
|
|
|
|
import PacketPlayer from "./packets/Player";
|
|
|
|
import PacketPlayerPosition from "./packets/PlayerPosition";
|
|
|
|
import PacketPlayerLook from "./packets/PlayerLook";
|
|
|
|
import PacketPlayerPositionLook from "./packets/PlayerPositionLook";
|
|
|
|
import PacketPlayerDigging from "./packets/PlayerDigging";
|
|
|
|
import PacketRespawn from "./packets/Respawn";
|
|
|
|
import PacketPlayerBlockPlacement from "./packets/PlayerBlockPlacement";
|
|
|
|
import PacketHoldingChange from "./packets/HoldingChange";
|
|
|
|
import PacketDisconnectKick from "./packets/DisconnectKick";
|
|
|
|
import PacketSoundEffect from "./packets/SoundEffect";
|
|
|
|
import PacketUseEntity from "./packets/UseEntity";
|
|
|
|
import Player from "./entities/Player";
|
2023-12-25 02:45:21 +00:00
|
|
|
import PlayerInventory from "./inventories/PlayerInventory";
|
2024-10-26 14:24:38 +01:00
|
|
|
import SoundEffects from "./enums/SoundEffects";
|
|
|
|
import Vec3 from "./Vec3";
|
2024-11-25 02:30:27 +00:00
|
|
|
import TextColorParser from "./TextColorParser";
|
2024-11-26 19:24:08 +00:00
|
|
|
import FunkyArray from "funky-array";
|
|
|
|
import Window from "./windows/Window";
|
|
|
|
import WindowChest from "./windows/WindowChest";
|
|
|
|
import TileEntityChest from "./tileentities/TileEntityChest";
|
2024-11-29 15:00:48 +00:00
|
|
|
import WindowCrafting from "./windows/WindowCrafting";
|
|
|
|
import PacketWindowClick from "./packets/WindowClick";
|
2024-12-01 00:19:32 +00:00
|
|
|
import PlayerCombinedInventory from "./inventories/PlayerCombinedInventory";
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2024-10-26 14:24:38 +01:00
|
|
|
export default class MPClient {
|
2023-04-10 14:42:14 +01:00
|
|
|
private readonly mcServer:MinecraftServer;
|
2023-04-08 20:52:47 +01:00
|
|
|
private readonly socket:Socket;
|
2023-09-04 23:42:38 +01:00
|
|
|
public entity:Player;
|
2023-12-25 02:45:21 +00:00
|
|
|
private inventory:PlayerInventory;
|
2023-11-05 00:55:23 +00:00
|
|
|
private dimension:number;
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-10-29 05:08:26 +00:00
|
|
|
private holdingIndex:number = 36; // First hotbar slot.
|
2023-04-10 21:52:30 +01:00
|
|
|
private diggingAt:Vec3;
|
|
|
|
|
2024-11-26 19:24:08 +00:00
|
|
|
private windows: FunkyArray<number, Window>;
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
public constructor(mcServer:MinecraftServer, socket:Socket, entity:Player) {
|
|
|
|
this.mcServer = mcServer;
|
2023-04-08 20:52:47 +01:00
|
|
|
this.socket = socket;
|
|
|
|
this.entity = entity;
|
2023-10-29 05:08:26 +00:00
|
|
|
this.inventory = entity.inventory;
|
2023-11-05 00:55:23 +00:00
|
|
|
this.dimension = 0;
|
2023-04-10 21:52:30 +01:00
|
|
|
|
|
|
|
this.diggingAt = new Vec3();
|
2024-11-26 19:24:08 +00:00
|
|
|
this.windows = new FunkyArray<number, Window>();
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-10-29 05:08:26 +00:00
|
|
|
private mapCoordsFromFace(pos:Vec3, face:number) {
|
2023-04-10 14:42:14 +01:00
|
|
|
switch (face) {
|
|
|
|
case 0:
|
|
|
|
pos.y--;
|
|
|
|
return pos;
|
|
|
|
case 1:
|
|
|
|
pos.y++;
|
|
|
|
return pos;
|
|
|
|
case 2:
|
|
|
|
pos.z--;
|
|
|
|
return pos;
|
|
|
|
case 3:
|
|
|
|
pos.z++;
|
|
|
|
return pos;
|
|
|
|
case 4:
|
|
|
|
pos.x--;
|
|
|
|
return pos;
|
|
|
|
case 5:
|
|
|
|
pos.x++;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:24:48 +01:00
|
|
|
public handlePacket(reader:IReader) {
|
2023-04-09 04:19:10 +01:00
|
|
|
const packetId = reader.readUByte();
|
|
|
|
|
|
|
|
switch (packetId) {
|
2023-04-10 21:52:30 +01:00
|
|
|
case Packet.Chat: this.handleChat(new PacketChat().readData(reader)); break;
|
2023-12-24 17:47:20 +00:00
|
|
|
case Packet.UseEntity: this.handleUseEntity(new PacketUseEntity().readData(reader)); break;
|
2023-11-13 00:00:33 +00:00
|
|
|
case Packet.Respawn: this.handlePacketRespawn(new PacketRespawn().readData(reader)); break;
|
2023-04-10 21:52:30 +01:00
|
|
|
case Packet.Player: this.handlePacketPlayer(new PacketPlayer().readData(reader)); break;
|
|
|
|
case Packet.PlayerPosition: this.handlePacketPlayerPosition(new PacketPlayerPosition().readData(reader)); break;
|
|
|
|
case Packet.PlayerLook: this.handlePacketPlayerLook(new PacketPlayerLook().readData(reader)); break;
|
|
|
|
case Packet.PlayerPositionLook: this.handlePacketPlayerPositionLook(new PacketPlayerPositionLook().readData(reader)); break;
|
|
|
|
case Packet.PlayerDigging: this.handlePacketPlayerDigging(new PacketPlayerDigging().readData(reader)); break;
|
2023-10-29 05:08:26 +00:00
|
|
|
case Packet.PlayerBlockPlacement: this.handlePacketBlockPlacement(new PacketPlayerBlockPlacement().readData(reader)); break;
|
|
|
|
case Packet.HoldingChange: this.handlePacketHoldingChange(new PacketHoldingChange().readData(reader)); break;
|
2023-04-10 21:52:30 +01:00
|
|
|
//case Packets.UseBed: break;
|
|
|
|
case Packet.Animation: this.handlePacketAnimation(new PacketAnimation().readData(reader)); break;
|
|
|
|
case Packet.EntityAction: this.handlePacketEntityAction(new PacketEntityAction().readData(reader)); break;
|
2024-11-29 15:00:48 +00:00
|
|
|
case Packet.WindowClick: this.handleWindowClick(new PacketWindowClick().readData(reader)); break;
|
2023-08-20 01:18:05 +01:00
|
|
|
case Packet.DisconnectKick: this.handleDisconnectKick(); break;
|
2024-11-29 15:00:48 +00:00
|
|
|
default: return Console.printWarn(`UNIMPLEMENTED PACKET: ${Packet[packetId]} 0x${packetId < 10 ? `0${packetId.toString(16).toUpperCase()}` : packetId.toString(16).toUpperCase()}`);
|
2023-11-07 01:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reader.readOffset < reader.length - 1) {
|
|
|
|
this.handlePacket(reader);
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:47:20 +00:00
|
|
|
private handleUseEntity(packet:PacketUseEntity) {
|
|
|
|
const attacker = this.entity.world.entites.get(packet.userId);
|
|
|
|
const target = this.entity.world.entites.get(packet.targetId);
|
|
|
|
if (attacker && target && target instanceof EntityLiving) {
|
|
|
|
if (packet.leftClick) {
|
|
|
|
target.damageFrom(2, attacker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
private handleChat(packet:PacketChat) {
|
2023-04-09 04:19:10 +01:00
|
|
|
const message = packet.message.split(" ");
|
2023-04-10 14:42:14 +01:00
|
|
|
if (message[0].startsWith("/")) {
|
|
|
|
packet.message = "";
|
|
|
|
if (message[0] === "/tp") {
|
2023-11-02 08:31:43 +00:00
|
|
|
const x = this.entity.position.x = parseFloat(message[1]);
|
|
|
|
const y = this.entity.position.y = parseFloat(message[2]);
|
|
|
|
const z = this.entity.position.z = parseFloat(message[3]);
|
2023-09-04 23:42:38 +01:00
|
|
|
this.send(new PacketPlayerPositionLook(x, y, y + 0.62, z, 0, 0, false).writeData());
|
2023-04-10 14:42:14 +01:00
|
|
|
Console.printInfo(packet.message = `Teleported ${this.entity.username} to ${message[1]} ${message[2]} ${message[3]}`);
|
|
|
|
} else if (message[0] === "/csay") {
|
2023-08-20 01:18:05 +01:00
|
|
|
this.mcServer.sendChatMessage(`[CONSOLE] ${message.slice(1, message.length).join(" ")}`);
|
2023-04-17 02:05:11 +01:00
|
|
|
} else if (message[0] === "/top") {
|
|
|
|
packet.message = `Woosh!`;
|
2023-11-02 08:31:43 +00:00
|
|
|
const topBlock = this.entity.chunk.getTopBlockY(this.entity.position.x & 0xf, this.entity.position.z & 0xf);
|
|
|
|
this.send(new PacketPlayerPosition(this.entity.position.x, topBlock + 3, topBlock + 3.62, this.entity.position.z, false).writeData());
|
2023-09-04 23:42:38 +01:00
|
|
|
} else if (message[0] === "/tpx") {
|
|
|
|
const dimension = parseInt(message[1]);
|
|
|
|
if (this.mcServer.worlds.has(dimension)) {
|
|
|
|
packet.message = "\u00a76Switching dimensions...";
|
|
|
|
this.switchDimension(dimension);
|
|
|
|
} else {
|
2023-11-02 08:31:43 +00:00
|
|
|
packet.message = `\u00a7cNo dimension by id "${dimension}" exists!`;
|
2023-09-04 23:42:38 +01:00
|
|
|
}
|
2023-04-10 14:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (packet.message !== "") {
|
|
|
|
this.send(packet.writeData());
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
2023-04-10 14:42:14 +01:00
|
|
|
|
|
|
|
packet.message = `<${this.entity.username}> ${packet.message}`;
|
2024-11-25 02:30:27 +00:00
|
|
|
Console.printInfo(`[CHAT] ${TextColorParser.ParseConsole(packet.message)}`);
|
2023-04-10 14:42:14 +01:00
|
|
|
this.mcServer.sendToAllClients(packet.writeData());
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 00:00:33 +00:00
|
|
|
private handlePacketRespawn(packet:PacketRespawn) {
|
|
|
|
if (!this.entity.isDead && packet.dimension === this.entity.world.dimension) {
|
2023-11-05 00:55:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-13 00:00:33 +00:00
|
|
|
|
|
|
|
const world = this.mcServer.worlds.get(this.entity.world.dimension);
|
|
|
|
if (world == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entity.world.removeEntity(this.entity);
|
|
|
|
const oldPlayerEntity = this.entity;
|
|
|
|
|
|
|
|
this.entity = new Player(this.mcServer, world, oldPlayerEntity.username);
|
|
|
|
this.entity.position.set(8, 70, 8);
|
|
|
|
world.addEntity(this.entity);
|
|
|
|
|
|
|
|
this.send(new PacketRespawn(world.dimension).writeData());
|
|
|
|
//this.send(new PacketSpawnPosition(8, 64, 8).writeData());
|
|
|
|
this.entity.position.set(this.entity.position.x, this.entity.position.y, this.entity.position.z);
|
|
|
|
this.send(new PacketPlayerPositionLook(this.entity.position.x, this.entity.position.y, this.entity.position.y + 0.62, this.entity.position.z, 0, 0, false).writeData());
|
|
|
|
|
|
|
|
this.entity.forceUpdatePlayerChunks();
|
2023-11-05 00:55:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
private handlePacketPlayer(packet:PacketPlayer) {
|
2023-04-09 04:47:23 +01:00
|
|
|
this.entity.onGround = packet.onGround;
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
private handlePacketPlayerPosition(packet:PacketPlayerPosition) {
|
2023-11-05 00:55:23 +00:00
|
|
|
this.entity.onGround = packet.onGround;
|
2023-11-02 08:31:43 +00:00
|
|
|
this.entity.position.set(packet.x, packet.y, packet.z);
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
private handlePacketPlayerLook(packet:PacketPlayerLook) {
|
2023-11-05 00:55:23 +00:00
|
|
|
this.entity.onGround = packet.onGround;
|
2023-11-02 08:31:43 +00:00
|
|
|
this.entity.rotation.set(packet.yaw, packet.pitch);
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
private handlePacketPlayerPositionLook(packet:PacketPlayerPositionLook) {
|
2023-11-05 00:55:23 +00:00
|
|
|
this.entity.onGround = packet.onGround;
|
2023-11-02 08:31:43 +00:00
|
|
|
this.entity.position.set(packet.x, packet.y, packet.z);
|
|
|
|
this.entity.rotation.set(packet.yaw, packet.pitch);
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-11-07 01:50:51 +00:00
|
|
|
private breakBlock(brokenBlockId:number, x:number, y:number, z:number) {
|
|
|
|
const metadata = this.entity.world.getBlockMetadata(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z);
|
|
|
|
this.entity.world.setBlockWithNotify(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z, 0);
|
2023-12-06 00:04:57 +00:00
|
|
|
//this.inventory.addItemStack(new ItemStack(Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId), 1, metadata));
|
|
|
|
//this.send(new PacketWindowItems(0, this.inventory.getInventorySize(), this.inventory.constructInventoryPayload()).writeData());
|
2023-12-24 17:47:20 +00:00
|
|
|
const blockBehaviour = Block.blockBehaviours[brokenBlockId];
|
2024-11-25 22:28:33 +00:00
|
|
|
blockBehaviour?.destroyed(this.entity.world, x, y, z);
|
2023-12-24 17:47:20 +00:00
|
|
|
const itemId = blockBehaviour.droppedItem(brokenBlockId);
|
2023-11-11 00:14:27 +00:00
|
|
|
if (itemId !== -1) {
|
2023-12-24 17:47:20 +00:00
|
|
|
const itemCount = blockBehaviour.droppedCount(brokenBlockId);
|
2023-12-25 02:45:21 +00:00
|
|
|
this.entity.sendToNearby(new PacketSoundEffect(SoundEffects.BLOCK_BREAK, x, y, z, brokenBlockId).writeData());
|
2023-12-24 17:47:20 +00:00
|
|
|
for (let i = 0; i < ((itemCount - 1) >> 6) + 1; i++) {
|
|
|
|
const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemId, Math.min(itemCount - 64 * i, 64), metadata));
|
|
|
|
itemEntity.position.set(x + 0.5, y + 0.5, z + 0.5);
|
2023-12-25 02:45:21 +00:00
|
|
|
itemEntity.pickupDelay = 10;
|
2023-12-24 17:47:20 +00:00
|
|
|
this.entity.world.addEntity(itemEntity);
|
|
|
|
}
|
2023-12-06 00:04:57 +00:00
|
|
|
}
|
2023-11-07 01:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Cap how far away a player is able to break blocks
|
2023-04-10 21:52:30 +01:00
|
|
|
private handlePacketPlayerDigging(packet:PacketPlayerDigging) {
|
2023-11-05 00:55:23 +00:00
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
// Special drop item case
|
|
|
|
if (packet.status === 4) {
|
2023-12-25 02:45:21 +00:00
|
|
|
const itemStack = this.getHeldItemStack();
|
|
|
|
if (itemStack !== null && itemStack.size > 0) {
|
|
|
|
itemStack.size--;
|
|
|
|
const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemStack.itemID, 1, itemStack.damage));
|
|
|
|
itemEntity.pickupDelay = 10;
|
|
|
|
itemEntity.position.set(this.entity.position.x, this.entity.position.y + 1.50, this.entity.position.z);
|
|
|
|
itemEntity.motion.set(
|
|
|
|
-Math.sin((this.entity.rotation.yaw / 180) * Math.PI) * Math.cos((this.entity.rotation.pitch / 180) * Math.PI) * 0.3,
|
|
|
|
-Math.sin((this.entity.rotation.pitch / 180) * Math.PI) * 0.3 + 0.1,
|
|
|
|
Math.cos((this.entity.rotation.yaw / 180) * Math.PI) * Math.cos((this.entity.rotation.pitch / 180) * Math.PI) * 0.3
|
|
|
|
);
|
|
|
|
// Add random motion vector
|
|
|
|
const twoPIRandomised = Math.random() * Math.PI * 2;
|
|
|
|
const rngMult = 0.02 * Math.random();
|
|
|
|
itemEntity.motion.add(
|
|
|
|
Math.cos(twoPIRandomised) * rngMult,
|
|
|
|
(Math.random() - Math.random()) * 0.1,
|
|
|
|
Math.sin(twoPIRandomised) * rngMult
|
|
|
|
);
|
|
|
|
this.entity.world.addEntity(itemEntity);
|
|
|
|
|
|
|
|
this.inventory.dropEmptyItemStacks();
|
|
|
|
this.inventory.sendUpdatedStacks([this.holdingIndex]);
|
|
|
|
}
|
2023-04-10 21:52:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.diggingAt.set(packet.x, packet.y, packet.z);
|
|
|
|
|
2023-11-07 01:50:51 +00:00
|
|
|
let brokenBlockId:number;
|
2023-04-10 21:52:30 +01:00
|
|
|
if (packet.status === 0) {
|
|
|
|
// Started digging
|
2023-11-07 01:50:51 +00:00
|
|
|
if ((brokenBlockId = this.entity.world.getBlockId(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z)) != 0 && Block.blocks[brokenBlockId].blockStrength() >= 1) {
|
|
|
|
this.breakBlock(brokenBlockId, this.diggingAt.x, this.diggingAt.y, this.diggingAt.z);
|
|
|
|
}
|
2023-04-10 21:52:30 +01:00
|
|
|
} else if (packet.status === 2) {
|
2023-11-02 08:31:43 +00:00
|
|
|
if ((brokenBlockId = this.entity.world.getBlockId(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z)) != 0) {
|
2023-11-07 01:50:51 +00:00
|
|
|
this.breakBlock(brokenBlockId, this.diggingAt.x, this.diggingAt.y, this.diggingAt.z);
|
2023-04-10 21:52:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
public getHeldItemStack() {
|
|
|
|
return this.inventory.getSlotItemStack(this.holdingIndex);
|
|
|
|
}
|
|
|
|
|
2023-10-29 05:08:26 +00:00
|
|
|
private handlePacketBlockPlacement(packet:PacketPlayerBlockPlacement) {
|
|
|
|
this.diggingAt.set(packet.x, packet.y, packet.z);
|
|
|
|
this.mapCoordsFromFace(this.diggingAt, packet.face);
|
|
|
|
|
2024-11-26 19:24:08 +00:00
|
|
|
const blockClicked = Block.blocks[this.entity.world.getBlockId(packet.x, packet.y, packet.z)];
|
|
|
|
if (!this.entity.crouching && blockClicked && blockClicked.behaviour.interactable()) {
|
|
|
|
if (blockClicked.is(Block.chest)) {
|
|
|
|
const tileEntity = this.entity.world.getChunk(packet.x >> 4, packet.z >> 4).getTileEntity(packet.x, packet.y, packet.z);
|
|
|
|
if (tileEntity && tileEntity instanceof TileEntityChest) {
|
2024-12-01 00:19:32 +00:00
|
|
|
const window = new WindowChest(PlayerCombinedInventory.FromExisting(this, tileEntity.inventory, tileEntity.inventory.name));
|
2024-11-26 19:24:08 +00:00
|
|
|
this.windows.set(window.windowId, window);
|
|
|
|
window.openWindow(this);
|
|
|
|
}
|
2024-11-29 15:00:48 +00:00
|
|
|
} else if (blockClicked.is(Block.craftingTable)) {
|
2024-12-01 00:19:32 +00:00
|
|
|
const window = new WindowCrafting(new PlayerCombinedInventory(this, 10, "Crafting"));
|
2024-11-29 15:00:48 +00:00
|
|
|
this.windows.set(window.windowId, window);
|
|
|
|
window.openWindow(this);
|
2024-11-26 19:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:47:20 +00:00
|
|
|
if (this.entity.entityAABB.intersects(AABB.getAABB(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z, this.diggingAt.x + 1, this.diggingAt.y + 1, this.diggingAt.z + 1))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
const itemStack = this.getHeldItemStack();
|
|
|
|
if (itemStack == null || itemStack.size == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-18 21:47:03 +01:00
|
|
|
if (itemStack.isBlock && Block.blocks[itemStack.itemID].behaviour.canPlaceBlockAt(this.entity.world, this.diggingAt.x, this.diggingAt.y, this.diggingAt.z)) {
|
2023-11-02 08:31:43 +00:00
|
|
|
if (this.entity.world.getBlockId(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z) === 0) {
|
|
|
|
itemStack.size--;
|
|
|
|
this.entity.world.setBlockAndMetadataWithNotify(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z, itemStack.itemID, itemStack.damage);
|
2024-11-25 22:28:33 +00:00
|
|
|
Block.blockBehaviours[itemStack.itemID]?.placed(this.entity.world, this.diggingAt.x, this.diggingAt.y, this.diggingAt.z);
|
2023-11-07 01:50:51 +00:00
|
|
|
this.inventory.dropEmptyItemStacks();
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: Handle item usage
|
2023-10-29 05:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private handlePacketHoldingChange(packet:PacketHoldingChange) {
|
|
|
|
if (packet.slotId < 0 || packet.slotId > 8) {
|
|
|
|
this.send(new PacketDisconnectKick("Out of Bounds Holding Index!").writeData());
|
|
|
|
this.socket.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.holdingIndex = 36 + packet.slotId;
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
// Animation start
|
|
|
|
private handlePacketAnimation(packet:PacketAnimation) {
|
|
|
|
// Forward this packet to all nearby clients
|
|
|
|
this.entity.world.sendToNearbyClients(this.entity, packet.writeData());
|
|
|
|
}
|
2023-04-10 14:42:14 +01:00
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
private handlePacketEntityAction(packet:PacketEntityAction) {
|
|
|
|
// Forward this packet to all nearby clients
|
|
|
|
switch (packet.action) {
|
|
|
|
case 1: this.entity.crouching = true; break;
|
|
|
|
case 2: this.entity.crouching = false; break;
|
|
|
|
case 3: break; // TODO: Leave Bed
|
|
|
|
}
|
2023-04-10 14:42:14 +01:00
|
|
|
}
|
|
|
|
|
2023-09-04 23:42:38 +01:00
|
|
|
private switchDimension(dimension:number) {
|
|
|
|
const world = this.mcServer.worlds.get(dimension);
|
|
|
|
if (world == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entity.world.removeEntity(this.entity);
|
|
|
|
this.entity.world = world;
|
|
|
|
world.addEntity(this.entity);
|
|
|
|
|
|
|
|
this.send(new PacketRespawn(dimension).writeData());
|
|
|
|
//this.send(new PacketSpawnPosition(8, 64, 8).writeData());
|
2023-11-02 08:31:43 +00:00
|
|
|
this.entity.position.set(8, 60, 8);
|
2023-09-04 23:42:38 +01:00
|
|
|
this.send(new PacketPlayerPositionLook(8, 70, 70.62, 8, 0, 0, false).writeData());
|
|
|
|
|
|
|
|
this.entity.forceUpdatePlayerChunks();
|
|
|
|
}
|
|
|
|
|
2024-11-29 15:00:48 +00:00
|
|
|
private handleWindowClick(windowClick: PacketWindowClick) {
|
|
|
|
console.log(windowClick);
|
|
|
|
}
|
|
|
|
|
2023-08-20 01:18:05 +01:00
|
|
|
private handleDisconnectKick() {
|
|
|
|
this.socket.end();
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:24:48 +01:00
|
|
|
public send(buffer:Buffer) {
|
|
|
|
this.socket.write(buffer);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
}
|