adapt: adapt to GMLIB v0.12.6

This commit is contained in:
Tsubasa6848
2024-05-26 06:40:52 +08:00
Unverified
parent 1e40ff52c6
commit b8718b941f
9 changed files with 100 additions and 110 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "${pluginName}",
"entry": "${pluginFile}",
"type": "native",
"version": "0.12.0",
"version": "0.12.1",
"author": "GroupMountain",
"description": "Better Death Messages",
"dependencies": [
+20 -16
View File
@@ -1,18 +1,22 @@
#pragma once
#include "Global.h"
#include <iostream>
std::string defaultConfig = R"({
"ConsoleLog": {
"JoinMessage": true,
"LeftMessage": true,
"DeathMessage": true
},
"FileLog": {
"Enabled": false,
"Path": "./logs/DeathLog.log"
},
"ServerSideTranslation": {
"Enabled": false,
"Language": "en_US"
}
})";
struct Config {
int version = 1;
struct consoleLog {
bool JoinMessage = true;
bool LeftMessage = true;
bool DeathMessage = true;
} ConsoleLog;
struct fileLog {
bool Enabled = false;
std::string Path = "./logs/DeathLog.log";
} FileLog;
struct serverSideTranslation {
bool Enabled = false;
std::string Language = "en_US";
} ServerSideTranslation;
};
+5 -3
View File
@@ -1,3 +1,4 @@
#include "Entry.h"
#include "Global.h"
void RegisterDamageDefinition() {
@@ -101,19 +102,20 @@ void RegisterDamageDefinition() {
}
void ListenEvents() {
auto& config = DeathMessages::Entry::getInstance()->getConfig();
auto& eventBus = ll::event::EventBus::getInstance();
if (ConfigData::mJoinMessage) {
if (config.ConsoleLog.JoinMessage) {
eventBus.emplaceListener<ll::event::player::PlayerConnectEvent>([](ll::event::player::PlayerConnectEvent& ev) {
infoLogger.info(fmt::format(fg(fmt::color::yellow), tr("multiplayer.player.joined", {ev.self().getName()}))
);
});
}
if (ConfigData::mLeftMessage) {
if (config.ConsoleLog.LeftMessage) {
eventBus.emplaceListener<ll::event::player::PlayerLeaveEvent>([](ll::event::player::PlayerLeaveEvent& ev) {
infoLogger.info(fmt::format(fg(fmt::color::yellow), tr("multiplayer.player.left", {ev.self().getName()})));
});
}
if (ConfigData::mDeathMessage) {
if (config.ConsoleLog.DeathMessage) {
eventBus.emplaceListener<GMLIB::Event::EntityEvent::DeathMessageAfterEvent>(
[](GMLIB::Event::EntityEvent::DeathMessageAfterEvent& ev) {
auto msg = ev.getDeathMessage();
+50 -1
View File
@@ -1,7 +1,10 @@
#include "Entry.h"
#include "Global.h"
#include "Language.h"
ll::Logger logger(PLUGIN_NAME);
ll::Logger deathLogger("Death");
ll::Logger infoLogger("Server");
namespace DeathMessages {
@@ -11,7 +14,19 @@ std::unique_ptr<Entry>& Entry::getInstance() {
}
bool Entry::load() {
initPlugin();
mConfig.emplace();
if (!ll::config::loadConfig(*mConfig, getSelf().getConfigDir() / u8"config.json")) {
ll::config::saveConfig(*mConfig, getSelf().getConfigDir() / u8"config.json");
}
if (getConfig().ServerSideTranslation.Enabled) {
loadI18n();
} else {
loadResourcePack();
}
if (getConfig().FileLog.Enabled) {
auto& path = getConfig().FileLog.Path;
deathLogger.setFile(path);
}
return true;
}
@@ -23,6 +38,40 @@ bool Entry::enable() {
bool Entry::disable() { return true; }
bool Entry::unload() {
mConfig.reset();
mI18n.reset();
getInstance().reset();
return true;
}
Config& Entry::getConfig() { return mConfig.value(); }
std::optional<LangI18n> Entry::getI18n() { return mI18n; }
void Entry::loadI18n() {
mI18n.emplace(getSelf().getLangDir(), getConfig().ServerSideTranslation.Language);
mI18n->updateOrCreateLanguage("en_US", en_US);
mI18n->updateOrCreateLanguage("zh_CN", zh_CN);
mI18n->loadAllLanguages();
}
void Entry::loadResourcePack() {
GMLIB::Mod::VanillaFix::setFixI18nEnabled();
std::string resourcePath = "./plugins/DeathMessages/resource/";
auto resource = GMLIB::Files::ResourceLanguage::ResourceLanguage(resourcePath, PLUGIN_NAME, 0, 8, 0);
resource.addLanguage("en_US", en_US);
resource.addLanguage("zh_CN", zh_CN);
resource.initLanguage();
}
} // namespace DeathMessages
LL_REGISTER_PLUGIN(DeathMessages::Entry, DeathMessages::Entry::getInstance());
std::string tr(std::string const& key, std::vector<std::string> const& params) {
if (auto i18n = DeathMessages::Entry::getInstance()->getI18n()) {
return i18n->get(key, params);
}
return I18nAPI::get(key, params);
}
+16 -5
View File
@@ -1,8 +1,11 @@
#pragma once
#include <ll/api/plugin/NativePlugin.h>
#include "Config.h"
#include "Global.h"
namespace DeathMessages {
using namespace GMLIB::Files::I18n;
class Entry {
public:
@@ -21,13 +24,21 @@ public:
/// @return True if the plugin is disabled successfully.
bool disable();
// TODO: Implement this method if you need to unload the plugin.
// /// @return True if the plugin is unloaded successfully.
// bool unload();
/// @return True if the plugin is unloaded successfully.
bool unload();
Config& getConfig();
std::optional<LangI18n> getI18n();
void loadI18n();
void loadResourcePack();
private:
ll::plugin::NativePlugin& mSelf;
std::optional<Config> mConfig;
std::optional<LangI18n> mI18n;
};
} // namespace DeathMessages
+2 -9
View File
@@ -7,15 +7,8 @@ extern ll::Logger logger;
extern ll::Logger deathLogger;
extern ll::Logger infoLogger;
namespace ConfigData {
extern bool mServerSideLang;
extern bool mJoinMessage;
extern bool mLeftMessage;
extern bool mDeathMessage;
} // namespace ConfigData
extern std::string tr(std::string const& key, std::vector<std::string> const& data = {});
extern std::string tr(std::string key, std::vector<std::string> data = {});
extern void initPlugin();
extern void RegisterDamageDefinition();
extern void ListenEvents();
extern void initPlugin();
-69
View File
@@ -1,69 +0,0 @@
#include "Config.h"
#include "Global.h"
#include "Language.h"
ll::Logger deathLogger("Death");
ll::Logger infoLogger("Server");
GMLIB::Files::JsonConfig* Config = nullptr;
GMLIB::Files::I18n::LangI18n* Language = nullptr;
namespace ConfigData {
bool mServerSideLang = false;
bool mJoinMessage = false;
bool mLeftMessage = false;
bool mDeathMessage = false;
} // namespace ConfigData
void initServerSideTranslation() {
std::string langPath = "./plugins/DeathMessages/language/";
std::string languageCode = Config->getValue<std::string>({"ServerSideTranslation", "Language"}, "en_US");
Language = new GMLIB::Files::I18n::LangI18n(langPath);
Language->updateOrCreateLanguage("en_US", en_US);
Language->updateOrCreateLanguage("zh_CN", zh_CN);
Language->loadAllLanguages();
Language->chooseLanguage(languageCode);
}
void initResourcePackTranslation() {
GMLIB::Mod::VanillaFix::setFixI18nEnabled();
std::string resourcePath = "./plugins/DeathMessages/resource/";
auto resource = GMLIB::Files::ResourceLanguage::ResourceLanguage(resourcePath, PLUGIN_NAME, 0, 8, 0);
resource.addLanguage("en_US", en_US);
resource.addLanguage("zh_CN", zh_CN);
resource.initLanguage();
}
std::string tr(std::string key, std::vector<std::string> data) {
if (ConfigData::mServerSideLang) {
for (auto& info : data) {
ll::utils::string_utils::replaceAll(info, "%", "");
info = Language->translate(info);
}
return Language->translate(key, data);
}
return I18nAPI::get(key, data);
}
void initConfig() {
Config = new GMLIB::Files::JsonConfig("./plugins/DeathMessages/config/config.json", defaultConfig);
Config->init();
ConfigData::mDeathMessage = Config->getValue<bool>({"ConsoleLog", "DeathMessage"}, true);
ConfigData::mJoinMessage = Config->getValue<bool>({"ConsoleLog", "JoinMessage"}, true);
ConfigData::mLeftMessage = Config->getValue<bool>({"ConsoleLog", "LeftMessage"}, true);
ConfigData::mServerSideLang = Config->getValue<bool>({"ServerSideTranslation", "Enabled"}, true);
}
void initPlugin() {
initConfig();
if (ConfigData::mServerSideLang) {
initServerSideTranslation();
} else {
initResourcePackTranslation();
}
if (Config->getValue<bool>({"FileLog", "Enabled"}, false)) {
auto path = Config->getValue<std::string>({"FileLog", "Path"}, "./logs/DeathLog.log");
deathLogger.setFile(path);
}
}
+3 -3
View File
@@ -1,7 +1,7 @@
{
"format_version": 2,
"tooth": "github.com/GroupMountain/DeathMessages",
"version": "0.12.0",
"version": "0.12.1",
"info": {
"name": "DeathMessages",
"description": "Better Death Messages on MCBE",
@@ -11,9 +11,9 @@
"DeathMessages"
]
},
"asset_url": "https://github.com/GroupMountain/DeathMessages/releases/download/v0.12.0/DeathMessages-windows-x64.zip",
"asset_url": "https://github.com/GroupMountain/DeathMessages/releases/download/v0.12.1/DeathMessages-windows-x64.zip",
"dependencies": {
"github.com/GroupMountain/GMLIB": ">=0.12.3"
"github.com/GroupMountain/GMLIB": ">=0.12.6"
},
"files": {
"place": [