feat: exported the BinaryStream API

This commit is contained in:
子沐呀
2024-10-10 00:59:14 +08:00
Unverified
parent 5b1467c8ef
commit 186d1ca72a
7 changed files with 670 additions and 5 deletions
+385 -2
View File
@@ -374,7 +374,61 @@ const GMLIB_API = {
getDefaultGameMode: ll.import("GMLIB_API", "getDefaultGameMode"),
/** 设置默认游戏模式 @type {function(mode):void} */
setDefaultGameMode: ll.import("GMLIB_API", "setDefaultGameMode"),
}
};
/** LRCA的二进制流数据包导出接口 */
const GMLIB_BinaryStream_API = {
/** 创建二进制流数据包 @type {function():number} */
create: ll.import("GMLIB_BinaryStream_API", "create"),
/** 发送二进制流数据包 @type {function(number,player, boolean):void} */
sendTo: ll.import("GMLIB_BinaryStream_API", "sendTo"),
/** 销毁二进制流数据包 @type {function(number):void} */
destroy: ll.import("GMLIB_BinaryStream_API", "destroy"),
/** 重置二进制流数据包 @type {function(number):void} */
reset: ll.import("GMLIB_BinaryStream_API", "reset"),
/** 写入二进制流数据包头部 @type {function(number, number):void} */
writePacketHeader: ll.import("GMLIB_BinaryStream_API", "writePacketHeader"),
/** 写入UUID @type {function(number, string):void} */
writeUuid: ll.import("GMLIB_BinaryStream_API", "writeUuid"),
/** 写入物品 @type {function(number, Item):void} */
writeItem: ll.import("GMLIB_BinaryStream_API", "writeItem"),
/** 写入NBT @type {function(NbtCompound, Item):void} */
writeCompoundTag: ll.import("GMLIB_BinaryStream_API", "writeCompoundTag"),
/** 写入字符串 @type {function(number, string):void} */
writeString: ll.import("GMLIB_BinaryStream_API", "writeString"),
/** 写入布尔值 @type {function(number, boolean):void} */
writeBool: ll.import("GMLIB_BinaryStream_API", "writeBool"),
/** 写入字节 @type {function(number, number):void} */
writeByte: ll.import("GMLIB_BinaryStream_API", "writeByte"),
/** 写入双精度浮点数 @type {function(number, number):void} */
writeDouble: ll.import("GMLIB_BinaryStream_API", "writeDouble"),
/** 写入浮点数 @type {function(number, number):void} */
writeFloat: ll.import("GMLIB_BinaryStream_API", "writeFloat"),
/** @type {function(number, number):void} */
writeSignedBigEndianInt: ll.import("GMLIB_BinaryStream_API", "writeSignedBigEndianInt"),
/** @type {function(number, number):void} */
writeSignedInt: ll.import("GMLIB_BinaryStream_API", "writeSignedInt"),
/** @type {function(number, number):void} */
writeSignedInt64: ll.import("GMLIB_BinaryStream_API", "writeSignedInt64"),
/** @type {function(number, number):void} */
writeSignedShort: ll.import("GMLIB_BinaryStream_API", "writeSignedShort"),
/** @type {function(number, number):void} */
writeUnsignedChar: ll.import("GMLIB_BinaryStream_API", "writeUnsignedChar"),
/** @type {function(number, number):void} */
writeUnsignedInt: ll.import("GMLIB_BinaryStream_API", "writeUnsignedInt"),
/** @type {function(number, number):void} */
writeUnsignedInt64: ll.import("GMLIB_BinaryStream_API", "writeUnsignedInt64"),
/** @type {function(number, number):void} */
writeUnsignedShort: ll.import("GMLIB_BinaryStream_API", "writeUnsignedShort"),
/** @type {function(number, number):void} */
writeUnsignedVarInt: ll.import("GMLIB_BinaryStream_API", "writeUnsignedVarInt"),
/** @type {function(number, number):void} */
writeUnsignedVarInt64: ll.import("GMLIB_BinaryStream_API", "writeUnsignedVarInt64"),
/** @type {function(number, number):void} */
writeVarInt: ll.import("GMLIB_BinaryStream_API", "writeVarInt"),
/** @type {function(number, number):void} */
writeVarInt64: ll.import("GMLIB_BinaryStream_API", "writeVarInt64"),
};
/** 静态悬浮字类列表 @type {Map<number,StaticFloatingText>} */
const mStaticFloatingTextMap = new Map();
@@ -2052,6 +2106,334 @@ class UserCache {
}
};
/** 二进制数据包API类 */
class GMLIB_BinaryStream {
/** 数据包ID @type {number} */
#id = null;
/** 数据包是否被销毁 */
#destroy = false;
constructor() {
this.#id = GMLIB_BinaryStream_API.create();
}
/**
* 发送数据包
* @param {Player} player 玩家
* @param {boolean} [free=true] 发送后销毁数据包
*/
sendTo(player, free = true) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.sendTo(this.#id, player, free);
if (free) {
this.destroy();
this.#destroy = true;
}
}
/**
* 发送数据包到玩家
* @param {Player[]} players 玩家对象数组
* @param {boolean} free 发送后销毁数据包
*/
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();
this.#destroy = true;
}
}
/**
* 发送数据包到所有玩家
* @param {boolean} [free=true] 发送后销毁数据包
*/
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();
this.#destroy = true;
}
}
/**
* 发送数据包到维度
* @param {number} dimid 维度ID
* @param {boolean} [free=true] 发送后销毁数据包
*/
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();
this.#destroy = true;
}
}
/**
* 销毁数据包
*/
destroy() {
GMLIB_BinaryStream_API.destroy(this.#id);
this.#destroy = true;
}
/**
* 重置数据包
*/
reset() {
GMLIB_BinaryStream_API.reset(this.#id);
this.#destroy = false;
}
/**
* 写入数据包头部
* @param {number} id 数据包ID
*/
writePacketHeader(id) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writePacketHeader(this.#id, id);
}
/**
* 写入UUID
* @param {string} value UUID
*/
writeUuid(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUuid(this.#id, value);
}
/**
* 写入物品
* @param {Item} value 物品
*/
writeItem(value){
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeItem(this.#id, value);
}
/**
* 写入NBT
* @param {NbtCompound} value NBT
*/
writeCompoundTag(value){
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeCompoundTag(this.#id, value);
}
/**
* @param {{"id":number,"type":number,"value":string|IntPos|FloatPos|number|NbtCompound}[]} value 数据
*/
writeDataItem(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
this.writeUnsignedVarInt(value.length);
for (const data of value) {
this.writeUnsignedVarInt(data.id);
this.writeUnsignedVarInt(data.type);
switch (data.type) {
case 0: this.writeByte(data.value); break;
case 1: this.writeSignedShort(data.value); break;
case 2: this.writeSignedInt(data.value); break;
case 3: this.writeFloat(data.value); break;
case 4: this.writeString(data.value); break;
case 5: this.writeCompoundTag(data.value); break;
case 6: this.writeBlockPos(data.value); break;
case 7: this.writeSignedInt64(data.value); break;
case 8: this.writeVec3(data.value); break;
default: throw new Error("Unknown data type");
}
}
}
/**
* @param {{"mA":number,"mB":number,"mType":number,"mImmediate":boolean,"mPassengerInitiated":boolean}} value 数据
*/
writeActorLink(value) {
this.writeVarInt64(value.mA);
this.writeVarInt64(value.mB);
this.writeUnsignedChar(value.mType);
this.writeBool(value.mImmediate);
this.writeBool(value.mPassengerInitiated);
}
/**
* 写入字符串
* @param {string} value 字符串
*/
writeString(value){
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeString(this.#id, value);
}
/**
* 写入布尔值
* @param {boolean} value 布尔值
*/
writeBool(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeBool(this.#id, value);
}
/**
* 写入字节
* @param {number} value 字节
*/
writeByte(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeByte(this.#id, value);
}
/**
* 写入(双精度)浮点数
* @param {number} value 浮点数
*/
writeDouble(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeDouble(this.#id, value);
}
/**
* 写入(单精度)浮点数
* @param {number} value 浮点数
*/
writeFloat(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeFloat(this.#id, value);
}
/**
* 写入浮点数坐标对象
* @param {FloatPos} value 浮点数坐标对象
*/
writeVec3({ x, y, z }) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
this.writeFloat(x);
this.writeFloat(y);
this.writeFloat(z);
}
/**
* 写入整数坐标对象
* @param {IntPos} value 整数坐标对象
*/
writeBlockPos({ x, y, z }) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
this.writeVarInt(x);
this.writeUnsignedVarInt(y);
this.writeVarInt(z);
}
/**
* 写入方向角对象
* @param {DirectionAngle} value 方向角对象
*/
writeVec2({ pitch, yaw }) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
this.writeFloat(pitch);
this.writeFloat(yaw);
}
/**
* @param {number} value 数值
*/
writeSignedBigEndianInt(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeSignedBigEndianInt(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeSignedInt(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeSignedInt(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeSignedInt64(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeSignedInt64(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeSignedShort(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeSignedShort(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeUnsignedChar(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUnsignedChar(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeUnsignedInt(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUnsignedInt(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeUnsignedInt64(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUnsignedInt64(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeUnsignedShort(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUnsignedShort(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeUnsignedVarInt(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUnsignedVarInt(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeUnsignedVarInt64(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeUnsignedVarInt64(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeVarInt(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeVarInt(this.#id, value);
}
/**
* @param {number} value 数值
*/
writeVarInt64(value) {
if (this.#destroy) throw new Error("The BinaryStream has been destroyed");
GMLIB_BinaryStream_API.writeVarInt64(this.#id, value);
}
}
LLSE_Player.prototype.toEntity =
/**
* 获取实体对象
@@ -2661,5 +3043,6 @@ module.exports = {
JsonI18n,
Version,
I18nAPI,
UserCache
UserCache,
GMLIB_BinaryStream
};