diff --git a/SDK-GMLIB b/SDK-GMLIB index 32687e7..0ad5f24 160000 --- a/SDK-GMLIB +++ b/SDK-GMLIB @@ -1 +1 @@ -Subproject commit 32687e7eda893078565e1b54c284eb3571a5cc59 +Subproject commit 0ad5f249edef7c909497e385699eb527b7acab97 diff --git a/lib/BEPlaceholderAPI-JS.js b/lib/BEPlaceholderAPI-JS.js new file mode 100644 index 0000000..b0ac46a --- /dev/null +++ b/lib/BEPlaceholderAPI-JS.js @@ -0,0 +1,54 @@ +const PAPIgetValueAPI = ll.import("BEPlaceholderAPI", "GetValue") +const PAPIgetValueByPlayerAPI = ll.import("BEPlaceholderAPI", "GetValueWithPlayer") +const PAPIregisterPlayerPlaceholderAPI = ll.import("BEPlaceholderAPI", "registerPlayerPlaceholder") +const PAPIregisterServerPlaceholderAPI = ll.import("BEPlaceholderAPI", "registerServerPlaceholder") +const PAPIregisterStaticPlaceholderAPI = ll.import("BEPlaceholderAPI", "registerStaticPlaceholder") +const PAPItranslateStringAPI = ll.import("BEPlaceholderAPI", "translateString") +const PAPIunRegisterPlaceholderAPI = ll.import("BEPlaceholderAPI", "unRegisterPlaceholder") + +Function.prototype.getName = function () { + + return this.name || this.toString().match(/function\s*([^(]*)\(/)[1] + +} + +class PAPI { + constructor() { } + + static registerPlayerPlaceholder(func, PluginName, PAPIName) { + + ll.export(func, PluginName, func.getName()) + PAPIregisterPlayerPlaceholderAPI(PluginName, func.getName(), PAPIName) + } + + static registerServerPlaceholder(func, PluginName, PAPIName) { + ll.export(func, PluginName, func.getName()) + PAPIregisterServerPlaceholderAPI(PluginName, func.getName(), PAPIName) + } + + static registerStaticPlaceholder(func, PluginName, PAPIName, UpdateInterval = 50) { + ll.export(func, PluginName, func.getName()) + PAPIregisterStaticPlaceholderAPI(PluginName, func.getName(), PAPIName, UpdateInterval) + } + + static getValue(key) { + return PAPIgetValueAPI(key) + } + + static getValueByPlayer(key, xuid) { + return PAPIgetValueByPlayerAPI(key, xuid) + } + + static translateString(str, xuid = "") { + return PAPItranslateStringAPI(str, xuid) + } + + static unRegisterPlaceholder(str) { + return PAPIunRegisterPlaceholderAPI(str) + } + +} + +module.exports = { + PAPI +}; \ No newline at end of file diff --git a/lib/GMLIB-API.js b/lib/GMLIB_API-JS.js similarity index 97% rename from lib/GMLIB-API.js rename to lib/GMLIB_API-JS.js index add472a..922217a 100644 --- a/lib/GMLIB-API.js +++ b/lib/GMLIB_API-JS.js @@ -41,10 +41,11 @@ const GMLIB_API = { const FloatingTextList = []; class StaticFloatingText { - constructor(pos, text) { + constructor(pos, text, papi = true) { this.mPosition = pos; this.mText = text; - this.mRuntimeId = GMLIB_API.createFloatingText(this.mPosition, this.mText); + this.mPlaceholderAPI = papi; + this.mRuntimeId = GMLIB_API.createFloatingText(this.mPosition, this.mText, this.mPlaceholderAPI); FloatingTextList.push(this); this.sendToClients(); mc.listen("onJoin", (pl) => { @@ -127,8 +128,8 @@ class StaticFloatingText { } class DynamicFloatingText extends StaticFloatingText { - constructor(pos, text, updateRate = 1) { - super(pos, text); + constructor(pos, text, updateRate = 1, papi = true) { + super(pos, text, papi); this.mUpdateRate = updateRate; this.mTaskId = null; this.startUpdate(); @@ -345,4 +346,4 @@ class Experiments { } } -export { StaticFloatingText, DynamicFloatingText, Minecraft, Recipes, Experiments }; \ No newline at end of file +module.exports = { StaticFloatingText, DynamicFloatingText, Minecraft, Recipes, Experiments }; \ No newline at end of file diff --git a/src/Global.h b/src/Global.h index c445e32..2dd567b 100644 --- a/src/Global.h +++ b/src/Global.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include #define PLUGIN_NAME "GMLIB-LRCA" @@ -8,4 +8,5 @@ extern ll::Logger logger; extern void Export_Legacy_GMLib_ModAPI(); extern void Export_Legacy_GMLib_ServerAPI(); -extern void Export_Compatibility_API(); \ No newline at end of file +extern void Export_Compatibility_API(); +extern void ExportPAPI(); \ No newline at end of file diff --git a/src/MemoryOperators.cpp b/src/MemoryOperators.cpp new file mode 100644 index 0000000..f7c918a --- /dev/null +++ b/src/MemoryOperators.cpp @@ -0,0 +1,7 @@ +// This file will make your plugin use LeviLamina's memory operators by default. +// This improves the memory management of your plugin and is recommended to use. +// You should not modify anything in this file. + +#define LL_MEMORY_OPERATORS + +#include diff --git a/src/PlaceholderApi.cpp b/src/PlaceholderApi.cpp new file mode 100644 index 0000000..ac7c4e5 --- /dev/null +++ b/src/PlaceholderApi.cpp @@ -0,0 +1,131 @@ +#include "Global.h" +#include + +namespace PAPIRemoteCall { + +std::string removeBrackets(std::string a1) { + a1.erase(a1.find_last_not_of("%") + 1); + return a1; +} + +bool isParameters(std::string str) { + std::regex reg("[<]([^<>]+)[>]"); + return std::regex_search(removeBrackets(str), reg); +} + +std::string GetValue(std::string const& from) { return GMLIB::Server::PlaceholderAPI::getValue(from); } + +std::string GetValueWithPlayer(std::string const& a1, std::string const& a2) { + return GMLIB::Server::PlaceholderAPI::getValue(a1, ll::service::bedrock::getLevel()->getPlayer(a2)); +} + +std::string +registerPlayerPlaceholder(std::string const& PluginName, std::string const& FuncName, std::string const& PAPIName) { + if (RemoteCall::hasFunc(PluginName, FuncName)) { + if (isParameters(PAPIName)) { + auto Call = + RemoteCall::importAs)>( + PluginName, + FuncName + ); + GMLIB::Server::PlaceholderAPI::registerPlayerPlaceholder( + PAPIName, + [Call](Player* sp, std::unordered_map map) { + return Call(sp->getXuid(), map); + }, + PluginName + ); + } else { + auto Call = RemoteCall::importAs(PluginName, FuncName); + GMLIB::Server::PlaceholderAPI::registerPlayerPlaceholder( + PAPIName, + [Call](Player* sp) { return Call(sp->getXuid()); }, + PluginName + ); + } + } else { + // logger.error("Function no find({}:{})", PluginName, FuncName); + return "Function no find"; + } + return "Register Success"; +} + +std::string +registerServerPlaceholder(std::string const& PluginName, std::string const& FuncName, std::string const& PAPIName) { + if (RemoteCall::hasFunc(PluginName, FuncName)) { + if (isParameters(PAPIName)) { + auto Call = + RemoteCall::importAs)>(PluginName, FuncName); + GMLIB::Server::PlaceholderAPI::registerServerPlaceholder( + PAPIName, + [Call](std::unordered_map map) { return Call(map); }, + PluginName + ); + } else { + auto Call = RemoteCall::importAs(PluginName, FuncName); + GMLIB::Server::PlaceholderAPI::registerServerPlaceholder( + PAPIName, + [Call]() { return Call(); }, + PluginName + ); + } + } else { + // logger.error("Function no find({}:{})", PluginName, FuncName); + return "Function no find"; + } + return "Register Success"; +} + +std::string registerStaticPlaceholder( + std::string const& PluginName, + std::string const& FuncName, + std::string const& PAPIName, + int num +) { + if (RemoteCall::hasFunc(PluginName, FuncName)) { + if (isParameters(PAPIName)) { + auto Call = RemoteCall::importAs(PluginName, FuncName); + if (num == -1) { + GMLIB::Server::PlaceholderAPI::registerStaticPlaceholder( + PAPIName, + [Call] { return Call(); }, + PluginName + ); + } else { + GMLIB::Server::PlaceholderAPI::registerStaticPlaceholder( + PAPIName, + num, + [Call] { return Call(); }, + PluginName + ); + } + } + } else { + // logger.error("Function no find({}:{})", PluginName, FuncName); + return "Function no find"; + } + return "Register Success"; +} + +std::string translateString(std::string const& str, std::string const& xuid) { + std::string _str = str; + GMLIB::Server::PlaceholderAPI::translateString(_str, ll::service::bedrock::getLevel()->getPlayerByXuid(xuid)); + return _str; +} + +bool unRegisterPlaceholder(std::string const& str) { return GMLIB::Server::PlaceholderAPI::unRegisterPlaceholder(str); } + +} // namespace PAPIRemoteCall + +#define EXPORTAPI(T) \ + RemoteCall::exportAs("BEPlaceholderAPI", ll::utils::string_utils::replaceAll(#T, "PAPIRemoteCall::", ""), T); + +void ExportPAPI() { + EXPORTAPI(PAPIRemoteCall::registerPlayerPlaceholder); + EXPORTAPI(PAPIRemoteCall::registerServerPlaceholder); + EXPORTAPI(PAPIRemoteCall::registerStaticPlaceholder); + EXPORTAPI(PAPIRemoteCall::GetValue); + EXPORTAPI(PAPIRemoteCall::GetValueWithPlayer); + EXPORTAPI(PAPIRemoteCall::translateString); + EXPORTAPI(PAPIRemoteCall::unRegisterPlaceholder); +} \ No newline at end of file diff --git a/src/Plugin.cpp b/src/Plugin.cpp index a9b0f8e..a8dd1ad 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -10,6 +10,7 @@ Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) { Export_Legacy_GMLib_ModAPI(); Export_Legacy_GMLib_ServerAPI(); Export_Compatibility_API(); + ExportPAPI(); logger.info("GMLIB-LegacyRemoteCallApi Loaded!"); logger.info("Author: GroupMountain"); logger.info("Repository: https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi");