From b0cff071dc61fe78f5d65353c0e8d8fa6d37baaa Mon Sep 17 00:00:00 2001 From: zimuya4153 Date: Mon, 13 Oct 2025 03:46:40 +0800 Subject: [PATCH] feat: adapt levilamina 1.5.x --- lib/EventAPI-JS.js | 93 +++++++++++++++++++++------------------- lib/PlaceholderAPI.js | 90 ++++++++++++++++++++------------------ manifest.json | 3 ++ src/CompatibilityApi.cpp | 26 +++++------ src/EventAPI.cpp | 5 ++- src/Global.h | 16 +++++-- tooth.json | 3 +- xmake.lua | 12 +++--- 8 files changed, 134 insertions(+), 114 deletions(-) diff --git a/lib/EventAPI-JS.js b/lib/EventAPI-JS.js index 06cf8b9..68fdb62 100644 --- a/lib/EventAPI-JS.js +++ b/lib/EventAPI-JS.js @@ -1,51 +1,54 @@ -/** - * @returns {string} - */ const getPluginName = () => { - // quickjs - try { - throw new Error("getPluginName"); - } catch (error) { - const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); - if (line.includes("")) { - return line.slice( - line.indexOf("(") + 1, - line.indexOf("\\") - ); - } - if (line.includes("")) { - return line.slice( - line.indexOf("/", line.indexOf("/") + 1) + 1, - line.indexOf("\\") - ); - } - } + if (getPluginName.PluginNameCache) return getPluginName.PluginNameCache; - // nodejs - try { - const path = require('path'); - const selfFileName = path.basename(__filename); - const pluginDirectory = Object.entries( - require('module')._pathCache - ).find( - ([key, _]) => - key.includes(selfFileName) - )[0].split("\u0000")[1]; - const directories = pluginDirectory.split("\\"); - const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); - if (pluginName) return pluginName; - } catch { } - try { - throw new Error("getPluginName"); - } catch (error) { - const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); - if (line.includes(".js") && /:\d+:\d+$/.test(line)) { - const directories = line.split("\\"); - const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); - if (pluginName) return pluginName; + return getPluginName.PluginNameCache = (() => { + if (ll.getCurrentPluginInfo) return ll.getCurrentPluginInfo().name; + + const error = new Error("getPluginName"); + switch (ll.scriptEngineVersion) { + case "QuickJS": { + if (!error.stack) break; + const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); + if (line.includes("")) { + return line.slice( + line.indexOf("(") + 1, + line.indexOf("\\") + ); + } + if (line.includes("")) { + return line.slice( + line.indexOf("/", line.indexOf("/") + 1) + 1, + line.indexOf("\\") + ); + } + break; + } + case "V8 12.4.254.21-node.21": { + try { + const selfFileName = require('path').basename(__filename); + const pluginDirectory = Object.entries( + require('module')._pathCache + ).find( + ([key, _]) => key.includes(selfFileName) + )[0].split("\u0000")[1]; + const directories = pluginDirectory.split("\\"); + const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); + if (pluginName) return pluginName; + } catch { } + + if (!error.stack) break; + const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); + if (line.includes(".js") && /:\d+:\d+$/.test(line)) { + const directories = line.split("\\"); + const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); + if (pluginName) return pluginName; + } + break; + } + default: break; } - } - return "Unknown"; + return "Unknown"; + })(); }; module.exports = { diff --git a/lib/PlaceholderAPI.js b/lib/PlaceholderAPI.js index d0b16ab..00bae9b 100644 --- a/lib/PlaceholderAPI.js +++ b/lib/PlaceholderAPI.js @@ -1,50 +1,56 @@ /// const getPluginName = () => { - // quickjs - try { - throw new Error("getPluginName"); - } catch (error) { - const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); - if (line.includes("")) { - return line.slice( - line.indexOf("(") + 1, - line.indexOf("\\") - ); - } - if (line.includes("")) { - return line.slice( - line.indexOf("/", line.indexOf("/") + 1) + 1, - line.indexOf("\\") - ); - } - } + if (getPluginName.PluginNameCache) return getPluginName.PluginNameCache; - // nodejs - try { - const path = require('path'); - const selfFileName = path.basename(__filename); - const pluginDirectory = Object.entries( - require('module')._pathCache - ).find( - ([key, _]) => - key.includes(selfFileName) - )[0].split("\u0000")[1]; - const directories = pluginDirectory.split("\\"); - const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); - if (pluginName) return pluginName; - } catch { } - try { - throw new Error("getPluginName"); - } catch (error) { - const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); - if (line.includes(".js") && /:\d+:\d+$/.test(line)) { - const directories = line.split("\\"); - const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); - if (pluginName) return pluginName; + return getPluginName.PluginNameCache = (() => { + if (ll.getCurrentPluginInfo) return ll.getCurrentPluginInfo().name; + + const error = new Error("getPluginName"); + switch (ll.scriptEngineVersion) { + case "QuickJS": { + if (!error.stack) break; + const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); + if (line.includes("")) { + return line.slice( + line.indexOf("(") + 1, + line.indexOf("\\") + ); + } + if (line.includes("")) { + return line.slice( + line.indexOf("/", line.indexOf("/") + 1) + 1, + line.indexOf("\\") + ); + } + break; + } + case "V8 12.4.254.21-node.21": { + try { + const selfFileName = require('path').basename(__filename); + const pluginDirectory = Object.entries( + require('module')._pathCache + ).find( + ([key, _]) => key.includes(selfFileName) + )[0].split("\u0000")[1]; + const directories = pluginDirectory.split("\\"); + const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); + if (pluginName) return pluginName; + } catch { } + + if (!error.stack) break; + const /** @type {string} */ line = error.stack.trim().split("\n").pop().trim(); + if (line.includes(".js") && /:\d+:\d+$/.test(line)) { + const directories = line.split("\\"); + const pluginName = directories[directories.findIndex(value => value === "plugins") + 1].trim(); + if (pluginName) return pluginName; + } + break; + } + default: break; } - } - return "Unknown"; + return "Unknown"; + })(); }; module.exports = { diff --git a/manifest.json b/manifest.json index f7d4713..9b78e1e 100644 --- a/manifest.json +++ b/manifest.json @@ -14,6 +14,9 @@ }, { "name": "iListenAttentively" + }, + { + "name": "ModAPI" } ] } \ No newline at end of file diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index a0f42d3..1f5aff6 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -11,7 +11,7 @@ void Export_Compatibility_API() { return CustomRecipeRegistry::getInstance().unregisterRecipe(id, true); }); RemoteCall::exportAs("GMLIB_API", "setCustomPackPath", [](std::string const& path) -> void { - AddonsLoader::addCustomPackPath(path); + AddonsLoader::getInstance().addCustomPackPath(path); }); RemoteCall::exportAs("GMLIB_API", "getServerMspt", []() -> double { return GMLevel::getInstance().transform( @@ -19,14 +19,10 @@ void Export_Compatibility_API() { ).value_or(0.0); }); RemoteCall::exportAs("GMLIB_API", "getServerCurrentTps", []() -> float { - return GMLevel::getInstance().transform( - [](GMLevel& level) -> float { return level.getServerCurrentTps(); } - ).value_or(0.0); + return TpsStatus::getInstance().getLevelCurrentTps(); }); RemoteCall::exportAs("GMLIB_API", "getServerAverageTps", []() -> double { - return GMLevel::getInstance().transform( - [](GMLevel& level) -> double { return level.getServerAverageTps(); } - ).value_or(0.0); + return TpsStatus::getInstance().getLevelAverageTps(); }); RemoteCall::exportAs("GMLIB_API", "getAllPlayerUuids", []() -> std::vector { std::vector result; @@ -534,7 +530,7 @@ void Export_Compatibility_API() { .value_or(0); }); RemoteCall::exportAs("GMLIB_API", "getBlockTranslateKey", [](Block const* block) -> std::string { - return block->getLegacyBlock().mDescriptionId.get() + ".name"; + return block->mBlockType->mDescriptionId.get() + ".name"; }); RemoteCall::exportAs("GMLIB_API", "getItemTranslateKey", [](ItemStack* item) -> std::string { return item->getDescriptionId(); @@ -573,7 +569,7 @@ void Export_Compatibility_API() { } ); RemoteCall::exportAs("GMLIB_API", "itemCanDestroyBlock", [](ItemStack const* item, Block const* block) -> bool { - return std::ranges::any_of(item->mCanDestroy, [block](BlockLegacy const* blockLegacy) { + return std::ranges::any_of(item->mCanDestroy, [block](BlockType const* blockLegacy) { return blockLegacy && block->getTypeName() == blockLegacy->getTypeName(); }); }); @@ -590,13 +586,13 @@ void Export_Compatibility_API() { return false; }); RemoteCall::exportAs("GMLIB_API", "blockCanDropWithAnyTool", [](Block const* block) -> bool { - return !block->getLegacyBlock().mRequiresCorrectToolForDrops; + return !block->mBlockType->mRequiresCorrectToolForDrops; }); RemoteCall::exportAs( "GMLIB_API", "blockPlayerWillDestroy", [](Block const* block, Player* player, std::pair pos) -> bool { - return block->getLegacyBlock().playerWillDestroy(*player, pos.first, *block); + return block->mBlockType->playerWillDestroy(*player, pos.first, *block); } ); RemoteCall::exportAs("GMLIB_API", "playerAttack", [](Player* player, Actor* entity) -> bool { @@ -745,9 +741,9 @@ void Export_Compatibility_API() { "GMLIB_API", "setItemCanDestroy", [](ItemStack const* item, std::vector blocks) -> void { - std::vector data; + std::vector data; for (auto& name : blocks) { - if (auto block = BlockLegacy::tryGetFromRegistry(name)) { + if (auto block = BlockType::tryGetFromRegistry(name)) { data.push_back(block.as_ptr()); } } @@ -765,9 +761,9 @@ void Export_Compatibility_API() { "GMLIB_API", "setItemCanPlaceOn", [](ItemStack const* item, std::vector blocks) -> void { - std::vector data; + std::vector data; for (auto& name : blocks) { - if (auto block = BlockLegacy::tryGetFromRegistry(name)) { + if (auto block = BlockType::tryGetFromRegistry(name)) { data.push_back(block.as_ptr()); } } diff --git a/src/EventAPI.cpp b/src/EventAPI.cpp index 28b5ec3..ba3dd14 100644 --- a/src/EventAPI.cpp +++ b/src/EventAPI.cpp @@ -216,10 +216,11 @@ void Export_Event_API() { ); } case doHash("gmlib::EndermanTakeBlockBeforeEvent"): { - REGISTER_EVENT_LISTEN(ila::mc::EndermanTakeBlockBeforeEvent, + REGISTER_EVENT_LISTEN(ila::mc::MobTakeBlockBeforeEvent, (Actor * mob, bool isCancelled), (&event.self(), event.isCancelled()), - event.setCancelled(result);); + event.setCancelled(result); + , if (!event.self().isType(ActorType::EnderMan)) return;); } case doHash("gmlib::DragonRespawnBeforeEvent"): { REGISTER_EVENT_LISTEN(ila::mc::DragonRespawnBeforeEvent, diff --git a/src/Global.h b/src/Global.h index d951d01..850acd1 100644 --- a/src/Global.h +++ b/src/Global.h @@ -2,12 +2,20 @@ // clang-format off #pragma warning(push) #pragma warning(disable : 4996) -#define GMLIB_GLOBAL_USING -#include -#include +#pragma include_alias("mc/world/item/HumanoidArmorItem.h", "modapi/item/types/mc/HumanoidArmorItem.h") +#include +#include +#include #include +#include +#include +#include #include -using namespace gmlib::mod; +using namespace gmlib; +using namespace gmlib::ui; +using namespace gmlib::i18n; +using namespace gmlib::memory; +using namespace modapi; #pragma warning(pop) // clang-format on diff --git a/tooth.json b/tooth.json index 13458a9..2116f51 100644 --- a/tooth.json +++ b/tooth.json @@ -2,7 +2,7 @@ "format_version": 3, "format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d", "tooth": "github.com/GroupMountain/GMLIB-LegacyRemoteCallApi", - "version": "1.4.1", + "version": "1.5.0", "info": { "name": "GMLIB-LegacyRemoteCallApi", "description": "Legacy RemoteCall API for GMLIB", @@ -21,6 +21,7 @@ "dependencies": { "github.com/LiteLDev/LeviLamina": ">=1.3.0", "github.com/GroupMountain/GMLIB-Release": ">=1.3.0-rc.1", + "github.com/GroupMountain/ModAPI-Release": ">=0.1.0", "github.com/MiracleForest/iListenAttentively-Release": ">=0.6.0" }, "assets": [ diff --git a/xmake.lua b/xmake.lua index 9222203..e8632eb 100644 --- a/xmake.lua +++ b/xmake.lua @@ -8,11 +8,12 @@ if not has_config("vs_runtime") then set_runtimes("MD") end -add_requires("levilamina 1.4.1", {configs = {target_type = "server"}}) +add_requires("levilamina 1.5.2", {configs = {target_type = "server"}}) add_requires("legacyremotecall") -add_requires("levibuildscript") -add_requires("ilistenattentively 0.7.0") -add_requires("gmlib 1.4.3") +add_requires("levibuildscript 0.5.2") +add_requires("ilistenattentively 0.8.0") +add_requires("gmlib 1.5.1") +add_requires("modapi 0.1.1") target("GMLIB-LegacyRemoteCallApi") add_cxflags( @@ -35,7 +36,8 @@ target("GMLIB-LegacyRemoteCallApi") "levilamina", "legacyremotecall", "ilistenattentively", - "gmlib" + "gmlib", + "modapi" ) add_rules("@levibuildscript/linkrule") add_rules("@levibuildscript/modpacker")