refactor: moved name storage into seperate class

This commit is contained in:
aro 2023-03-11 11:49:56 +01:00
parent e7bd920e92
commit 8792c59568
6 changed files with 54 additions and 32 deletions

View file

@ -16,6 +16,7 @@ import net.artelnatif.nicko.impl.InternalsProvider;
import net.artelnatif.nicko.mojang.MojangAPI; import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.placeholder.PlaceHolderHook; import net.artelnatif.nicko.placeholder.PlaceHolderHook;
import net.artelnatif.nicko.storage.PlayerDataStore; import net.artelnatif.nicko.storage.PlayerDataStore;
import net.artelnatif.nicko.storage.name.PlayerNameStore;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
@ -36,6 +37,7 @@ public class NickoBukkit extends JavaPlugin {
private ConfigurationManager configurationManager; private ConfigurationManager configurationManager;
private Configuration configuration; private Configuration configuration;
private LocaleFileManager localeFileManager; private LocaleFileManager localeFileManager;
private PlayerNameStore nameStore;
public NickoBukkit() { this.unitTesting = false; } public NickoBukkit() { this.unitTesting = false; }
@ -57,11 +59,12 @@ public class NickoBukkit extends JavaPlugin {
mojangAPI = new MojangAPI(); mojangAPI = new MojangAPI();
dataStore = new PlayerDataStore(mojangAPI, getNickoConfig()); dataStore = new PlayerDataStore(mojangAPI, getNickoConfig());
nameStore = new PlayerNameStore();
if (!getDataStore().getStorage().isError()) { if (!dataStore.getStorage().isError()) {
getLogger().info("Loading persistence..."); getLogger().info("Loading persistence...");
if (!getDataStore().getStorage().getProvider().init()) { if (!dataStore.getStorage().getProvider().init()) {
getDataStore().getStorage().setError(true); dataStore.getStorage().setError(true);
getLogger().severe("Failed to open persistence, data will NOT be saved!"); getLogger().severe("Failed to open persistence, data will NOT be saved!");
} }
} }
@ -70,13 +73,13 @@ public class NickoBukkit extends JavaPlugin {
getLogger().info("Loading internals..."); getLogger().info("Loading internals...");
if (getInternals() == null) { if (getInternals() == null) {
getLogger().severe("Nicko could not find a valid implementation for this server version. Is your server supported?"); getLogger().severe("Nicko could not find a valid implementation for this server version. Is your server supported?");
getDataStore().getStorage().setError(true); dataStore.getStorage().setError(true);
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
} }
localeFileManager = new LocaleFileManager(); localeFileManager = new LocaleFileManager();
if (getNickoConfig().isCustomLocale()) { if (configuration.isCustomLocale()) {
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) { if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
getLogger().info("Successfully loaded custom language file."); getLogger().info("Successfully loaded custom language file.");
} else { } else {
@ -106,9 +109,9 @@ public class NickoBukkit extends JavaPlugin {
public void onDisable() { public void onDisable() {
if (!getDataStore().getStorage().isError()) { if (!getDataStore().getStorage().isError()) {
getLogger().info("Closing persistence..."); getLogger().info("Closing persistence...");
getDataStore().clearStoredNames(); nameStore.clearStoredNames();
Bukkit.getOnlinePlayers().forEach(player -> getDataStore().saveData(player)); Bukkit.getOnlinePlayers().forEach(player -> dataStore.saveData(player));
if (!getDataStore().getStorage().getProvider().close()) { if (!dataStore.getStorage().getProvider().close()) {
getLogger().severe("Failed to close persistence!"); getLogger().severe("Failed to close persistence!");
} }
} }
@ -134,6 +137,10 @@ public class NickoBukkit extends JavaPlugin {
return dataStore; return dataStore;
} }
public PlayerNameStore getNameStore() {
return nameStore;
}
public MojangAPI getMojangAPI() { public MojangAPI getMojangAPI() {
return mojangAPI; return mojangAPI;
} }

View file

@ -4,6 +4,7 @@ import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult; import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile; import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.storage.PlayerDataStore; import net.artelnatif.nicko.storage.PlayerDataStore;
import net.artelnatif.nicko.storage.name.PlayerNameStore;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,6 +15,7 @@ public class AppearanceManager {
private final Player player; private final Player player;
private final NickoBukkit instance = NickoBukkit.getInstance(); private final NickoBukkit instance = NickoBukkit.getInstance();
private final PlayerDataStore dataStore = instance.getDataStore(); private final PlayerDataStore dataStore = instance.getDataStore();
private final PlayerNameStore nameStore = instance.getNameStore();
private AppearanceManager(UUID uuid) { private AppearanceManager(UUID uuid) {
this.player = Bukkit.getPlayer(uuid); this.player = Bukkit.getPlayer(uuid);
@ -68,7 +70,7 @@ public class AppearanceManager {
} }
public ActionResult<Void> reset() { public ActionResult<Void> reset() {
final String defaultName = dataStore.getStoredName(player); final String defaultName = nameStore.getStoredName(player);
this.profile.setName(defaultName); this.profile.setName(defaultName);
this.profile.setSkin(defaultName); this.profile.setSkin(defaultName);
final ActionResult<Void> actionResult = resetPlayer(); final ActionResult<Void> actionResult = resetPlayer();

View file

@ -7,6 +7,7 @@ import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18N; import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict; import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.storage.PlayerDataStore; import net.artelnatif.nicko.storage.PlayerDataStore;
import net.artelnatif.nicko.storage.name.PlayerNameStore;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -17,15 +18,17 @@ public class PlayerJoinListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
final PlayerDataStore dataStore = NickoBukkit.getInstance().getDataStore(); final NickoBukkit instance = NickoBukkit.getInstance();
dataStore.storeName(player);
final PlayerDataStore dataStore = instance.getDataStore();
final PlayerNameStore nameStore = instance.getNameStore();
nameStore.storeName(player);
// TODO: 2/20/23 BungeeCord transfer // TODO: 2/20/23 BungeeCord transfer
dataStore.performProfileUpdate(player.getUniqueId(), NickoProfile.EMPTY_PROFILE); dataStore.performProfileUpdate(player.getUniqueId(), NickoProfile.EMPTY_PROFILE);
Bukkit.getScheduler().runTaskLater(NickoBukkit.getInstance(), () -> { Bukkit.getScheduler().runTaskLater(instance, () -> {
final AppearanceManager appearanceManager = AppearanceManager.get(player); final AppearanceManager appearanceManager = AppearanceManager.get(player);
if (appearanceManager.hasData()) { if (appearanceManager.hasData()) {
final ActionResult<Void> actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange()); final ActionResult<Void> actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
if (!actionResult.isError()) { if (!actionResult.isError()) {

View file

@ -19,31 +19,12 @@ public class PlayerDataStore {
private final Storage storage; private final Storage storage;
private final MojangAPI mojangAPI; private final MojangAPI mojangAPI;
private final HashMap<UUID, NickoProfile> profiles = new HashMap<>(); private final HashMap<UUID, NickoProfile> profiles = new HashMap<>();
private final HashMap<UUID, String> names = new HashMap<>();
public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) { public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) {
this.mojangAPI = mojangAPI; this.mojangAPI = mojangAPI;
this.storage = configuration.isLocal() ? new JSONStorage() : new SQLStorage(configuration); this.storage = configuration.isLocal() ? new JSONStorage() : new SQLStorage(configuration);
} }
public void storeName(Player player) {
if (!isNameStored(player)) {
names.put(player.getUniqueId(), player.getName());
}
}
public String getStoredName(Player player) {
return names.get(player.getUniqueId());
}
private boolean isNameStored(Player player) {
return names.containsKey(player.getUniqueId());
}
public void clearStoredNames() {
names.clear();
}
public void performProfileUpdate(UUID uuid, NickoProfile profile) { public void performProfileUpdate(UUID uuid, NickoProfile profile) {
if (!profiles.containsKey(uuid)) { if (!profiles.containsKey(uuid)) {
profiles.put(uuid, profile); profiles.put(uuid, profile);

View file

@ -0,0 +1,28 @@
package net.artelnatif.nicko.storage.name;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.UUID;
public class PlayerNameStore {
private final HashMap<UUID, String> names = new HashMap<>();
public void storeName(Player player) {
if (!isNameStored(player)) {
names.put(player.getUniqueId(), player.getName());
}
}
public String getStoredName(Player player) {
return names.get(player.getUniqueId());
}
private boolean isNameStored(Player player) {
return names.containsKey(player.getUniqueId());
}
public void clearStoredNames() {
names.clear();
}
}

View file

@ -36,6 +36,7 @@ public class CacheStorageTest {
public void cachePlayerData() { public void cachePlayerData() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent()); assertTrue(optionalProfile.isPresent());
//assertTrue(plugin.getDataStore().isCached(player.getUniqueId()));
} }
@AfterAll @AfterAll