feat: add copy constructor and getId method for GMLIB_BinaryStream

This commit is contained in:
子沐呀
2024-12-17 20:36:12 +08:00
Unverified
parent 4c57b04b66
commit cf8076a9fc
3 changed files with 42 additions and 9 deletions
+18 -7
View File
@@ -380,6 +380,8 @@ const GMLIB_API = {
const GMLIB_BinaryStream_API = {
/** 创建二进制流数据包 @type {function():number} */
create: ll.import("GMLIB_BinaryStream_API", "create"),
/** 拷贝二进制流数据包 @type {function(number):number} */
copy: ll.import("GMLIB_BinaryStream_API", "copy"),
/** 发送二进制流数据包 @type {function(number,player):void} */
sendTo: ll.import("GMLIB_BinaryStream_API", "sendTo"),
/** 销毁二进制流数据包 @type {function(number):void} */
@@ -2116,30 +2118,39 @@ class GMLIB_BinaryStream {
/**
* 创建二进制流数据包
* @param {{"type":string,"value":any}[]?} value 写入数据
* @param {({"type":string,"value":any}[]|GMLIB_BinaryStream)?} value 写入数据
* @returns {GMLIB_BinaryStream}
*/
constructor(value) {
this.#id = GMLIB_BinaryStream_API.create();
this.#id = value instanceof GMLIB_BinaryStream ? GMLIB_BinaryStream_API.copy(value.getId()) : GMLIB_BinaryStream_API.create();
if (Array.isArray(value)) this.write(value);
}
/**
* 获取数据包的ID
* @returns {number}
*/
getId() {
return this.#id;
}
/**
* 发送数据包
* @param {Player} player 玩家
* @param {Player|Entity} 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);
if (player instanceof LLSE_Entity && player.isPlayer()) player = player.toPlayer();
if (player instanceof LLSE_Player && !player.isSimulatedPlayer()) GMLIB_BinaryStream_API.sendTo(this.#id, player);
if (free) this.destroy();
return this;
}
/**
* 发送数据包到玩家
* @param {Player[]} players 玩家对象数组
* @param {Player[]|Entity[]} players 玩家对象数组
* @param {boolean} free 发送后销毁数据包
* @returns {GMLIB_BinaryStream}
*/
@@ -2324,7 +2335,7 @@ class GMLIB_BinaryStream {
*/
writeBool(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeBool(this.#id, value);
GMLIB_BinaryStream_API.writeBool(this.#id, Boolean(value));
return this;
}
@@ -2335,7 +2346,7 @@ class GMLIB_BinaryStream {
*/
writeByte(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeByte(this.#id, value);
GMLIB_BinaryStream_API.writeByte(this.#id, Number(value));
return this;
}