Rework to use new bufferStuff submodule

This commit is contained in:
Holly Stubbs 2023-05-02 10:24:48 +01:00 committed by Holly
parent ef1e7a278e
commit 15917f1533
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
37 changed files with 226 additions and 390 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "bufferStuff"]
path = bufferStuff
url = https://github.com/tgpholly/bufferStuff.git

1
bufferStuff Submodule

@ -0,0 +1 @@
Subproject commit 02a44936ea99c3dec0f369f843882de67dccb93d

View file

@ -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;
}
}

View file

@ -40,7 +40,7 @@ export class Chunk {
this.metadata = new NibbleArray(metadata); this.metadata = new NibbleArray(metadata);
this.skyLight = new NibbleArray(16 * 16 * this.MAX_HEIGHT); this.skyLight = new NibbleArray(16 * 16 * this.MAX_HEIGHT);
this.blockLight = new NibbleArray(16 * 16 * this.MAX_HEIGHT); this.blockLight = new NibbleArray(16 * 16 * this.MAX_HEIGHT);
this.calculateLighting(); //this.calculateLighting();
} else { } else {
this.blocks = new Uint8Array(16 * 16 * this.MAX_HEIGHT); this.blocks = new Uint8Array(16 * 16 * this.MAX_HEIGHT);
this.metadata = new NibbleArray(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) { if (typeof(generateOrBlockData) === "boolean" && generateOrBlockData) {
this.world.generator.generate(this); this.world.generator.generate(this);
this.calculateLighting(); //this.calculateLighting();
} }
} }
} }

View file

@ -1,20 +1,18 @@
import { Socket } from "net"; import { Console } from "../console";
import { Reader, Writer } from "../bufferStuff"; import { IReader } from "../bufferStuff/readers/IReader";
import { MinecraftServer } from "./MinecraftServer";
import { Packet } from "./enums/Packet"; 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 { PacketPlayer } from "./packets/Player";
import { PacketPlayerPosition } from "./packets/PlayerPosition"; import { PacketPlayerPosition } from "./packets/PlayerPosition";
import { PacketPlayerLook } from "./packets/PlayerLook"; import { PacketPlayerLook } from "./packets/PlayerLook";
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook"; 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 { PacketPlayerDigging } from "./packets/PlayerDigging";
import { PacketAnimation } from "./packets/Animation"; import { Player } from "./entities/Player";
import { PacketEntityAction } from "./packets/EntityAction"; import { Socket } from "net";
import { IPacket } from "./packets/IPacket"; import { Vec3 } from "./Vec3";
import { Animation } from "./enums/Animation";
export class MPClient { export class MPClient {
private readonly mcServer:MinecraftServer; private readonly mcServer:MinecraftServer;
@ -54,7 +52,7 @@ export class MPClient {
} }
} }
public handlePacket(reader:Reader) { public handlePacket(reader:IReader) {
const packetId = reader.readUByte(); const packetId = reader.readUByte();
switch (packetId) { switch (packetId) {
@ -161,11 +159,7 @@ export class MPClient {
} }
} }
public send(buffer:Buffer|Writer) { public send(buffer:Buffer) {
if (buffer instanceof Writer) { this.socket.write(buffer);
this.socket.write(buffer.toBuffer());
} else {
this.socket.write(buffer);
}
} }
} }

View file

@ -1,4 +1,5 @@
import { Writer } from "../bufferStuff"; import { createWriter } from "../bufferStuff/index";
import { Endian } from "../bufferStuff/Endian";
import { FunkyArray } from "../funkyArray"; import { FunkyArray } from "../funkyArray";
import { MetadataFieldType } from "./enums/MetadataFieldType"; import { MetadataFieldType } from "./enums/MetadataFieldType";
@ -43,7 +44,7 @@ export class MetadataWriter {
} }
public writeBuffer() { public writeBuffer() {
const writer = new Writer(this.calculateBufferSize()); const writer = createWriter(Endian.BE, this.calculateBufferSize());
for (let key of this.entries.keys) { for (let key of this.entries.keys) {
const entry = this.entries.get(key); const entry = this.entries.get(key);
if (entry instanceof MetadataEntry) { if (entry instanceof MetadataEntry) {
@ -56,7 +57,7 @@ export class MetadataWriter {
case MetadataFieldType.Float: writer.writeFloat(entry.value); break; case MetadataFieldType.Float: writer.writeFloat(entry.value); break;
} }
} else if (typeof(entry.value) === "string") { } else if (typeof(entry.value) === "string") {
writer.writeString(entry.value); writer.writeString16(entry.value);
} }
} }
} }

View file

@ -1,22 +1,24 @@
import { Config } from "../config"; import { Config } from "../config";
import { Console } from "../console"; import { Console } from "../console";
import { Server, Socket, SocketAddress } from "net"; import { createReader } from "../bufferStuff/index";
import { FunkyArray } from "../funkyArray"; import { FunkyArray } from "../funkyArray";
import { World } from "./World"; import { IReader } from "../bufferStuff/readers/IReader";
import { Reader } from "../bufferStuff"; import { Server, Socket, SocketAddress } from "net";
import { Packet } from "./enums/Packet";
import { PacketHandshake } from "./packets/Handshake";
import { MPClient } from "./MPClient"; import { MPClient } from "./MPClient";
import { Packet } from "./enums/Packet";
import { PacketKeepAlive } from "./packets/KeepAlive"; import { PacketKeepAlive } from "./packets/KeepAlive";
import { PacketHandshake } from "./packets/Handshake";
import { PacketLoginRequest } from "./packets/LoginRequest"; import { PacketLoginRequest } from "./packets/LoginRequest";
import { PacketDisconnectKick } from "./packets/DisconnectKick"; import { PacketChat } from "./packets/Chat";
import { Player } from "./entities/Player";
import { PacketSpawnPosition } from "./packets/SpawnPosition"; import { PacketSpawnPosition } from "./packets/SpawnPosition";
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook"; import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
import { PacketChat } from "./packets/Chat";
import { PacketNamedEntitySpawn } from "./packets/NamedEntitySpawn"; 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 { SaveCompressionType } from "./enums/SaveCompressionType";
import { WorldSaveManager } from "./WorldSaveManager";
import { World } from "./World";
import { Endian } from "../bufferStuff/Endian";
export class MinecraftServer { export class MinecraftServer {
private static readonly PROTOCOL_VERSION = 14; 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); const loginPacket = new PacketLoginRequest().readData(reader);
if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) { if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) {
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); const handshakePacket = new PacketHandshake().readData(reader);
socket.write(handshakePacket.writeData()); socket.write(handshakePacket.writeData());
} }
@ -174,7 +176,7 @@ export class MinecraftServer {
socket.on("error", playerDisconnect.bind(this)); socket.on("error", playerDisconnect.bind(this));
socket.on("data", chunk => { socket.on("data", chunk => {
const reader = new Reader(chunk); const reader = createReader(Endian.BE, chunk);
// Let mpClient take over if it exists // Let mpClient take over if it exists
if (mpClient instanceof MPClient) { if (mpClient instanceof MPClient) {

View file

@ -1,10 +1,11 @@
import { readFileSync, readFile, writeFile, existsSync, mkdirSync, writeFileSync, readdirSync } from "fs"; 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 { Config } from "../config";
import { Chunk } from "./Chunk"; import { Chunk } from "./Chunk";
import { SaveCompressionType } from "./enums/SaveCompressionType"; import { SaveCompressionType } from "./enums/SaveCompressionType";
import { deflate, inflate } from "zlib"; import { deflate, inflate } from "zlib";
import { World } from "./World"; import { World } from "./World";
import { Endian } from "../bufferStuff/Endian";
export class WorldSaveManager { export class WorldSaveManager {
private readonly worldFolderPath; private readonly worldFolderPath;
@ -61,7 +62,7 @@ export class WorldSaveManager {
} }
private createInfoFile(numericalSeed:number) { private createInfoFile(numericalSeed:number) {
const infoFileWriter = new Writer(26); const infoFileWriter = createWriter(Endian.BE, 26);
infoFileWriter.writeUByte(0xFD); // Info File Magic infoFileWriter.writeUByte(0xFD); // Info File Magic
infoFileWriter.writeUByte(0); // File Version infoFileWriter.writeUByte(0); // File Version
infoFileWriter.writeLong(this.worldCreationDate.getTime()); // World creation date infoFileWriter.writeLong(this.worldCreationDate.getTime()); // World creation date
@ -71,7 +72,7 @@ export class WorldSaveManager {
} }
private readInfoFile() { private readInfoFile() {
const infoFileReader = new Reader(readFileSync(this.infoFilePath)); const infoFileReader = createReader(Endian.BE, readFileSync(this.infoFilePath));
const fileMagic = infoFileReader.readUByte(); const fileMagic = infoFileReader.readUByte();
if (fileMagic !== 0xFD) { if (fileMagic !== 0xFD) {
throw new Error("World info file is invalid"); throw new Error("World info file is invalid");
@ -88,7 +89,7 @@ export class WorldSaveManager {
public writeChunkToDisk(chunk:Chunk) { public writeChunkToDisk(chunk:Chunk) {
return new Promise<boolean>((resolve, reject) => { return new Promise<boolean>((resolve, reject) => {
const saveType = this.config.saveCompression; const saveType = this.config.saveCompression;
const chunkFileWriter = new Writer(10); const chunkFileWriter = createWriter(Endian.BE, 10);
chunkFileWriter.writeUByte(0xFC); // Chunk File Magic chunkFileWriter.writeUByte(0xFC); // Chunk File Magic
// TODO: Change to 1 when lighting actually works // TODO: Change to 1 when lighting actually works
chunkFileWriter.writeUByte(0); // File Version chunkFileWriter.writeUByte(0); // File Version
@ -97,7 +98,7 @@ export class WorldSaveManager {
chunkFileWriter.writeUByte(128); // Chunk Y chunkFileWriter.writeUByte(128); // Chunk Y
chunkFileWriter.writeUByte(16); // Chunk Z chunkFileWriter.writeUByte(16); // Chunk Z
const chunkData = new Writer() const chunkData = createWriter(Endian.BE)
.writeBuffer(Buffer.from(chunk.getData())) .writeBuffer(Buffer.from(chunk.getData()))
.writeBuffer(chunk.getMetadataBuffer()).toBuffer() .writeBuffer(chunk.getMetadataBuffer()).toBuffer()
//.writeBuffer(chunk.getBlockLightBuffer()) //.writeBuffer(chunk.getBlockLightBuffer())
@ -146,7 +147,7 @@ export class WorldSaveManager {
return reject(err); return reject(err);
} }
const chunkFileReader = new Reader(data); const chunkFileReader = createReader(Endian.BE, data);
// Check file validity // Check file validity
if (chunkFileReader.readUByte() !== 0xFC) { if (chunkFileReader.readUByte() !== 0xFC) {
@ -163,7 +164,7 @@ export class WorldSaveManager {
const contentLength = chunkFileReader.readInt(); const contentLength = chunkFileReader.readInt();
if (saveCompressionType === SaveCompressionType.NONE) { 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)); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2));
resolve(chunk); resolve(chunk);
} else if (saveCompressionType === SaveCompressionType.DEFLATE) { } else if (saveCompressionType === SaveCompressionType.DEFLATE) {
@ -172,7 +173,7 @@ export class WorldSaveManager {
return reject(err); 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)); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2));
resolve(chunk); resolve(chunk);
}); });
@ -186,7 +187,7 @@ export class WorldSaveManager {
const contentLength = chunkFileReader.readInt(); const contentLength = chunkFileReader.readInt();
if (saveCompressionType === SaveCompressionType.NONE) { 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)); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2));
resolve(chunk); resolve(chunk);
} else if (saveCompressionType === SaveCompressionType.DEFLATE) { } else if (saveCompressionType === SaveCompressionType.DEFLATE) {
@ -195,7 +196,7 @@ export class WorldSaveManager {
return reject(err); 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)); const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2), chunkData.readUint8Array(totalByteSize / 2), chunkData.readUint8Array(totalByteSize / 2));
resolve(chunk); resolve(chunk);
}); });

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketAnimation implements IPacket { export class PacketAnimation implements IPacket {
public packetId = Packet.Animation; 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.entityId = reader.readInt();
this.animation = reader.readByte(); this.animation = reader.readByte();
@ -25,6 +27,6 @@ export class PacketAnimation implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketBlockChange implements IPacket { export class PacketBlockChange implements IPacket {
public packetId = Packet.BlockChange; 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.x = reader.readInt();
this.y = reader.readByte(); this.y = reader.readByte();
this.z = reader.readInt(); this.z = reader.readInt();
@ -37,6 +39,6 @@ export class PacketBlockChange implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketChat implements IPacket { export class PacketChat implements IPacket {
public packetId = Packet.Chat; public packetId = Packet.Chat;
@ -14,13 +16,13 @@ export class PacketChat implements IPacket {
} }
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.message = reader.readString(); this.message = reader.readString16();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketDisconnectKick implements IPacket { export class PacketDisconnectKick implements IPacket {
public packetId = Packet.DisconnectKick; public packetId = Packet.DisconnectKick;
@ -14,12 +16,12 @@ export class PacketDisconnectKick implements IPacket {
} }
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.reason = reader.readString(); this.reason = reader.readString16();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntity implements IPacket { export class PacketEntity implements IPacket {
public packetId = Packet.Entity; 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(); this.entityId = reader.readInt();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntityAction implements IPacket { export class PacketEntityAction implements IPacket {
public packetId = Packet.EntityAction; 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.entityId = reader.readInt();
this.action = reader.readByte(); this.action = reader.readByte();
@ -25,6 +27,6 @@ export class PacketEntityAction implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntityEquipment implements IPacket { export class PacketEntityEquipment implements IPacket {
public packetId = Packet.EntityEquipment; public packetId = Packet.EntityEquipment;
@ -16,7 +18,7 @@ export class PacketEntityEquipment implements IPacket {
this.damage = damage; this.damage = damage;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.entityId = reader.readInt(); this.entityId = reader.readInt();
this.slot = reader.readShort(); this.slot = reader.readShort();
this.itemId = reader.readShort(); this.itemId = reader.readShort();
@ -26,6 +28,6 @@ export class PacketEntityEquipment implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntityLook implements IPacket { export class PacketEntityLook implements IPacket {
public packetId = Packet.EntityLook; 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.entityId = reader.readInt();
this.yaw = reader.readByte(); this.yaw = reader.readByte();
this.pitch = reader.readByte(); this.pitch = reader.readByte();
@ -29,6 +31,6 @@ export class PacketEntityLook implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntityLookRelativeMove implements IPacket { export class PacketEntityLookRelativeMove implements IPacket {
public packetId = Packet.EntityLookRelativeMove; 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.entityId = reader.readInt();
this.dX = reader.readByte(); this.dX = reader.readByte();
this.dY = reader.readByte(); this.dY = reader.readByte();
@ -41,6 +43,6 @@ export class PacketEntityLookRelativeMove implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
const EMPTY_BUFFER = Buffer.alloc(0); 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 // TODO: EntityMetadata reading
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntityRelativeMove implements IPacket { export class PacketEntityRelativeMove implements IPacket {
public packetId = Packet.EntityRelativeMove; 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.entityId = reader.readInt();
this.dX = reader.readByte(); this.dX = reader.readByte();
this.dY = reader.readByte(); this.dY = reader.readByte();
@ -33,6 +35,6 @@ export class PacketEntityRelativeMove implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketEntityTeleport implements IPacket { export class PacketEntityTeleport implements IPacket {
public packetId = Packet.EntityTeleport; 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.entityId = reader.readInt();
this.x = reader.readInt(); this.x = reader.readInt();
this.y = reader.readInt(); this.y = reader.readInt();
@ -41,6 +43,6 @@ export class PacketEntityTeleport implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createReader, createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketHandshake implements IPacket { export class PacketHandshake implements IPacket {
public packetId:Packet = Packet.Handshake; public packetId:Packet = Packet.Handshake;
@ -14,13 +16,13 @@ export class PacketHandshake implements IPacket {
} }
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.username = reader.readString(); this.username = reader.readString16();
return this; return this;
} }
public writeData() { public writeData() {
return new Writer(5).writeUByte(this.packetId).writeString("-").toBuffer(); return createWriter(Endian.BE, 5).writeUByte(this.packetId).writeString16("-").toBuffer();
} }
} }

View file

@ -1,8 +1,8 @@
import { Reader } from "../../bufferStuff"; import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet"; import { Packet } from "../enums/Packet";
export interface IPacket { export interface IPacket {
packetId: Packet, packetId: Packet,
readData: (reader:Reader) => IPacket, readData: (reader:IReader) => IPacket,
writeData: () => Buffer|Promise<Buffer> writeData: () => Buffer|Promise<Buffer>
} }

View file

@ -1,12 +1,14 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketKeepAlive implements IPacket { export class PacketKeepAlive implements IPacket {
public packetId = Packet.KeepAlive; 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; reader;
return this; return this;
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketLoginRequest implements IPacket { export class PacketLoginRequest implements IPacket {
public packetId = Packet.LoginRequest; 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.protocolVersion = reader.readInt();
this.username = reader.readString(); this.username = reader.readString16();
this.mapSeed = Number(reader.readLong()); this.mapSeed = Number(reader.readLong());
this.dimension = reader.readByte(); this.dimension = reader.readByte();
@ -33,6 +35,6 @@ export class PacketLoginRequest implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -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 { 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 { export class PacketMapChunk implements IPacket {
public packetId = Packet.MapChunk; public packetId = Packet.MapChunk;
@ -24,7 +26,7 @@ export class PacketMapChunk implements IPacket {
this.chunk = chunk; this.chunk = chunk;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
// TODO: Implement MapChunk reading // TODO: Implement MapChunk reading
reader.readBool(); reader.readBool();
@ -34,19 +36,19 @@ export class PacketMapChunk implements IPacket {
public writeData() { public writeData() {
return new Promise<Buffer>((resolve, reject) => { return new Promise<Buffer>((resolve, reject) => {
// TODO: Use block and sky nibble array buffers // 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++) { for (let i = 0; i < 16384; i++) {
fakeLighting.writeUByte(0xFF); 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) => { deflate(data.toBuffer(), (err, data) => {
if (err) { if (err) {
return reject(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());
}); });
}); });
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketNamedEntitySpawn implements IPacket { export class PacketNamedEntitySpawn implements IPacket {
public packetId = Packet.NamedEntitySpawn; 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.entityId = reader.readInt();
this.playerName = reader.readString(); this.playerName = reader.readString16();
this.x = reader.readInt(); this.x = reader.readInt();
this.y = reader.readInt(); this.y = reader.readInt();
this.z = reader.readInt(); this.z = reader.readInt();
@ -48,6 +50,6 @@ export class PacketNamedEntitySpawn implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketPlayer implements IPacket { export class PacketPlayer implements IPacket {
public packetId = Packet.Player; 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(); this.onGround = reader.readBool();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketPlayerDigging implements IPacket { export class PacketPlayerDigging implements IPacket {
public packetId = Packet.PlayerDigging; 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.status = reader.readByte();
this.x = reader.readInt(); this.x = reader.readInt();
this.y = reader.readByte(); this.y = reader.readByte();
@ -37,6 +39,6 @@ export class PacketPlayerDigging implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketPlayerLook implements IPacket { export class PacketPlayerLook implements IPacket {
public packetId = Packet.Player; 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.yaw = reader.readFloat();
this.pitch = reader.readFloat(); this.pitch = reader.readFloat();
this.onGround = reader.readBool(); this.onGround = reader.readBool();
@ -29,6 +31,6 @@ export class PacketPlayerLook implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketPlayerPosition implements IPacket { export class PacketPlayerPosition implements IPacket {
public packetId = Packet.PlayerPosition; 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.x = reader.readDouble();
this.y = reader.readDouble(); this.y = reader.readDouble();
this.stance = reader.readDouble(); this.stance = reader.readDouble();
@ -37,6 +39,6 @@ export class PacketPlayerPosition implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketPlayerPositionLook implements IPacket { export class PacketPlayerPositionLook implements IPacket {
public packetId = Packet.PlayerPosition; 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.x = reader.readDouble();
this.y = reader.readDouble(); this.y = reader.readDouble();
this.stance = reader.readDouble(); this.stance = reader.readDouble();
@ -45,6 +47,6 @@ export class PacketPlayerPositionLook implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketPreChunk implements IPacket { export class PacketPreChunk implements IPacket {
public packetId = Packet.PreChunk; public packetId = Packet.PreChunk;
@ -14,7 +16,7 @@ export class PacketPreChunk implements IPacket {
this.mode = mode; this.mode = mode;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.x = reader.readInt(); this.x = reader.readInt();
this.z = reader.readInt(); this.z = reader.readInt();
this.mode = reader.readBool(); this.mode = reader.readBool();
@ -23,6 +25,6 @@ export class PacketPreChunk implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketRespawn implements IPacket { export class PacketRespawn implements IPacket {
public packetId = Packet.Respawn; public packetId = Packet.Respawn;
@ -10,13 +12,13 @@ export class PacketRespawn implements IPacket {
this.health = health; this.health = health;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.health = reader.readShort(); this.health = reader.readShort();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketSpawnPosition implements IPacket { export class PacketSpawnPosition implements IPacket {
public packetId = Packet.SpawnPosition; public packetId = Packet.SpawnPosition;
@ -14,7 +16,7 @@ export class PacketSpawnPosition implements IPacket {
this.z = z; this.z = z;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.x = reader.readInt(); this.x = reader.readInt();
this.y = reader.readInt(); this.y = reader.readInt();
this.z = reader.readInt(); this.z = reader.readInt();
@ -23,6 +25,6 @@ export class PacketSpawnPosition implements IPacket {
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketTimeUpdate implements IPacket { export class PacketTimeUpdate implements IPacket {
public packetId = Packet.TimeUpdate; public packetId = Packet.TimeUpdate;
@ -10,13 +12,13 @@ export class PacketTimeUpdate implements IPacket {
this.time = time; this.time = time;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.time = reader.readLong(); this.time = reader.readLong();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketUpdateHealth implements IPacket { export class PacketUpdateHealth implements IPacket {
public packetId = Packet.UpdateHealth; public packetId = Packet.UpdateHealth;
@ -10,13 +12,13 @@ export class PacketUpdateHealth implements IPacket {
this.health = health; this.health = health;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.health = reader.readShort(); this.health = reader.readShort();
return this; return this;
} }
public writeData() { 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();
} }
} }

View file

@ -1,6 +1,8 @@
import { Reader, Writer } from "../../bufferStuff"; import { createWriter } from "../../bufferStuff/index";
import { Packet } from "../enums/Packet"; import { Endian } from "../../bufferStuff/Endian";
import { IPacket } from "./IPacket"; import { IPacket } from "./IPacket";
import { IReader } from "../../bufferStuff/readers/IReader";
import { Packet } from "../enums/Packet";
export class PacketUseEntity implements IPacket { export class PacketUseEntity implements IPacket {
public packetId = Packet.UseEntity; public packetId = Packet.UseEntity;
@ -14,7 +16,7 @@ export class PacketUseEntity implements IPacket {
this.leftClick = leftClick; this.leftClick = leftClick;
} }
public readData(reader:Reader) { public readData(reader:IReader) {
this.userId = reader.readInt(); this.userId = reader.readInt();
this.targetId = reader.readInt(); this.targetId = reader.readInt();
this.leftClick = reader.readBool(); this.leftClick = reader.readBool();
@ -23,6 +25,6 @@ export class PacketUseEntity implements IPacket {
} }
public writeData() { 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();
} }
} }