mirror of
https://github.com/MrMasrozYTLIVE/MCServer.git
synced 2024-12-25 10:38:21 -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 {
|
||||
entityID: Number
|
||||
entityID: number
|
||||
}
|
@ -24,6 +24,7 @@ export class MinecraftServer {
|
||||
});
|
||||
|
||||
this.server.on('close', this.stop);
|
||||
this.server.on('drop', () => {})
|
||||
}
|
||||
|
||||
public stop() {
|
||||
|
@ -1,24 +1,60 @@
|
||||
import {Socket} from "node:net";
|
||||
import {IEntity} from "./IEntity";
|
||||
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 {
|
||||
public entityID: Number;
|
||||
export class Player extends Entity {
|
||||
public username: String;
|
||||
public socket: Socket;
|
||||
public playerManager: PlayerManager;
|
||||
|
||||
public xPosition: number = 0;
|
||||
public prevXPosition: number = this.xPosition;
|
||||
public yPosition: number = 100;
|
||||
public stance: number = 102;
|
||||
public prevYPosition: number = this.yPosition;
|
||||
public zPosition: number = 0;
|
||||
public prevZPosition: number = this.zPosition;
|
||||
public stance: number = 102;
|
||||
public yaw: number = 0;
|
||||
public pitch: number = 0;
|
||||
public onGround: boolean = false;
|
||||
|
||||
constructor(username: String, socket: Socket, playerManager: PlayerManager) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.socket = socket;
|
||||
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 {Player} from "../Player";
|
||||
import {Socket} from "node:net";
|
||||
import {Entity} from "../Entity";
|
||||
import {IEntity} from "../IEntity";
|
||||
|
||||
export class Packet {
|
||||
constructor(public options: IPacketOption) {
|
||||
options.name = PacketEnum[options.packetID].toString();
|
||||
}
|
||||
|
||||
readData(reader: IReader, player: Player) {}
|
||||
readData(reader: IReader, entity: IEntity) {}
|
||||
writeData() {
|
||||
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 {PlayerManager} from "../../../utils/PlayerManager";
|
||||
import {PacketPreChunk} from "../world/PacketPreChunk";
|
||||
import {PacketPositionLook} from "./PacketPositionLook";
|
||||
|
||||
export class PacketChat extends Packet {
|
||||
constructor(public message: string) {
|
||||
@ -13,9 +14,31 @@ export class PacketChat extends Packet {
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
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() {
|
||||
|
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) {
|
||||
player.xPosition = reader.readDouble();
|
||||
player.yPosition = reader.readDouble();
|
||||
player.stance = reader.readDouble();
|
||||
player.zPosition = reader.readDouble();
|
||||
player.onGround = reader.readBool();
|
||||
player.updatePosition(
|
||||
reader.readDouble(), // X
|
||||
reader.readDouble(), // Y
|
||||
reader.readDouble(), // Stance
|
||||
reader.readDouble(), // Z
|
||||
undefined, // Yaw
|
||||
undefined, // Pitch
|
||||
reader.readBool() // onGround
|
||||
);
|
||||
}
|
||||
|
||||
writeData() {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import {Packet} from "../../Packet";
|
||||
import {PacketEnum} from "../../../utils/PacketEnum";
|
||||
import {createWriter, Endian, IReader} from "bufferstuff";
|
||||
import {createWriter, Endian, IReader, IWriter} from "bufferstuff";
|
||||
import {Player} from "../../../Player";
|
||||
|
||||
export class PacketPositionLook extends Packet {
|
||||
@ -11,13 +11,15 @@ export class PacketPositionLook extends Packet {
|
||||
}
|
||||
|
||||
readData(reader: IReader, player: Player) {
|
||||
player.xPosition = reader.readDouble();
|
||||
player.yPosition = reader.readDouble();
|
||||
player.stance = reader.readDouble();
|
||||
player.zPosition = reader.readDouble();
|
||||
player.yaw = reader.readFloat();
|
||||
player.pitch = reader.readFloat();
|
||||
player.onGround = reader.readBool();
|
||||
player.updatePosition(
|
||||
reader.readDouble(), // X
|
||||
reader.readDouble(), // Y
|
||||
reader.readDouble(), // Stance
|
||||
reader.readDouble(), // Z
|
||||
reader.readFloat(), // Yaw
|
||||
reader.readFloat(), // Pitch
|
||||
reader.readBool() // onGround
|
||||
);
|
||||
}
|
||||
|
||||
writeData() {
|
||||
@ -28,8 +30,8 @@ export class PacketPositionLook extends Packet {
|
||||
.writeDouble(player.stance)
|
||||
.writeDouble(player.yPosition)
|
||||
.writeDouble(player.zPosition)
|
||||
.writeLong(player.yaw)
|
||||
.writeLong(player.pitch)
|
||||
.writeFloat(player.yaw)
|
||||
.writeFloat(player.pitch)
|
||||
.writeBool(player.onGround)
|
||||
.toBuffer();
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ export enum PacketEnum {
|
||||
Look = 0x0C,
|
||||
PositionLook = 0x0D,
|
||||
Animation = 0x12,
|
||||
EntityPositionLook = 0x21,
|
||||
EntityTeleport = 0x22,
|
||||
PreChunk = 0x32,
|
||||
MapChunk = 0x33,
|
||||
MultiBlockChange = 0x34,
|
||||
|
@ -30,7 +30,7 @@ export class PlayerManager {
|
||||
|
||||
if(packet === undefined) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -42,6 +42,7 @@ export class PlayerManager {
|
||||
|
||||
this.socket.on('close', () => this.playerDisconnected());
|
||||
this.socket.on('timeout', () => this.playerDisconnected());
|
||||
this.socket.on('error', () => this.playerDisconnected());
|
||||
}
|
||||
|
||||
public sendPacket(packet: Packet) {
|
||||
|
Loading…
Reference in New Issue
Block a user