From fddfdf4eebf0a055ef3337fa7674da34ce8b6c2e Mon Sep 17 00:00:00 2001 From: wed150 Date: Mon, 22 Jun 2026 13:10:53 +0800 Subject: [PATCH] fix: fix translate --- README.md | 15 ++++++++++++--- manifest.json | 2 +- src/DeathMessage.cpp | 15 ++++++++++++++- src/Entry.cpp | 19 ++++++++++++++++--- src/Global.h | 2 +- src/Hooks.cpp | 10 +++++----- src/Log.cpp | 14 +++----------- src/MemoryOperators.cpp | 1 + tooth.json | 2 +- 9 files changed, 54 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 914e929..72e9d86 100644 --- a/README.md +++ b/README.md @@ -19,17 +19,26 @@ That allows players to freely switch languages on their own. ## Death Messages Translation -By default, this plugin uses resource packs for translation to ensure perfect compatibility with various addons.
`This requires ModAPI to be enabled.` +### Resource Pack Translation +By default, this plugin uses resource packs for translation to ensure perfect compatibility with various addons.

(The plugin automatically builds the resource pack and sends it to players upon joining. Using resource pack-based translation is recommended.) +>[!IMPORTANT] +>If you wnt to use Resource Pack Translation
+>This requires ModAPI to be enabled.
+>You can configure language for console in `server.properties` like `language=en_US` +### Server-Side Translation If you prefer not to use resource packs, you can enable server-side translation in the configuration file.
This way, all translations are handled entirely on the server without requiring any client-side resource packs. +>[!IMPORTANT] +>You can configure language for Server-Side Translation in `./plugins/DeathMessages/config/config.json` like `Language=en_US` #### For issues, please report them via GitHub Issues!!! #### Pull Requests are also welcome! You can also customize death messages by editing the language files.
-(The resource pack will be generated in the ./plugins/DeathMessages/log directory; feel free to modify it as needed.) -
`This requires ModAPI to be enabled.` +(The language files will be generated in the ./plugins/DeathMessages/lang directory; feel free to modify it as needed.) +>[!WARNING] +> DO NOT modify files in directory `./plugins/DeathMessages/resource`, unless you know what you are doing High performance: Implemented using native functions with no additional data storage, resulting in minimal performance overhead. Uses native death message broadcasting, preserving the vanilla showdeathmessages game rule behavior. diff --git a/manifest.json b/manifest.json index 2d0b785..3737971 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "name": "${modName}", "entry": "${modFile}", "type": "native", - "version": "0.14.0", + "version": "0.14.1", "author": "GroupMountain", "description": "Better Death Messages", "dependencies": [ diff --git a/src/DeathMessage.cpp b/src/DeathMessage.cpp index f16ec67..88d4abf 100644 --- a/src/DeathMessage.cpp +++ b/src/DeathMessage.cpp @@ -10,6 +10,8 @@ #include "mc/world/actor/Mob.h" #include "mc/world/level/Level.h" +#include + //===============GMLIB 0.13.5 ActorDamageCause.cc=============== phmap::flat_hash_map mFallHeightMap; @@ -83,10 +85,21 @@ DeathMessageResult makeDeathMessage( // 如果没有击杀者和试图逃离,那么不可能使用 .player 结尾 ll::utils::string_utils::replaceAll(res.first, ".player", ""); } + if (!DeathMessages::Entry::getInstance().isResourceI18nLoaded) { - res.first=tr(res.first, res.second); + auto translatedMessage=tr(res.first, res.second); + res.first = translatedMessage; res.second = {}; + + } + auto& config = DeathMessages::Entry::getInstance().getConfig(); + + if (config.ConsoleLog.DeathMessage) { + auto translatedMessage=tr(res.first, res.second); + deathLogger->info(translatedMessage); + } + return res; } // 未定义返回vanilla值 diff --git a/src/Entry.cpp b/src/Entry.cpp index 12af14b..241834c 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -17,6 +17,7 @@ #include "gmlib/gm/i18n/LangI18n.h" #include "gmlib/gm/i18n/ResourceI18n.h" +#include "gmlib/mc/locale/I18nAPI.h" ll::io::LoggerRegistry& loggerRegistry = ll::io::LoggerRegistry::getInstance(); std::shared_ptr logger = loggerRegistry.getOrCreate(MOD_NAME); @@ -53,9 +54,11 @@ bool Entry::load() { ll::makePolymorphic("[{3:.3%F %T.} {2}][{1}] {0}", false); deathLogger->addSink(std::make_shared(path, polymorphicFormatter)); } - loadI18n(); if ((!getConfig().ServerSideTranslation.Enabled)&&ll::mod::ModManagerRegistry::getInstance().hasMod("ModAPI")) { loadResourcePack(); + }else { + loadI18n(); + } return true; } @@ -133,9 +136,19 @@ void Entry::loadResourcePack() { LL_REGISTER_MOD(DeathMessages::Entry, DeathMessages::Entry::getInstance()); -std::string tr(std::string const& key, std::vector const& params) { +std::string tr(std::string const& key, std::vector params) { + if (DeathMessages::Entry::getInstance().isResourceI18nLoaded) { + return gmlib::I18nAPI::get(key, params); + } auto& i18n = DeathMessages::Entry::getInstance().getI18n(); - return i18n.get(key, params); + logger->info(DeathMessages::Entry::getInstance().isResourceI18nLoaded ? "ResourceI18nLoaded" : "LangI18nLoaded"); + if (params.size() > 1) + params.at(1) = gmlib::I18nAPI::get( + params.at(1), + {}, + DeathMessages::Entry::getInstance().getConfig().ServerSideTranslation.Language + ); + return i18n.get(key, params); return key; } diff --git a/src/Global.h b/src/Global.h index 9f31f8a..25a5685 100644 --- a/src/Global.h +++ b/src/Global.h @@ -17,4 +17,4 @@ extern bool isCrystal; using DeathMessageResult = std::pair>; extern DeathMessageResult translateDeathMessage(DeathMessageResult origin, std::string name, Actor* dead, ActorDamageSource* ads); -extern std::string tr(std::string const& key, std::vector const& params = {}); +extern std::string tr(std::string const& key, std::vector params = {}); diff --git a/src/Hooks.cpp b/src/Hooks.cpp index bba230b..d07662a 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -87,8 +87,8 @@ LL_AUTO_TYPE_INSTANCE_HOOK( auto res = translateDeathMessage(originl, a1, a2, this); return res; } - -LL_AUTO_TYPE_INSTANCE_HOOK( +// +/*LL_AUTO_TYPE_INSTANCE_HOOK( ActorDamageByActorHook, HookPriority::Lowest, ActorDamageByActorSource, @@ -128,13 +128,13 @@ LL_AUTO_TYPE_INSTANCE_HOOK( auto originl = origin(a1, a2); auto res = translateDeathMessage(originl, a1, a2, this); return res; -} +}*/ void uninstallHooks() { MobDieHook::unhook(); CryatslHurtHook::unhook(); ActorDamageHook::unhook(); - ActorDamageByActorHook::unhook(); + /*ActorDamageByActorHook::unhook(); ActorDamageByBlockHook::unhook(); - ActorDamageByChildHook::unhook(); + ActorDamageByChildHook::unhook();*/ } diff --git a/src/Log.cpp b/src/Log.cpp index d19e177..5c377a2 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -8,7 +8,6 @@ #include "ll/api/event/EventBus.h" ll::event::ListenerPtr playerConnectEventListener; -ll::event::ListenerPtr playerDieEventListener; ll::event::ListenerPtr playerDisconnectEventListener; void ListenEvents() { auto& config = DeathMessages::Entry::getInstance().getConfig(); @@ -30,20 +29,13 @@ void ListenEvents() { } ); } - if (config.ConsoleLog.DeathMessage) { - playerDieEventListener=eventBus.emplaceListener( - [](ll::event::player::PlayerDieEvent& ev) { - auto [key, params] = ev.source().getDeathMessage(ev.self().getRealName(), &ev.self()); - auto info = tr(key, params); - deathLogger->info(info); - } - ); - } + + //Die Message move to DeathMessage.cpp 93L + } void uninstallEventListeners() { auto& eventBus = ll::event::EventBus::getInstance(); - eventBus.removeListener(playerDieEventListener); eventBus.removeListener(playerConnectEventListener); eventBus.removeListener(playerDisconnectEventListener); } \ No newline at end of file diff --git a/src/MemoryOperators.cpp b/src/MemoryOperators.cpp index 0172f0d..c9efced 100644 --- a/src/MemoryOperators.cpp +++ b/src/MemoryOperators.cpp @@ -4,3 +4,4 @@ #define LL_MEMORY_OPERATORS #include "ll/api/memory/MemoryOperators.h" // IWYU pragma: keep + diff --git a/tooth.json b/tooth.json index 9d3228b..6319568 100644 --- a/tooth.json +++ b/tooth.json @@ -2,7 +2,7 @@ "format_version": 3, "format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d", "tooth": "github.com/GroupMountain/DeathMessages", - "version": "0.14.0", + "version": "0.14.1", "info": { "name": "DeathMessages", "description": "Better Death Messages on MCBE",