Add Packets DestroyEntity and EntityVelocity

This commit is contained in:
Holly Stubbs 2023-11-05 10:55:49 +00:00
parent bb733ff2af
commit 1b2b1a091e
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
3 changed files with 77 additions and 0 deletions

View file

@ -22,6 +22,9 @@ export enum Packet {
EntityAction = 0x13,
NamedEntitySpawn = 0x14,
EntityVelocity = 0x1C,
DestroyEntity = 0x1D,
EntityStatus = 0x26,
EntityMetadata = 0x28,

View file

@ -0,0 +1,25 @@
import { createWriter, IReader, Endian } from "bufferstuff";
import { IPacket } from "./IPacket";
import { Packet } from "../enums/Packet";
export class PacketDestroyEntity implements IPacket {
public packetId = Packet.DestroyEntity;
public entityId:number;
public constructor(entityId?:number) {
if (typeof(entityId) === "number") {
this.entityId = entityId;
} else {
this.entityId = Number.MIN_VALUE;
}
}
public readData(reader:IReader) {
this.entityId = reader.readInt();
return this;
}
public writeData() {
return createWriter(Endian.BE, 5).writeUByte(this.packetId).writeInt(this.entityId).toBuffer();
}
}

View file

@ -0,0 +1,49 @@
import { createWriter, IReader, Endian } from "bufferstuff";
import { IPacket } from "./IPacket";
import { Packet } from "../enums/Packet";
const MOTION_MAX = 3.9;
function maxMotion(value:number) {
if (value < -MOTION_MAX) {
value = -MOTION_MAX;
} else if (value > MOTION_MAX) {
value = MOTION_MAX;
}
return value;
}
export class PacketEntityVelocity implements IPacket {
public packetId = Packet.EntityVelocity;
public entityId:number;
public x:number;
public y:number;
public z:number;
public constructor(entityId?:number, x?:number, y?:number, z?:number) {
if (typeof(entityId) == "number" && typeof(x) === "number" && typeof(y) === "number" && typeof(z) === "number") {
this.entityId = entityId;
this.x = maxMotion(x) * 8000;
this.y = maxMotion(y) * 8000;
this.z = maxMotion(z) * 8000;
} else {
this.entityId = Number.MIN_VALUE;
this.x = Number.MIN_VALUE;
this.y = Number.MIN_VALUE;
this.z = Number.MIN_VALUE;
}
}
public readData(reader:IReader) {
this.entityId = reader.readInt();
this.x = reader.readShort() / 8000;
this.y = reader.readShort() / 8000;
this.z = reader.readShort() / 8000;
return this;
}
public writeData() {
return createWriter(Endian.BE, 11).writeUByte(this.packetId).writeInt(this.entityId).writeShort(this.x).writeShort(this.y).writeShort(this.z).toBuffer();
}
}