From 42bd769d5cef59c80f8248fded73f2012589d3cc Mon Sep 17 00:00:00 2001 From: Tsubasa6848 Date: Sun, 24 Mar 2024 05:16:18 +0800 Subject: [PATCH 1/4] feat: l18n --- lib/GMLIB_API-JS.js | 62 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 90e4da1..869cdb4 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -649,6 +649,7 @@ class JsonConfig { constructor(path, defultValue = {}) { this.mData = defultValue; this.mPath = path; + this.init(); } init() { @@ -688,6 +689,63 @@ class JsonConfig { } } +class JsonLanguage extends JsonConfig { + constructor(path, defultValue = {}) { + super(path, defultValue); + } + + translate(key, data = []) { + let result = this.get(key); + if (result == null) { + return key; + } + data.forEach((val, index) => { + let old = `{${index + 1}}`; + result = result.split(old).join(val); + }); + return result; + } +} + +class JsonI18n { + constructor(path, localLangCode = "en_US") { + this.mPath = path; + this.mLangCode = localLangCode; + this.mAllLanguages = {}; + this.mDefaultLangCode = "en_US" + } + + loadLanguage(langCode, defaultData = {}) { + let langPath = this.mPath; + if (!langPath.endsWith("/") && !langPath.endsWith("\\")) { + langPath = langPath + "/"; + } + langPath = langPath + langCode + ".json"; + let language = new JsonLanguage(langPath, defaultData); + this.mAllLanguages[langCode] = language; + } + + chooseLanguage(langCode) { + this.mLangCode = langCode; + } + + setDefaultLanguage(langCode) { + this.mDefaultLangCode = langCode; + } + + translate(key, data = [], langCode = this.mLangCode) { + let language = this.mAllLanguages[langCode]; + let result = language.translate(key, data); + if (result == key) { + let language = this.mAllLanguages[this.mDefaultLangCode]; + if (language) { + result = language.translate(key, data); + } + } + return result; + } +}; + module.exports = { StaticFloatingText, DynamicFloatingText, @@ -696,5 +754,7 @@ module.exports = { Experiments, GMLIB, Scoreboard, - JsonConfig + JsonConfig, + JsonLanguage, + JsonI18n }; \ No newline at end of file From 3488c812e3062aa80d1ee2dec3e497b6835bd223 Mon Sep 17 00:00:00 2001 From: Tsubasa6848 Date: Sun, 24 Mar 2024 17:47:07 +0800 Subject: [PATCH 2/4] fix: fix custom l18n --- lib/GMLIB_API-JS.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 869cdb4..14efef4 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -709,17 +709,28 @@ class JsonLanguage extends JsonConfig { class JsonI18n { constructor(path, localLangCode = "en_US") { + if (!path.endsWith("/") && !path.endsWith("\\")) { + path = path + "/"; + } this.mPath = path; this.mLangCode = localLangCode; this.mAllLanguages = {}; - this.mDefaultLangCode = "en_US" + this.mDefaultLangCode = "en_US"; + this.loadAllLanguages(); + } + + loadAllLanguages() { + let exist_list = File.getFilesList(this.mPath); + exist_list.forEach((name) => { + let code = name.replace(".json", ""); + let path = this.mPath + name; + let language = new JsonLanguage(path); + this.mAllLanguages[code] = language; + }); } loadLanguage(langCode, defaultData = {}) { let langPath = this.mPath; - if (!langPath.endsWith("/") && !langPath.endsWith("\\")) { - langPath = langPath + "/"; - } langPath = langPath + langCode + ".json"; let language = new JsonLanguage(langPath, defaultData); this.mAllLanguages[langCode] = language; From 29ad4e1f3e258b9bc8ffbad760189a5497aeae24 Mon Sep 17 00:00:00 2001 From: Tsubasa6848 Date: Mon, 25 Mar 2024 18:15:39 +0800 Subject: [PATCH 3/4] adapt: adapt GMLIB v0.9.8 --- SDK-GMLIB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDK-GMLIB b/SDK-GMLIB index 9935ed4..cda5bfb 160000 --- a/SDK-GMLIB +++ b/SDK-GMLIB @@ -1 +1 @@ -Subproject commit 9935ed40635eacf491ad40fd7a93629ee34847bc +Subproject commit cda5bfbf85a291a85a78529f760883d1ca7216da From e50c3aedcee15594aadbe85afc9ce8d1f98285f0 Mon Sep 17 00:00:00 2001 From: Tsubasa6848 Date: Mon, 25 Mar 2024 23:07:28 +0800 Subject: [PATCH 4/4] chore: update version --- lib/GMLIB_API-JS.js | 19 +++++++++++++++++-- manifest.json | 3 ++- src/CompatibilityApi.cpp | 13 +++++++++++++ src/EventAPI.cpp | 34 ++++++++++++++++++++++++++++------ tooth.json | 6 +++--- 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 14efef4..0201a96 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -16,6 +16,7 @@ const GMLIB_API = { setPlayerNbt: ll.import("GMLIB_API", "setPlayerNbt"), setPlayerNbtTags: ll.import("GMLIB_API", "setPlayerNbtTags"), resourcePackTranslate: ll.import("GMLIB_API", "resourcePackTranslate"), + getResourcePackI18nLanguage: ll.import("GMLIB_API", "getResourcePackI18nLanguage"), chooseResourcePackI18nLanguage: ll.import("GMLIB_API", "chooseResourcePackI18nLanguage"), setEducationFeatureEnabled: ll.import("GMLib_ServerAPI", "setEducationFeatureEnabled"), registerAbilityCommand: ll.import("GMLib_ServerAPI", "registerAbilityCommand"), @@ -34,6 +35,7 @@ const GMLIB_API = { getExperimentTranslatedName: ll.import("GMLIB_API", "getExperimentTranslatedName"), getExperimentEnabled: ll.import("GMLib_ModAPI", "getExperimentEnabled"), setExperimentEnabled: ll.import("GMLib_ModAPI", "setExperimentEnabled"), + unregisterRecipe: ll.import("GMLIB_API", "unregisterRecipe"), registerExperimentsRequire: ll.import("GMLib_ModAPI", "registerExperimentsRequire"), registerStoneCutterRecipe: ll.import("GMLib_ModAPI", "registerStoneCutterRecipe"), registerSmithingTrimRecipe: ll.import("GMLib_ModAPI", "registerSmithingTrimRecipe"), @@ -87,7 +89,8 @@ const GMLIB_API = { setWorldSpawn: ll.import("GMLIB_API", "setWorldSpawn"), getPlayerSpawnPoint: ll.import("GMLIB_API", "getPlayerSpawnPoint"), setPlayerSpawnPoint: ll.import("GMLIB_API", "setPlayerSpawnPoint"), - clearPlayerSpawnPoint: ll.import("GMLIB_API", "clearPlayerSpawnPoint") + clearPlayerSpawnPoint: ll.import("GMLIB_API", "clearPlayerSpawnPoint"), + setCustomPackPath: ll.import("GMLIB_API", "setCustomPackPath") } const FloatingTextList = []; @@ -380,10 +383,18 @@ class Minecraft { return GMLIB_API.throwEntity(entity, proj, speed = 2, offset = 3); } - static chooseResourcePackI18nLanguage(language) { + static getServerLanguage() { + return GMLIB_API.getResourcePackI18nLanguage(language); + } + + static setServerLanguage(language) { return GMLIB_API.chooseResourcePackI18nLanguage(language); } + static setCustomPackPath(path) { + GMLIB_API.setCustomPackPath(path); + } + static resourcePackTranslate(key, params = []) { return GMLIB_API.resourcePackTranslate(key, params); } @@ -394,6 +405,10 @@ class Recipes { throw new Error("Static class cannot be instantiated"); } + static unregisterRecipe(recipeId) { + return GMLIB_API.unregisterRecipe(recipeId); + } + static registerStoneCutterRecipe(recipeId, inputName, inputAux, outputName, outputAux, outputCount) { return GMLIB_API.registerStoneCutterRecipe(recipeId, inputName, inputAux, outputName, outputAux, outputCount); } diff --git a/manifest.json b/manifest.json index 2dea787..1f154fe 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,8 @@ { "name": "${pluginName}", "entry": "${pluginFile}", - "version": "0.9.4", + "version": "0.9.5", + "author": "GroupMountain", "type": "native", "dependencies": [ { diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index 26e21dd..62413be 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -14,6 +14,16 @@ ActorUniqueID parseScriptUniqueID(std::string uniqueId) { } void Export_Compatibility_API() { + RemoteCall::exportAs("GMLIB_API", "unregisterRecipe", [](std::string id) -> bool { + auto level = GMLIB_Level::getInstance(); + if (!level) { + return false; + } + return GMLIB::Mod::CustomRecipe::unregisterRecipe(id); + }); + RemoteCall::exportAs("GMLIB_API", "setCustomPackPath", [](std::string path) -> void { + GMLIB::Mod::CustomPacks::addCustomPackPath(path); + }); RemoteCall::exportAs("GMLIB_API", "getServerMspt", []() -> float { auto level = GMLIB_Level::getInstance(); if (!level) { @@ -163,6 +173,9 @@ void Export_Compatibility_API() { RemoteCall::exportAs("GMLIB_API", "chooseResourcePackI18nLanguage", [](std::string code) -> void { I18n::chooseLanguage(code); }); + RemoteCall::exportAs("GMLIB_API", "getResourcePackI18nLanguage", []() -> std::string { + return I18n::getCurrentLanguage()->getFullLanguageCode(); + }); RemoteCall::exportAs("GMLIB_API", "getPlayerPosition", [](std::string uuid) -> std::pair { auto uid = mce::UUID::fromString(uuid); auto pos = GMLIB_Player::getPlayerPosition(uid); diff --git a/src/EventAPI.cpp b/src/EventAPI.cpp index 0011644..388def3 100644 --- a/src/EventAPI.cpp +++ b/src/EventAPI.cpp @@ -105,14 +105,13 @@ void Export_Event_API() { ); return true; } - case doHash("onTextSend"): { - auto Call = RemoteCall::importAs(eventName, eventId); - eventBus->emplaceListener( - [Call](GMLIB::Event::PacketEvent::TextPacketSendBeforeEvent& ev) { - auto pkt = ev.getPacket(); + case doHash("onEntityChangeDim"): { + auto Call = RemoteCall::importAs(eventName, eventId); + eventBus->emplaceListener( + [Call](GMLIB::Event::EntityEvent::ActorChangeDimensionBeforeEvent& ev) { bool result = true; try { - result = Call(pkt.mAuthor, pkt.mMessage); + result = Call(&ev.self(), ev.getToDimensionId()); } catch (...) {} if (!result) { ev.cancel(); @@ -152,6 +151,29 @@ void Export_Event_API() { ); return true; } + case doHash("onMobHurted"): { + auto Call = RemoteCall::importAs( + eventName, + eventId + ); + eventBus->emplaceListener( + [Call](GMLIB::Event::EntityEvent::MobHurtAfterEvent& ev) { + auto& damageSource = ev.getSource(); + Actor* source = nullptr; + if (damageSource.isEntitySource()) { + auto uniqueId = damageSource.getDamagingEntityUniqueID(); + source = ll::service::getLevel()->fetchEntity(uniqueId); + if (source->getOwner()) { + source = source->getOwner(); + } + } + try { + Call(&ev.self(), source, ev.getDamage(), (int)damageSource.getCause()); + } catch (...) {} + } + ); + return true; + } default: return false; } diff --git a/tooth.json b/tooth.json index 735ca71..4c1c55c 100644 --- a/tooth.json +++ b/tooth.json @@ -1,7 +1,7 @@ { "format_version": 2, "tooth": "github.com/GroupMountain/GMLIB-LegacyRemoteCallApi", - "version": "0.9.4", + "version": "0.9.5", "info": { "name": "GMLIB-LegacyRemoteCallApi", "description": "Legacy RemoteCall API for GMLIB", @@ -14,9 +14,9 @@ "library" ] }, - "asset_url": "https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/releases/download/v0.9.4/GMLIB-LegacyRemoteCallApi-windows-x64.zip", + "asset_url": "https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/releases/download/v0.9.5/GMLIB-LegacyRemoteCallApi-windows-x64.zip", "dependencies": { - "github.com/GroupMountain/GMLIB": ">=0.9.6" + "github.com/GroupMountain/GMLIB": ">=0.9.8" }, "files": { "place": [