Skip to content

Commit

Permalink
adapt: adapt GMLIB v0.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsubasa6848 committed Mar 23, 2024
1 parent 161123a commit a82a8cf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
46 changes: 45 additions & 1 deletion lib/GMLIB_API-JS.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,56 @@ 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,
Minecraft,
Recipes,
Experiments,
GMLIB,
Scoreboard
Scoreboard,
JsonConfig
};
17 changes: 11 additions & 6 deletions src/CompatibilityApi.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "Global.h"
#include <regex>

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));
Expand Down Expand Up @@ -36,7 +36,12 @@ void Export_Compatibility_API() {
return level->getServerAverageTps();
});
RemoteCall::exportAs("GMLIB_API", "getAllPlayerUuids", []() -> std::vector<std::string> {
return GMLIB_Player::getAllUuids();
std::vector<std::string> result;
std::vector<mce::UUID> 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);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit a82a8cf

Please sign in to comment.