package net.crystalyx.bukkit.simplyperms.io; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import net.crystalyx.bukkit.simplyperms.SimplyPlugin; public class ConfigFile implements PermsConfig { private SimplyPlugin plugin; public ConfigFile(SimplyPlugin plugin) { this.plugin = plugin; } @Override public void removePlayer(String player) { plugin.getConfig().set("users/" + player, null); } @Override public void removePlayerGroups(String player) { plugin.getConfig().set("users/" + player + "/groups", null); } @Override public void removePlayerGroup(String player, String group) { List groups = getPlayerGroups(player); groups.remove(group); plugin.getConfig().set("users/" + player + "/groups", (groups.isEmpty()) ? null : groups); } @Override public void addPlayerGroup(String player, String group) { if (group.isEmpty()) return; List groups = getPlayerGroups(player); if (!groups.contains(group)) groups.add(group); plugin.getConfig().set("users/" + player + "/groups", groups); } @Override public void addPlayerPermission(String player, String permission, boolean value) { addPlayerPermission(player, "", permission, value); } @Override public void addPlayerPermission(String player, String world, String permission, boolean value) { Map permissions = getPlayerPermissions(player, world); if (permissions.containsKey(permission)) permissions.remove(permission); permissions.put(permission, value); if (!world.isEmpty()) { plugin.getConfig().set("users/" + player + "/worlds/" + world, permissions); } else { plugin.getConfig().set("users/" + player + "/permissions", permissions); } } @Override public void removePlayerPermissions(String player) { plugin.getConfig().set("users/" + player + "/permissions", null); } @Override public void removePlayerPermission(String player, String permission) { removePlayerPermission(player, "", permission); } @Override public void removePlayerPermission(String player, String world, String permission) { Map permissions = getPlayerPermissions(player, world); permissions.remove(permission); if (!world.isEmpty()) { plugin.getConfig().set("users/" + player + "/worlds/" + world, (permissions.isEmpty()) ? null : permissions); } else { plugin.getConfig().set("users/" + player + "/permissions", (permissions.isEmpty()) ? null : permissions); } } @Override public List getPlayers(String group) { List players = new ArrayList(); for (String player : getAllPlayers()) { for (String groupName : getPlayerGroups(player)) { if (groupName.equals(group)) { players.add(player); } } } return players; } @Override public List getPlayerGroups(String player) { return plugin.getConfig().getStringList("users/" + player + "/groups"); } @Override public Map getPlayerPermissions(String player) { return getPlayerPermissions(player, ""); } @Override public Map getPlayerPermissions(String player, String world) { Map finalPerms = new HashMap(); String permNode = (!world.isEmpty()) ? "users/" + player + "/worlds/" + world : "users/" + player + "/permissions"; if (plugin.getNode(permNode) != null) { for (Entry permPlayer : plugin.getNode(permNode).getValues(false).entrySet()) { finalPerms.put(permPlayer.getKey(), (Boolean) permPlayer.getValue()); } } return finalPerms; } @Override public boolean isPlayerInDB(String player) { return plugin.getNode("users/" + player) != null; } @Override public List getPlayerWorlds(String player) { if (plugin.getNode("users/" + player + "/worlds") != null) { return new ArrayList(plugin.getNode("users/" + player + "/worlds").getKeys(false)); } else { return new ArrayList(); } } @Override public List getAllPlayers() { if (plugin.getNode("users") != null) { return new ArrayList(plugin.getNode("users").getKeys(false)); } else { return new ArrayList(); } } }