feat: add PlayerCache for Client Options
This commit is contained in:
		
							parent
							
								
									774ac9b9bd
								
							
						
					
					
						commit
						19eb0fe80a
					
				
					 2 changed files with 79 additions and 3 deletions
				
			
		
							
								
								
									
										73
									
								
								varo/src/main/java/de/cliffbreak/varo/PlayerCache.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								varo/src/main/java/de/cliffbreak/varo/PlayerCache.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -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,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -17,6 +17,7 @@ import de.cliffbreak.varo.listeners.BannedItemListener;
 | 
				
			||||||
import de.cliffbreak.varo.listeners.ChatListener;
 | 
					import de.cliffbreak.varo.listeners.ChatListener;
 | 
				
			||||||
import de.cliffbreak.varo.listeners.CreatureSpawnListener;
 | 
					import de.cliffbreak.varo.listeners.CreatureSpawnListener;
 | 
				
			||||||
import de.cliffbreak.varo.listeners.EntityRegainHealthListener;
 | 
					import de.cliffbreak.varo.listeners.EntityRegainHealthListener;
 | 
				
			||||||
 | 
					import de.cliffbreak.varo.listeners.PlayerClientOptionsChangeListener;
 | 
				
			||||||
import de.cliffbreak.varo.listeners.PlayerDeathListener;
 | 
					import de.cliffbreak.varo.listeners.PlayerDeathListener;
 | 
				
			||||||
import de.cliffbreak.varo.listeners.PlayerJoinQuitListener;
 | 
					import de.cliffbreak.varo.listeners.PlayerJoinQuitListener;
 | 
				
			||||||
import de.cliffbreak.varo.listeners.PlayerPreLoginListener;
 | 
					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 File configurationFile = new File("plugins/CliffbreakVaro", "config.yml");
 | 
				
			||||||
    public FileConfiguration config = YamlConfiguration.loadConfiguration(this.configurationFile);
 | 
					    public FileConfiguration config = YamlConfiguration.loadConfiguration(this.configurationFile);
 | 
				
			||||||
 | 
					    public PlayerCache playerCache = new PlayerCache(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onEnable() {
 | 
					    public void onEnable() {
 | 
				
			||||||
| 
						 | 
					@ -36,15 +38,14 @@ public class Varo extends JavaPlugin {
 | 
				
			||||||
        this.config.options().copyDefaults(true);
 | 
					        this.config.options().copyDefaults(true);
 | 
				
			||||||
        this.saveConfiguration();
 | 
					        this.saveConfiguration();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // this.config.getStringList(path)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        getServer().getPluginManager().registerEvents(new PlayerPreLoginListener(this), this);
 | 
					        getServer().getPluginManager().registerEvents(new PlayerPreLoginListener(this), this);
 | 
				
			||||||
        getServer().getPluginManager().registerEvents(new PlayerJoinQuitListener(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 ChatListener(), this);
 | 
				
			||||||
        getServer().getPluginManager().registerEvents(new EntityRegainHealthListener(), this);
 | 
					        getServer().getPluginManager().registerEvents(new EntityRegainHealthListener(), this);
 | 
				
			||||||
        getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this);
 | 
					        getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this);
 | 
				
			||||||
        getServer().getPluginManager().registerEvents(new BannedItemListener(), 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));
 | 
					        getCommand("varo").setExecutor(new VaroCommand(this));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,6 +60,8 @@ public class Varo extends JavaPlugin {
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onDisable() {
 | 
					    public void onDisable() {
 | 
				
			||||||
        getLogger().info("Stopping CliffbreakVaro!");
 | 
					        getLogger().info("Stopping CliffbreakVaro!");
 | 
				
			||||||
 | 
					        saveConfiguration();
 | 
				
			||||||
 | 
					        this.playerCache.shutdown();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void saveConfiguration() {
 | 
					    public void saveConfiguration() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue