feat: modify GMLIB_BinaryStream API to return the BinaryStream object itself after each method call
This commit is contained in:
+88
-6
@@ -380,7 +380,7 @@ const GMLIB_API = {
|
||||
const GMLIB_BinaryStream_API = {
|
||||
/** 创建二进制流数据包 @type {function():number} */
|
||||
create: ll.import("GMLIB_BinaryStream_API", "create"),
|
||||
/** 发送二进制流数据包 @type {function(number,player, boolean):void} */
|
||||
/** 发送二进制流数据包 @type {function(number,player):void} */
|
||||
sendTo: ll.import("GMLIB_BinaryStream_API", "sendTo"),
|
||||
/** 销毁二进制流数据包 @type {function(number):void} */
|
||||
destroy: ll.import("GMLIB_BinaryStream_API", "destroy"),
|
||||
@@ -2114,107 +2114,146 @@ class GMLIB_BinaryStream {
|
||||
/** 数据包是否被销毁 */
|
||||
#destroy = false;
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* 创建二进制流数据包
|
||||
* @param {{"type":string,"value":any}[]?} value 写入数据
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
constructor(value) {
|
||||
this.#id = GMLIB_BinaryStream_API.create();
|
||||
if (Array.isArray(value)) this.write(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据包
|
||||
* @param {Player} player 玩家
|
||||
* @param {boolean} [free=true] 发送后销毁数据包
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
sendTo(player, free = true) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.sendTo(this.#id, player, free);
|
||||
GMLIB_BinaryStream_API.sendTo(this.#id, player);
|
||||
if (free) this.destroy();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据包到玩家
|
||||
* @param {Player[]} players 玩家对象数组
|
||||
* @param {boolean} free 发送后销毁数据包
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
sendToPlayers(players, free = true) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
players.forEach(player => this.sendTo(player, false));
|
||||
if (free) this.destroy();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据包到所有玩家
|
||||
* @param {boolean} [free=true] 发送后销毁数据包
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
sendToAll(free = true) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
mc.getOnlinePlayers().forEach(player => this.sendTo(player, false));
|
||||
if (free) this.destroy();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送数据包到维度
|
||||
* @param {number} dimid 维度ID
|
||||
* @param {boolean} [free=true] 发送后销毁数据包
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
sendToDimension(dimid, free = true) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
mc.getOnlinePlayers().filter(player => player.pos.dimid === dimid).forEach(player => this.sendTo(player, false));
|
||||
if (free) this.destroy();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁数据包
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
destroy() {
|
||||
GMLIB_BinaryStream_API.destroy(this.#id);
|
||||
this.#destroy = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置数据包
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
reset() {
|
||||
GMLIB_BinaryStream_API.reset(this.#id);
|
||||
this.#destroy = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入数据
|
||||
* @param {{"type":string,"value":any}[]|{"type":string,"value":any}} value 数据
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
write(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
if (Array.isArray(value)) value.forEach(v => this.write(v));
|
||||
if (value?.type != null && value?.value != null) this[`write${value.type}`]?.(value.value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入数据包头部
|
||||
* @param {number} id 数据包ID
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writePacketHeader(id) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writePacketHeader(this.#id, id);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入UUID
|
||||
* @param {string} value UUID
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUuid(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUuid(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入物品
|
||||
* @param {Item} value 物品
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeItem(value){
|
||||
writeItem(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeItem(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入NBT
|
||||
* @param {NbtCompound} value NBT
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeCompoundTag(value){
|
||||
writeCompoundTag(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeCompoundTag(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{"id":number,"type":number,"value":string|IntPos|FloatPos|number|NbtCompound}[]} value 数据
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeDataItem(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
@@ -2235,10 +2274,12 @@ class GMLIB_BinaryStream {
|
||||
default: throw new Error("Unknown data type");
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{"mA":number,"mB":number,"mType":number,"mImmediate":boolean,"mPassengerInitiated":boolean}} value 数据
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeActorLink(value) {
|
||||
this.writeVarInt64(value.mA);
|
||||
@@ -2246,179 +2287,220 @@ class GMLIB_BinaryStream {
|
||||
this.writeUnsignedChar(value.mType);
|
||||
this.writeBool(value.mImmediate);
|
||||
this.writeBool(value.mPassengerInitiated);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入字符串
|
||||
* @param {string} value 字符串
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeString(value){
|
||||
writeString(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeString(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入布尔值
|
||||
* @param {boolean} value 布尔值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeBool(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeBool(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入字节
|
||||
* @param {number} value 字节
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeByte(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeByte(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入(双精度)浮点数
|
||||
* @param {number} value 浮点数
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeDouble(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeDouble(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入(单精度)浮点数
|
||||
* @param {number} value 浮点数
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeFloat(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeFloat(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入浮点数坐标对象
|
||||
* @param {FloatPos} value 浮点数坐标对象
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeVec3({ x, y, z }) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
this.writeFloat(x);
|
||||
this.writeFloat(y);
|
||||
this.writeFloat(z);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入整数坐标对象
|
||||
* @param {IntPos} value 整数坐标对象
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeBlockPos({ x, y, z }) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
this.writeVarInt(x);
|
||||
this.writeUnsignedVarInt(y);
|
||||
this.writeVarInt(z);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入方向角对象
|
||||
* @param {DirectionAngle} value 方向角对象
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeVec2({ pitch, yaw }) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
this.writeFloat(pitch);
|
||||
this.writeFloat(yaw);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeSignedBigEndianInt(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeSignedBigEndianInt(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeSignedInt(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeSignedInt(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeSignedInt64(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeSignedInt64(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeSignedShort(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeSignedShort(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUnsignedChar(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUnsignedChar(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUnsignedInt(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUnsignedInt(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUnsignedInt64(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUnsignedInt64(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUnsignedShort(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUnsignedShort(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUnsignedVarInt(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUnsignedVarInt(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeUnsignedVarInt64(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeUnsignedVarInt64(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeVarInt(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeVarInt(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value 数值
|
||||
* @returns {GMLIB_BinaryStream}
|
||||
*/
|
||||
writeVarInt64(value) {
|
||||
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
|
||||
GMLIB_BinaryStream_API.writeVarInt64(this.#id, value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user