So I am using a hashmap and config for deathbans in a plugin that i am making and for some reason it is not loading/saving through the reload. Here is where I load & save Code (Text): public static final HashMap<UUID, Long> banned = new HashMap<UUID, Long>(); public void setupBanManager() { try { this.file2 = new File(getDataFolder() + File.separator + "bans.yml"); this.yaml2 = YamlConfiguration.loadConfiguration(this.file2); if (file2.exists()) { try { for (String uuid : getDeathBans().getYaml() .getConfigurationSection("uuids").getKeys(false)) { UUID u = UUID.fromString(uuid); Long l = getDeathBans().getYaml().getLong(u + ".long"); banned.put(u, l); } } catch (NullPointerException ignored) { // empty block catch } } } catch (Exception e) { e.printStackTrace(); } } public void startBanCycle() { Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { @Override public void run() { if (!banned.isEmpty()) { for (String uuid : getDeathBans().getYaml() .getConfigurationSection("uuids").getKeys(false)) { UUID u = UUID.fromString(uuid); Long l = banned.get(u); banned.put(u, l - 1); Long update = banned.get(u); if (update <= 0) { banned.remove(u); getDeathBans().getYaml().set("uuids" + "." + u, null); getDeathBans().reload(); } } } } }, 20, 20); } public void onDisable() { if (!banned.isEmpty()) { for (UUID u : banned.keySet()) { Long l = banned.get(u); this.yaml2.set("uuids." + u.toString(), l); saveBans(); } } } //In onEnable I have this setupBanManager(); this.dManager = new DeathBans(yaml2, file2); startBanCycle(); saveBans();
Here is the DeathBans class too if it helps Code (Text): public class DeathBans { private YamlConfiguration yaml; private File file; public DeathBans(YamlConfiguration yaml, File file) { this.file = file; this.yaml = yaml; } public YamlConfiguration getYaml() { return this.yaml; } public File getFile() { return this.file; } public void addDeathBan(Player p, long time) { yaml.set("uuids." + p.getUniqueId().toString(), time); A.banned.put(p.getUniqueId(), time); save(); } public void unBan(OfflinePlayer p) { if (isBanned(p)) { yaml.set("uuids." + p.getUniqueId().toString(), null); save(); } } public boolean isBanned(OfflinePlayer p) { return yaml.contains("uuids." + p.getUniqueId().toString()); } public void reload() { save(); } private void save() { try { this.yaml.save(this.file); } catch (IOException e) { e.printStackTrace(); } } }
Writing everything to the same file will get laggy after a while, you might want to consider making a file for each player, when they are unbanned delete the file.
Wouldnt that save it then? but thats not the issue, it does not load the bans when the server is restarted or reloaded
You're setting stuff to the custom config. You then need to reload than config followed by saving it in that order, or else it does nothing like you are having happen.
So I added this Code (Text): public void reloadConfig() throws UnsupportedEncodingException { Reader defConfigStream = new InputStreamReader(A.getInstance() .getResource("bans.yml"), "UTF8"); try { save(); if (defConfigStream != null) { YamlConfiguration defConfig = YamlConfiguration .loadConfiguration(defConfigStream); yaml.setDefaults(defConfig); Bukkit.getServer().getConsoleSender() .sendMessage("Reloaded 'bans.yml'!"); } reloadConfig(); } catch (NullPointerException e) { Bukkit.getServer().getConsoleSender() .sendMessage("Null resource 'bans.yml'"); } } to my Deathbans class and put it everywhere where i save data but what about loading it. It seems to save fine but does not load into the hashmap
Now i get a NPE on this line Code (Text): Reader defConfigStream = new InputStreamReader(A.getInstance() .getResource("bans.yml"), "UTF8"); when I reload the server
Sorry if this counts as a bump but also now the timer does not tick for the deathban it stays at the time placed into the map
I suggest using the default ban system, Bukkit.getServer().getBanList(BanList.Type.NAME).addBan(String playerName, String reason, Date until, String source);
Thanks but I would prefer to not use the default ban system IF possible, I fixed the NPE error and it saves the time to the config onDisable but will not load it from the onEnable
You know, you really could just create an empty file with the banned player's UUID as the file name, and if that file exists, they are still banned. In order to know when the file should be deleted, just write a time stamp into the file, and check it on join. The rest is pretty straight forward.