diff --git a/src/Entry.cpp b/src/Entry.cpp index ea7402b..3bd6fa8 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -14,13 +14,14 @@ auto disable(ll::plugin::NativePlugin& /*self*/) -> bool { return true; } auto enable(ll::plugin::NativePlugin& /*self*/) -> bool { RegisterCommand(); + listenEvent(); return true; } auto load(ll::plugin::NativePlugin& self) -> bool { selfPluginInstance = std::make_unique>(self); - GMLIB::Server::UserCache::enableUserCache(); initPlugin(); + initPlayerDataCache(); return true; } diff --git a/src/Global.h b/src/Global.h index ce622f2..586fd23 100644 --- a/src/Global.h +++ b/src/Global.h @@ -10,14 +10,19 @@ extern std::string tr(std::string key, std::vector data = {}); extern void initPlugin(); extern void RegisterCommand(); -extern void mainForm(Player& pl) ; +extern void mainForm(Player& pl); extern void checkOnlineForm(Player& pl); -extern void checkAllForm(Player& pl) ; +extern void checkAllForm(Player& pl); extern void searchPlayerForm(Player& pl); extern void searchResultForm(Player& pl, std::unordered_map resultList); -extern void searchNotFoundForm(Player& pl, std::string& name) ; +extern void searchNotFoundForm(Player& pl, std::string& name); extern void checkPlayerForm(Player& pl, mce::UUID uuid); extern void invalidInputForm(Player& pl); extern void confirmWriteForm(Player& pl, mce::UUID uuid, std::string name); extern void confirmDeleteForm(Player& pl, mce::UUID uuid, std::string name); -extern void deleteFailedForm(Player& pl, mce::UUID uuid, std::string name); \ No newline at end of file +extern void deleteFailedForm(Player& pl, mce::UUID uuid, std::string name); + +extern void listenEvent(); +extern void initPlayerDataCache(); + +extern std::optional getNameFromUuid(std::string uuid); \ No newline at end of file diff --git a/src/Initialization.cpp b/src/Initialization.cpp index 826b995..988d9e6 100644 --- a/src/Initialization.cpp +++ b/src/Initialization.cpp @@ -4,6 +4,29 @@ GMLIB::Files::JsonConfig* Config = nullptr; GMLIB::Files::I18n::LangI18n* Language = nullptr; +nlohmann::json mPlayerDataCache; + +std::optional readFile(std::string path) { + if (std::filesystem::exists(path)) { + return GMLIB::Files::JsonFile::readFromFile(path); + } + return {}; +} + +void initPlayerDataCache() { + mPlayerDataCache = nlohmann::json::object(); + auto data = readFile("./plugins/InventoryCheck/data/PlayerDataCache.json"); + if (data.has_value()) { + try { + mPlayerDataCache = nlohmann::json::parse(data.value()); + } catch (...) {} + } +} + +void saveCacheData() { + std::string path = "./plugins/InventoryCheck/data/PlayerDataCache.json"; + GMLIB::Files::JsonFile::writeFile(path, mPlayerDataCache); +} void initPlugin() { Config = new GMLIB::Files::JsonConfig("./plugins/InventoryCheck/config/config.json", defaultConfig); @@ -24,3 +47,19 @@ void initPlugin() { } std::string tr(std::string key, std::vector data) { return Language->translate(key, data); } + +void listenEvent() { + auto& eventBus = ll::event::EventBus::getInstance(); + eventBus.emplaceListener([](ll::event::player::PlayerConnectEvent& ev) { + auto& pl = ev.self(); + mPlayerDataCache[pl.getUuid().asString()] = pl.getRealName(); + saveCacheData(); + }); +} + +std::optional getNameFromUuid(std::string uuid) { + if (mPlayerDataCache.contains(uuid)) { + return mPlayerDataCache[uuid].get(); + } + return {}; +} \ No newline at end of file diff --git a/src/InventoryCheck.cpp b/src/InventoryCheck.cpp index 0ba3c0f..5281cf5 100644 --- a/src/InventoryCheck.cpp +++ b/src/InventoryCheck.cpp @@ -10,7 +10,6 @@ bool saveInventory(Player& player) { if (newFile.is_open()) { newFile.write(nbt.c_str(), nbt.size()); newFile.close(); - return true; } } @@ -89,7 +88,7 @@ std::string getNameFormUuid(mce::UUID& uuid) { name = player->getRealName(); } else { auto strUuid = uuid.asString(); - auto cachename = GMLIB::Server::UserCache::getNameByUuid(strUuid); + auto cachename = getNameFromUuid(strUuid); if (cachename.has_value()) { name = cachename.value(); } else {