From 1b513884c1962160a9d6d742a3d31cd47d2c7e4f Mon Sep 17 00:00:00 2001 From: wed150 Date: Sun, 21 Jun 2026 07:45:29 +0800 Subject: [PATCH] reflect: format includes --- src/DeathMessage.cpp | 43 ++++++++++++++++--------------------------- src/Entry.cpp | 18 +++++++++++++----- src/Entry.h | 2 +- src/Global.h | 6 ------ src/Hooks.cpp | 25 +++++++------------------ src/Language.h | 4 ++-- src/Log.cpp | 22 ++++++++++++++++------ 7 files changed, 55 insertions(+), 65 deletions(-) diff --git a/src/DeathMessage.cpp b/src/DeathMessage.cpp index db9540a..e4a4ac2 100644 --- a/src/DeathMessage.cpp +++ b/src/DeathMessage.cpp @@ -1,43 +1,30 @@ #include "Entry.h" -#include "ll/api/memory/Hook.h" + #include "ll/api/chrono/GameChrono.h" -#include "mc/world/actor/Mob.h" +#include "ll/api/memory/Hook.h" +#include "ll/api/service/Bedrock.h" + #include "mc/world/actor/ActorDamageSource.h" #include "mc/world/actor/ActorDamageByActorSource.h" - -#include "ll/api/service/Bedrock.h" +#include "mc/world/actor/Actor.h" +#include "mc/world/actor/Mob.h" #include "mc/world/level/Level.h" +//===============GMLIB 0.13.5 ActorDamageCause.cc=============== -#include "mc/world/actor/Actor.h" phmap::flat_hash_map mFallHeightMap; phmap::flat_hash_map mHurtByEntityMap; -std::vector> mCustomCauseMap; phmap::flat_hash_map mVanillaCauseMessage; - phmap::flat_hash_map> mHardCodedDeathMessage; bool isCrystal = false; std::string getCause(SharedTypes::Legacy::ActorDamageCause cause) { auto id = (int)cause; - for (auto& key : mCustomCauseMap) { - if (key.second == id) { - for (auto& key1 : mCustomCauseMap) { - if ((SharedTypes::Legacy::ActorDamageCause)key1.second == cause) { - return key1.first; - } - } - return "none"; - } - } - if (mVanillaCauseMessage.count(id)) { + if (mVanillaCauseMessage.count(id)) return std::string(mVanillaCauseMessage[id]); - } return ""; } - - Actor* getDamagingEntity(ActorDamageSource* ads) { auto id = ads->getDamagingEntityUniqueID(); return ll::service::getLevel()->fetchEntity(id,true); @@ -45,9 +32,8 @@ Actor* getDamagingEntity(ActorDamageSource* ads) { // %entity.warden.name std::string getResourcePackKey(std::string const& name, Actor* ac) { - if (ac->hasCategory(ActorCategory::Player)) { + if (ac->hasCategory(ActorCategory::Player)) return name; - } auto type = ac->getTypeName(); ll::utils::string_utils::replaceAll(type, "minecraft:", ""); std::string res = "%entity." + type + ".name"; @@ -68,16 +54,16 @@ DeathMessageResult makeDeathMessage( if (cause >= 35||mVanillaCauseMessage.count(cause) || isHardCodedMessage) { std::string msg = "death.attack.damageCause.item"; ll::utils::string_utils::replaceAll(msg, "damageCause", getCause(SharedTypes::Legacy::ActorDamageCause(cause))); - if (isHardCodedMessage) { + if (isHardCodedMessage) msg = deathMessage.first; - } + // 没有武器名不使用.item结尾 if (weaponName.empty()) { // 试图逃离 xx 使用.player if (killer) { - if (isEscaping) { + if (isEscaping) ll::utils::string_utils::replaceAll(msg, ".item", ".player"); - } + } else { ll::utils::string_utils::replaceAll(msg, ".item", ""); } @@ -268,6 +254,9 @@ bool registerCustomDeathMessage( mHardCodedDeathMessage[cause][killerType] = message; return true; } +//===============Copy finish=============== + + void RegisterDamageDefinition() { setVanillaCauseMessage(SharedTypes::Legacy::ActorDamageCause::Anvil, "anvil"); setVanillaCauseMessage(SharedTypes::Legacy::ActorDamageCause::EntityAttack, "mob"); diff --git a/src/Entry.cpp b/src/Entry.cpp index b1fb611..c52bfd5 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -1,24 +1,29 @@ #include "Entry.h" #include "Global.h" #include "Language.h" -#include "ll/api/mod/Mod.h" + #include "ll/api/Config.h" +#include "ll/api/data/IndirectValue.h" #include "ll/api/io/PatternFormatter.h" #include "ll/api/io/Logger.h" #include "ll/api/io/LoggerRegistry.h" #include "ll/api/io/FileSink.h" +#include "ll/api/mod/Mod.h" +#include "ll/api/mod/ModManagerRegistry.h" #include "ll/api/mod/RegisterHelper.h" -#include "ll/api/data/IndirectValue.h" +#include "ll/api/service/Bedrock.h" + #include "gmlib/gm/i18n/LangI18n.h" #include "gmlib/gm/i18n/ResourceI18n.h" -#include "ll/api/mod/ModManagerRegistry.h" -#include ll::io::LoggerRegistry& loggerRegistry = ll::io::LoggerRegistry::getInstance(); std::shared_ptr logger = loggerRegistry.getOrCreate(MOD_NAME); std::shared_ptr deathLogger = loggerRegistry.getOrCreate("Death"); std::shared_ptr infoLogger = loggerRegistry.getOrCreate("Server"); - +extern void uninstallHooks(); +extern void RegisterDamageDefinition(); +extern void uninstallEventListeners(); +extern void ListenEvents(); namespace DeathMessages { @@ -63,6 +68,9 @@ bool Entry::unload() { mConfig.reset(); mI18n.reset(); uninstallHooks(); + loggerRegistry.erase(MOD_NAME); + loggerRegistry.erase("Death"); + loggerRegistry.erase("Server"); // getInstance().reset(); return true; } diff --git a/src/Entry.h b/src/Entry.h index 57cbbfa..5172f91 100644 --- a/src/Entry.h +++ b/src/Entry.h @@ -1,8 +1,8 @@ #pragma once #include "Config.h" #include "Global.h" -#include "ll/api/mod/Mod.h" +#include "ll/api/mod/Mod.h" #include #include diff --git a/src/Global.h b/src/Global.h index bb87552..9f31f8a 100644 --- a/src/Global.h +++ b/src/Global.h @@ -4,7 +4,6 @@ #include #include #include -#include "ll/api/i18n/I18n.h" #define MOD_NAME "DeathMessages" @@ -16,11 +15,6 @@ extern phmap::flat_hash_map mHurtByEntityMap; extern bool isCrystal; using DeathMessageResult = std::pair>; -using namespace ll::i18n_literals; extern DeathMessageResult translateDeathMessage(DeathMessageResult origin, std::string name, Actor* dead, ActorDamageSource* ads); -extern void uninstallHooks(); -extern void RegisterDamageDefinition(); - extern std::string tr(std::string const& key, std::vector const& params = {}); -extern void ListenEvents(); \ No newline at end of file diff --git a/src/Hooks.cpp b/src/Hooks.cpp index d660ac2..74d599d 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -1,19 +1,22 @@ #include "Entry.h" +#include "Hooks.h" +#include "Global.h" + #include "ll/api/memory/Hook.h" #include "ll/api/chrono/GameChrono.h" #include "ll/api/thread/ThreadPoolExecutor.h" -#include + #include "mc/world/actor/Mob.h" +#include "mc/world/actor/monster/EnderCrystal.h" #include "mc/world/actor/ActorDamageSource.h" #include "mc/world/actor/ActorDamageByActorSource.h" #include "mc/world/actor/ActorDamageByBlockSource.h" #include "mc/world/actor/ActorDamageByChildActorSource.h" -#include "mc/world/actor/monster/EnderCrystal.h" #include "mc/world/actor/ActorHurtResult.h" -#include "Global.h" #include "mc/world/actor/Actor.h" -#include "Hooks.h" +#include + @@ -29,17 +32,11 @@ LL_AUTO_TYPE_INSTANCE_HOOK( if (ads.mCause == SharedTypes::Legacy::ActorDamageCause::Fall) { mFallHeightMap[uid.rawID] = getFallDistance(); - logger->info("跌落高度为 {}", getFallDistance()); } if (hasCategory(ActorCategory::Player) || isTame()) { if (mLastHurtByMobTime != 0) { auto hm = getLastHurtByMob(); mHurtByEntityMap[uid.rawID] = hm->getOrCreateUniqueID(); - if (!getLastHurtByMob()->getNameTag().empty()) logger->info("伤害来源为 {}", getLastHurtByMob()->getNameTag()); - logger->info("伤害来源为 {}", getLastHurtByMob()->getTypeName()); - - - if (!getLastHurtByMob()->getCarriedItem().getCustomName().empty()) logger->info("伤害来源为 {}", getLastHurtByMob()->getCarriedItem().getCustomName()); } } origin(ads); @@ -94,8 +91,6 @@ LL_AUTO_TYPE_INSTANCE_HOOK( auto originl = origin(a1, a2); - logger->info("原消息name为 {}", a1); - logger->info("原消息为 {}", originl.first); auto res = translateDeathMessage(originl, a1, a2, this); return res; } @@ -110,8 +105,6 @@ LL_AUTO_TYPE_INSTANCE_HOOK( Actor* a2 ) { auto originl = origin(a1, a2); - logger->info("原消息name为 {}", a1); - logger->info("原消息为 {}", originl.first); auto res = translateDeathMessage(originl, a1, a2, this); return res; } @@ -127,8 +120,6 @@ LL_AUTO_TYPE_INSTANCE_HOOK( Actor* a2 ) { auto originl = origin(a1, a2); - logger->info("原消息name为 {}", a1); - logger->info("原消息为 {}", originl.first); auto res = translateDeathMessage(originl, a1, a2, this); return res; } @@ -145,8 +136,6 @@ LL_AUTO_TYPE_INSTANCE_HOOK( Actor* a2 ) { auto originl = origin(a1, a2); - logger->info("原消息name为 {}", a1); - logger->info("原消息为 {}", originl.first); auto res = translateDeathMessage(originl, a1, a2, this); return res; } diff --git a/src/Language.h b/src/Language.h index 6070524..b3c40b1 100644 --- a/src/Language.h +++ b/src/Language.h @@ -250,7 +250,7 @@ entity.breeze.name=Breeze entity.creaking.name=Creaking multiplayer.player.joined=%s joined the game multiplayer.player.left=%s left the game -plugin.deathmessages.modapi.warn=The plugin ModAPI is not loaded, so client-side translation is unavailable. Only server-side translation can be used. +plugin.deathmessages.modapi.warn=The optional Dependency plugin ModAPI is not loaded, so client-side translation is unavailable. Only server-side translation can be used. )"; std::string zh_CN = R"( @@ -504,5 +504,5 @@ entity.breeze.name=旋风人 entity.creaking.name=嘎枝 multiplayer.player.joined=%s 加入了游戏 multiplayer.player.left=%s 退出了游戏 -plugin.deathmessages.modapi.warn=插件ModAPI没有被加载,所以客户端侧翻译无法使用,正在启用服务端侧翻译 +plugin.deathmessages.modapi.warn=可选依赖ModAPI没有被加载,所以客户端侧翻译无法使用,正在启用服务端侧翻译 )"; \ No newline at end of file diff --git a/src/Log.cpp b/src/Log.cpp index 31c992a..d19e177 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -1,17 +1,20 @@ - #include "Entry.h" #include "Global.h" #include "ll/api/event/player/PlayerConnectEvent.h" -#include "ll/api/event/EventBus.h" #include "ll/api/event/player/PlayerDieEvent.h" - #include "ll/api/event/player/PlayerDisconnectEvent.h" + +#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(); auto& eventBus = ll::event::EventBus::getInstance(); if (config.ConsoleLog.JoinMessage) { - eventBus.emplaceListener([](ll::event::player::PlayerConnectEvent& ev) { + playerConnectEventListener=eventBus.emplaceListener([](ll::event::player::PlayerConnectEvent& ev) { infoLogger->info( fmt::format(fg(fmt::color::yellow), fmt::runtime(tr("multiplayer.player.joined", {ev.self().getRealName()}))) ); @@ -19,7 +22,7 @@ void ListenEvents() { }); } if (config.ConsoleLog.LeftMessage) { - eventBus.emplaceListener( + playerDisconnectEventListener=eventBus.emplaceListener( [](ll::event::player::PlayerDisconnectEvent& ev) { infoLogger->info( fmt::format(fg(fmt::color::yellow), fmt::runtime(tr("multiplayer.player.left", {ev.self().getRealName()}))) @@ -28,7 +31,7 @@ void ListenEvents() { ); } if (config.ConsoleLog.DeathMessage) { - eventBus.emplaceListener( + playerDieEventListener=eventBus.emplaceListener( [](ll::event::player::PlayerDieEvent& ev) { auto [key, params] = ev.source().getDeathMessage(ev.self().getRealName(), &ev.self()); auto info = tr(key, params); @@ -36,4 +39,11 @@ void ListenEvents() { } ); } +} + +void uninstallEventListeners() { + auto& eventBus = ll::event::EventBus::getInstance(); + eventBus.removeListener(playerDieEventListener); + eventBus.removeListener(playerConnectEventListener); + eventBus.removeListener(playerDisconnectEventListener); } \ No newline at end of file