From 19eb0fe80a14089f99626f4919457174a4c51cec Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 4 May 2020 18:33:45 +0200 Subject: [PATCH] feat: add PlayerCache for Client Options --- .../java/de/cliffbreak/varo/PlayerCache.java | 73 +++++++++++++++++++ .../main/java/de/cliffbreak/varo/Varo.java | 9 ++- 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/PlayerCache.java diff --git a/varo/src/main/java/de/cliffbreak/varo/PlayerCache.java b/varo/src/main/java/de/cliffbreak/varo/PlayerCache.java new file mode 100644 index 0000000..bb3fbd3 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/PlayerCache.java @@ -0,0 +1,73 @@ +package de.cliffbreak.varo; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.MainHand; +import org.bukkit.scheduler.BukkitRunnable; + +public class PlayerCache { + + private Varo plugin; + private File playerCacheFile = new File("plugins/CliffbreakVaro", "playercache.yml"); + private FileConfiguration playerCache = YamlConfiguration.loadConfiguration(this.playerCacheFile); + private boolean wasSaved = false; + + public PlayerCache(Varo plugin) { + this.plugin = plugin; + } + + public void setPlayerCache(final String uuid, PlayerCacheType type, Object value) { + this.playerCache.set("Players." + uuid + "." + type.toString(), value); + savePlayerCache(); + } + + public int getSkinParts(final String uuid) { + return (int) this.playerCache.get("Players." + uuid + "." + PlayerCacheType.SKINPARTS.toString()); + } + + public MainHand getMainHand(final String uuid) { + return MainHand.valueOf( + this.playerCache.get("Players." + uuid + "." + PlayerCacheType.MAINHAND.toString()).toString()); + } + + public byte getMainHandAsByte(final String uuid) { + if (getMainHand(uuid) == MainHand.RIGHT) + return (byte) 1; + else + return (byte) 0; + } + + public void shutdown() { + try { + plugin.getLogger().info("Saving PlayerCache..."); + this.playerCache.save(this.playerCacheFile); + } catch (final IOException e) { + e.printStackTrace(); + } + } + + private void savePlayerCache() { + if (!this.wasSaved) + try { + plugin.getLogger().info("Saving PlayerCache..."); + this.playerCache.save(this.playerCacheFile); + this.wasSaved = true; + new BukkitRunnable() { + + @Override + public void run() { + wasSaved = false; + } + }.runTaskLater(plugin, 20); // Only save once every second to prevent disklock + } catch (final IOException e) { + e.printStackTrace(); + } + } + + public enum PlayerCacheType { + SKINPARTS, MAINHAND, + } +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index d241b2c..3341ebc 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -17,6 +17,7 @@ import de.cliffbreak.varo.listeners.BannedItemListener; import de.cliffbreak.varo.listeners.ChatListener; import de.cliffbreak.varo.listeners.CreatureSpawnListener; import de.cliffbreak.varo.listeners.EntityRegainHealthListener; +import de.cliffbreak.varo.listeners.PlayerClientOptionsChangeListener; import de.cliffbreak.varo.listeners.PlayerDeathListener; import de.cliffbreak.varo.listeners.PlayerJoinQuitListener; import de.cliffbreak.varo.listeners.PlayerPreLoginListener; @@ -25,6 +26,7 @@ public class Varo extends JavaPlugin { public File configurationFile = new File("plugins/CliffbreakVaro", "config.yml"); public FileConfiguration config = YamlConfiguration.loadConfiguration(this.configurationFile); + public PlayerCache playerCache = new PlayerCache(this); @Override public void onEnable() { @@ -36,15 +38,14 @@ public class Varo extends JavaPlugin { this.config.options().copyDefaults(true); this.saveConfiguration(); - // this.config.getStringList(path) - getServer().getPluginManager().registerEvents(new PlayerPreLoginListener(this), this); getServer().getPluginManager().registerEvents(new PlayerJoinQuitListener(this), this); + getServer().getPluginManager().registerEvents(new PlayerClientOptionsChangeListener(this), this); getServer().getPluginManager().registerEvents(new ChatListener(), this); getServer().getPluginManager().registerEvents(new EntityRegainHealthListener(), this); getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this); getServer().getPluginManager().registerEvents(new BannedItemListener(), this); - getServer().getPluginManager().registerEvents(new CreatureSpawnListener(), this); + getServer().getPluginManager().registerEvents(new CreatureSpawnListener(this), this); getCommand("varo").setExecutor(new VaroCommand(this)); @@ -59,6 +60,8 @@ public class Varo extends JavaPlugin { @Override public void onDisable() { getLogger().info("Stopping CliffbreakVaro!"); + saveConfiguration(); + this.playerCache.shutdown(); } public void saveConfiguration() {