diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e8f945f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "bufferStuff"] + path = bufferStuff + url = https://github.com/tgpholly/bufferStuff.git diff --git a/bufferStuff b/bufferStuff new file mode 160000 index 0000000..02a4493 --- /dev/null +++ b/bufferStuff @@ -0,0 +1 @@ +Subproject commit 02a44936ea99c3dec0f369f843882de67dccb93d diff --git a/bufferStuff.ts b/bufferStuff.ts deleted file mode 100644 index 532e553..0000000 --- a/bufferStuff.ts +++ /dev/null @@ -1,222 +0,0 @@ -export class Reader { - private buffer:Buffer; - private offset:number; - - public constructor(buffer:Buffer) { - this.buffer = buffer; - this.offset = 0; - } - - public readBuffer(bytes:number) { - const value = this.buffer.subarray(this.offset, this.offset + bytes); - this.offset += bytes; - return value; - } - - // NOTE: This had to be a copy as the subarray is only cropped & offset - public readUint8Array(bytes:number) { - const croppedBuffer = this.readBuffer(bytes); - const newArray = new Uint8Array(croppedBuffer.length); - for (let i = 0; i < croppedBuffer.length; i++) { - newArray[i] = croppedBuffer[i]; - } - return newArray; - } - - public readByte() { - const value = this.buffer.readInt8(this.offset); - this.offset++; - return value; - } - - public readUByte() { - const value = this.buffer.readUInt8(this.offset); - this.offset++; - return value; - } - - public readBool() { - return Boolean(this.readUByte()); - } - - public readShort() { - const value = this.buffer.readInt16BE(this.offset); - this.offset += 2 - return value; - } - - public readInt() { - const value = this.buffer.readInt32BE(this.offset); - this.offset += 4; - return value; - } - - public readLong() { - const value = this.buffer.readBigInt64BE(this.offset); - this.offset += 8; - return value; - } - - public readFloat() { - const value = this.buffer.readFloatBE(this.offset); - this.offset += 4; - return value; - } - - public readDouble() { - const value = this.buffer.readDoubleBE(this.offset); - this.offset += 8; - return value; - } - - public readString() { - const length = this.readShort(); - let text:string = ""; - - for (let i = 0; i < length; i++) { - text += String.fromCharCode(this.readShort()); - } - - return text; - } -} - -export class Writer { - private buffer:Buffer; - private offset:number; - private resizable:boolean; - - public constructor(size:number = 0) { - this.buffer = Buffer.alloc(size); - this.offset = 0; - this.resizable = size === 0; - } - - public toBuffer() { - return this.buffer; - } - - public toString() { - return this.buffer.toString(); - } - - public writeBuffer(buffer:Buffer) { - this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length); - - return this; - } - - public writeByte(value:number) { - if (this.resizable) { - const buffer = Buffer.alloc(1); - buffer.writeInt8(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeInt8(value, this.offset); - this.offset++; - } - - return this; - } - - public writeUByte(value:number) { - if (this.resizable) { - const buffer = Buffer.alloc(1); - buffer.writeUInt8(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeUInt8(value, this.offset); - this.offset++; - } - - return this; - } - - public writeBool(value:boolean|number) { - if (typeof(value) === "number") { - value = Boolean(value); - } - this.writeUByte(value ? 1 : 0); - - return this; - } - - public writeShort(value:number) { - if (this.resizable) { - const buffer = Buffer.alloc(2); - buffer.writeInt16BE(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeInt16BE(value, this.offset); - this.offset += 2; - } - - return this; - } - - public writeInt(value:number) { - if (this.resizable) { - const buffer = Buffer.alloc(4); - buffer.writeInt32BE(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeInt32BE(value, this.offset); - this.offset += 4; - } - - return this; - } - - public writeLong(value:number|bigint) { - if (typeof(value) !== "bigint") { - value = BigInt(value); - } - - if (this.resizable) { - const buffer = Buffer.alloc(8); - buffer.writeBigInt64BE(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeBigInt64BE(value, this.offset); - this.offset += 8; - } - - return this; - } - - public writeFloat(value:number) { - if (this.resizable) { - const buffer = Buffer.alloc(4); - buffer.writeFloatBE(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeFloatBE(value, this.offset); - this.offset += 4; - } - - return this; - } - - public writeDouble(value:number) { - if (this.resizable) { - const buffer = Buffer.alloc(8); - buffer.writeDoubleBE(value); - this.writeBuffer(buffer); - } else { - this.buffer.writeDoubleBE(value, this.offset); - this.offset += 8; - } - - return this; - } - - public writeString(text:string) { - this.writeShort(text.length); - - for (let i = 0; i < text.length; i++) { - this.writeShort(text.charCodeAt(i)); - } - - return this; - } -} \ No newline at end of file diff --git a/server/Chunk.ts b/server/Chunk.ts index b84e04a..8fb4aca 100644 --- a/server/Chunk.ts +++ b/server/Chunk.ts @@ -40,7 +40,7 @@ export class Chunk { this.metadata = new NibbleArray(metadata); this.skyLight = new NibbleArray(16 * 16 * this.MAX_HEIGHT); this.blockLight = new NibbleArray(16 * 16 * this.MAX_HEIGHT); - this.calculateLighting(); + //this.calculateLighting(); } else { this.blocks = new Uint8Array(16 * 16 * this.MAX_HEIGHT); this.metadata = new NibbleArray(16 * 16 * this.MAX_HEIGHT); @@ -49,7 +49,7 @@ export class Chunk { if (typeof(generateOrBlockData) === "boolean" && generateOrBlockData) { this.world.generator.generate(this); - this.calculateLighting(); + //this.calculateLighting(); } } } diff --git a/server/MPClient.ts b/server/MPClient.ts index c4a7be0..74642ac 100644 --- a/server/MPClient.ts +++ b/server/MPClient.ts @@ -1,20 +1,18 @@ -import { Socket } from "net"; -import { Reader, Writer } from "../bufferStuff"; +import { Console } from "../console"; +import { IReader } from "../bufferStuff/readers/IReader"; +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 { Player } from "./entities/Player"; -import { PacketChat } from "./packets/Chat"; -import { MinecraftServer } from "./MinecraftServer"; -import { Vec3 } from "./Vec3"; -import { Console } from "../console"; import { PacketPlayerDigging } from "./packets/PlayerDigging"; -import { PacketAnimation } from "./packets/Animation"; -import { PacketEntityAction } from "./packets/EntityAction"; -import { IPacket } from "./packets/IPacket"; -import { Animation } from "./enums/Animation"; +import { Player } from "./entities/Player"; +import { Socket } from "net"; +import { Vec3 } from "./Vec3"; export class MPClient { private readonly mcServer:MinecraftServer; @@ -54,7 +52,7 @@ export class MPClient { } } - public handlePacket(reader:Reader) { + public handlePacket(reader:IReader) { const packetId = reader.readUByte(); switch (packetId) { @@ -161,11 +159,7 @@ export class MPClient { } } - public send(buffer:Buffer|Writer) { - if (buffer instanceof Writer) { - this.socket.write(buffer.toBuffer()); - } else { - this.socket.write(buffer); - } + public send(buffer:Buffer) { + this.socket.write(buffer); } } \ No newline at end of file diff --git a/server/MetadataWriter.ts b/server/MetadataWriter.ts index 73c3515..79b0d5b 100644 --- a/server/MetadataWriter.ts +++ b/server/MetadataWriter.ts @@ -1,4 +1,5 @@ -import { Writer } from "../bufferStuff"; +import { createWriter } from "../bufferStuff/index"; +import { Endian } from "../bufferStuff/Endian"; import { FunkyArray } from "../funkyArray"; import { MetadataFieldType } from "./enums/MetadataFieldType"; @@ -43,7 +44,7 @@ export class MetadataWriter { } public writeBuffer() { - const writer = new Writer(this.calculateBufferSize()); + const writer = createWriter(Endian.BE, this.calculateBufferSize()); for (let key of this.entries.keys) { const entry = this.entries.get(key); if (entry instanceof MetadataEntry) { @@ -56,7 +57,7 @@ export class MetadataWriter { case MetadataFieldType.Float: writer.writeFloat(entry.value); break; } } else if (typeof(entry.value) === "string") { - writer.writeString(entry.value); + writer.writeString16(entry.value); } } } diff --git a/server/MinecraftServer.ts b/server/MinecraftServer.ts index 5fb5b0e..cb26aa4 100644 --- a/server/MinecraftServer.ts +++ b/server/MinecraftServer.ts @@ -1,22 +1,24 @@ import { Config } from "../config"; import { Console } from "../console"; -import { Server, Socket, SocketAddress } from "net"; +import { createReader } from "../bufferStuff/index"; import { FunkyArray } from "../funkyArray"; -import { World } from "./World"; -import { Reader } from "../bufferStuff"; -import { Packet } from "./enums/Packet"; -import { PacketHandshake } from "./packets/Handshake"; +import { IReader } from "../bufferStuff/readers/IReader"; +import { Server, Socket, SocketAddress } from "net"; import { MPClient } from "./MPClient"; +import { Packet } from "./enums/Packet"; import { PacketKeepAlive } from "./packets/KeepAlive"; +import { PacketHandshake } from "./packets/Handshake"; import { PacketLoginRequest } from "./packets/LoginRequest"; -import { PacketDisconnectKick } from "./packets/DisconnectKick"; -import { Player } from "./entities/Player"; +import { PacketChat } from "./packets/Chat"; import { PacketSpawnPosition } from "./packets/SpawnPosition"; import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook"; -import { PacketChat } from "./packets/Chat"; import { PacketNamedEntitySpawn } from "./packets/NamedEntitySpawn"; -import { WorldSaveManager } from "./WorldSaveManager"; +import { PacketDisconnectKick } from "./packets/DisconnectKick"; +import { Player } from "./entities/Player"; import { SaveCompressionType } from "./enums/SaveCompressionType"; +import { WorldSaveManager } from "./WorldSaveManager"; +import { World } from "./World"; +import { Endian } from "../bufferStuff/Endian"; export class MinecraftServer { private static readonly PROTOCOL_VERSION = 14; @@ -111,7 +113,7 @@ export class MinecraftServer { }); } - handleLoginRequest(reader:Reader, socket:Socket, setMPClient:(mpclient:MPClient) => void) { + handleLoginRequest(reader:IReader, socket:Socket, setMPClient:(mpclient:MPClient) => void) { const loginPacket = new PacketLoginRequest().readData(reader); if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) { if (loginPacket.protocolVersion > MinecraftServer.PROTOCOL_VERSION) { @@ -151,7 +153,7 @@ export class MinecraftServer { } } - handleHandshake(reader:Reader, socket:Socket) { + handleHandshake(reader:IReader, socket:Socket) { const handshakePacket = new PacketHandshake().readData(reader); socket.write(handshakePacket.writeData()); } @@ -174,7 +176,7 @@ export class MinecraftServer { socket.on("error", playerDisconnect.bind(this)); socket.on("data", chunk => { - const reader = new Reader(chunk); + const reader = createReader(Endian.BE, chunk); // Let mpClient take over if it exists if (mpClient instanceof MPClient) { diff --git a/server/WorldSaveManager.ts b/server/WorldSaveManager.ts index a5f5476..ec033ac 100644 --- a/server/WorldSaveManager.ts +++ b/server/WorldSaveManager.ts @@ -1,10 +1,11 @@ import { readFileSync, readFile, writeFile, existsSync, mkdirSync, writeFileSync, readdirSync } from "fs"; -import { Reader, Writer } from "../bufferStuff"; +import { createWriter, createReader } from "../bufferStuff/index"; import { Config } from "../config"; import { Chunk } from "./Chunk"; import { SaveCompressionType } from "./enums/SaveCompressionType"; import { deflate, inflate } from "zlib"; import { World } from "./World"; +import { Endian } from "../bufferStuff/Endian"; export class WorldSaveManager { private readonly worldFolderPath; @@ -61,7 +62,7 @@ export class WorldSaveManager { } private createInfoFile(numericalSeed:number) { - const infoFileWriter = new Writer(26); + const infoFileWriter = createWriter(Endian.BE, 26); infoFileWriter.writeUByte(0xFD); // Info File Magic infoFileWriter.writeUByte(0); // File Version infoFileWriter.writeLong(this.worldCreationDate.getTime()); // World creation date @@ -71,7 +72,7 @@ export class WorldSaveManager { } private readInfoFile() { - const infoFileReader = new Reader(readFileSync(this.infoFilePath)); + const infoFileReader = createReader(Endian.BE, readFileSync(this.infoFilePath)); const fileMagic = infoFileReader.readUByte(); if (fileMagic !== 0xFD) { throw new Error("World info file is invalid"); @@ -88,7 +89,7 @@ export class WorldSaveManager { public writeChunkToDisk(chunk:Chunk) { return new Promise((resolve, reject) => { const saveType = this.config.saveCompression; - const chunkFileWriter = new Writer(10); + const chunkFileWriter = createWriter(Endian.BE, 10); chunkFileWriter.writeUByte(0xFC); // Chunk File Magic // TODO: Change to 1 when lighting actually works chunkFileWriter.writeUByte(0); // File Version @@ -97,7 +98,7 @@ export class WorldSaveManager { chunkFileWriter.writeUByte(128); // Chunk Y chunkFileWriter.writeUByte(16); // Chunk Z - const chunkData = new Writer() + const chunkData = createWriter(Endian.BE) .writeBuffer(Buffer.from(chunk.getData())) .writeBuffer(chunk.getMetadataBuffer()).toBuffer() //.writeBuffer(chunk.getBlockLightBuffer()) @@ -146,7 +147,7 @@ export class WorldSaveManager { return reject(err); } - const chunkFileReader = new Reader(data); + const chunkFileReader = createReader(Endian.BE, data); // Check file validity if (chunkFileReader.readUByte() !== 0xFC) { @@ -163,7 +164,7 @@ export class WorldSaveManager { const contentLength = chunkFileReader.readInt(); if (saveCompressionType === SaveCompressionType.NONE) { - const chunkData = new Reader(chunkFileReader.readBuffer(contentLength)); + const chunkData = createReader(Endian.BE, chunkFileReader.readBuffer(contentLength)); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2)); resolve(chunk); } else if (saveCompressionType === SaveCompressionType.DEFLATE) { @@ -172,7 +173,7 @@ export class WorldSaveManager { return reject(err); } - const chunkData = new Reader(data); + const chunkData = createReader(Endian.BE, data); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2)); resolve(chunk); }); @@ -186,7 +187,7 @@ export class WorldSaveManager { const contentLength = chunkFileReader.readInt(); if (saveCompressionType === SaveCompressionType.NONE) { - const chunkData = new Reader(chunkFileReader.readBuffer(contentLength)); + const chunkData = createReader(Endian.BE, chunkFileReader.readBuffer(contentLength)); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2)); resolve(chunk); } else if (saveCompressionType === SaveCompressionType.DEFLATE) { @@ -195,7 +196,7 @@ export class WorldSaveManager { return reject(err); } - const chunkData = new Reader(data); + const chunkData = createReader(Endian.BE, data); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2), chunkData.readUint8Array(totalByteSize / 2), chunkData.readUint8Array(totalByteSize / 2)); resolve(chunk); }); diff --git a/server/packets/Animation.ts b/server/packets/Animation.ts index b57401e..c3d2bca 100644 --- a/server/packets/Animation.ts +++ b/server/packets/Animation.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketAnimation implements IPacket { public packetId = Packet.Animation; @@ -17,7 +19,7 @@ export class PacketAnimation implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.animation = reader.readByte(); @@ -25,6 +27,6 @@ export class PacketAnimation implements IPacket { } public writeData() { - return new Writer(6).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.animation).toBuffer(); + return createWriter(Endian.BE, 6).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.animation).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/BlockChange.ts b/server/packets/BlockChange.ts index 3855e65..aaced44 100644 --- a/server/packets/BlockChange.ts +++ b/server/packets/BlockChange.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketBlockChange implements IPacket { public packetId = Packet.BlockChange; @@ -26,7 +28,7 @@ export class PacketBlockChange implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.x = reader.readInt(); this.y = reader.readByte(); this.z = reader.readInt(); @@ -37,6 +39,6 @@ export class PacketBlockChange implements IPacket { } public writeData() { - return new Writer(12).writeUByte(this.packetId).writeInt(this.x).writeByte(this.y).writeInt(this.z).writeByte(this.blockType).writeByte(this.blockMetadata).toBuffer(); + return createWriter(Endian.BE, 12).writeUByte(this.packetId).writeInt(this.x).writeByte(this.y).writeInt(this.z).writeByte(this.blockType).writeByte(this.blockMetadata).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/Chat.ts b/server/packets/Chat.ts index 2de7e2d..01010ec 100644 --- a/server/packets/Chat.ts +++ b/server/packets/Chat.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketChat implements IPacket { public packetId = Packet.Chat; @@ -14,13 +16,13 @@ export class PacketChat implements IPacket { } } - public readData(reader:Reader) { - this.message = reader.readString(); + public readData(reader:IReader) { + this.message = reader.readString16(); return this; } public writeData() { - return new Writer(3 + this.message.length * 2).writeUByte(this.packetId).writeString(this.message).toBuffer(); + return createWriter(Endian.BE, 3 + this.message.length * 2).writeUByte(this.packetId).writeString16(this.message).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/DisconnectKick.ts b/server/packets/DisconnectKick.ts index 98b5004..6645886 100644 --- a/server/packets/DisconnectKick.ts +++ b/server/packets/DisconnectKick.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketDisconnectKick implements IPacket { public packetId = Packet.DisconnectKick; @@ -14,12 +16,12 @@ export class PacketDisconnectKick implements IPacket { } } - public readData(reader:Reader) { - this.reason = reader.readString(); + public readData(reader:IReader) { + this.reason = reader.readString16(); return this; } public writeData() { - return new Writer(3 + this.reason.length * 2).writeUByte(this.packetId).writeString(this.reason).toBuffer(); + return createWriter(Endian.BE, 3 + this.reason.length * 2).writeUByte(this.packetId).writeString16(this.reason).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/Entity.ts b/server/packets/Entity.ts index 527e0fb..41cdd3b 100644 --- a/server/packets/Entity.ts +++ b/server/packets/Entity.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntity implements IPacket { public packetId = Packet.Entity; @@ -14,13 +16,13 @@ export class PacketEntity implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); return this; } public writeData() { - return new Writer(5).writeUByte(this.packetId).writeInt(this.entityId).toBuffer(); + return createWriter(Endian.BE, 5).writeUByte(this.packetId).writeInt(this.entityId).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityAction.ts b/server/packets/EntityAction.ts index 8bf5c3c..88c5919 100644 --- a/server/packets/EntityAction.ts +++ b/server/packets/EntityAction.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntityAction implements IPacket { public packetId = Packet.EntityAction; @@ -17,7 +19,7 @@ export class PacketEntityAction implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.action = reader.readByte(); @@ -25,6 +27,6 @@ export class PacketEntityAction implements IPacket { } public writeData() { - return new Writer(6).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.action).toBuffer(); + return createWriter(Endian.BE, 6).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.action).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityEquipment.ts b/server/packets/EntityEquipment.ts index b4c9cd9..8a5df21 100644 --- a/server/packets/EntityEquipment.ts +++ b/server/packets/EntityEquipment.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntityEquipment implements IPacket { public packetId = Packet.EntityEquipment; @@ -16,7 +18,7 @@ export class PacketEntityEquipment implements IPacket { this.damage = damage; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.slot = reader.readShort(); this.itemId = reader.readShort(); @@ -26,6 +28,6 @@ export class PacketEntityEquipment implements IPacket { } public writeData() { - return new Writer(10).writeUByte(this.packetId).writeInt(this.entityId).writeShort(this.slot).writeShort(this.itemId).writeShort(this.damage).toBuffer(); + return createWriter(Endian.BE, 10).writeUByte(this.packetId).writeInt(this.entityId).writeShort(this.slot).writeShort(this.itemId).writeShort(this.damage).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityLook.ts b/server/packets/EntityLook.ts index c9ea0c6..4277236 100644 --- a/server/packets/EntityLook.ts +++ b/server/packets/EntityLook.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntityLook implements IPacket { public packetId = Packet.EntityLook; @@ -20,7 +22,7 @@ export class PacketEntityLook implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.yaw = reader.readByte(); this.pitch = reader.readByte(); @@ -29,6 +31,6 @@ export class PacketEntityLook implements IPacket { } public writeData() { - return new Writer(7).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.yaw).writeByte(this.pitch).toBuffer(); + return createWriter(Endian.BE, 7).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.yaw).writeByte(this.pitch).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityLookRelativeMove.ts b/server/packets/EntityLookRelativeMove.ts index a8d9190..6a9565f 100644 --- a/server/packets/EntityLookRelativeMove.ts +++ b/server/packets/EntityLookRelativeMove.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntityLookRelativeMove implements IPacket { public packetId = Packet.EntityLookRelativeMove; @@ -29,7 +31,7 @@ export class PacketEntityLookRelativeMove implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.dX = reader.readByte(); this.dY = reader.readByte(); @@ -41,6 +43,6 @@ export class PacketEntityLookRelativeMove implements IPacket { } public writeData() { - return new Writer(10).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.dX).writeByte(this.dY).writeByte(this.dZ).writeByte(this.yaw).writeByte(this.pitch).toBuffer(); + return createWriter(Endian.BE, 10).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.dX).writeByte(this.dY).writeByte(this.dZ).writeByte(this.yaw).writeByte(this.pitch).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityMetadata.ts b/server/packets/EntityMetadata.ts index a215991..ed9f56f 100644 --- a/server/packets/EntityMetadata.ts +++ b/server/packets/EntityMetadata.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; const EMPTY_BUFFER = Buffer.alloc(0); @@ -19,13 +21,13 @@ export class PacketEntityMetadata implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { // TODO: EntityMetadata reading return this; } public writeData() { - return new Writer(5).writeUByte(this.packetId).writeInt(this.entityId).writeBuffer(this.metadata).toBuffer(); + return createWriter(Endian.BE, 5).writeUByte(this.packetId).writeInt(this.entityId).writeBuffer(this.metadata).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityRelativeMove.ts b/server/packets/EntityRelativeMove.ts index 3aac6c5..5612370 100644 --- a/server/packets/EntityRelativeMove.ts +++ b/server/packets/EntityRelativeMove.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntityRelativeMove implements IPacket { public packetId = Packet.EntityRelativeMove; @@ -23,7 +25,7 @@ export class PacketEntityRelativeMove implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.dX = reader.readByte(); this.dY = reader.readByte(); @@ -33,6 +35,6 @@ export class PacketEntityRelativeMove implements IPacket { } public writeData() { - return new Writer(8).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.dX).writeByte(this.dY).writeByte(this.dZ).toBuffer(); + return createWriter(Endian.BE, 8).writeUByte(this.packetId).writeInt(this.entityId).writeByte(this.dX).writeByte(this.dY).writeByte(this.dZ).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/EntityTeleport.ts b/server/packets/EntityTeleport.ts index 8c28dd4..36a13f1 100644 --- a/server/packets/EntityTeleport.ts +++ b/server/packets/EntityTeleport.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketEntityTeleport implements IPacket { public packetId = Packet.EntityTeleport; @@ -29,7 +31,7 @@ export class PacketEntityTeleport implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); this.x = reader.readInt(); this.y = reader.readInt(); @@ -41,6 +43,6 @@ export class PacketEntityTeleport implements IPacket { } public writeData() { - return new Writer(19).writeUByte(this.packetId).writeInt(this.entityId).writeInt(this.x).writeInt(this.y).writeInt(this.z).writeByte(this.yaw).writeByte(this.pitch).toBuffer(); + return createWriter(Endian.BE, 19).writeUByte(this.packetId).writeInt(this.entityId).writeInt(this.x).writeInt(this.y).writeInt(this.z).writeByte(this.yaw).writeByte(this.pitch).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/Handshake.ts b/server/packets/Handshake.ts index 3de41bc..798a2dd 100644 --- a/server/packets/Handshake.ts +++ b/server/packets/Handshake.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createReader, createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketHandshake implements IPacket { public packetId:Packet = Packet.Handshake; @@ -14,13 +16,13 @@ export class PacketHandshake implements IPacket { } } - public readData(reader:Reader) { - this.username = reader.readString(); + public readData(reader:IReader) { + this.username = reader.readString16(); return this; } public writeData() { - return new Writer(5).writeUByte(this.packetId).writeString("-").toBuffer(); + return createWriter(Endian.BE, 5).writeUByte(this.packetId).writeString16("-").toBuffer(); } } \ No newline at end of file diff --git a/server/packets/IPacket.ts b/server/packets/IPacket.ts index d9da384..98a4588 100644 --- a/server/packets/IPacket.ts +++ b/server/packets/IPacket.ts @@ -1,8 +1,8 @@ -import { Reader } from "../../bufferStuff"; +import { IReader } from "../../bufferStuff/readers/IReader"; import { Packet } from "../enums/Packet"; export interface IPacket { packetId: Packet, - readData: (reader:Reader) => IPacket, + readData: (reader:IReader) => IPacket, writeData: () => Buffer|Promise } \ No newline at end of file diff --git a/server/packets/KeepAlive.ts b/server/packets/KeepAlive.ts index dbd2c9d..d562a01 100644 --- a/server/packets/KeepAlive.ts +++ b/server/packets/KeepAlive.ts @@ -1,12 +1,14 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketKeepAlive implements IPacket { public packetId = Packet.KeepAlive; - private static readonly KeepAliveBuffer:Buffer = new Writer(1).writeByte(Packet.KeepAlive).toBuffer(); + private static readonly KeepAliveBuffer:Buffer = createWriter(Endian.BE, 1).writeByte(Packet.KeepAlive).toBuffer(); - public readData(reader:Reader) { + public readData(reader:IReader) { reader; return this; } diff --git a/server/packets/LoginRequest.ts b/server/packets/LoginRequest.ts index 52afe67..e60f99c 100644 --- a/server/packets/LoginRequest.ts +++ b/server/packets/LoginRequest.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketLoginRequest implements IPacket { public packetId = Packet.LoginRequest; @@ -23,9 +25,9 @@ export class PacketLoginRequest implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.protocolVersion = reader.readInt(); - this.username = reader.readString(); + this.username = reader.readString16(); this.mapSeed = Number(reader.readLong()); this.dimension = reader.readByte(); @@ -33,6 +35,6 @@ export class PacketLoginRequest implements IPacket { } public writeData() { - return new Writer(16 + (2 * this.username.length)).writeUByte(this.packetId).writeInt(this.protocolVersion).writeString(this.username).writeLong(this.mapSeed).writeByte(this.dimension).toBuffer(); + return createWriter(Endian.BE, 16 + (2 * this.username.length)).writeUByte(this.packetId).writeInt(this.protocolVersion).writeString16(this.username).writeLong(this.mapSeed).writeByte(this.dimension).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/MapChunk.ts b/server/packets/MapChunk.ts index 28f73e3..691b65e 100644 --- a/server/packets/MapChunk.ts +++ b/server/packets/MapChunk.ts @@ -1,8 +1,10 @@ -import { deflate } from "zlib"; -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; -import { IPacket } from "./IPacket"; import { Chunk } from "../Chunk"; +import { createWriter } from "../../bufferStuff/index"; +import { deflate } from "zlib"; +import { Endian } from "../../bufferStuff/Endian"; +import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketMapChunk implements IPacket { public packetId = Packet.MapChunk; @@ -24,7 +26,7 @@ export class PacketMapChunk implements IPacket { this.chunk = chunk; } - public readData(reader:Reader) { + public readData(reader:IReader) { // TODO: Implement MapChunk reading reader.readBool(); @@ -34,19 +36,19 @@ export class PacketMapChunk implements IPacket { public writeData() { return new Promise((resolve, reject) => { // TODO: Use block and sky nibble array buffers - const fakeLighting = new Writer(16384); + const fakeLighting = createWriter(Endian.BE, 16384); for (let i = 0; i < 16384; i++) { fakeLighting.writeUByte(0xFF); } - const data = new Writer().writeBuffer(this.chunk.getBlockBuffer()).writeBuffer(this.chunk.getMetadataBuffer()).writeBuffer(fakeLighting.toBuffer()).writeBuffer(fakeLighting.toBuffer());//.writeBuffer(this.chunk.blockLight.toBuffer()).writeBuffer(this.chunk.skyLight.toBuffer()); + const data = createWriter(Endian.BE).writeBuffer(this.chunk.getBlockBuffer()).writeBuffer(this.chunk.getMetadataBuffer()).writeBuffer(fakeLighting.toBuffer()).writeBuffer(fakeLighting.toBuffer());//.writeBuffer(this.chunk.blockLight.toBuffer()).writeBuffer(this.chunk.skyLight.toBuffer()); deflate(data.toBuffer(), (err, data) => { if (err) { return reject(err); } - resolve(new Writer(18).writeUByte(this.packetId).writeInt(this.x << 4).writeShort(this.y).writeInt(this.z << 4).writeUByte(this.sizeX).writeUByte(this.sizeY).writeUByte(this.sizeZ).writeInt(data.length).writeBuffer(data).toBuffer()); + resolve(createWriter(Endian.BE, 18).writeUByte(this.packetId).writeInt(this.x << 4).writeShort(this.y).writeInt(this.z << 4).writeUByte(this.sizeX).writeUByte(this.sizeY).writeUByte(this.sizeZ).writeInt(data.length).writeBuffer(data).toBuffer()); }); }); } diff --git a/server/packets/NamedEntitySpawn.ts b/server/packets/NamedEntitySpawn.ts index 1ee4abb..0fc5339 100644 --- a/server/packets/NamedEntitySpawn.ts +++ b/server/packets/NamedEntitySpawn.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketNamedEntitySpawn implements IPacket { public packetId = Packet.NamedEntitySpawn; @@ -35,9 +37,9 @@ export class PacketNamedEntitySpawn implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.entityId = reader.readInt(); - this.playerName = reader.readString(); + this.playerName = reader.readString16(); this.x = reader.readInt(); this.y = reader.readInt(); this.z = reader.readInt(); @@ -48,6 +50,6 @@ export class PacketNamedEntitySpawn implements IPacket { } public writeData() { - return new Writer(23 + this.playerName.length * 2).writeUByte(this.packetId).writeInt(this.entityId).writeString(this.playerName).writeInt(this.x).writeInt(this.y).writeInt(this.z).writeByte(this.yaw).writeByte(this.pitch).writeShort(this.currentItem).toBuffer(); + return createWriter(Endian.BE, 23 + this.playerName.length * 2).writeUByte(this.packetId).writeInt(this.entityId).writeString16(this.playerName).writeInt(this.x).writeInt(this.y).writeInt(this.z).writeByte(this.yaw).writeByte(this.pitch).writeShort(this.currentItem).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/Player.ts b/server/packets/Player.ts index 8d877bc..3576736 100644 --- a/server/packets/Player.ts +++ b/server/packets/Player.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketPlayer implements IPacket { public packetId = Packet.Player; @@ -14,13 +16,13 @@ export class PacketPlayer implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.onGround = reader.readBool(); return this; } public writeData() { - return new Writer(2).writeUByte(this.packetId).writeBool(this.onGround).toBuffer(); + return createWriter(Endian.BE, 2).writeUByte(this.packetId).writeBool(this.onGround).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/PlayerDigging.ts b/server/packets/PlayerDigging.ts index 85c6da7..28de656 100644 --- a/server/packets/PlayerDigging.ts +++ b/server/packets/PlayerDigging.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketPlayerDigging implements IPacket { public packetId = Packet.PlayerDigging; @@ -26,7 +28,7 @@ export class PacketPlayerDigging implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.status = reader.readByte(); this.x = reader.readInt(); this.y = reader.readByte(); @@ -37,6 +39,6 @@ export class PacketPlayerDigging implements IPacket { } public writeData() { - return new Writer(12).writeUByte(this.packetId).writeByte(this.status).writeInt(this.x).writeByte(this.y).writeInt(this.z).writeByte(this.face).toBuffer(); + return createWriter(Endian.BE, 12).writeUByte(this.packetId).writeByte(this.status).writeInt(this.x).writeByte(this.y).writeInt(this.z).writeByte(this.face).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/PlayerLook.ts b/server/packets/PlayerLook.ts index bab77b7..6deb716 100644 --- a/server/packets/PlayerLook.ts +++ b/server/packets/PlayerLook.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketPlayerLook implements IPacket { public packetId = Packet.Player; @@ -20,7 +22,7 @@ export class PacketPlayerLook implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.yaw = reader.readFloat(); this.pitch = reader.readFloat(); this.onGround = reader.readBool(); @@ -29,6 +31,6 @@ export class PacketPlayerLook implements IPacket { } public writeData() { - return new Writer(10).writeUByte(this.packetId).writeFloat(this.yaw).writeFloat(this.pitch).writeBool(this.onGround).toBuffer(); + return createWriter(Endian.BE, 10).writeUByte(this.packetId).writeFloat(this.yaw).writeFloat(this.pitch).writeBool(this.onGround).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/PlayerPosition.ts b/server/packets/PlayerPosition.ts index ae1a9e1..4164d70 100644 --- a/server/packets/PlayerPosition.ts +++ b/server/packets/PlayerPosition.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketPlayerPosition implements IPacket { public packetId = Packet.PlayerPosition; @@ -26,7 +28,7 @@ export class PacketPlayerPosition implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.x = reader.readDouble(); this.y = reader.readDouble(); this.stance = reader.readDouble(); @@ -37,6 +39,6 @@ export class PacketPlayerPosition implements IPacket { } public writeData() { - return new Writer(34).writeUByte(this.packetId).writeDouble(this.x).writeDouble(this.y).writeDouble(this.stance).writeDouble(this.z).writeBool(this.onGround).toBuffer(); + return createWriter(Endian.BE, 34).writeUByte(this.packetId).writeDouble(this.x).writeDouble(this.y).writeDouble(this.stance).writeDouble(this.z).writeBool(this.onGround).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/PlayerPositionLook.ts b/server/packets/PlayerPositionLook.ts index 7119e9b..3341cb2 100644 --- a/server/packets/PlayerPositionLook.ts +++ b/server/packets/PlayerPositionLook.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketPlayerPositionLook implements IPacket { public packetId = Packet.PlayerPosition; @@ -32,7 +34,7 @@ export class PacketPlayerPositionLook implements IPacket { } } - public readData(reader:Reader) { + public readData(reader:IReader) { this.x = reader.readDouble(); this.y = reader.readDouble(); this.stance = reader.readDouble(); @@ -45,6 +47,6 @@ export class PacketPlayerPositionLook implements IPacket { } public writeData() { - return new Writer(42).writeUByte(this.packetId).writeDouble(this.x).writeDouble(this.y).writeDouble(this.stance).writeDouble(this.z).writeFloat(this.yaw).writeFloat(this.pitch).writeBool(this.onGround).toBuffer(); + return createWriter(Endian.BE, 42).writeUByte(this.packetId).writeDouble(this.x).writeDouble(this.y).writeDouble(this.stance).writeDouble(this.z).writeFloat(this.yaw).writeFloat(this.pitch).writeBool(this.onGround).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/PreChunk.ts b/server/packets/PreChunk.ts index 679cae5..6ac7462 100644 --- a/server/packets/PreChunk.ts +++ b/server/packets/PreChunk.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketPreChunk implements IPacket { public packetId = Packet.PreChunk; @@ -14,7 +16,7 @@ export class PacketPreChunk implements IPacket { this.mode = mode; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.x = reader.readInt(); this.z = reader.readInt(); this.mode = reader.readBool(); @@ -23,6 +25,6 @@ export class PacketPreChunk implements IPacket { } public writeData() { - return new Writer(10).writeUByte(this.packetId).writeInt(this.x).writeInt(this.z).writeBool(this.mode).toBuffer(); + return createWriter(Endian.BE, 10).writeUByte(this.packetId).writeInt(this.x).writeInt(this.z).writeBool(this.mode).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/Respawn.ts b/server/packets/Respawn.ts index 93367b8..d69a19e 100644 --- a/server/packets/Respawn.ts +++ b/server/packets/Respawn.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketRespawn implements IPacket { public packetId = Packet.Respawn; @@ -10,13 +12,13 @@ export class PacketRespawn implements IPacket { this.health = health; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.health = reader.readShort(); return this; } public writeData() { - return new Writer(3).writeUByte(this.packetId).writeShort(this.health).toBuffer(); + return createWriter(Endian.BE, 3).writeUByte(this.packetId).writeShort(this.health).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/SpawnPosition.ts b/server/packets/SpawnPosition.ts index dccac3a..feed560 100644 --- a/server/packets/SpawnPosition.ts +++ b/server/packets/SpawnPosition.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketSpawnPosition implements IPacket { public packetId = Packet.SpawnPosition; @@ -14,7 +16,7 @@ export class PacketSpawnPosition implements IPacket { this.z = z; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.x = reader.readInt(); this.y = reader.readInt(); this.z = reader.readInt(); @@ -23,6 +25,6 @@ export class PacketSpawnPosition implements IPacket { } public writeData() { - return new Writer(13).writeUByte(this.packetId).writeInt(this.x).writeInt(this.y).writeInt(this.z).toBuffer(); + return createWriter(Endian.BE, 13).writeUByte(this.packetId).writeInt(this.x).writeInt(this.y).writeInt(this.z).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/TimeUpdate.ts b/server/packets/TimeUpdate.ts index 06b3334..ebab851 100644 --- a/server/packets/TimeUpdate.ts +++ b/server/packets/TimeUpdate.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketTimeUpdate implements IPacket { public packetId = Packet.TimeUpdate; @@ -10,13 +12,13 @@ export class PacketTimeUpdate implements IPacket { this.time = time; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.time = reader.readLong(); return this; } public writeData() { - return new Writer(9).writeUByte(this.packetId).writeLong(this.time).toBuffer(); + return createWriter(Endian.BE, 9).writeUByte(this.packetId).writeLong(this.time).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/UpdateHealth.ts b/server/packets/UpdateHealth.ts index 8658cc1..34bb926 100644 --- a/server/packets/UpdateHealth.ts +++ b/server/packets/UpdateHealth.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketUpdateHealth implements IPacket { public packetId = Packet.UpdateHealth; @@ -10,13 +12,13 @@ export class PacketUpdateHealth implements IPacket { this.health = health; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.health = reader.readShort(); return this; } public writeData() { - return new Writer(3).writeUByte(this.packetId).writeShort(this.health).toBuffer(); + return createWriter(Endian.BE, 3).writeUByte(this.packetId).writeShort(this.health).toBuffer(); } } \ No newline at end of file diff --git a/server/packets/UseEntity.ts b/server/packets/UseEntity.ts index 37a9f0c..8f2a15d 100644 --- a/server/packets/UseEntity.ts +++ b/server/packets/UseEntity.ts @@ -1,6 +1,8 @@ -import { Reader, Writer } from "../../bufferStuff"; -import { Packet } from "../enums/Packet"; +import { createWriter } from "../../bufferStuff/index"; +import { Endian } from "../../bufferStuff/Endian"; import { IPacket } from "./IPacket"; +import { IReader } from "../../bufferStuff/readers/IReader"; +import { Packet } from "../enums/Packet"; export class PacketUseEntity implements IPacket { public packetId = Packet.UseEntity; @@ -14,7 +16,7 @@ export class PacketUseEntity implements IPacket { this.leftClick = leftClick; } - public readData(reader:Reader) { + public readData(reader:IReader) { this.userId = reader.readInt(); this.targetId = reader.readInt(); this.leftClick = reader.readBool(); @@ -23,6 +25,6 @@ export class PacketUseEntity implements IPacket { } public writeData() { - return new Writer(10).writeUByte(this.packetId).writeInt(this.userId).writeInt(this.targetId).writeBool(this.leftClick).toBuffer(); + return createWriter(Endian.BE, 10).writeUByte(this.packetId).writeInt(this.userId).writeInt(this.targetId).writeBool(this.leftClick).toBuffer(); } } \ No newline at end of file