mirror of
https://github.com/MrMasrozYTLIVE/MCServer.git
synced 2024-11-25 06:18:22 -05:00
Added more packets (Player mostly), reworked player position update, started to implement entity stuff.
This commit is contained in:
parent
8847842c88
commit
251b016169
5
src/Entity.ts
Normal file
5
src/Entity.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import {IEntity} from "./IEntity";
|
||||||
|
|
||||||
|
export class Entity implements IEntity {
|
||||||
|
entityID: number;
|
||||||
|
}
|
@ -1,3 +1,3 @@
|
|||||||
export interface IEntity {
|
export interface IEntity {
|
||||||
entityID: Number
|
entityID: number
|
||||||
}
|
}
|
@ -24,6 +24,7 @@ export class MinecraftServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.server.on('close', this.stop);
|
this.server.on('close', this.stop);
|
||||||
|
this.server.on('drop', () => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
public stop() {
|
public stop() {
|
||||||
|
@ -1,24 +1,60 @@
|
|||||||
import {Socket} from "node:net";
|
import {Socket} from "node:net";
|
||||||
import {IEntity} from "./IEntity";
|
import {IEntity} from "./IEntity";
|
||||||
import {PlayerManager} from "./utils/PlayerManager";
|
import {PlayerManager} from "./utils/PlayerManager";
|
||||||
|
import {Entity} from "./Entity";
|
||||||
|
import {PacketEntityPositionLook} from "./packet/impl/entity/PacketEntityPositionLook";
|
||||||
|
import {PacketEntityTeleport} from "./packet/impl/entity/PacketEntityTeleport";
|
||||||
|
|
||||||
export class Player implements IEntity {
|
export class Player extends Entity {
|
||||||
public entityID: Number;
|
|
||||||
public username: String;
|
public username: String;
|
||||||
public socket: Socket;
|
public socket: Socket;
|
||||||
public playerManager: PlayerManager;
|
public playerManager: PlayerManager;
|
||||||
|
|
||||||
public xPosition: number = 0;
|
public xPosition: number = 0;
|
||||||
|
public prevXPosition: number = this.xPosition;
|
||||||
public yPosition: number = 100;
|
public yPosition: number = 100;
|
||||||
public stance: number = 102;
|
public prevYPosition: number = this.yPosition;
|
||||||
public zPosition: number = 0;
|
public zPosition: number = 0;
|
||||||
|
public prevZPosition: number = this.zPosition;
|
||||||
|
public stance: number = 102;
|
||||||
public yaw: number = 0;
|
public yaw: number = 0;
|
||||||
public pitch: number = 0;
|
public pitch: number = 0;
|
||||||
public onGround: boolean = false;
|
public onGround: boolean = false;
|
||||||
|
|
||||||
constructor(username: String, socket: Socket, playerManager: PlayerManager) {
|
constructor(username: String, socket: Socket, playerManager: PlayerManager) {
|
||||||
|
super();
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.playerManager = playerManager;
|
this.playerManager = playerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public updatePosition(x?: number, y?: number, stance?: number, z?: number, yaw?: number, pitch?: number, onGround?: boolean) {
|
||||||
|
if(x) {
|
||||||
|
this.prevXPosition = this.xPosition;
|
||||||
|
this.xPosition = x;
|
||||||
|
}
|
||||||
|
if(y && stance) {
|
||||||
|
this.prevYPosition = this.yPosition;
|
||||||
|
this.yPosition = y;
|
||||||
|
this.stance = stance;
|
||||||
|
|
||||||
|
const sy = stance - y;
|
||||||
|
if(sy < 0.1 || sy > 1.65) this.playerManager.kickPlayer("Illegal Stance");
|
||||||
|
}
|
||||||
|
if(z) {
|
||||||
|
this.prevZPosition = this.zPosition;
|
||||||
|
this.zPosition = z;
|
||||||
|
}
|
||||||
|
if(yaw) this.yaw = yaw;
|
||||||
|
if(pitch) this.pitch = pitch;
|
||||||
|
if(onGround != undefined) this.onGround = onGround;
|
||||||
|
|
||||||
|
// console.log(this.toString());
|
||||||
|
PlayerManager.sendPacketToAll(new PacketEntityTeleport(this.entityID, this.xPosition, this.yPosition, this.zPosition, 0, 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
public toString() {
|
||||||
|
return `[Player ${this.username}] X=${this.xPosition},Y=${this.yPosition},stance=${this.stance},` +
|
||||||
|
`Z=${this.zPosition},yaw=${this.yaw},pitch=${this.pitch},onGround=${this.onGround}`;
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,13 +2,15 @@ import {PacketEnum} from "../utils/PacketEnum";
|
|||||||
import {createWriter, Endian, IReader} from "bufferstuff";
|
import {createWriter, Endian, IReader} from "bufferstuff";
|
||||||
import {Player} from "../Player";
|
import {Player} from "../Player";
|
||||||
import {Socket} from "node:net";
|
import {Socket} from "node:net";
|
||||||
|
import {Entity} from "../Entity";
|
||||||
|
import {IEntity} from "../IEntity";
|
||||||
|
|
||||||
export class Packet {
|
export class Packet {
|
||||||
constructor(public options: IPacketOption) {
|
constructor(public options: IPacketOption) {
|
||||||
options.name = PacketEnum[options.packetID].toString();
|
options.name = PacketEnum[options.packetID].toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
readData(reader: IReader, player: Player) {}
|
readData(reader: IReader, entity: IEntity) {}
|
||||||
writeData() {
|
writeData() {
|
||||||
return createWriter(Endian.BE).toBuffer();
|
return createWriter(Endian.BE).toBuffer();
|
||||||
}
|
}
|
||||||
|
29
src/packet/impl/entity/PacketEntityPositionLook.ts
Normal file
29
src/packet/impl/entity/PacketEntityPositionLook.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import {Packet} from "../../Packet";
|
||||||
|
import {PacketEnum} from "../../../utils/PacketEnum";
|
||||||
|
import {createWriter, Endian, IReader} from "bufferstuff";
|
||||||
|
import {Player} from "../../../Player";
|
||||||
|
import {PlayerManager} from "../../../utils/PlayerManager";
|
||||||
|
import {Entity} from "../../../Entity";
|
||||||
|
|
||||||
|
export class PacketEntityPositionLook extends Packet {
|
||||||
|
constructor(private entityID: number, private dX: number, private dY: number, private dZ: number, private yaw: number, private pitch: number) {
|
||||||
|
super({
|
||||||
|
packetID: PacketEnum.EntityPositionLook
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
readData(reader: IReader, entity: Entity) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
writeData() {
|
||||||
|
return createWriter(Endian.BE).writeUByte(this.options.packetID)
|
||||||
|
.writeInt(this.entityID)
|
||||||
|
.writeByte(this.dX)
|
||||||
|
.writeByte(this.dY)
|
||||||
|
.writeByte(this.dZ)
|
||||||
|
.writeByte(this.yaw)
|
||||||
|
.writeByte(this.pitch)
|
||||||
|
.toBuffer();
|
||||||
|
}
|
||||||
|
}
|
29
src/packet/impl/entity/PacketEntityTeleport.ts
Normal file
29
src/packet/impl/entity/PacketEntityTeleport.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import {Packet} from "../../Packet";
|
||||||
|
import {PacketEnum} from "../../../utils/PacketEnum";
|
||||||
|
import {createWriter, Endian, IReader} from "bufferstuff";
|
||||||
|
import {Player} from "../../../Player";
|
||||||
|
import {PlayerManager} from "../../../utils/PlayerManager";
|
||||||
|
import {Entity} from "../../../Entity";
|
||||||
|
|
||||||
|
export class PacketEntityTeleport extends Packet {
|
||||||
|
constructor(private entityID: number, private dX: number, private dY: number, private dZ: number, private yaw: number, private pitch: number) {
|
||||||
|
super({
|
||||||
|
packetID: PacketEnum.EntityTeleport
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
readData(reader: IReader, entity: Entity) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
writeData() {
|
||||||
|
return createWriter(Endian.BE).writeUByte(this.options.packetID)
|
||||||
|
.writeInt(this.entityID)
|
||||||
|
.writeInt(this.dX)
|
||||||
|
.writeInt(this.dY)
|
||||||
|
.writeInt(this.dZ)
|
||||||
|
.writeByte(this.yaw)
|
||||||
|
.writeByte(this.pitch)
|
||||||
|
.toBuffer();
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import {createWriter, Endian, IReader} from "bufferstuff";
|
|||||||
import {Player} from "../../../Player";
|
import {Player} from "../../../Player";
|
||||||
import {PlayerManager} from "../../../utils/PlayerManager";
|
import {PlayerManager} from "../../../utils/PlayerManager";
|
||||||
import {PacketPreChunk} from "../world/PacketPreChunk";
|
import {PacketPreChunk} from "../world/PacketPreChunk";
|
||||||
|
import {PacketPositionLook} from "./PacketPositionLook";
|
||||||
|
|
||||||
export class PacketChat extends Packet {
|
export class PacketChat extends Packet {
|
||||||
constructor(public message: string) {
|
constructor(public message: string) {
|
||||||
@ -13,9 +14,31 @@ export class PacketChat extends Packet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readData(reader: IReader, player: Player) {
|
readData(reader: IReader, player: Player) {
|
||||||
this.message = `<${player.username}> ${reader.readString16()}`
|
const msg = reader.readString16();
|
||||||
|
this.message = `<${player.username}> ${msg}`
|
||||||
|
|
||||||
|
if(msg.startsWith("/")) {
|
||||||
|
this.handleCommand(player, msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
PlayerManager.sendPacketToAll(this);
|
PlayerManager.sendPacketToAll(this);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
handleCommand(player: Player, msg: string) {
|
||||||
|
if(!msg.startsWith("/")) return;
|
||||||
|
const cmd = msg.replace("/", "").split(" ");
|
||||||
|
if(cmd.length < 1) {
|
||||||
|
this.message = "Unknown command!";
|
||||||
|
return player.playerManager.sendPacket(this)
|
||||||
|
}
|
||||||
|
if(cmd[0] == "tp") {
|
||||||
|
player.xPosition = 0;
|
||||||
|
player.yPosition = 100;
|
||||||
|
player.zPosition = 0;
|
||||||
|
player.playerManager.sendPacket(new PacketPositionLook())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writeData() {
|
writeData() {
|
||||||
|
34
src/packet/impl/player/PacketLook.ts
Normal file
34
src/packet/impl/player/PacketLook.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import {Packet} from "../../Packet";
|
||||||
|
import {PacketEnum} from "../../../utils/PacketEnum";
|
||||||
|
import {createWriter, Endian, IReader} from "bufferstuff";
|
||||||
|
import {Player} from "../../../Player";
|
||||||
|
|
||||||
|
export class PacketLook extends Packet {
|
||||||
|
constructor() {
|
||||||
|
super({
|
||||||
|
packetID: PacketEnum.Look
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
readData(reader: IReader, player: Player) {
|
||||||
|
player.updatePosition(
|
||||||
|
undefined, // X
|
||||||
|
undefined, // Y
|
||||||
|
undefined, // Stance
|
||||||
|
undefined, // Z
|
||||||
|
reader.readFloat(), // Yaw
|
||||||
|
reader.readFloat(), // Pitch
|
||||||
|
reader.readBool() // onGround
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
writeData() {
|
||||||
|
const player: Player = this.options.player;
|
||||||
|
|
||||||
|
return createWriter(Endian.BE).writeUByte(this.options.packetID)
|
||||||
|
.writeLong(player.yaw)
|
||||||
|
.writeLong(player.pitch)
|
||||||
|
.writeBool(player.onGround)
|
||||||
|
.toBuffer();
|
||||||
|
}
|
||||||
|
}
|
@ -11,11 +11,15 @@ export class PacketPosition extends Packet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readData(reader: IReader, player: Player) {
|
readData(reader: IReader, player: Player) {
|
||||||
player.xPosition = reader.readDouble();
|
player.updatePosition(
|
||||||
player.yPosition = reader.readDouble();
|
reader.readDouble(), // X
|
||||||
player.stance = reader.readDouble();
|
reader.readDouble(), // Y
|
||||||
player.zPosition = reader.readDouble();
|
reader.readDouble(), // Stance
|
||||||
player.onGround = reader.readBool();
|
reader.readDouble(), // Z
|
||||||
|
undefined, // Yaw
|
||||||
|
undefined, // Pitch
|
||||||
|
reader.readBool() // onGround
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
writeData() {
|
writeData() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {Packet} from "../../Packet";
|
import {Packet} from "../../Packet";
|
||||||
import {PacketEnum} from "../../../utils/PacketEnum";
|
import {PacketEnum} from "../../../utils/PacketEnum";
|
||||||
import {createWriter, Endian, IReader} from "bufferstuff";
|
import {createWriter, Endian, IReader, IWriter} from "bufferstuff";
|
||||||
import {Player} from "../../../Player";
|
import {Player} from "../../../Player";
|
||||||
|
|
||||||
export class PacketPositionLook extends Packet {
|
export class PacketPositionLook extends Packet {
|
||||||
@ -11,13 +11,15 @@ export class PacketPositionLook extends Packet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readData(reader: IReader, player: Player) {
|
readData(reader: IReader, player: Player) {
|
||||||
player.xPosition = reader.readDouble();
|
player.updatePosition(
|
||||||
player.yPosition = reader.readDouble();
|
reader.readDouble(), // X
|
||||||
player.stance = reader.readDouble();
|
reader.readDouble(), // Y
|
||||||
player.zPosition = reader.readDouble();
|
reader.readDouble(), // Stance
|
||||||
player.yaw = reader.readFloat();
|
reader.readDouble(), // Z
|
||||||
player.pitch = reader.readFloat();
|
reader.readFloat(), // Yaw
|
||||||
player.onGround = reader.readBool();
|
reader.readFloat(), // Pitch
|
||||||
|
reader.readBool() // onGround
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
writeData() {
|
writeData() {
|
||||||
@ -28,8 +30,8 @@ export class PacketPositionLook extends Packet {
|
|||||||
.writeDouble(player.stance)
|
.writeDouble(player.stance)
|
||||||
.writeDouble(player.yPosition)
|
.writeDouble(player.yPosition)
|
||||||
.writeDouble(player.zPosition)
|
.writeDouble(player.zPosition)
|
||||||
.writeLong(player.yaw)
|
.writeFloat(player.yaw)
|
||||||
.writeLong(player.pitch)
|
.writeFloat(player.pitch)
|
||||||
.writeBool(player.onGround)
|
.writeBool(player.onGround)
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@ export enum PacketEnum {
|
|||||||
Look = 0x0C,
|
Look = 0x0C,
|
||||||
PositionLook = 0x0D,
|
PositionLook = 0x0D,
|
||||||
Animation = 0x12,
|
Animation = 0x12,
|
||||||
|
EntityPositionLook = 0x21,
|
||||||
|
EntityTeleport = 0x22,
|
||||||
PreChunk = 0x32,
|
PreChunk = 0x32,
|
||||||
MapChunk = 0x33,
|
MapChunk = 0x33,
|
||||||
MultiBlockChange = 0x34,
|
MultiBlockChange = 0x34,
|
||||||
|
@ -30,7 +30,7 @@ export class PlayerManager {
|
|||||||
|
|
||||||
if(packet === undefined) {
|
if(packet === undefined) {
|
||||||
if(MinecraftServer.debug) console.log(`Received Unknown packet: ${packetID}. Kicking the player.`);
|
if(MinecraftServer.debug) console.log(`Received Unknown packet: ${packetID}. Kicking the player.`);
|
||||||
this.kickPlayer(`Sent unknown packet ${packetID}`);
|
// this.kickPlayer(`Sent unknown packet ${packetID}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +42,7 @@ export class PlayerManager {
|
|||||||
|
|
||||||
this.socket.on('close', () => this.playerDisconnected());
|
this.socket.on('close', () => this.playerDisconnected());
|
||||||
this.socket.on('timeout', () => this.playerDisconnected());
|
this.socket.on('timeout', () => this.playerDisconnected());
|
||||||
|
this.socket.on('error', () => this.playerDisconnected());
|
||||||
}
|
}
|
||||||
|
|
||||||
public sendPacket(packet: Packet) {
|
public sendPacket(packet: Packet) {
|
||||||
|
Loading…
Reference in New Issue
Block a user