From 0e3ea2ab0f1fa19850d55fa60c933a8ddca7df69 Mon Sep 17 00:00:00 2001 From: ORelio Date: Thu, 25 Aug 2016 11:53:22 +0200 Subject: [PATCH] Save sit disable list between server restarts --- .../cnaude/chairs/commands/ChairsCommand.java | 4 +- src/com/cnaude/chairs/core/Chairs.java | 42 ++++++++++++++++++- .../chairs/listeners/TrySitEventListener.java | 2 +- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/com/cnaude/chairs/commands/ChairsCommand.java b/src/com/cnaude/chairs/commands/ChairsCommand.java index 638fd6c..847d872 100644 --- a/src/com/cnaude/chairs/commands/ChairsCommand.java +++ b/src/com/cnaude/chairs/commands/ChairsCommand.java @@ -41,10 +41,10 @@ public class ChairsCommand implements CommandExecutor { if (sender instanceof Player) { Player player = (Player) sender; if (args[0].equalsIgnoreCase("off")) { - plugin.sitDisabled.add(player.getName()); + plugin.sitDisabled.add(player.getUniqueId()); player.sendMessage(plugin.msgDisabled); } else if (args[0].equalsIgnoreCase("on")) { - plugin.sitDisabled.remove(player.getName()); + plugin.sitDisabled.remove(player.getUniqueId()); player.sendMessage(plugin.msgEnabled); } } diff --git a/src/com/cnaude/chairs/core/Chairs.java b/src/com/cnaude/chairs/core/Chairs.java index 8798d02..04f7129 100644 --- a/src/com/cnaude/chairs/core/Chairs.java +++ b/src/com/cnaude/chairs/core/Chairs.java @@ -1,9 +1,14 @@ package com.cnaude.chairs.core; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -26,7 +31,7 @@ import com.cnaude.chairs.vehiclearrow.NMSAccess; public class Chairs extends JavaPlugin { - public HashSet sitDisabled = new HashSet(); + public HashSet sitDisabled = new HashSet(); public ChairEffects chairEffects; public List allowedBlocks; public List validSigns; @@ -100,6 +105,7 @@ public class Chairs extends JavaPlugin { log = null; nmsaccess = null; psitdata = null; + saveSitDisabled(); } public void loadConfig() { @@ -157,6 +163,40 @@ public class Chairs extends JavaPlugin { logError(e.getMessage()); } } + + try { + File sitDisabledFile = new File(getDataFolder(), "sit-disabled.txt"); + if (sitDisabledFile.exists()) { + String line = null; + sitDisabled.clear(); + BufferedReader br = new BufferedReader(new FileReader(sitDisabledFile)); + while ((line = br.readLine()) != null) { + try { + sitDisabled.add(UUID.fromString(line)); + } catch (IllegalArgumentException e) { + /* Not a UUID */ + } + } + br.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void saveSitDisabled() { + try { + File sitDisabledFile = new File(getDataFolder(), "sit-disabled.txt"); + if (!sitDisabledFile.exists()) + sitDisabledFile.createNewFile(); + PrintWriter writer = new PrintWriter(sitDisabledFile, "UTF-8"); + writer.println("# The following players disabled Chairs for themselves"); + for (UUID uuid : sitDisabled) + writer.println(uuid.toString()); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } public void logInfo(String _message) { diff --git a/src/com/cnaude/chairs/listeners/TrySitEventListener.java b/src/com/cnaude/chairs/listeners/TrySitEventListener.java index 7b801a7..1505ed0 100644 --- a/src/com/cnaude/chairs/listeners/TrySitEventListener.java +++ b/src/com/cnaude/chairs/listeners/TrySitEventListener.java @@ -44,7 +44,7 @@ public class TrySitEventListener implements Listener { private boolean sitAllowed(Player player, Block block) { // Check for sitting disabled - if (plugin.sitDisabled.contains(player.getName())) { + if (plugin.sitDisabled.contains(player.getUniqueId())) { return false; }