From a82a8cfb94aad27d6cb04d4c6679151aa27bd08d Mon Sep 17 00:00:00 2001 From: Tsubasa6848 Date: Sat, 23 Mar 2024 19:11:00 +0800 Subject: [PATCH] adapt: adapt GMLIB v0.9.7 --- SDK-GMLIB | 2 +- lib/GMLIB_API-JS.js | 46 +++++++++++++++++++++++++++++++++++++++- src/CompatibilityApi.cpp | 17 +++++++++------ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/SDK-GMLIB b/SDK-GMLIB index b1e7ebd..9935ed4 160000 --- a/SDK-GMLIB +++ b/SDK-GMLIB @@ -1 +1 @@ -Subproject commit b1e7ebd770e667604016f93dc43359dc8212c043 +Subproject commit 9935ed40635eacf491ad40fd7a93629ee34847bc diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index e545489..f475aae 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -620,6 +620,49 @@ class Scoreboard { } +class JsonConfig { + constructor(path, defultValue = {}) { + this.mData = defultValue; + this.mPath = path; + } + + init() { + if (File.exists(this.mPath)) { + let existDataStr = File.readFrom(this.mPath); + this.mData = Object.assign({}, this.mData, JSON.parse(existDataStr)); + } + this.save(); + } + + save(format = 4) { + let dataStr = JSON.stringify(this.mData, null, format); + File.writeTo(this.mPath, dataStr); + } + + getData() { + return this.mData; + } + + get(key, defultValue = null) { + let result = this.getData()[key]; + if (!result && defultValue != null) { + this.set(key, defultValue); + return defultValue; + } + return result; + } + + set(key, value) { + this.getData()[key] = value; + this.save(); + } + + delete(key) { + delete this.getData()[key]; + this.save(); + } +} + module.exports = { StaticFloatingText, DynamicFloatingText, @@ -627,5 +670,6 @@ module.exports = { Recipes, Experiments, GMLIB, - Scoreboard + Scoreboard, + JsonConfig }; \ No newline at end of file diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index 1756790..a2d142a 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -1,13 +1,13 @@ #include "Global.h" #include -bool isNumber(const std::string& str) { - std::regex pattern("[-+]?[0-9]*\\.?[0-9]+"); +bool isInteger(const std::string& str) { + std::regex pattern("^[+-]?\\d+$"); return std::regex_match(str, pattern); } ActorUniqueID parseScriptUniqueID(std::string uniqueId) { - if (!isNumber(uniqueId)) { + if (!isInteger(uniqueId)) { return ActorUniqueID::INVALID_ID; } return ActorUniqueID(std::stoll(uniqueId)); @@ -36,7 +36,12 @@ void Export_Compatibility_API() { return level->getServerAverageTps(); }); RemoteCall::exportAs("GMLIB_API", "getAllPlayerUuids", []() -> std::vector { - return GMLIB_Player::getAllUuids(); + std::vector result; + std::vector uuids = GMLIB_Player::getAllUuids(); + for (auto& uuid : uuids) { + result.push_back(uuid.asString()); + } + return result; }); RemoteCall::exportAs("GMLIB_API", "getPlayerNbt", [](std::string uuid) -> std::string { auto uid = mce::UUID::fromString(uuid); @@ -262,7 +267,7 @@ void Export_Compatibility_API() { }); RemoteCall::exportAs("GMLIB_API", "resetEntityScores", [](std::string uniqueId) -> bool { auto auid = parseScriptUniqueID(uniqueId); - return GMLIB_Scoreboard::getInstance()->resetAllScores(auid); + return GMLIB_Scoreboard::getInstance()->resetScore(auid); }); RemoteCall::exportAs("GMLIB_API", "fakePlayerHasScore", [](std::string name, std::string obj) -> bool { if (auto result = GMLIB_Scoreboard::getInstance()->getScore(obj, name)) { @@ -303,7 +308,7 @@ void Export_Compatibility_API() { return GMLIB_Scoreboard::getInstance()->resetScore(obj, name); }); RemoteCall::exportAs("GMLIB_API", "resetFakePlayerScores", [](std::string name) -> bool { - return GMLIB_Scoreboard::getInstance()->resetAllScores(name); + return GMLIB_Scoreboard::getInstance()->resetScore(name); }); RemoteCall::exportAs("GMLIB_API", "addObjective", [](std::string obj) -> bool { if (auto res = GMLIB_Scoreboard::getInstance()->getInstance()->addObjective(obj)) {