public class BasePromotionTrack implements PromotionTrack { final File tracks = new File("plugins/bPermissions/tracks.yml"); final WorldManager wm = WorldManager.getInstance(); org.bukkit.configuration.file.YamlConfiguration config = new org.bukkit.configuration.file.YamlConfiguration(); Map<String, List<String>> trackmap = new HashMap<String, List<String>>(); public void load() { try { // Tidy up config = new org.bukkit.configuration.file.YamlConfiguration(); trackmap.clear(); // Then do your basic if exists checks if (!tracks.exists()) { tracks.getParentFile().mkdirs(); tracks.createNewFile(); } config.load(tracks); if (config.getKeys(false) == null || config.getKeys(false).size() == 0) { List<String> defTrack = new ArrayList<String>(); defTrack.add("default"); defTrack.add("moderator"); defTrack.add("admin"); config.set("default", defTrack); config.save(tracks); } else { Set<String> keys = config.getKeys(false); Map<String, Boolean> children = new HashMap<String, Boolean>(); if (keys != null && keys.size() > 0) { for (String key : keys) { children.put("tracks." + key.toLowerCase(), true); List<String> groups = config.getStringList(key); if (groups != null && groups.size() > 0) { trackmap.put(key.toLowerCase(), groups); } } } Permission perm = new Permission("tracks.*", PermissionDefault.OP, children); Permission permCheck = Bukkit.getServer().getPluginManager().getPermission("tracks.*"); if (permCheck != null) Bukkit.getServer().getPluginManager().removePermission(permCheck); Bukkit.getPluginManager().addPermission(perm); } } catch (Exception e) { e.printStackTrace(); } } @Override public void promote(String player, String track, String world) {} @Override public void demote(String player, String track, String world) {} @Override public boolean containsTrack(String track) { return trackmap.containsKey(track.toLowerCase()); } @Override public List<String> getGroups(String track) { return config.getStringList(track); } }
public class ImportManager { private WorldManager wm = WorldManager.getInstance(); private final JavaPlugin plugin; public ImportManager(JavaPlugin plugin) { this.plugin = plugin; } public void importPEX() throws Exception { File file = new File("plugins/PermissionsEx/permissions.yml"); // No point doing anything if the file doesn't exist if (!file.exists()) { System.err.println("File not exist"); return; } YamlConfiguration perm = new YamlConfiguration(); perm.load(file); World world = plugin.getServer().getWorlds().get(0); de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName()); ConfigurationSection users = perm.getConfigurationSection("users"); ConfigurationSection groups = perm.getConfigurationSection("groups"); if (users.getKeys(false) != null && users.getKeys(false).size() > 0) { for (String user : users.getKeys(false)) { List<String> g = users.getStringList(user + ".group"); List<String> p = users.getStringList(user + ".permissions"); User u = wd.getUser(user); // Remove the existing groups u.getGroupsAsString().clear(); // Add all the groups if (g != null && g.size() > 0) for (String gr : g) { u.addGroup(gr); } if (p != null && p.size() > 0) for (String pr : p) { if (pr.startsWith("-")) { u.addPermission(pr.replace("-", ""), false); } else { u.addPermission(pr, true); } } String prefix = users.getString(user + ".prefix"); if (prefix != null) u.setValue("prefix", prefix); String suffix = users.getString(user + ".suffix"); if (suffix != null) u.setValue("suffix", suffix); } } if (groups.getKeys(false) != null && groups.getKeys(false).size() > 0) { for (String group : groups.getKeys(false)) { if (groups.getBoolean(group + ".default")) { wd.setDefaultGroup(group); System.out.println("DEFAULT GROUP DETECTED: " + group); } List<String> g = groups.getStringList(group + ".inheritance"); List<String> p = groups.getStringList(group + ".permissions"); Group u = wd.getGroup(group); // Remove the existing groups u.getGroupsAsString().clear(); // Add all the groups if (g != null && g.size() > 0) for (String gr : g) { u.addGroup(gr); } if (p != null && p.size() > 0) for (String pr : p) { if (pr.startsWith("-")) { u.addPermission(pr.replace("-", ""), false); } else { u.addPermission(pr, true); } } String prefix = groups.getString(group + ".prefix"); if (prefix != null) u.setValue("prefix", prefix); String suffix = groups.getString(group + ".suffix"); if (suffix != null) u.setValue("suffix", suffix); String priority = groups.getString(group + ".options.rank"); if (priority != null) u.setValue("priority", priority); } } } public void importYML() throws Exception { for (World world : plugin.getServer().getWorlds()) { de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName()); File perms = new File("plugins/bPermissions/worlds/" + world.getName() + ".yml"); if (perms.exists()) { System.out.println("Importing world: " + world.getName()); YamlConfiguration pConfig = new YamlConfiguration(); // new Configuration(perms); pConfig.load(perms); // Here we grab the different bits and bobs ConfigurationSection users = pConfig.getConfigurationSection("players"); ConfigurationSection groups = pConfig.getConfigurationSection("groups"); // Load users if (users != null && users.getKeys(false) != null && users.getKeys(false).size() > 0) { Set<String> u = users.getKeys(false); for (String usr : u) { System.out.println("Importing user: "******"Importing group: " + grp); List<String> p = groups.getStringList(grp); if (p != null && p.size() > 0) for (String perm : p) wd.getGroup(grp).getPermissions().add(Permission.loadFromString(perm)); } } } // Forgot to save after importing! wd.save(); } wm.cleanup(); } public void importGroupManager() throws Exception { for (World world : plugin.getServer().getWorlds()) { de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName()); File users = new File("plugins/GroupManager/worlds/" + world.getName() + "/users.yml"); File groups = new File("plugins/GroupManager/worlds/" + world.getName() + "/groups.yml"); if (users.exists() && groups.exists()) { System.out.println("Importing world: " + world.getName()); YamlConfiguration uConfig = new YamlConfiguration(); YamlConfiguration gConfig = new YamlConfiguration(); try { uConfig.load(users); gConfig.load(groups); } catch (Exception e) { e.printStackTrace(); } ConfigurationSection usConfig = uConfig.getConfigurationSection("users"); ConfigurationSection grConfig = gConfig.getConfigurationSection("groups"); Set<String> usersList = null; if (usConfig != null) usersList = usConfig.getKeys(false); Set<String> groupsList = null; if (grConfig != null) groupsList = grConfig.getKeys(false); if (usersList != null) for (String player : usersList) { System.out.println("Importing user: "******"users." + player + ".permissions"); List<String> i = uConfig.getStringList("users." + player + ".subgroups"); i.add(uConfig.getString("users." + player + ".group")); String prefix = uConfig.getString("users." + player + ".info." + "prefix"); String suffix = uConfig.getString("users." + player + ".info." + "suffix"); if (p != null) user.getPermissions().addAll(Permission.loadFromString(p)); if (i != null) { user.getGroupsAsString().clear(); user.getGroupsAsString().addAll(i); } if (prefix != null) user.setValue("prefix", prefix); if (suffix != null) user.setValue("suffix", suffix); } catch (Exception e) { System.err.println("Error importing user: "******"Importing group: " + group); Group gr = wd.getGroup(group); try { List<String> p = gConfig.getStringList("groups." + group + ".permissions"); List<String> i = gConfig.getStringList("groups." + group + ".inheritance"); String prefix = gConfig.getString("groups." + group + ".info." + "prefix"); String suffix = gConfig.getString("groups." + group + ".info." + "suffix"); if (gConfig.getBoolean("groups." + group + ".default")) { wd.setDefaultGroup(group); System.out.println("DEFAULT GROUP DETECTED: " + group); } if (p != null) gr.getPermissions().addAll(Permission.loadFromString(p)); if (i != null) { List<String> fp = new ArrayList<String>(); for (int j = 0; j < i.size(); j++) { String fpp = i.get(j); if (fpp.startsWith("g:")) { // do nothing } else { fp.add(fpp); } } i.clear(); i.addAll(fp); gr.getGroupsAsString().addAll(i); } if (prefix != null) gr.setValue("prefix", prefix); if (suffix != null) gr.setValue("suffix", suffix); } catch (Exception e) { System.err.println("Error importing group: " + group); } } wd.save(); } } wm.cleanup(); } public void importPermissions3() throws Exception { for (World world : plugin.getServer().getWorlds()) { de.bananaco.bpermissions.api.World wd = wm.getWorld(world.getName()); File users = new File("plugins/Permissions/" + world.getName() + "/users.yml"); File groups = new File("plugins/Permissions/" + world.getName() + "/groups.yml"); if (users.exists() && groups.exists()) { System.out.println("Importing world: " + world.getName()); YamlConfiguration uConfig = new YamlConfiguration(); YamlConfiguration gConfig = new YamlConfiguration(); try { uConfig.load(users); gConfig.load(groups); } catch (Exception e) { e.printStackTrace(); } ConfigurationSection usConfig = uConfig.getConfigurationSection("users"); ConfigurationSection grConfig = gConfig.getConfigurationSection("groups"); Set<String> usersList = null; if (usConfig != null) usersList = usConfig.getKeys(false); Set<String> groupsList = null; if (grConfig != null) groupsList = grConfig.getKeys(false); if (usersList != null) for (String player : usersList) { System.out.println("Importing user: "******"users." + player + ".permissions"); List<String> i = uConfig.getStringList("users." + player + ".groups"); String prefix = uConfig.getString("users." + player + ".info." + "prefix"); String suffix = uConfig.getString("users." + player + ".info." + "suffix"); if (p != null) user.getPermissions().addAll(Permission.loadFromString(p)); if (i != null) { user.getGroupsAsString().clear(); user.getGroupsAsString().addAll(i); } if (prefix != null) user.setValue("prefix", prefix); if (suffix != null) user.setValue("suffix", suffix); } catch (Exception e) { System.err.println("Error importing user: "******"Importing group: " + group); Group gr = wd.getGroup(group); try { List<String> p = gConfig.getStringList("groups." + group + ".permissions"); List<String> i = gConfig.getStringList("groups." + group + ".inheritance"); String prefix = gConfig.getString("groups." + group + ".info." + "prefix"); String suffix = gConfig.getString("groups." + group + ".info." + "suffix"); if (p != null) gr.getPermissions().addAll(Permission.loadFromString(p)); if (i != null) gr.getGroupsAsString().addAll(i); if (prefix != null) gr.setValue("prefix", prefix); if (suffix != null) gr.setValue("suffix", suffix); } catch (Exception e) { System.err.println("Error importing group: " + group); } } wd.save(); } } wm.cleanup(); } }
/** * This creates and propagates the default users.yml and groups.yml * * <p>This will be filled on the command /permissions helpme (yes, we brought it back) */ public class DefaultWorld extends World { private static final String GROUPS = "groups"; private static final String PERMISSIONS = "permissions"; private static final String META = "meta"; private static final String USERS = "users"; private YamlConfiguration uconfig = new YamlConfiguration();; private YamlConfiguration gconfig = new YamlConfiguration();; private final File ufile = new File("plugins/bPermissions/users.yml"); private final File gfile = new File("plugins/bPermissions/groups.yml"); protected final Permissions permissions; private final WorldManager wm = WorldManager.getInstance(); public DefaultWorld(Permissions permissions) { super("*"); this.permissions = permissions; } @Override public String getDefaultGroup() { if (gconfig != null) return gconfig.getString("default", "default"); return "default"; } public boolean load() { try { clear(); loadUnsafe(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public void test() throws Exception { if (!ufile.exists()) { if (ufile.getParentFile() != null) ufile.getParentFile().mkdirs(); ufile.createNewFile(); gfile.createNewFile(); } else { uconfig = new YamlConfiguration(); gconfig = new YamlConfiguration(); uconfig.load(ufile); gconfig.load(gfile); } } private void loadUnsafe() throws Exception { boolean autoSave = wm.getAutoSave(); wm.setAutoSave(false); if (!ufile.exists()) { if (ufile.getParentFile() != null) ufile.getParentFile().mkdirs(); ufile.createNewFile(); gfile.createNewFile(); } else { uconfig = new YamlConfiguration(); gconfig = new YamlConfiguration(); uconfig.load(ufile); gconfig.load(gfile); } /* * Load the users */ ConfigurationSection usersConfig = uconfig.getConfigurationSection(USERS); if (usersConfig != null) { Set<String> names = usersConfig.getKeys(false); for (String name : names) { List<String> nPerm = usersConfig.getStringList(name + "." + PERMISSIONS); List<String> nGroup = usersConfig.getStringList(name + "." + GROUPS); Set<Permission> perms = Permission.loadFromString(nPerm); // Create the new user User user = new User(name, nGroup, perms, getName(), this); // MetaData ConfigurationSection meta = usersConfig.getConfigurationSection(name + "." + META); if (meta != null) { Set<String> keys = meta.getKeys(false); if (keys != null && keys.size() > 0) for (String key : keys) user.setValue(key, meta.get(key).toString()); } // Upload to API add(user); } } /* * Load the groups */ ConfigurationSection groupsConfig = gconfig.getConfigurationSection(GROUPS); if (groupsConfig != null) { Set<String> names = groupsConfig.getKeys(false); for (String name : names) { List<String> nPerm = groupsConfig.getStringList(name + "." + PERMISSIONS); List<String> nGroup = groupsConfig.getStringList(name + "." + GROUPS); Set<Permission> perms = Permission.loadFromString(nPerm); // Create the new group Group group = new Group(name, nGroup, perms, getName(), this); // MetaData ConfigurationSection meta = groupsConfig.getConfigurationSection(name + "." + META); if (meta != null) { Set<String> keys = meta.getKeys(false); if (keys != null && keys.size() > 0) for (String key : keys) group.setValue(key, meta.get(key).toString()); } // Upload to API add(group); } } // for (Calculable user : getAll(CalculableType.USER)) { // try { // user.calculateEffectivePermissions(); // user.calculateEffectiveMeta(); // } catch (RecursiveGroupException e) { // System.err.println(e.getMessage()); // } // } for (Player player : this.permissions.getServer().getOnlinePlayers()) { String name = player.getName(); String world = player.getWorld().getName(); if (wm.getWorld(world) == this) { getUser(name).calculateEffectivePermissions(); } } wm.setAutoSave(autoSave); } public boolean save() { try { saveUnsafe(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } private void saveUnsafe() throws Exception { if (!ufile.exists()) { ufile.getParentFile().mkdirs(); ufile.createNewFile(); gfile.createNewFile(); } String def = getDefaultGroup(); uconfig = new YamlConfiguration(); gconfig = new YamlConfiguration(); gconfig.set("default", def); Set<Calculable> usr = getAll(CalculableType.USER); // Sort them :D List<Calculable> users = new ArrayList<Calculable>(usr); MetaData.sort(users); for (Calculable user : users) { String name = user.getName(); uconfig.set(USERS + "." + name + "." + PERMISSIONS, user.serialisePermissions()); uconfig.set(USERS + "." + name + "." + GROUPS, user.serialiseGroups()); // MetaData Map<String, String> meta = user.getMeta(); if (meta.size() > 0) for (String key : meta.keySet()) uconfig.set(USERS + "." + name + "." + META + "." + key, meta.get(key)); } Set<Calculable> grp = getAll(CalculableType.GROUP); // Sort them :D List<Calculable> groups = new ArrayList<Calculable>(grp); MetaData.sort(groups); for (Calculable group : groups) { String name = group.getName(); gconfig.set(GROUPS + "." + name + "." + PERMISSIONS, group.serialisePermissions()); gconfig.set(GROUPS + "." + name + "." + GROUPS, group.serialiseGroups()); // MetaData Map<String, String> meta = group.getMeta(); if (meta.size() > 0) for (String key : meta.keySet()) gconfig.set(GROUPS + "." + name + "." + META + "." + key, meta.get(key)); } uconfig.save(ufile); gconfig.save(gfile); for (Player player : this.permissions.getServer().getOnlinePlayers()) { String name = player.getName(); String world = player.getWorld().getName(); if (wm.getWorld(world) == this) { getUser(name).calculateEffectivePermissions(); } } } @Override public boolean setupPlayer(String player) { return false; } @Override public void setDefaultGroup(String group) { gconfig.set("default", group); try { gconfig.save(gfile); } catch (IOException e) { } } }