feat: name cache
This commit is contained in:
+2
-1
@@ -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<std::reference_wrapper<ll::plugin::NativePlugin>>(self);
|
||||
GMLIB::Server::UserCache::enableUserCache();
|
||||
initPlugin();
|
||||
initPlayerDataCache();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+9
-4
@@ -10,14 +10,19 @@ extern std::string tr(std::string key, std::vector<std::string> 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<mce::UUID, std::string> 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);
|
||||
extern void deleteFailedForm(Player& pl, mce::UUID uuid, std::string name);
|
||||
|
||||
extern void listenEvent();
|
||||
extern void initPlayerDataCache();
|
||||
|
||||
extern std::optional<std::string> getNameFromUuid(std::string uuid);
|
||||
@@ -4,6 +4,29 @@
|
||||
|
||||
GMLIB::Files::JsonConfig* Config = nullptr;
|
||||
GMLIB::Files::I18n::LangI18n* Language = nullptr;
|
||||
nlohmann::json mPlayerDataCache;
|
||||
|
||||
std::optional<std::string> 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<std::string> data) { return Language->translate(key, data); }
|
||||
|
||||
void listenEvent() {
|
||||
auto& eventBus = ll::event::EventBus::getInstance();
|
||||
eventBus.emplaceListener<ll::event::player::PlayerConnectEvent>([](ll::event::player::PlayerConnectEvent& ev) {
|
||||
auto& pl = ev.self();
|
||||
mPlayerDataCache[pl.getUuid().asString()] = pl.getRealName();
|
||||
saveCacheData();
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<std::string> getNameFromUuid(std::string uuid) {
|
||||
if (mPlayerDataCache.contains(uuid)) {
|
||||
return mPlayerDataCache[uuid].get<std::string>();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user