Code refractoring

This commit is contained in:
Michel Roux 2012-05-04 23:23:37 +02:00
parent 39c125d1a3
commit b19fd41b39
8 changed files with 345 additions and 100 deletions

View File

@ -1,12 +1,7 @@
package net.crystalyx.bukkit.simplyperms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.configuration.file.YamlConfiguration;
import net.crystalyx.bukkit.simplyperms.io.PermsConfig;
@ -98,148 +93,103 @@ public class SimplyAPI implements PermsConfig {
return plugin.config.getAllPlayers();
}
@Override
public List<String> getAllGroups() {
if (plugin.getNode("groups") != null) {
return new ArrayList<String>(plugin.getNode("groups").getKeys(false));
}
else {
return new ArrayList<String>();
}
return plugin.config.getAllGroups();
}
@Override
public List<String> getGroupWorlds(String group) {
if (group.isEmpty()) group = getDefaultGroup();
if (plugin.getNode("groups/" + group + "/worlds") != null) {
return new ArrayList<String>(plugin.getNode("groups/" + group + "/worlds").getKeys(false));
}
else {
return new ArrayList<String>();
}
return plugin.config.getGroupWorlds(group);
}
@Override
public List<String> getGroupInheritance(String group) {
if (group.isEmpty()) group = getDefaultGroup();
return plugin.getConfig().getStringList("groups/" + group + "/inheritance");
return plugin.config.getGroupInheritance(group);
}
@Override
public void addGroupInheritance(String group, String inherit) {
if (group.isEmpty()) group = getDefaultGroup();
List<String> inheritances = getGroupInheritance(group);
if (!inheritances.contains(inherit)) inheritances.add(inherit);
plugin.getConfig().set("groups/" + group + "/inheritance", inheritances);
plugin.config.addGroupInheritance(group, inherit);
}
@Override
public void removeGroupInheritance(String group, String inherit) {
if (group.isEmpty()) group = getDefaultGroup();
List<String> inheritances = getGroupInheritance(group);
inheritances.remove(inherit);
plugin.getConfig().set("groups/" + group + "/inheritance", inheritances);
plugin.config.removeGroupInheritance(group, inherit);
}
@Override
public void removeGroupInheritances(String group) {
if (group.isEmpty()) group = getDefaultGroup();
plugin.getConfig().set("groups/" + group + "/inheritance", null);
plugin.config.removeGroupInheritances(group);
}
@Override
public Map<String, Boolean> getGroupPermissions(String group, String world) {
if (group.isEmpty()) group = getDefaultGroup();
Map<String, Boolean> finalPerms = new HashMap<String, Boolean>();
String permNode = (!world.isEmpty()) ? "groups/" + group + "/worlds/" + world : "groups/" + group + "/permissions";
if (plugin.getNode(permNode) != null) {
for (Entry<String, Object> permGroup : plugin.getNode(permNode).getValues(false).entrySet()) {
finalPerms.put(permGroup.getKey(), (Boolean) permGroup.getValue());
}
}
return finalPerms;
return plugin.config.getGroupPermissions(group, world);
}
@Override
public Map<String, Boolean> getGroupPermissions(String group) {
return getGroupPermissions(group, "");
return plugin.config.getGroupPermissions(group);
}
@Override
public void addGroupPermission(String group, String world, String permission, boolean value) {
if (group.isEmpty()) group = getDefaultGroup();
Map<String, Boolean> permissions = getGroupPermissions(group, world);
if (permissions.containsKey(permission)) permissions.remove(permission);
permissions.put(permission, value);
if (!world.isEmpty()) {
plugin.getConfig().set("groups/" + group + "/worlds/" + world, permissions);
}
else {
plugin.getConfig().set("groups/" + group + "/permissions", permissions);
}
plugin.config.addGroupPermission(group, world, permission, value);
}
@Override
public void addGroupPermission(String group, String permission, boolean value) {
addGroupPermission(group, "", permission, value);
plugin.config.addGroupPermission(group, permission, value);
}
@Override
public void removeGroupPermission(String group, String world, String permission) {
if (group.isEmpty()) group = getDefaultGroup();
Map<String, Boolean> permissions = getGroupPermissions(group, world);
permissions.remove(permission);
if (!world.isEmpty()) {
plugin.getConfig().set("groups/" + group + "/worlds/" + world, (permissions.isEmpty()) ? null : permissions);
}
else {
plugin.getConfig().set("groups/" + group + "/permissions", (permissions.isEmpty()) ? null : permissions);
}
plugin.config.removeGroupPermission(group, world, permission);
}
@Override
public void removeGroupPermission(String group, String permission) {
removeGroupPermission(group, "", permission);
plugin.config.removeGroupPermission(group, permission);
}
@Override
public void removeGroupPermissions(String group) {
if (group.isEmpty()) group = getDefaultGroup();
plugin.getConfig().set("groups/" + group + "/permissions", null);
plugin.config.removeGroupPermissions(group);
}
@Override
public void removeGroup(String group) {
plugin.getConfig().set("groups/" + group, null);
plugin.config.removeGroup(group);
}
@Override
public Map<String, Object> getMessages() {
if (plugin.getNode("messages") != null) {
return plugin.getNode("messages").getValues(false);
}
else {
return new HashMap<String, Object>();
}
return plugin.config.getMessages();
}
@Override
public void addMessage(String key, String message) {
Map<String, Object> messages = getMessages();
if (!messages.containsKey(key)) messages.put(key, message);
plugin.getConfig().set("messages", messages);
plugin.config.addMessage(key, message);
}
@Override
public void removeMessage(String key) {
Map<String, Object> messages = getMessages();
messages.remove(key);
plugin.getConfig().set("messages", messages);
plugin.config.removeMessage(key);
}
@Override
public String getDefaultGroup() {
return plugin.getConfig().getString("default", "default");
return plugin.config.getDefaultGroup();
}
@Override
public void setDefaultGroup(String group) {
if (group.isEmpty()) group = "default";
plugin.getConfig().set("default", group);
plugin.config.setDefaultGroup(group);
}
public void refreshPermissions() {
plugin.refreshPermissions();
}
protected List<String> getKeys(YamlConfiguration config, String node) {
if (config.isConfigurationSection(node)) {
return new ArrayList<String>(config.getConfigurationSection(node).getKeys(false));
}
else {
return new ArrayList<String>();
}
}
}

View File

@ -2,6 +2,7 @@ package net.crystalyx.bukkit.simplyperms;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -204,6 +205,15 @@ public class SimplyPlugin extends JavaPlugin {
}
}
public List<String> getKeys(YamlConfiguration config, String node) {
if (config.isConfigurationSection(node)) {
return new ArrayList<String>(config.getConfigurationSection(node).getKeys(false));
}
else {
return new ArrayList<String>();
}
}
protected void calculateAttachment(Player player) {
if (player == null) {
return;

View File

@ -26,7 +26,7 @@ public class ImportPermBukkit extends SimplyAPI implements ImportManager {
plugin.getConfig().set("debug", permBukkit.getBoolean("debug"));
addMessage("build", permBukkit.getString("messages/build"));
for (String player : getKeys(permBukkit, "users")) {
for (String player : plugin.getKeys(permBukkit, "users")) {
for (String group : permBukkit.getStringList("users/" + player + "/groups")) {
addPlayerGroup(player, group);
}
@ -35,19 +35,19 @@ public class ImportPermBukkit extends SimplyAPI implements ImportManager {
addPlayerPermission(player, perm.getKey(), (Boolean) perm.getValue());
}
for (String world : getKeys(permBukkit, "users/" + player + "/worlds")) {
for (String world : plugin.getKeys(permBukkit, "users/" + player + "/worlds")) {
for (Entry<String, Object> worldperm : permBukkit.getConfigurationSection("users/" + player + "/worlds/" + world).getValues(false).entrySet()) {
addPlayerPermission(player, world, worldperm.getKey(), (Boolean) worldperm.getValue());
}
}
}
for (String group : getKeys(permBukkit, "groups")) {
for (String group : plugin.getKeys(permBukkit, "groups")) {
for (Entry<String, Object> perms : permBukkit.getConfigurationSection("groups/" + group + "/permissions").getValues(false).entrySet()) {
addGroupPermission(group, perms.getKey(), (Boolean) perms.getValue());
}
for (String world : getKeys(permBukkit, "groups/" + group + "/worlds")) {
for (String world : plugin.getKeys(permBukkit, "groups/" + group + "/worlds")) {
for (Entry<String, Object> worldperm : permBukkit.getConfigurationSection("groups/" + group + "/worlds/" + world).getValues(false).entrySet()) {
addGroupPermission(group, world, worldperm.getKey(), (Boolean) worldperm.getValue());
}

View File

@ -23,7 +23,7 @@ public class ImportPermEx extends SimplyAPI implements ImportManager {
plugin.getConfig().set("debug", permEx.getBoolean("permissions.debug"));
permEx.load("plugins/PermissionsEx/" + permEx.getString("permissions.backends.file.file"));
for (String player : getKeys(permEx, "users")) {
for (String player : plugin.getKeys(permEx, "users")) {
for (String group : permEx.getStringList("users." + player + ".group")) {
addPlayerGroup(player, group);
}
@ -32,19 +32,19 @@ public class ImportPermEx extends SimplyAPI implements ImportManager {
addPlayerPermission(player, perm.replace("-", ""), !perm.startsWith("-"));
}
for (String world : getKeys(permEx, "users." + player + ".permissions.worlds")) {
for (String world : plugin.getKeys(permEx, "users." + player + ".permissions.worlds")) {
for (String worldperm : permEx.getStringList("users." + player + ".permissions.worlds." + world)) {
addPlayerPermission(player, world, worldperm.replace("-", ""), !worldperm.startsWith("-"));
}
}
}
for (String group : getKeys(permEx, "groups")) {
for (String group : plugin.getKeys(permEx, "groups")) {
for (String perm : permEx.getStringList("groups." + group + ".permissions")) {
addGroupPermission(group, perm.replace("-", ""), !perm.startsWith("-"));
}
for (String world : getKeys(permEx, "groups." + group + ".permissions.worlds")) {
for (String world : plugin.getKeys(permEx, "groups." + group + ".permissions.worlds")) {
for (String worldperm : permEx.getStringList("groups." + group + ".permissions.worlds." + world)) {
addGroupPermission(group, world, worldperm.replace("-", ""), !worldperm.startsWith("-"));
}

View File

@ -24,14 +24,14 @@ public class ImportPrivileges extends SimplyAPI implements ImportManager {
plugin.getConfig().set("debug", privileges.getBoolean("debug"));
privileges.load("plugins/Privileges/users.yml");
for (String player : getKeys(privileges, "users")) {
for (String player : plugin.getKeys(privileges, "users")) {
addPlayerGroup(player, privileges.getString("users." + player + ".group"));
for (String permission : privileges.getStringList("users." + player + ".permissions")) {
addPlayerPermission(player, permission.replace("-", ""), !permission.startsWith("-"));
}
for (String world : getKeys(privileges, "users." + player + ".worlds")) {
for (String world : plugin.getKeys(privileges, "users." + player + ".worlds")) {
for (String worldpermission : privileges.getStringList("users." + player + ".worlds." + world)) {
addPlayerPermission(player, world, worldpermission.replace("-", ""), !worldpermission.startsWith("-"));
}
@ -39,12 +39,12 @@ public class ImportPrivileges extends SimplyAPI implements ImportManager {
}
privileges.load("plugins/Privileges/groups.yml");
for (String group : getKeys(privileges, "groups")) {
for (String group : plugin.getKeys(privileges, "groups")) {
for (String permission : privileges.getStringList("groups." + group + ".permissions")) {
addGroupPermission(group, permission.replace("-", ""), !permission.startsWith("-"));
}
for (String world : getKeys(privileges, "groups." + group + ".worlds")) {
for (String world : plugin.getKeys(privileges, "groups." + group + ".worlds")) {
for (String worldpermission : privileges.getStringList("groups." + group + ".worlds." + world)) {
addGroupPermission(group, world, worldpermission.replace("-", ""), !worldpermission.startsWith("-"));
}

View File

@ -141,4 +141,154 @@ public class ConfigFile implements PermsConfig {
}
}
@Override
public List<String> getAllGroups() {
if (plugin.getNode("groups") != null) {
return new ArrayList<String>(plugin.getNode("groups").getKeys(false));
}
else {
return new ArrayList<String>();
}
}
@Override
public List<String> getGroupWorlds(String group) {
if (group.isEmpty()) group = getDefaultGroup();
if (plugin.getNode("groups/" + group + "/worlds") != null) {
return new ArrayList<String>(plugin.getNode("groups/" + group + "/worlds").getKeys(false));
}
else {
return new ArrayList<String>();
}
}
@Override
public List<String> getGroupInheritance(String group) {
if (group.isEmpty()) group = getDefaultGroup();
return plugin.getConfig().getStringList("groups/" + group + "/inheritance");
}
@Override
public void addGroupInheritance(String group, String inherit) {
if (group.isEmpty()) group = getDefaultGroup();
List<String> inheritances = getGroupInheritance(group);
if (!inheritances.contains(inherit)) inheritances.add(inherit);
plugin.getConfig().set("groups/" + group + "/inheritance", inheritances);
}
@Override
public void removeGroupInheritance(String group, String inherit) {
if (group.isEmpty()) group = getDefaultGroup();
List<String> inheritances = getGroupInheritance(group);
inheritances.remove(inherit);
plugin.getConfig().set("groups/" + group + "/inheritance", inheritances);
}
@Override
public void removeGroupInheritances(String group) {
if (group.isEmpty()) group = getDefaultGroup();
plugin.getConfig().set("groups/" + group + "/inheritance", null);
}
@Override
public Map<String, Boolean> getGroupPermissions(String group, String world) {
if (group.isEmpty()) group = getDefaultGroup();
Map<String, Boolean> finalPerms = new HashMap<String, Boolean>();
String permNode = (!world.isEmpty()) ? "groups/" + group + "/worlds/" + world : "groups/" + group + "/permissions";
if (plugin.getNode(permNode) != null) {
for (Entry<String, Object> permGroup : plugin.getNode(permNode).getValues(false).entrySet()) {
finalPerms.put(permGroup.getKey(), (Boolean) permGroup.getValue());
}
}
return finalPerms;
}
@Override
public Map<String, Boolean> getGroupPermissions(String group) {
return getGroupPermissions(group, "");
}
@Override
public void addGroupPermission(String group, String world, String permission, boolean value) {
if (group.isEmpty()) group = getDefaultGroup();
Map<String, Boolean> permissions = getGroupPermissions(group, world);
if (permissions.containsKey(permission)) permissions.remove(permission);
permissions.put(permission, value);
if (!world.isEmpty()) {
plugin.getConfig().set("groups/" + group + "/worlds/" + world, permissions);
}
else {
plugin.getConfig().set("groups/" + group + "/permissions", permissions);
}
}
@Override
public void addGroupPermission(String group, String permission, boolean value) {
addGroupPermission(group, "", permission, value);
}
@Override
public void removeGroupPermission(String group, String world, String permission) {
if (group.isEmpty()) group = getDefaultGroup();
Map<String, Boolean> permissions = getGroupPermissions(group, world);
permissions.remove(permission);
if (!world.isEmpty()) {
plugin.getConfig().set("groups/" + group + "/worlds/" + world, (permissions.isEmpty()) ? null : permissions);
}
else {
plugin.getConfig().set("groups/" + group + "/permissions", (permissions.isEmpty()) ? null : permissions);
}
}
@Override
public void removeGroupPermission(String group, String permission) {
removeGroupPermission(group, "", permission);
}
@Override
public void removeGroupPermissions(String group) {
if (group.isEmpty()) group = getDefaultGroup();
plugin.getConfig().set("groups/" + group + "/permissions", null);
}
@Override
public void removeGroup(String group) {
plugin.getConfig().set("groups/" + group, null);
}
@Override
public Map<String, Object> getMessages() {
if (plugin.getNode("messages") != null) {
return plugin.getNode("messages").getValues(false);
}
else {
return new HashMap<String, Object>();
}
}
@Override
public void addMessage(String key, String message) {
Map<String, Object> messages = getMessages();
if (!messages.containsKey(key)) messages.put(key, message);
plugin.getConfig().set("messages", messages);
}
@Override
public void removeMessage(String key) {
Map<String, Object> messages = getMessages();
messages.remove(key);
plugin.getConfig().set("messages", messages);
}
@Override
public String getDefaultGroup() {
return plugin.getConfig().getString("default", "default");
}
@Override
public void setDefaultGroup(String group) {
if (group.isEmpty()) group = "default";
plugin.getConfig().set("default", group);
}
}

View File

@ -15,6 +15,7 @@ import net.crystalyx.bukkit.simplyperms.SimplyPlugin;
public class ConfigSQL implements PermsConfig {
private SimplyPlugin plugin;
private ConfigFile config;
private Connection connection;
private String table_players;
private String table_groups;
@ -27,6 +28,7 @@ public class ConfigSQL implements PermsConfig {
public ConfigSQL(SimplyPlugin plugin) {
this.plugin = plugin;
config = new ConfigFile(plugin);
table_players = plugin.getConfig().getString("db/table/players");
table_groups = plugin.getConfig().getString("db/table/groups");
column_playerid = plugin.getConfig().getString("db/column/playerid");
@ -327,4 +329,99 @@ public class ConfigSQL implements PermsConfig {
return results;
}
@Override
public List<String> getAllGroups() {
return config.getAllGroups();
}
@Override
public List<String> getGroupWorlds(String group) {
return config.getGroupWorlds(group);
}
@Override
public List<String> getGroupInheritance(String group) {
return config.getGroupInheritance(group);
}
@Override
public void addGroupInheritance(String group, String inherit) {
config.addGroupInheritance(group, inherit);
}
@Override
public void removeGroupInheritance(String group, String inherit) {
config.removeGroupInheritance(group, inherit);
}
@Override
public void removeGroupInheritances(String group) {
config.removeGroupInheritances(group);
}
@Override
public Map<String, Boolean> getGroupPermissions(String group, String world) {
return config.getGroupPermissions(group, world);
}
@Override
public Map<String, Boolean> getGroupPermissions(String group) {
return config.getGroupPermissions(group);
}
@Override
public void addGroupPermission(String group, String world, String permission, boolean value) {
config.addGroupPermission(group, world, permission, value);
}
@Override
public void addGroupPermission(String group, String permission, boolean value) {
config.addGroupPermission(group, permission, value);
}
@Override
public void removeGroupPermission(String group, String world, String permission) {
config.removeGroupPermission(group, world, permission);
}
@Override
public void removeGroupPermission(String group, String permission) {
config.removeGroupPermission(group, permission);
}
@Override
public void removeGroupPermissions(String group) {
config.removeGroupPermissions(group);
}
@Override
public void removeGroup(String group) {
config.removeGroup(group);
}
@Override
public Map<String, Object> getMessages() {
return config.getMessages();
}
@Override
public void addMessage(String key, String message) {
config.addMessage(key, message);
}
@Override
public void removeMessage(String key) {
config.removeMessage(key);
}
@Override
public String getDefaultGroup() {
return config.getDefaultGroup();
}
@Override
public void setDefaultGroup(String group) {
config.setDefaultGroup(group);
}
}

View File

@ -37,4 +37,42 @@ public interface PermsConfig {
public List<String> getAllPlayers();
public List<String> getAllGroups();
public List<String> getGroupWorlds(String group);
public List<String> getGroupInheritance(String group);
public void addGroupInheritance(String group, String inherit);
public void removeGroupInheritance(String group, String inherit);
public void removeGroupInheritances(String group);
public Map<String, Boolean> getGroupPermissions(String group, String world);
public Map<String, Boolean> getGroupPermissions(String group);
public void addGroupPermission(String group, String world, String permission, boolean value);
public void addGroupPermission(String group, String permission, boolean value);
public void removeGroupPermission(String group, String world, String permission);
public void removeGroupPermission(String group, String permission);
public void removeGroupPermissions(String group);
public void removeGroup(String group);
public Map<String, Object> getMessages();
public void addMessage(String key, String message);
public void removeMessage(String key);
public String getDefaultGroup();
public void setDefaultGroup(String group);
}