refactor: moved name storage into seperate class
This commit is contained in:
parent
e7bd920e92
commit
8792c59568
6 changed files with 54 additions and 32 deletions
|
@ -16,6 +16,7 @@ import net.artelnatif.nicko.impl.InternalsProvider;
|
|||
import net.artelnatif.nicko.mojang.MojangAPI;
|
||||
import net.artelnatif.nicko.placeholder.PlaceHolderHook;
|
||||
import net.artelnatif.nicko.storage.PlayerDataStore;
|
||||
import net.artelnatif.nicko.storage.name.PlayerNameStore;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
|
@ -36,6 +37,7 @@ public class NickoBukkit extends JavaPlugin {
|
|||
private ConfigurationManager configurationManager;
|
||||
private Configuration configuration;
|
||||
private LocaleFileManager localeFileManager;
|
||||
private PlayerNameStore nameStore;
|
||||
|
||||
public NickoBukkit() { this.unitTesting = false; }
|
||||
|
||||
|
@ -57,11 +59,12 @@ public class NickoBukkit extends JavaPlugin {
|
|||
|
||||
mojangAPI = new MojangAPI();
|
||||
dataStore = new PlayerDataStore(mojangAPI, getNickoConfig());
|
||||
nameStore = new PlayerNameStore();
|
||||
|
||||
if (!getDataStore().getStorage().isError()) {
|
||||
if (!dataStore.getStorage().isError()) {
|
||||
getLogger().info("Loading persistence...");
|
||||
if (!getDataStore().getStorage().getProvider().init()) {
|
||||
getDataStore().getStorage().setError(true);
|
||||
if (!dataStore.getStorage().getProvider().init()) {
|
||||
dataStore.getStorage().setError(true);
|
||||
getLogger().severe("Failed to open persistence, data will NOT be saved!");
|
||||
}
|
||||
}
|
||||
|
@ -70,13 +73,13 @@ public class NickoBukkit extends JavaPlugin {
|
|||
getLogger().info("Loading internals...");
|
||||
if (getInternals() == null) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
localeFileManager = new LocaleFileManager();
|
||||
if (getNickoConfig().isCustomLocale()) {
|
||||
if (configuration.isCustomLocale()) {
|
||||
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
|
||||
getLogger().info("Successfully loaded custom language file.");
|
||||
} else {
|
||||
|
@ -106,9 +109,9 @@ public class NickoBukkit extends JavaPlugin {
|
|||
public void onDisable() {
|
||||
if (!getDataStore().getStorage().isError()) {
|
||||
getLogger().info("Closing persistence...");
|
||||
getDataStore().clearStoredNames();
|
||||
Bukkit.getOnlinePlayers().forEach(player -> getDataStore().saveData(player));
|
||||
if (!getDataStore().getStorage().getProvider().close()) {
|
||||
nameStore.clearStoredNames();
|
||||
Bukkit.getOnlinePlayers().forEach(player -> dataStore.saveData(player));
|
||||
if (!dataStore.getStorage().getProvider().close()) {
|
||||
getLogger().severe("Failed to close persistence!");
|
||||
}
|
||||
}
|
||||
|
@ -134,6 +137,10 @@ public class NickoBukkit extends JavaPlugin {
|
|||
return dataStore;
|
||||
}
|
||||
|
||||
public PlayerNameStore getNameStore() {
|
||||
return nameStore;
|
||||
}
|
||||
|
||||
public MojangAPI getMojangAPI() {
|
||||
return mojangAPI;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import net.artelnatif.nicko.NickoBukkit;
|
|||
import net.artelnatif.nicko.disguise.ActionResult;
|
||||
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||
import net.artelnatif.nicko.storage.PlayerDataStore;
|
||||
import net.artelnatif.nicko.storage.name.PlayerNameStore;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
@ -14,6 +15,7 @@ public class AppearanceManager {
|
|||
private final Player player;
|
||||
private final NickoBukkit instance = NickoBukkit.getInstance();
|
||||
private final PlayerDataStore dataStore = instance.getDataStore();
|
||||
private final PlayerNameStore nameStore = instance.getNameStore();
|
||||
|
||||
private AppearanceManager(UUID uuid) {
|
||||
this.player = Bukkit.getPlayer(uuid);
|
||||
|
@ -68,7 +70,7 @@ public class AppearanceManager {
|
|||
}
|
||||
|
||||
public ActionResult<Void> reset() {
|
||||
final String defaultName = dataStore.getStoredName(player);
|
||||
final String defaultName = nameStore.getStoredName(player);
|
||||
this.profile.setName(defaultName);
|
||||
this.profile.setSkin(defaultName);
|
||||
final ActionResult<Void> actionResult = resetPlayer();
|
||||
|
|
|
@ -7,6 +7,7 @@ import net.artelnatif.nicko.disguise.NickoProfile;
|
|||
import net.artelnatif.nicko.i18n.I18N;
|
||||
import net.artelnatif.nicko.i18n.I18NDict;
|
||||
import net.artelnatif.nicko.storage.PlayerDataStore;
|
||||
import net.artelnatif.nicko.storage.name.PlayerNameStore;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
|
@ -17,15 +18,17 @@ public class PlayerJoinListener implements Listener {
|
|||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
final PlayerDataStore dataStore = NickoBukkit.getInstance().getDataStore();
|
||||
dataStore.storeName(player);
|
||||
final NickoBukkit instance = NickoBukkit.getInstance();
|
||||
|
||||
final PlayerDataStore dataStore = instance.getDataStore();
|
||||
final PlayerNameStore nameStore = instance.getNameStore();
|
||||
nameStore.storeName(player);
|
||||
|
||||
// TODO: 2/20/23 BungeeCord transfer
|
||||
|
||||
dataStore.performProfileUpdate(player.getUniqueId(), NickoProfile.EMPTY_PROFILE);
|
||||
Bukkit.getScheduler().runTaskLater(NickoBukkit.getInstance(), () -> {
|
||||
Bukkit.getScheduler().runTaskLater(instance, () -> {
|
||||
final AppearanceManager appearanceManager = AppearanceManager.get(player);
|
||||
|
||||
if (appearanceManager.hasData()) {
|
||||
final ActionResult<Void> actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
|
||||
if (!actionResult.isError()) {
|
||||
|
|
|
@ -19,31 +19,12 @@ public class PlayerDataStore {
|
|||
private final Storage storage;
|
||||
private final MojangAPI mojangAPI;
|
||||
private final HashMap<UUID, NickoProfile> profiles = new HashMap<>();
|
||||
private final HashMap<UUID, String> names = new HashMap<>();
|
||||
|
||||
public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) {
|
||||
this.mojangAPI = mojangAPI;
|
||||
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) {
|
||||
if (!profiles.containsKey(uuid)) {
|
||||
profiles.put(uuid, profile);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -36,6 +36,7 @@ public class CacheStorageTest {
|
|||
public void cachePlayerData() {
|
||||
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
|
||||
assertTrue(optionalProfile.isPresent());
|
||||
//assertTrue(plugin.getDataStore().isCached(player.getUniqueId()));
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
|
|
Loading…
Reference in a new issue