From 52897a25a7c40438a5ef8da022d60746856b3c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E6=B2=90=E5=91=80?= <163636894+zimuya4153@users.noreply.github.com> Date: Thu, 18 Jul 2024 21:39:49 +0800 Subject: [PATCH] =?UTF-8?q?1=E6=96=B0=E5=A2=9E=202=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增getEntityOwnerUniqueId接口 - 修复注册PAPI变量无法使用匿名函数 - 修复注册相同PAPI名字无法重载 --- lib/BEPlaceholderAPI-JS.js | 29 ++++++++++++++++++++--------- lib/GMLIB_API-JS.d.ts | 3 +++ lib/GMLIB_API-JS.js | 18 +++++++++++++++--- src/CompatibilityApi.cpp | 5 +++++ src/PlaceholderApi.cpp | 4 ++++ 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/lib/BEPlaceholderAPI-JS.js b/lib/BEPlaceholderAPI-JS.js index 6d5af74..b2ce12d 100644 --- a/lib/BEPlaceholderAPI-JS.js +++ b/lib/BEPlaceholderAPI-JS.js @@ -19,14 +19,25 @@ const PlaceholderAPI = { getAllPAPI: ll.import("BEPlaceholderAPI", "getAllPAPI") } -Function.prototype.getName = -/** - * 获取函数名字 - * @returns {string} - */ -function () { - return this.name || this.toString().match(/function\s*([^(]*)\(/)[1] || Math.ceil(Math.random() * 1000000).toString(16); -} +Function.prototype.getName = + /** + * 获取函数名字 + * @returns {string} + */ + function () { + return this.name || this.toString().match(/function\s*([^(]*)\(/)?.[1] || this.toString().hashCode().toString(16); + } + +String.prototype.hashCode = function () { + var hash = 0, i, chr; + if (this.length === 0) return hash; + for (i = 0; i < this.length; i++) { + chr = this.charCodeAt(i); + hash = ((hash << 5) - hash) + chr; + hash |= 0; + } + return hash; +}; /** PAPI变量类 */ class PAPI { @@ -79,7 +90,7 @@ class PAPI { static getValue(key) { return PlaceholderAPI.getValueAPI(key); } - + /** * 获取一个玩家变量的值 * @param {string} key PAPI变量名 diff --git a/lib/GMLIB_API-JS.d.ts b/lib/GMLIB_API-JS.d.ts index b21eeff..4d6380b 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -1083,6 +1083,9 @@ interface Entity { /** (GMLIB)获取实体的命名 */ getNameTag(): string; + + /** 获取实体的主人 */ + getEntityOwner(): Entity | null; } interface Block { diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index a9d3e1c..acb42d9 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -318,6 +318,8 @@ const GMLIB_API = { getPlayerArmorCoverPercentage: ll.import("GMLIB_API", "getPlayerArmorCoverPercentage"), /** 获取玩家盔甲值 @type {function(Player):number} */ getPlayerArmorValue: ll.import("GMLIB_API", "getPlayerArmorValue"), + /** 获取实体主人的UniqueID @type {function(Entity):Entity} */ + getEntityOwnerUniqueId: ll.import("GMLIB_API", "getEntityOwnerUniqueId") } /** 静态悬浮字类列表 @type {Map} */ @@ -2357,7 +2359,7 @@ LLSE_Player.prototype.getHungry = * @returns {number} */ function () { - return GMLIB_API.getPlayerHungry(this) + return GMLIB_API.getPlayerHungry(this); } LLSE_Player.prototype.getArmorCoverPercentage = @@ -2366,7 +2368,7 @@ LLSE_Player.prototype.getArmorCoverPercentage = * @returns {number} */ function () { - return GMLIB_API.getPlayerArmorCoverPercentage(this) + return GMLIB_API.getPlayerArmorCoverPercentage(this); } LLSE_Player.prototype.getArmorValue = @@ -2375,7 +2377,17 @@ LLSE_Player.prototype.getArmorValue = * @returns {number} */ function () { - return GMLIB_API.getPlayerArmorValue(this) + return GMLIB_API.getPlayerArmorValue(this); + } + +LLSE_Player.prototype.getEntityOwner = + /** + * 获取实体的主人 + * @returns {Entity|null} + */ + function () { + const uniqueId = GMLIB_API.getEntityOwnerUniqueId(this); + return uniqueId === -1 ? null : Minecraft.getEntityFromUniqueId(uniqueId); } module.exports = { diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index 9503c39..c4cf94b 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -1,5 +1,9 @@ #include "Global.h" +#include "magic_enum.hpp" +#include "mc/entity/systems/common/ServerCameraStateSystem.h" #include "mc/enums/AbilitiesIndex.h" +#include "mc/scripting/modules/minecraft/ScriptCameraSetLocationOptions.h" +#include "mc/world/actor/common/CameraInstruction.h" #include #include @@ -843,4 +847,5 @@ void Export_Compatibility_API() { RemoteCall::exportAs("GMLIB_API", "getPlayerArmorValue", [](Player* player) -> int { return player->getArmorValue(); }); + RemoteCall::exportAs("GMLIB_API", "getEntityOwnerUniqueId", [](Actor* entity) -> int64 { return entity->getOwnerId().id; }); } \ No newline at end of file diff --git a/src/PlaceholderApi.cpp b/src/PlaceholderApi.cpp index 16341c4..a01a164 100644 --- a/src/PlaceholderApi.cpp +++ b/src/PlaceholderApi.cpp @@ -1,3 +1,4 @@ +#include "GMLIB/Server/PlaceholderAPI.h" #include "Global.h" #include @@ -25,6 +26,7 @@ bool registerPlayerPlaceholder( std::string const& PAPIName ) { if (RemoteCall::hasFunc(PluginName, FuncName)) { + PlaceholderAPI::unregisterPlaceholder(PAPIName); if (isParameters(PAPIName)) { auto Call = RemoteCall::importAs)>( PluginName, @@ -54,6 +56,7 @@ bool registerServerPlaceholder( std::string const& PAPIName ) { if (RemoteCall::hasFunc(PluginName, FuncName)) { + PlaceholderAPI::unregisterPlaceholder(PAPIName); if (isParameters(PAPIName)) { auto Call = RemoteCall::importAs)>(PluginName, FuncName); @@ -78,6 +81,7 @@ bool registerStaticPlaceholder( int num ) { if (RemoteCall::hasFunc(PluginName, FuncName)) { + PlaceholderAPI::unregisterPlaceholder(PAPIName); if (isParameters(PAPIName)) { auto Call = RemoteCall::importAs(PluginName, FuncName); if (num == -1) {