feat: add PlaceholderAPI

This commit is contained in:
Tsubasa6848
2024-02-22 22:29:42 +08:00
Unverified
parent b2737599c8
commit 0001f80ff2
7 changed files with 203 additions and 8 deletions
+54
View File
@@ -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
};
+6 -5
View File
@@ -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 };
module.exports = { StaticFloatingText, DynamicFloatingText, Minecraft, Recipes, Experiments };
+2 -1
View File
@@ -1,5 +1,5 @@
#pragma once
#include <include_all.h>
#include <GMLIB/include_lib.h>
#include <RemoteCallAPI.h>
#define PLUGIN_NAME "GMLIB-LRCA"
@@ -9,3 +9,4 @@ extern ll::Logger logger;
extern void Export_Legacy_GMLib_ModAPI();
extern void Export_Legacy_GMLib_ServerAPI();
extern void Export_Compatibility_API();
extern void ExportPAPI();
+7
View File
@@ -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 <ll/api/memory/MemoryOperators.h>
+131
View File
@@ -0,0 +1,131 @@
#include "Global.h"
#include <regex>
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<std::string(std::string const& arg, std::unordered_map<std::string, std::string>)>(
PluginName,
FuncName
);
GMLIB::Server::PlaceholderAPI::registerPlayerPlaceholder(
PAPIName,
[Call](Player* sp, std::unordered_map<std::string, std::string> map) {
return Call(sp->getXuid(), map);
},
PluginName
);
} else {
auto Call = RemoteCall::importAs<std::string(std::string const& arg)>(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<std::string(std::unordered_map<std::string, std::string>)>(PluginName, FuncName);
GMLIB::Server::PlaceholderAPI::registerServerPlaceholder(
PAPIName,
[Call](std::unordered_map<std::string, std::string> map) { return Call(map); },
PluginName
);
} else {
auto Call = RemoteCall::importAs<std::string()>(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<std::string()>(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);
}
+1
View File
@@ -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");