mc-beta-server/server/MPClient.ts

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-04-08 20:52:47 +01:00
import { Socket } from "net";
2023-04-09 04:19:10 +01:00
import { Reader, Writer } from "../bufferStuff";
import { Packets } from "./enums/Packets";
import { PacketPlayer } from "./packets/Player";
import { PacketPlayerPosition } from "./packets/PlayerPosition";
import { PacketPlayerLook } from "./packets/PlayerLook";
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
import { Player } from "./entities/Player";
import { PacketChat } from "./packets/Chat";
2023-04-10 14:42:14 +01:00
import { MinecraftServer } from "./MinecraftServer";
import { Vec3 } from "./Vec3";
import { Console } from "../console";
2023-04-08 20:52:47 +01:00
export 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-04-09 04:19:10 +01:00
public readonly entity:Player;
2023-04-08 20:52:47 +01:00
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-04-10 14:42:14 +01:00
private mapCoordsToFace(pos:Vec3, face:number) {
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;
}
}
public handlePacket(reader:Reader) {
2023-04-09 04:19:10 +01:00
const packetId = reader.readUByte();
switch (packetId) {
case Packets.Chat: this.handleChat(new PacketChat().readData(reader)); break;
case Packets.Player: this.handlePacketPlayer(new PacketPlayer().readData(reader)); break;
case Packets.PlayerPosition: this.handlePacketPlayerPosition(new PacketPlayerPosition().readData(reader)); break;
case Packets.PlayerLook: this.handlePacketPlayerLook(new PacketPlayerLook().readData(reader)); break;
case Packets.PlayerPositionLook: this.handlePacketPlayerPositionLook(new PacketPlayerPositionLook().readData(reader)); break;
2023-04-10 14:42:14 +01:00
case Packets.PlayerDigging: this.handlePacketPlayerDigging(); break;
2023-04-09 04:19:10 +01:00
}
}
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") {
this.send(new PacketPlayerPositionLook(parseFloat(message[1]), parseFloat(message[2]), parseFloat(message[2]) + 0.62, parseFloat(message[3]), 0, 0, false).writeData());
Console.printInfo(packet.message = `Teleported ${this.entity.username} to ${message[1]} ${message[2]} ${message[3]}`);
} else if (message[0] === "/csay") {
const consoleMessage = `[CONSOLE] ${message.slice(1, message.length).join(" ")}`;
Console.printChat(consoleMessage);
this.mcServer.sendToAllClients(new PacketChat(consoleMessage).writeData());
}
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}`;
Console.printChat(packet.message);
this.mcServer.sendToAllClients(packet.writeData());
2023-04-09 04:19:10 +01: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-04-09 04:19:10 +01:00
this.entity.x = packet.x;
this.entity.y = packet.y;
this.entity.z = packet.z;
}
2023-04-10 14:42:14 +01:00
private handlePacketPlayerLook(packet:PacketPlayerLook) {
2023-04-09 04:19:10 +01:00
this.entity.yaw = packet.yaw;
this.entity.pitch = packet.pitch;
}
2023-04-10 14:42:14 +01:00
private handlePacketPlayerPositionLook(packet:PacketPlayerPositionLook) {
2023-04-09 04:19:10 +01:00
this.entity.x = packet.x;
this.entity.y = packet.y;
this.entity.z = packet.z;
this.entity.yaw = packet.yaw;
this.entity.pitch = packet.pitch;
}
2023-04-10 14:42:14 +01:00
private handlePacketPlayerDigging() {
}
public send(buffer:Buffer|Writer) {
2023-04-08 20:52:47 +01:00
if (buffer instanceof Writer) {
this.socket.write(buffer.toBuffer());
} else {
this.socket.write(buffer);
}
}
}