├── README ├── src └── main │ ├── resources │ ├── config.yml │ ├── users.yml │ ├── groups.yml │ └── plugin.yml │ └── java │ └── org │ └── anjocaido │ └── groupmanager │ ├── utils │ ├── GMLoggerHandler.java │ ├── GroupManagerPermissions.java │ ├── StringPermissionComparator.java │ ├── PermissionCheckResult.java │ └── Tasks.java │ ├── data │ ├── UserVariables.java │ ├── GroupVariables.java │ ├── DataUnit.java │ ├── Group.java │ ├── Variables.java │ └── User.java │ ├── permissions │ ├── PermissionsReaderInterface.java │ └── AnjoPermissionsHandler.java │ ├── GMConfiguration.java │ └── dataholder │ ├── OverloadedWorldHolder.java │ ├── worlds │ └── WorldsHolder.java │ └── WorldDataHolder.java └── pom.xml /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmcouto/GroupManager/HEAD/README -------------------------------------------------------------------------------- /src/main/resources/config.yml: -------------------------------------------------------------------------------- 1 | settings: 2 | data: 3 | save: 4 | minutes: 10 5 | logging: 6 | level: INFO 7 | permission: 8 | world: 9 | mirror: 10 | world1: 11 | - world2 12 | - world3 13 | world4: 14 | - world5 -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/utils/GMLoggerHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.utils; 6 | 7 | import java.util.logging.ConsoleHandler; 8 | import java.util.logging.Level; 9 | import java.util.logging.LogRecord; 10 | 11 | /** 12 | * 13 | * @author gabrielcouto 14 | */ 15 | public class GMLoggerHandler extends ConsoleHandler { 16 | 17 | @Override 18 | public void publish(LogRecord record) { 19 | String message = "GroupManager - " + record.getLevel() + " - " + record.getMessage(); 20 | if (record.getLevel().equals(Level.SEVERE) || record.getLevel().equals(Level.WARNING)) { 21 | System.err.println(message); 22 | } else { 23 | System.out.println(message); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/utils/GroupManagerPermissions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.utils; 6 | 7 | /** 8 | * Just a list of commands for this plugin 9 | * @author gabrielcouto 10 | */ 11 | public enum GroupManagerPermissions { 12 | manuadd, 13 | manudel, 14 | manuaddsub, 15 | manudelsub, 16 | mangadd, 17 | mangdel, 18 | manuaddp, 19 | manudelp, 20 | manulistp, 21 | manucheckp, 22 | mangaddp, 23 | mangdelp, 24 | manglistp, 25 | mangcheckp, 26 | mangaddi, 27 | mangdeli, 28 | manuaddv, 29 | manudelv, 30 | manulistv, 31 | manucheckv, 32 | mangaddv, 33 | mangdelv, 34 | manglistv, 35 | mangcheckv, 36 | manwhois, 37 | tempadd, 38 | tempdel, 39 | templist, 40 | tempdelall, 41 | mansave, 42 | manload, 43 | listgroups, 44 | manpromote, 45 | mandemote, 46 | mantogglevalidate, 47 | mantogglesave, 48 | manworld, 49 | manselect, 50 | manclear 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/data/UserVariables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.anjocaido.groupmanager.data; 7 | 8 | import java.util.Map; 9 | 10 | /** 11 | * 12 | * @author gabrielcouto 13 | */ 14 | public class UserVariables extends Variables{ 15 | private User owner; 16 | public UserVariables(User owner){ 17 | super(owner); 18 | this.owner = owner; 19 | } 20 | public UserVariables(User owner, Map varList) { 21 | super(owner); 22 | this.variables = varList; 23 | this.owner = owner; 24 | } 25 | /** 26 | * A clone of all vars here. 27 | * @return 28 | */ 29 | protected UserVariables clone(User newOwner) { 30 | UserVariables clone = new UserVariables(newOwner); 31 | for (String key : variables.keySet()) { 32 | clone.variables.put(key, variables.get(key)); 33 | } 34 | newOwner.flagAsChanged(); 35 | return clone; 36 | } 37 | /** 38 | * @return the owner 39 | */ 40 | @Override 41 | public User getOwner() { 42 | return owner; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/resources/users.yml: -------------------------------------------------------------------------------- 1 | users: 2 | aMiner: 3 | subgroups: 4 | - Miner 5 | permissions: [] 6 | group: BlueFaction 7 | info: 8 | prefix: '&d' 9 | suffix: Miner 10 | gmcouto: 11 | subgroups: [] 12 | permissions: [] 13 | group: SemiAdmin 14 | tempRailer: 15 | subgroups: 16 | - Miner 17 | - Railer 18 | permissions: [] 19 | group: BlueFaction 20 | info: 21 | prefix: '&d' 22 | suffix: Miner 23 | anjocaido: 24 | subgroups: [] 25 | permissions: [] 26 | group: Admin 27 | info: 28 | prefix: '&c' 29 | suffix: King 30 | aFarmer: 31 | subgroups: 32 | - Farmer 33 | permissions: [] 34 | group: RedFaction 35 | info: 36 | prefix: '&d' 37 | suffix: Farmer 38 | zenexer: 39 | subgroups: [] 40 | permissions: 41 | - essentials.god 42 | group: Moderator 43 | Teste: 44 | subgroups: 45 | - Miner 46 | - Railer 47 | - SuperCart 48 | - Fighter 49 | - FlyingMan 50 | permissions: [] 51 | group: BlueFaction 52 | aHealer: 53 | subgroups: 54 | - Healer 55 | permissions: [] 56 | group: RedFaction 57 | info: 58 | prefix: '&d' 59 | suffix: Healer 60 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/utils/StringPermissionComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.anjocaido.groupmanager.utils; 7 | 8 | import java.util.Comparator; 9 | 10 | /** 11 | * 12 | * @author gabrielcouto 13 | */ 14 | public class StringPermissionComparator implements Comparator { 15 | 16 | @Override 17 | public int compare(String permA, String permB) { 18 | boolean ap = permA.startsWith("+"); 19 | boolean bp = permB.startsWith("+"); 20 | boolean am = permA.startsWith("-"); 21 | boolean bm = permB.startsWith("-"); 22 | if(ap&&bp){ 23 | return 0; 24 | } 25 | if(ap&&!bp){ 26 | return -1; 27 | } 28 | if(!ap&&bp){ 29 | return 1; 30 | } 31 | if(am&&bm){ 32 | return 0; 33 | } 34 | if(am&&!bm){ 35 | return -1; 36 | } 37 | if(!am&&bm){ 38 | return 1; 39 | } 40 | return permA.compareToIgnoreCase(permB); 41 | } 42 | private static StringPermissionComparator instance; 43 | public static StringPermissionComparator getInstance(){ 44 | if(instance==null){ 45 | instance = new StringPermissionComparator(); 46 | } 47 | return instance; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/utils/PermissionCheckResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.utils; 6 | 7 | import org.anjocaido.groupmanager.data.DataUnit; 8 | 9 | /** 10 | * 11 | * @author gabrielcouto 12 | */ 13 | public class PermissionCheckResult { 14 | /** 15 | * It should be the owner of the access level found. 16 | * 17 | * Use instanceof to find the owner type 18 | */ 19 | public DataUnit owner; 20 | /** 21 | * The permission node found in the DataUnit. 22 | */ 23 | public String accessLevel; 24 | /** 25 | * The full name of the permission you are looking for 26 | */ 27 | public String askedPermission; 28 | /** 29 | * The result conclusion of the search. 30 | * It determines if the owner can do, or not. 31 | * 32 | * It even determines if it has an owner. 33 | */ 34 | public Type resultType = Type.NOTFOUND; 35 | 36 | /** 37 | * The type of result the search can give. 38 | */ 39 | public enum Type { 40 | 41 | /** 42 | * If found a matching node starting with '+'. 43 | * It means the user CAN do the permission. 44 | */ 45 | EXCEPTION, 46 | /** 47 | * If found a matching node starting with '-'. 48 | * It means the user CANNOT do the permission. 49 | */ 50 | NEGATION, 51 | /** 52 | * If just found a common matching node. 53 | * IT means the user CAN do the permission. 54 | */ 55 | FOUND, 56 | /** 57 | * If no matchin node was found. 58 | * It means the user CANNOT do the permission. 59 | * 60 | * owner field and accessLevel field should not be considered, 61 | * when type is 62 | * NOTFOUND 63 | */ 64 | NOTFOUND 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/data/GroupVariables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.data; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * 11 | * @author gabrielcouto 12 | */ 13 | public class GroupVariables extends Variables implements Cloneable { 14 | 15 | private Group owner; 16 | 17 | public GroupVariables(Group owner) { 18 | super(owner); 19 | this.owner = owner; 20 | addVar("prefix", ""); 21 | addVar("suffix", ""); 22 | addVar("build", false); 23 | } 24 | 25 | public GroupVariables(Group owner, Map varList) { 26 | super(owner); 27 | variables = varList; 28 | if (variables.get("prefix") == null) { 29 | variables.put("prefix", ""); 30 | owner.flagAsChanged(); 31 | } 32 | //thisGrp.prefix = infoNode.get("prefix").toString(); 33 | 34 | if (variables.get("suffix") == null) { 35 | variables.put("suffix", ""); 36 | owner.flagAsChanged(); 37 | } 38 | //thisGrp.suffix = infoNode.get("suffix").toString(); 39 | 40 | if (variables.get("build") == null) { 41 | variables.put("build", false); 42 | owner.flagAsChanged(); 43 | } 44 | this.owner = owner; 45 | } 46 | 47 | /** 48 | * A clone of all vars here. 49 | * @return 50 | */ 51 | protected GroupVariables clone(Group newOwner) { 52 | GroupVariables clone = new GroupVariables(newOwner); 53 | for (String key : variables.keySet()) { 54 | clone.variables.put(key, variables.get(key)); 55 | } 56 | newOwner.flagAsChanged(); 57 | return clone; 58 | } 59 | 60 | /** 61 | * Remove a var from the list 62 | * @param name 63 | */ 64 | @Override 65 | public void removeVar(String name) { 66 | try { 67 | this.variables.remove(name); 68 | } catch (Exception e) { 69 | } 70 | if (name.equals("prefix")) { 71 | addVar("prefix", ""); 72 | } else if (name.equals("suffix")) { 73 | addVar("suffix", ""); 74 | } else if (name.equals("build")) { 75 | addVar("build", false); 76 | } 77 | owner.flagAsChanged(); 78 | } 79 | 80 | /** 81 | * @return the owner 82 | */ 83 | @Override 84 | public Group getOwner() { 85 | return owner; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | org.anjocaido 6 | GroupManager 7 | jar 8 | 1.0-alpha-5-SNAPSHOT 9 | GroupManager 10 | http://maven.apache.org 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 2.0.2 17 | 18 | 1.6 19 | 1.6 20 | ${project.build.sourceEncoding} 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-resources-plugin 26 | 2.2 27 | 28 | ${project.build.sourceEncoding} 29 | 30 | 31 | 32 | org.apache.maven.plugins 33 | maven-shade-plugin 34 | 35 | 36 | package 37 | 38 | shade 39 | 40 | 41 | 42 | 43 | uber-${artifactId}-${version} 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.bukkit 51 | bukkit 52 | 0.0.1 53 | system 54 | ${project.basedir}/../bukkit-0.0.1-SNAPSHOT.jar 55 | 56 | 57 | org.yaml 58 | snakeyaml 59 | 1.7 60 | 61 | 62 | 63 | UTF-8 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/utils/Tasks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.utils; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.FileOutputStream; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.io.OutputStream; 13 | import java.util.ArrayList; 14 | import java.util.GregorianCalendar; 15 | import java.util.List; 16 | import org.anjocaido.groupmanager.data.Group; 17 | 18 | /** 19 | * 20 | * @author gabrielcouto 21 | */ 22 | public abstract class Tasks { 23 | 24 | public static void copy(InputStream src, File dst) throws IOException { 25 | InputStream in = src; 26 | OutputStream out = new FileOutputStream(dst); 27 | 28 | // Transfer bytes from in to out 29 | byte[] buf = new byte[1024]; 30 | int len; 31 | while ((len = in.read(buf)) > 0) { 32 | out.write(buf, 0, len); 33 | } 34 | out.close(); 35 | try { 36 | in.close(); 37 | } catch (Exception e) { 38 | } 39 | } 40 | 41 | public static void copy(File src, File dst) throws IOException { 42 | InputStream in = new FileInputStream(src); 43 | copy(in, dst); 44 | } 45 | 46 | public static void removeOldFiles(File folder) { 47 | if (folder.isDirectory()) { 48 | long oldTime = System.currentTimeMillis() - 86400000L; 49 | for (File olds : folder.listFiles()) { 50 | if (olds.isFile()) { 51 | if (olds.lastModified() < oldTime) { 52 | try { 53 | olds.delete(); 54 | } catch (Exception e) { 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | 62 | public static String getDateString() { 63 | GregorianCalendar now = new GregorianCalendar(); 64 | String date = ""; 65 | date += now.get(GregorianCalendar.DAY_OF_MONTH); 66 | date += "-"; 67 | date += now.get(GregorianCalendar.HOUR); 68 | date += "-"; 69 | date += now.get(GregorianCalendar.MINUTE); 70 | return date; 71 | } 72 | public static String getStringListInString(List list){ 73 | if(list==null){ 74 | return ""; 75 | } 76 | String result=""; 77 | for(int i=0;i list){ 99 | if(list==null){ 100 | return ""; 101 | } 102 | String result=""; 103 | for(int i=0;i permissions = new ArrayList(); 23 | 24 | public DataUnit(WorldDataHolder dataSource, String name) { 25 | this.dataSource = dataSource; 26 | this.name = name; 27 | } 28 | 29 | /** 30 | * Every group is matched only by their names and DataSources names. 31 | * @param o 32 | * @return true if they are equal. false if not. 33 | */ 34 | @Override 35 | public boolean equals(Object o) { 36 | if (o instanceof DataUnit) { 37 | DataUnit go = (DataUnit) o; 38 | if (this.getName().equalsIgnoreCase(go.getName()) && this.dataSource.getName().equalsIgnoreCase(go.getDataSource().getName())) { 39 | return true; 40 | } 41 | } 42 | return false; 43 | } 44 | 45 | @Override 46 | public int hashCode() { 47 | int hash = 5; 48 | hash = 71 * hash + (this.name != null ? this.name.toLowerCase().hashCode() : 0); 49 | return hash; 50 | } 51 | 52 | 53 | 54 | 55 | /** 56 | * @return the dataSource 57 | */ 58 | public WorldDataHolder getDataSource() { 59 | return dataSource; 60 | } 61 | 62 | /** 63 | * @return the name 64 | */ 65 | public String getName() { 66 | return name; 67 | } 68 | 69 | public void flagAsChanged() { 70 | GroupManager.logger.finest("DataSource: "+getDataSource().getName()+" - DataUnit: "+getName()+" flagged as changed!"); 71 | // for(StackTraceElement st: Thread.currentThread().getStackTrace()){ 72 | // GroupManager.logger.finest(st.toString()); 73 | // } 74 | changed = true; 75 | } 76 | 77 | public boolean isChanged() { 78 | return changed; 79 | } 80 | 81 | public void flagAsSaved() { 82 | GroupManager.logger.finest("DataSource: "+getDataSource().getName()+" - DataUnit: "+getName()+" flagged as saved!"); 83 | changed = false; 84 | } 85 | 86 | public boolean hasSamePermissionNode(String permission) { 87 | return permissions.contains(permission); 88 | } 89 | 90 | public void addPermission(String permission) { 91 | if (!hasSamePermissionNode(permission)) { 92 | permissions.add(permission); 93 | } 94 | flagAsChanged(); 95 | } 96 | 97 | public boolean removePermission(String permission) { 98 | flagAsChanged(); 99 | return permissions.remove(permission); 100 | } 101 | 102 | /** 103 | * Use this only to list permissions. 104 | * You can't edit the permissions using the returned ArrayList instance 105 | * @return a copy of the permission list 106 | */ 107 | public ArrayList getPermissionList() { 108 | return (ArrayList) permissions.clone(); 109 | } 110 | 111 | public void sortPermissions(){ 112 | Collections.sort(permissions, StringPermissionComparator.getInstance()); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/data/Group.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.data; 6 | 7 | import org.anjocaido.groupmanager.dataholder.WorldDataHolder; 8 | import java.util.ArrayList; 9 | import java.util.Map; 10 | 11 | /** 12 | * 13 | * @author gabrielcouto 14 | */ 15 | public class Group extends DataUnit implements Cloneable { 16 | 17 | /** 18 | * The group it inherits DIRECTLY! 19 | */ 20 | private ArrayList inherits = new ArrayList(); 21 | /** 22 | *This one holds the fields in INFO node. 23 | * like prefix = 'c' 24 | * or build = false 25 | */ 26 | private GroupVariables variables = new GroupVariables(this); 27 | 28 | /** 29 | * 30 | * @param name 31 | */ 32 | public Group(WorldDataHolder source, String name) { 33 | super(source,name); 34 | } 35 | 36 | /** 37 | * Clone this group 38 | * @return a clone of this group 39 | */ 40 | @Override 41 | public Group clone() { 42 | Group clone = new Group(getDataSource(), this.getName()); 43 | clone.inherits = ((ArrayList) this.getInherits().clone()); 44 | for(String perm: this.getPermissionList()){ 45 | clone.addPermission(perm); 46 | } 47 | clone.variables = ((GroupVariables) variables).clone(clone); 48 | //clone.flagAsChanged(); 49 | return clone; 50 | } 51 | 52 | /** 53 | * Use this to deliver a group from a different dataSource to another 54 | * @param dataSource 55 | * @return 56 | */ 57 | public Group clone(WorldDataHolder dataSource) { 58 | if (dataSource.groupExists(this.getName())) { 59 | return null; 60 | } 61 | Group clone = getDataSource().createGroup(this.getName()); 62 | clone.inherits = ((ArrayList) this.getInherits().clone()); 63 | for(String perm: this.getPermissionList()){ 64 | clone.addPermission(perm); 65 | } 66 | clone.variables = variables.clone(clone); 67 | clone.flagAsChanged(); //use this to make the new dataSource save the new group 68 | return clone; 69 | } 70 | 71 | /** 72 | * a COPY of inherits list 73 | * You can't manage the list by here 74 | * Lol... version 0.6 had a problem because this. 75 | * @return the inherits 76 | */ 77 | public ArrayList getInherits() { 78 | return (ArrayList) inherits.clone(); 79 | } 80 | 81 | /** 82 | * @param inherits the inherits to set 83 | */ 84 | public void addInherits(Group inherit) { 85 | if (!this.getDataSource().groupExists(inherit.getName())) { 86 | getDataSource().addGroup(inherit); 87 | } 88 | if (!inherits.contains(inherit.getName().toLowerCase())) { 89 | inherits.add(inherit.getName().toLowerCase()); 90 | } 91 | flagAsChanged(); 92 | } 93 | 94 | public boolean removeInherits(String inherit) { 95 | if (this.inherits.contains(inherit.toLowerCase())) { 96 | this.inherits.remove(inherit.toLowerCase()); 97 | flagAsChanged(); 98 | return true; 99 | } 100 | return false; 101 | } 102 | 103 | /** 104 | * @return the variables 105 | */ 106 | public GroupVariables getVariables() { 107 | return variables; 108 | } 109 | 110 | /** 111 | * 112 | * @param varList 113 | */ 114 | public void setVariables(Map varList) { 115 | GroupVariables temp = new GroupVariables(this, varList); 116 | variables.clearVars(); 117 | for(String key: temp.getVarKeyList()){ 118 | variables.addVar(key, temp.getVarObject(key)); 119 | } 120 | flagAsChanged(); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/permissions/PermissionsReaderInterface.java: -------------------------------------------------------------------------------- 1 | package org.anjocaido.groupmanager.permissions; 2 | 3 | import org.bukkit.entity.Player; 4 | 5 | /** 6 | * Made by Nijikokun. Changed by Gabriel Couto 7 | * 8 | * This class is intended to *read* permissions from a single world. 9 | * 10 | * @author Nijikokun 11 | * @author Gabriel Couto 12 | */ 13 | public abstract class PermissionsReaderInterface { 14 | 15 | 16 | /** 17 | * 18 | * @param player 19 | * @param string 20 | * @return 21 | */ 22 | public abstract boolean has(Player player, String string); 23 | 24 | /** 25 | * 26 | * @param player 27 | * @param string 28 | * @return 29 | */ 30 | public abstract boolean permission(Player player, String string); 31 | 32 | /** 33 | * 34 | * @param string 35 | * @return 36 | */ 37 | public abstract String getGroup(String string); 38 | 39 | /** 40 | * 41 | * @param string 42 | * @param string1 43 | * @return 44 | */ 45 | public abstract boolean inGroup(String string, String string1); 46 | 47 | /** 48 | * 49 | * @param string 50 | * @return 51 | */ 52 | public abstract String getGroupPrefix(String string); 53 | 54 | /** 55 | * 56 | * @param string 57 | * @return 58 | */ 59 | public abstract String getGroupSuffix(String string); 60 | 61 | /** 62 | * 63 | * @param string 64 | * @return 65 | */ 66 | public abstract boolean canGroupBuild(String string); 67 | 68 | /** 69 | * 70 | * @param string 71 | * @param string1 72 | * @return 73 | */ 74 | public abstract String getGroupPermissionString(String string, String string1); 75 | 76 | /** 77 | * 78 | * @param string 79 | * @param string1 80 | * @return 81 | */ 82 | public abstract int getGroupPermissionInteger(String string, String string1); 83 | 84 | /** 85 | * 86 | * @param string 87 | * @param string1 88 | * @return 89 | */ 90 | public abstract boolean getGroupPermissionBoolean(String string, String string1); 91 | 92 | /** 93 | * 94 | * @param string 95 | * @param string1 96 | * @return 97 | */ 98 | public abstract double getGroupPermissionDouble(String string, String string1); 99 | 100 | /** 101 | * 102 | * @param string 103 | * @param string1 104 | * @return 105 | */ 106 | public abstract String getUserPermissionString(String string, String string1); 107 | 108 | /** 109 | * 110 | * @param string 111 | * @param string1 112 | * @return 113 | */ 114 | public abstract int getUserPermissionInteger(String string, String string1); 115 | 116 | /** 117 | * 118 | * @param string 119 | * @param string1 120 | * @return 121 | */ 122 | public abstract boolean getUserPermissionBoolean(String string, String string1); 123 | 124 | /** 125 | * 126 | * @param string 127 | * @param string1 128 | * @return 129 | */ 130 | public abstract double getUserPermissionDouble(String string, String string1); 131 | 132 | /** 133 | * 134 | * @param string 135 | * @param string1 136 | * @return 137 | */ 138 | public abstract String getPermissionString(String string, String string1); 139 | 140 | /** 141 | * 142 | * @param string 143 | * @param string1 144 | * @return 145 | */ 146 | public abstract int getPermissionInteger(String string, String string1); 147 | 148 | /** 149 | * 150 | * @param string 151 | * @param string1 152 | * @return 153 | */ 154 | public abstract boolean getPermissionBoolean(String string, String string1); 155 | 156 | /** 157 | * 158 | * @param string 159 | * @param string1 160 | * @return 161 | */ 162 | public abstract double getPermissionDouble(String string, String string1); 163 | } 164 | -------------------------------------------------------------------------------- /src/main/resources/groups.yml: -------------------------------------------------------------------------------- 1 | groups: 2 | Default: 3 | default: true 4 | permissions: 5 | - essentials.help 6 | - essentials.home 7 | - essentials.motd 8 | - essentials.sethome 9 | - essentials.spawn 10 | inheritance: [] 11 | info: 12 | prefix: '' 13 | build: false 14 | suffix: '' 15 | SemiAdmin: 16 | default: false 17 | permissions: 18 | - +groupmanager.mandemote 19 | - +groupmanager.manpromote 20 | - -groupmanager.* 21 | - '*' 22 | inheritance: 23 | - moderator 24 | info: 25 | prefix: '&c' 26 | build: true 27 | suffix: SemiAdmin 28 | RedFaction: 29 | default: false 30 | permissions: [] 31 | inheritance: 32 | - peasant 33 | info: 34 | prefix: '&c' 35 | roles-category: faction 36 | build: true 37 | suffix: Red 38 | Farmer: 39 | default: false 40 | permissions: 41 | - essentials.kit 42 | - essentials.kit.farmer 43 | - essentials.spawnmob 44 | inheritance: [] 45 | info: 46 | roles-requirement: 47 | - BlueFaction 48 | - RedFaction 49 | prefix: '' 50 | roles-category: job 51 | build: false 52 | suffix: '' 53 | Healer: 54 | default: false 55 | permissions: 56 | - essentials.kit 57 | - essentials.kit.healer 58 | - essentials.heal 59 | inheritance: [] 60 | info: 61 | roles-requirement: 62 | - BlueFaction 63 | - RedFaction 64 | prefix: '' 65 | roles-category: job 66 | build: false 67 | suffix: '' 68 | Fighter: 69 | default: false 70 | permissions: 71 | - essentials.kit 72 | - essentials.kit.fighter 73 | inheritance: [] 74 | info: 75 | prefix: '' 76 | roles-category: skill 77 | build: false 78 | suffix: '' 79 | Admin: 80 | default: false 81 | permissions: 82 | - '*' 83 | inheritance: 84 | - semiadmin 85 | info: 86 | prefix: '' 87 | build: true 88 | suffix: '' 89 | Miner: 90 | default: false 91 | permissions: 92 | - essentials.kit 93 | - essentials.kit.miner 94 | - flashlight.regular 95 | inheritance: [] 96 | info: 97 | roles-requirement: 98 | - BlueFaction 99 | - RedFaction 100 | prefix: '' 101 | roles-category: job 102 | build: false 103 | suffix: '' 104 | FlyingMan: 105 | default: false 106 | permissions: 107 | - nocheat.moving 108 | inheritance: [] 109 | info: 110 | roles-requirement: Fighter&SuperCart 111 | prefix: '' 112 | roles-category: skill 113 | build: false 114 | suffix: '' 115 | Peasant: 116 | default: false 117 | permissions: 118 | - roles.joinrole 119 | - roles.leaverole 120 | - roles.myroles 121 | inheritance: 122 | - default 123 | info: 124 | prefix: '&e' 125 | build: true 126 | suffix: Peasant 127 | BlueFaction: 128 | default: false 129 | permissions: [] 130 | inheritance: 131 | - peasant 132 | info: 133 | prefix: '&d' 134 | roles-category: faction 135 | build: true 136 | suffix: Blue 137 | Railer: 138 | default: false 139 | permissions: 140 | - essentials.kit 141 | - essentials.kit.railer 142 | inheritance: [] 143 | info: 144 | roles-requirement: Miner 145 | prefix: '' 146 | roles-category: subjob 147 | build: false 148 | suffix: '' 149 | SuperCart: 150 | default: false 151 | permissions: 152 | - minecartmania.* 153 | inheritance: [] 154 | info: 155 | roles-requirement: Railer 156 | prefix: '' 157 | roles-category: skill 158 | build: false 159 | suffix: '' 160 | Moderator: 161 | default: false 162 | permissions: 163 | - essentials.tp 164 | - essentials.tphere 165 | - essentials.item 166 | - essentials.give 167 | inheritance: 168 | - default 169 | info: 170 | prefix: '&c' 171 | build: true 172 | suffix: Mod -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/GMConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.FileNotFoundException; 10 | import java.io.IOException; 11 | import java.util.Map; 12 | import java.util.logging.Level; 13 | import org.anjocaido.groupmanager.utils.Tasks; 14 | import org.yaml.snakeyaml.Yaml; 15 | import org.yaml.snakeyaml.constructor.SafeConstructor; 16 | import org.yaml.snakeyaml.reader.UnicodeReader; 17 | 18 | /** 19 | * 20 | * @author gabrielcouto 21 | */ 22 | public class GMConfiguration { 23 | 24 | private GroupManager plugin; 25 | private Map rootDataNode; 26 | private File configFile; 27 | 28 | public GMConfiguration(GroupManager plugin) { 29 | this.plugin = plugin; 30 | load(); 31 | } 32 | 33 | public void load() { 34 | if (!plugin.getDataFolder().exists()) { 35 | plugin.getDataFolder().mkdirs(); 36 | } 37 | configFile = new File(plugin.getDataFolder(), "config.yml"); 38 | 39 | if (!configFile.exists()) { 40 | try { 41 | Tasks.copy(plugin.getResourceAsStream("config.yml"), configFile); 42 | } catch (IOException ex) { 43 | GroupManager.logger.log(Level.SEVERE, null, ex); 44 | } 45 | } 46 | 47 | Yaml yaml = new Yaml(new SafeConstructor()); 48 | FileInputStream rx = null; 49 | try { 50 | rx = new FileInputStream(configFile); 51 | } catch (FileNotFoundException ex) { 52 | GroupManager.logger.log(Level.SEVERE, null, ex); 53 | } 54 | try { 55 | rootDataNode = (Map) yaml.load(new UnicodeReader(rx)); 56 | if (rootDataNode == null) { 57 | throw new NullPointerException(); 58 | } 59 | } catch (Exception ex) { 60 | throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + configFile.getPath(), ex); 61 | } finally { 62 | try { 63 | rx.close(); 64 | } catch (IOException ex) { 65 | } 66 | } 67 | adjustLoggerLevel(); 68 | } 69 | 70 | public Map getMirrorsMap() { 71 | if (rootDataNode.get("settings") instanceof Map) { 72 | Map settingsNode = (Map) rootDataNode.get("settings"); 73 | if (settingsNode.get("permission") instanceof Map) { 74 | Map permissionNode = (Map) settingsNode.get("permission"); 75 | if (permissionNode.get("world") instanceof Map) { 76 | Map worldsNode = (Map) permissionNode.get("world"); 77 | if (worldsNode.get("mirror") instanceof Map) { 78 | Map mirrorsNode = (Map) worldsNode.get("mirror"); 79 | return mirrorsNode; 80 | } 81 | } 82 | } 83 | } 84 | return null; 85 | } 86 | 87 | public Integer getSaveInterval() { 88 | if (rootDataNode.get("settings") instanceof Map) { 89 | Map settingsNode = (Map) rootDataNode.get("settings"); 90 | if (settingsNode.get("data") instanceof Map) { 91 | Map dataNode = (Map) settingsNode.get("data"); 92 | if (dataNode.get("save") instanceof Map) { 93 | Map saveNode = (Map) dataNode.get("save"); 94 | if (saveNode.get("minutes") instanceof Integer) { 95 | return (Integer) saveNode.get("minutes"); 96 | } 97 | } 98 | } 99 | } 100 | return 10; 101 | } 102 | 103 | public void adjustLoggerLevel() { 104 | if (rootDataNode.get("settings") instanceof Map) { 105 | Map settingsNode = (Map) rootDataNode.get("settings"); 106 | if (settingsNode.get("logging") instanceof Map) { 107 | Map loggingNode = (Map) settingsNode.get("logging"); 108 | if (loggingNode.get("level") instanceof String) { 109 | String level = (String) loggingNode.get("level"); 110 | try { 111 | GroupManager.logger.setLevel(Level.parse(level)); 112 | return; 113 | } catch (Exception e) { 114 | } 115 | } 116 | } 117 | } 118 | GroupManager.logger.setLevel(Level.INFO); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/data/Variables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.data; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | import java.util.Set; 10 | 11 | /** 12 | *A class that holds variables of a user/group. 13 | * In groups, it holds the contents of INFO node. 14 | * Like: 15 | * prefix 16 | * suffix 17 | * build 18 | * 19 | * @author gabrielcouto 20 | */ 21 | public abstract class Variables implements Cloneable { 22 | private DataUnit owner; 23 | protected Map variables = new HashMap(); 24 | 25 | public Variables(DataUnit owner){ 26 | this.owner = owner; 27 | } 28 | /** 29 | * Add var to the the INFO node. 30 | * examples: 31 | * addVar("build",true); 32 | * addVar("prefix","c"); 33 | * @param name key name of the var 34 | * @param o the object value of the var 35 | */ 36 | public void addVar(String name, Object o) { 37 | if (o == null) { 38 | return; 39 | } 40 | if (variables.containsKey(name)) { 41 | variables.remove(name); 42 | } 43 | variables.put(name, o); 44 | owner.flagAsChanged(); 45 | } 46 | 47 | /** 48 | * Returns the object inside the var 49 | * @param name 50 | * @return a Object if exists. null if doesn't exists 51 | */ 52 | public Object getVarObject(String name) { 53 | return variables.get(name); 54 | } 55 | 56 | /** 57 | * Get the String value for the given var name 58 | * @param name the var key name 59 | * @return "" if null. or the toString() value of object 60 | */ 61 | public String getVarString(String name) { 62 | Object o = variables.get(name); 63 | try { 64 | return o == null ? "" : o.toString(); 65 | } catch (Exception e) { 66 | return ""; 67 | } 68 | } 69 | 70 | /** 71 | * 72 | * @param name 73 | * @return false if null. or a Boolean.parseBoolean of the string 74 | */ 75 | public Boolean getVarBoolean(String name) { 76 | Object o = variables.get(name); 77 | try { 78 | return o == null ? false : Boolean.parseBoolean(o.toString()); 79 | } catch (Exception e) { 80 | return false; 81 | } 82 | } 83 | 84 | /** 85 | * 86 | * @param name 87 | * @return -1 if null. or a parseInt of the string 88 | */ 89 | public Integer getVarInteger(String name) { 90 | Object o = variables.get(name); 91 | try { 92 | return o == null ? -1 : Integer.parseInt(o.toString()); 93 | } catch (Exception e) { 94 | return -1; 95 | } 96 | } 97 | 98 | /** 99 | * 100 | * @param name 101 | * @return -1 if null. or a parseDouble of the string 102 | */ 103 | public Double getVarDouble(String name) { 104 | Object o = variables.get(name); 105 | try { 106 | return o == null ? -1.0D : Double.parseDouble(o.toString()); 107 | } catch (Exception e) { 108 | return -1.0D; 109 | 110 | 111 | } 112 | } 113 | 114 | /** 115 | * All variable keys this is holding 116 | * @return 117 | */ 118 | public Set getVarKeyList() { 119 | return variables.keySet(); 120 | 121 | 122 | } 123 | 124 | /** 125 | * verify is a var exists 126 | * @param name the key name of the var 127 | * @return true if that var exists 128 | */ 129 | public boolean hasVar(String name) { 130 | return variables.containsKey(name); 131 | 132 | 133 | } 134 | 135 | /** 136 | * Returns the quantity of vars this is holding 137 | * @return the number of vars 138 | */ 139 | public int getSize() { 140 | return variables.size(); 141 | 142 | 143 | } 144 | 145 | /** 146 | * Remove a var from the list 147 | * @param name 148 | */ 149 | public void removeVar(String name) { 150 | try { 151 | variables.remove(name); 152 | } catch (Exception e) { 153 | } 154 | owner.flagAsChanged(); 155 | } 156 | 157 | public static Object parseVariableValue(String value) { 158 | try { 159 | Integer i = Integer.parseInt(value); 160 | return i; 161 | } catch (NumberFormatException e) { 162 | } 163 | try { 164 | Double d = Double.parseDouble(value); 165 | return d; 166 | } catch (NumberFormatException e) { 167 | } 168 | if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("on")) { 169 | return true; 170 | } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) { 171 | return false; 172 | } 173 | return value; 174 | 175 | } 176 | 177 | public void clearVars(){ 178 | variables.clear(); 179 | owner.flagAsChanged(); 180 | } 181 | 182 | /** 183 | * @return the owner 184 | */ 185 | public DataUnit getOwner() { 186 | return owner; 187 | } 188 | 189 | public boolean isEmpty(){ 190 | return variables.isEmpty(); 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/data/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.data; 6 | 7 | import com.sun.org.apache.bcel.internal.generic.AALOAD; 8 | import java.util.ArrayList; 9 | import org.anjocaido.groupmanager.dataholder.WorldDataHolder; 10 | import java.util.Map; 11 | 12 | /** 13 | * 14 | * @author gabrielcouto 15 | */ 16 | public class User extends DataUnit implements Cloneable { 17 | 18 | /** 19 | * 20 | */ 21 | private String group = null; 22 | private ArrayList subGroups = new ArrayList(); 23 | /** 24 | *This one holds the fields in INFO node. 25 | * like prefix = 'c' 26 | * or build = false 27 | */ 28 | private UserVariables variables = new UserVariables(this); 29 | 30 | 31 | /** 32 | * 33 | * @param name 34 | */ 35 | public User(WorldDataHolder source, String name) { 36 | super(source,name); 37 | this.group = source.getDefaultGroup().getName(); 38 | } 39 | 40 | /** 41 | * 42 | * @return 43 | */ 44 | @Override 45 | public User clone() { 46 | User clone = new User(getDataSource(), this.getName()); 47 | clone.group = this.group; 48 | for(String perm: this.getPermissionList()){ 49 | clone.addPermission(perm); 50 | } 51 | //clone.variables = this.variables.clone(); 52 | //clone.flagAsChanged(); 53 | return clone; 54 | } 55 | 56 | /** 57 | * Use this to deliver a user from one WorldDataHolder to another 58 | * @param dataSource 59 | * @return null if given dataSource already contains the same user 60 | */ 61 | public User clone(WorldDataHolder dataSource) { 62 | if (dataSource.isUserDeclared(this.getName())) { 63 | return null; 64 | } 65 | User clone = dataSource.createUser(this.getName()); 66 | if (dataSource.getGroup(group) == null) { 67 | clone.setGroup(dataSource.getDefaultGroup()); 68 | } else { 69 | clone.setGroup(this.getGroupName()); 70 | } 71 | for(String perm: this.getPermissionList()){ 72 | clone.addPermission(perm); 73 | } 74 | //clone.variables = this.variables.clone(); 75 | clone.flagAsChanged(); 76 | return clone; 77 | } 78 | 79 | public Group getGroup() { 80 | Group result = getDataSource().getGroup(group); 81 | if (result == null) { 82 | this.setGroup(getDataSource().getDefaultGroup()); 83 | result = getDataSource().getDefaultGroup(); 84 | } 85 | return result; 86 | } 87 | 88 | /** 89 | * @return the group 90 | */ 91 | public String getGroupName() { 92 | Group result = getDataSource().getGroup(group); 93 | if (result == null) { 94 | group = getDataSource().getDefaultGroup().getName(); 95 | } 96 | return group; 97 | } 98 | 99 | /** 100 | * @param group the group to set 101 | */ 102 | @Deprecated 103 | public void setGroup(String group) { 104 | this.group = group; 105 | flagAsChanged(); 106 | } 107 | 108 | /** 109 | * @param group the group to set 110 | */ 111 | public void setGroup(Group group) { 112 | if (!this.getDataSource().groupExists(group.getName())) { 113 | getDataSource().addGroup(group); 114 | } 115 | group = getDataSource().getGroup(group.getName()); 116 | this.group = group.getName(); 117 | flagAsChanged(); 118 | } 119 | 120 | public void addSubGroup(Group subGroup){ 121 | if(this.group.equalsIgnoreCase(subGroup.getName())){ 122 | return; 123 | } 124 | if (!this.getDataSource().groupExists(subGroup.getName())) { 125 | getDataSource().addGroup(subGroup); 126 | } 127 | subGroup = getDataSource().getGroup(subGroup.getName()); 128 | removeSubGroup(subGroup); 129 | subGroups.add(subGroup.getName()); 130 | flagAsChanged(); 131 | } 132 | public int subGroupsSize(){ 133 | return subGroups.size(); 134 | } 135 | public boolean isSubGroupsEmpty(){ 136 | return subGroups.isEmpty(); 137 | } 138 | public boolean containsSubGroup(Group subGroup){ 139 | return subGroups.contains(subGroup.getName()); 140 | } 141 | public boolean removeSubGroup(Group subGroup){ 142 | try{ 143 | if(subGroups.remove(subGroup.getName())){ 144 | flagAsChanged(); 145 | return true; 146 | } 147 | } catch (Exception e){ 148 | 149 | } 150 | return false; 151 | } 152 | public ArrayList subGroupListCopy(){ 153 | ArrayList val = new ArrayList(); 154 | for(String gstr: subGroups){ 155 | Group g = getDataSource().getGroup(gstr); 156 | if(g==null){ 157 | removeSubGroup(g); 158 | continue; 159 | } 160 | val.add(g); 161 | } 162 | return val; 163 | } 164 | public ArrayList subGroupListStringCopy(){ 165 | return (ArrayList) subGroups.clone(); 166 | } 167 | 168 | /** 169 | * @return the variables 170 | */ 171 | public UserVariables getVariables() { 172 | return variables; 173 | } 174 | 175 | /** 176 | * 177 | * @param varList 178 | */ 179 | public void setVariables(Map varList) { 180 | UserVariables temp = new UserVariables(this, varList); 181 | variables.clearVars(); 182 | for(String key: temp.getVarKeyList()){ 183 | variables.addVar(key, temp.getVarObject(key)); 184 | } 185 | flagAsChanged(); 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.dataholder; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collection; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | import org.anjocaido.groupmanager.data.User; 12 | 13 | /** 14 | * 15 | * @author gabrielcouto 16 | */ 17 | public class OverloadedWorldHolder extends WorldDataHolder { 18 | 19 | /** 20 | * 21 | */ 22 | protected Map overloadedUsers = new HashMap(); 23 | 24 | /** 25 | * 26 | * @param ph 27 | */ 28 | public OverloadedWorldHolder(WorldDataHolder ph) { 29 | super(ph.getName()); 30 | this.f = ph.f; 31 | this.groupsFile = ph.groupsFile; 32 | this.usersFile = ph.usersFile; 33 | this.defaultGroup = ph.defaultGroup; 34 | this.groups = ph.groups; 35 | this.users = ph.users; 36 | } 37 | 38 | /** 39 | * 40 | * @param userName 41 | * @return 42 | */ 43 | @Override 44 | public User getUser(String userName) { 45 | //OVERLOADED CODE 46 | if (overloadedUsers.containsKey(userName.toLowerCase())) { 47 | return overloadedUsers.get(userName.toLowerCase()); 48 | } 49 | //END CODE 50 | if (users.containsKey(userName.toLowerCase())) { 51 | return users.get(userName.toLowerCase()); 52 | } 53 | User newUser = createUser(userName); 54 | haveUsersChanged = true; 55 | return newUser; 56 | } 57 | 58 | /** 59 | * 60 | * @param theUser 61 | */ 62 | @Override 63 | public void addUser(User theUser) { 64 | if (theUser.getDataSource() != this) { 65 | theUser = theUser.clone(this); 66 | } 67 | if (theUser == null) { 68 | return; 69 | } 70 | if ((theUser.getGroup() == null) || (!groups.containsKey(theUser.getGroupName().toLowerCase()))) { 71 | theUser.setGroup(defaultGroup); 72 | } 73 | //OVERLOADED CODE 74 | if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) { 75 | overloadedUsers.remove(theUser.getName().toLowerCase()); 76 | overloadedUsers.put(theUser.getName().toLowerCase(), theUser); 77 | return; 78 | } 79 | //END CODE 80 | removeUser(theUser.getName()); 81 | users.put(theUser.getName().toLowerCase(), theUser); 82 | haveUsersChanged = true; 83 | } 84 | 85 | /** 86 | * 87 | * @param userName 88 | * @return 89 | */ 90 | @Override 91 | public boolean removeUser(String userName) { 92 | //OVERLOADED CODE 93 | if (overloadedUsers.containsKey(userName.toLowerCase())) { 94 | overloadedUsers.remove(userName.toLowerCase()); 95 | return true; 96 | } 97 | //END CODE 98 | if (users.containsKey(userName.toLowerCase())) { 99 | users.remove(userName.toLowerCase()); 100 | haveUsersChanged = true; 101 | return true; 102 | } 103 | return false; 104 | } 105 | 106 | @Override 107 | public boolean removeGroup(String groupName) { 108 | if (groupName.equals(defaultGroup)) { 109 | return false; 110 | } 111 | for (String key : groups.keySet()) { 112 | if (groupName.equalsIgnoreCase(key)) { 113 | groups.remove(key); 114 | for (String userKey : users.keySet()) { 115 | User user = users.get(userKey); 116 | if (user.getGroupName().equalsIgnoreCase(key)) { 117 | user.setGroup(defaultGroup); 118 | } 119 | 120 | } 121 | //OVERLOADED CODE 122 | for (String userKey : overloadedUsers.keySet()) { 123 | User user = overloadedUsers.get(userKey); 124 | if (user.getGroupName().equalsIgnoreCase(key)) { 125 | user.setGroup(defaultGroup); 126 | } 127 | 128 | } 129 | //END OVERLOAD 130 | haveGroupsChanged = true; 131 | return true; 132 | } 133 | } 134 | return false; 135 | } 136 | 137 | /** 138 | * 139 | * @return 140 | */ 141 | @Override 142 | public Collection getUserList() { 143 | Collection overloadedList = new ArrayList(); 144 | Collection normalList = users.values(); 145 | for (User u : normalList) { 146 | if (overloadedUsers.containsKey(u.getName().toLowerCase())) { 147 | overloadedList.add(overloadedUsers.get(u.getName().toLowerCase())); 148 | } else { 149 | overloadedList.add(u); 150 | } 151 | } 152 | return overloadedList; 153 | } 154 | 155 | /** 156 | * 157 | * @param userName 158 | * @return 159 | */ 160 | public boolean isOverloaded(String userName) { 161 | return overloadedUsers.containsKey(userName.toLowerCase()); 162 | } 163 | 164 | /** 165 | * 166 | * @param userName 167 | */ 168 | public void overloadUser(String userName) { 169 | if (!isOverloaded(userName)) { 170 | User theUser = getUser(userName); 171 | theUser = theUser.clone(); 172 | if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) { 173 | overloadedUsers.remove(theUser.getName().toLowerCase()); 174 | } 175 | overloadedUsers.put(theUser.getName().toLowerCase(), theUser); 176 | } 177 | } 178 | 179 | /** 180 | * 181 | * @param userName 182 | */ 183 | public void removeOverload(String userName) { 184 | overloadedUsers.remove(userName.toLowerCase()); 185 | } 186 | 187 | /** 188 | * Gets the user in normal state. Surpassing the overload state. 189 | * It doesn't affect permissions. But it enables plugins change the 190 | * actual user permissions even in overload mode. 191 | * @param userName 192 | * @return 193 | */ 194 | public User surpassOverload(String userName) { 195 | if (!isOverloaded(userName)) { 196 | return getUser(userName); 197 | } 198 | if (users.containsKey(userName.toLowerCase())) { 199 | return users.get(userName.toLowerCase()); 200 | } 201 | User newUser = createUser(userName); 202 | return newUser; 203 | } 204 | } 205 | -------------------------------------------------------------------------------- /src/main/resources/plugin.yml: -------------------------------------------------------------------------------- 1 | name: GroupManager 2 | version: 1.0(alpha-5) 3 | main: org.anjocaido.groupmanager.GroupManager 4 | website: http://www.anjocaido.info/ 5 | description: Provides on-the-fly system for Permission system created by Nijikokun. But all in memory, and with flat-file saving schedule. 6 | authors: 7 | - AnjoCaido 8 | - Gabriel Couto 9 | commands: 10 | manuadd: 11 | description: Move a player to desired group.(Adds to the file if not exists) 12 | usage: / 13 | permission: groupmanager.manuadd 14 | manudel: 15 | description: Remove any user specific configuration. Make him default group. 16 | usage: / 17 | permission: groupmanager.manudel 18 | manuaddsub: 19 | description: Add a group to a player's subgroup list. 20 | usage: / 21 | permission: groupmanager.manuaddsub 22 | manudelsub: 23 | description: Remove a group to a player's subgroup list. 24 | usage: / 25 | permission: groupmanager.manudelsub 26 | mangadd: 27 | description: Add group to the system. 28 | usage: / 29 | permission: groupmanager.mangadd 30 | mangdel: 31 | description: Removes group from the system(all it's users become default) 32 | usage: / 33 | permission: groupmanager.mangdel 34 | manuaddp: 35 | description: Add permission diretly to the player. 36 | usage: / 37 | permission: groupmanager.manuaddp 38 | manudelp: 39 | description: Removes permission diretly from the player. 40 | usage: / 41 | permission: groupmanager.manudelp 42 | manulistp: 43 | description: List all permissions from a player. 44 | usage: / 45 | permission: groupmanager.manulistp 46 | manucheckp: 47 | description: Verify if user has a permission, and where it comes from. 48 | usage: / 49 | permission: groupmanager.manucheckp 50 | mangaddp: 51 | description: Add permission to a group. 52 | usage: / 53 | permission: groupmanager.mangaddp 54 | mangdelp: 55 | description: Removes permission from a group. 56 | usage: / 57 | permission: groupmanager.mangdelp 58 | manglistp: 59 | description: Lists all permissions from a group. 60 | usage: / 61 | permission: groupmanager.manglistp 62 | mangcheckp: 63 | description: Check if group has a permission, and where it comes from. 64 | usage: / 65 | permission: groupmanager.mangcheckp 66 | mangaddi: 67 | description: Add a group to another group inheritance list. 68 | usage: / 69 | permission: groupmanager.mangaddi 70 | mangdeli: 71 | description: Remove a group from another group inheritance list. 72 | usage: / 73 | permission: groupmanager.mangdeli 74 | manuaddv: 75 | description: Add, or replaces, a variable to a user (like prefix or suffix). 76 | usage: / 77 | permission: groupmanager.manuaddv 78 | manudelv: 79 | description: Remove a variable from a user. 80 | usage: / 81 | permission: groupmanager.manudelv 82 | manulistv: 83 | description: List variables a user has (like prefix or suffix). 84 | usage: / 85 | permission: groupmanager.manulistv 86 | manucheckv: 87 | description: Verify a value of a variable of user, and where it comes from. 88 | usage: / 89 | permission: groupmanager.manucheckv 90 | mangaddv: 91 | description: Add, or replaces, a variable to a group (like prefix or suffix). 92 | usage: / 93 | permission: groupmanager.mangaddv 94 | mangdelv: 95 | description: Remove a variable from a group. 96 | usage: / 97 | permission: groupmanager.mangdelv 98 | manglistv: 99 | description: List variables a group has (like prefix or suffix). 100 | usage: / 101 | permission: groupmanager.manglistv 102 | mangcheckv: 103 | description: Verify a value of a variable of group, and where it comes from. 104 | usage: / 105 | permission: groupmanager.mangckeckv 106 | manwhois: 107 | description: Tell the group that user belongs. 108 | usage: / 109 | permission: groupmanager.manwhois 110 | tempadd: 111 | description: Creates a temporary permission copy for that user. 112 | usage: / 113 | permission: groupmanager.tempadd 114 | tempdel: 115 | description: Remove the temporary permission copy for player. 116 | usage: / 117 | permission: groupmanager.tempdel 118 | templist: 119 | description: List players in overload-permissions mode made by /tempadd. 120 | usage: / 121 | permission: groupmanager.templist 122 | tempdelall: 123 | description: Remove all overrides made by command /tempadd. 124 | usage: / 125 | permission: groupmanager.tempdelall 126 | mansave: 127 | description: Save all permissions on file. 128 | usage: / 129 | permission: groupmanager.mansave 130 | manload: 131 | description: Reload current world and config.yml. Or load given world. 132 | usage: / [world] 133 | permission: groupmanager.manload 134 | listgroups: 135 | description: List the groups available. 136 | usage: / 137 | permission: groupmanager.listgroups 138 | manpromote: 139 | description: Promote a player in the same heritage line to a higher rank. 140 | usage: / 141 | permission: groupmanager.manpromote 142 | mandemote: 143 | description: Demote a player in the same heritage line to a lower rank. 144 | usage: / 145 | permission: groupmanager.mandemote 146 | mantogglevalidate: 147 | description: Toggle on/off the validating if player is online. 148 | usage: / 149 | permission: groupmanager.mantogglevalidate 150 | mantogglesave: 151 | description: Toggle on/ff the autosave. 152 | usage: / 153 | permission: groupmanager.mantogglesave 154 | manworld: 155 | description: Prints the selected world name 156 | usage: / 157 | permission: groupmanager.manworld 158 | manselect: 159 | description: Select a world to work with next commands. 160 | usage: / 161 | permission: groupmanager.manselect 162 | manclear: 163 | description: Clear world selection. Next commands will work on your world. 164 | usage: / 165 | permission: groupmanager.manclear -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.dataholder.worlds; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.FileNotFoundException; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.ArrayList; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | import java.util.Properties; 17 | import java.util.logging.Level; 18 | import java.util.logging.Logger; 19 | import org.anjocaido.groupmanager.GroupManager; 20 | import org.anjocaido.groupmanager.dataholder.WorldDataHolder; 21 | import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder; 22 | import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; 23 | import org.anjocaido.groupmanager.utils.Tasks; 24 | import org.bukkit.entity.Player; 25 | 26 | /** 27 | * 28 | * @author gabrielcouto 29 | */ 30 | public class WorldsHolder { 31 | 32 | /** 33 | * Map with instances of loaded worlds. 34 | */ 35 | private Map worldsData = new HashMap(); 36 | /** 37 | * Map of mirrors: 38 | * The key is the mirror. 39 | * The object is the mirrored. 40 | * 41 | * Mirror shows the same data of mirrored. 42 | */ 43 | private Map mirrors = new HashMap(); 44 | private OverloadedWorldHolder defaultWorld; 45 | private String serverDefaultWorldName; 46 | private GroupManager plugin; 47 | private File worldsFolder; 48 | 49 | /** 50 | * 51 | * @param plugin 52 | */ 53 | public WorldsHolder(GroupManager plugin) { 54 | this.plugin = plugin; 55 | verifyFirstRun(); 56 | initialLoad(); 57 | if (defaultWorld == null) { 58 | throw new IllegalStateException("There is no default group! OMG!"); 59 | } 60 | } 61 | 62 | private void initialLoad() { 63 | initialWorldLoading(); 64 | mirrorSetUp(); 65 | } 66 | private void initialWorldLoading(){ 67 | //LOAD EVERY WORLD POSSIBLE 68 | loadWorld(serverDefaultWorldName); 69 | defaultWorld = worldsData.get(serverDefaultWorldName); 70 | 71 | for (File folder : worldsFolder.listFiles()) { 72 | if (folder.getName().equalsIgnoreCase(serverDefaultWorldName)) { 73 | continue; 74 | } 75 | if (folder.isDirectory()) { 76 | loadWorld(folder.getName()); 77 | } 78 | } 79 | } 80 | public void mirrorSetUp(){ 81 | mirrors.clear(); 82 | Map mirrorsMap = plugin.getConfig().getMirrorsMap(); 83 | if (mirrorsMap != null) { 84 | for (String source : mirrorsMap.keySet()) { 85 | if (mirrorsMap.get(source) instanceof ArrayList) { 86 | ArrayList mirrorList = (ArrayList) mirrorsMap.get(source); 87 | for (Object o : mirrorList) { 88 | try { 89 | mirrors.remove(o.toString().toLowerCase()); 90 | } catch (Exception e) { 91 | } 92 | mirrors.put(o.toString().toLowerCase(), getWorldData(source).getName()); 93 | } 94 | } else if (mirrorsMap.get(source) instanceof Object) { 95 | String aMirror = mirrorsMap.get(source).toString(); 96 | mirrors.put(aMirror.toLowerCase(), getWorldData(source).getName()); 97 | } 98 | } 99 | } 100 | } 101 | 102 | /** 103 | * 104 | */ 105 | public void reloadAll() { 106 | ArrayList alreadyDone = new ArrayList(); 107 | for (WorldDataHolder w : worldsData.values()) { 108 | if (alreadyDone.contains(w)) { 109 | continue; 110 | } 111 | w.reload(); 112 | alreadyDone.add(w); 113 | } 114 | } 115 | 116 | /** 117 | * 118 | * @param worldName 119 | */ 120 | public void reloadWorld(String worldName) { 121 | getWorldData(worldName).reload(); 122 | } 123 | 124 | /** 125 | * 126 | */ 127 | public void saveChanges() { 128 | ArrayList alreadyDone = new ArrayList(); 129 | for (OverloadedWorldHolder w : worldsData.values()) { 130 | if (alreadyDone.contains(w)) { 131 | continue; 132 | } 133 | Tasks.removeOldFiles(plugin.getBackupFolder()); 134 | if (w == null) { 135 | GroupManager.logger.severe("WHAT HAPPENED?"); 136 | continue; 137 | } 138 | if (w.haveGroupsChanged()) { 139 | String groupsFolderName = w.getGroupsFile().getParentFile().getName(); 140 | File backupGroups = new File(plugin.getBackupFolder(), "bkp_" + w.getName() + "_g_" + Tasks.getDateString() + ".yml"); 141 | try { 142 | Tasks.copy(w.getGroupsFile(), backupGroups); 143 | } catch (IOException ex) { 144 | GroupManager.logger.log(Level.SEVERE, null, ex); 145 | } 146 | WorldDataHolder.writeGroups(w, w.getGroupsFile()); 147 | w.removeGroupsChangedFlag(); 148 | } 149 | if (w.haveUsersChanged()) { 150 | File backupUsers = new File(plugin.getBackupFolder(), "bkp_" + w.getName() + "_u_" + Tasks.getDateString() + ".yml"); 151 | try { 152 | Tasks.copy(w.getUsersFile(), backupUsers); 153 | } catch (IOException ex) { 154 | GroupManager.logger.log(Level.SEVERE, null, ex); 155 | } 156 | WorldDataHolder.writeUsers(w, w.getUsersFile()); 157 | w.removeUsersChangedFlag(); 158 | } 159 | alreadyDone.add(w); 160 | } 161 | } 162 | 163 | /** 164 | * Returns the dataHolder for the given world. 165 | * If the world is not on the worlds list, returns the default world 166 | * holder. 167 | * 168 | * (WHEN A WORLD IS CONFIGURED TO MIRROR, IT WILL BE ON THE LIST, BUT 169 | * POINTING TO ANOTHER WORLD HOLDER) 170 | * 171 | * Mirrors prevails original data. 172 | * 173 | * @param worldName 174 | * @return 175 | */ 176 | public OverloadedWorldHolder getWorldData(String worldName) { 177 | OverloadedWorldHolder data = worldsData.get(worldName.toLowerCase()); 178 | if (mirrors.containsKey(worldName.toLowerCase())) { 179 | String realOne = mirrors.get(worldName.toLowerCase()); 180 | data = worldsData.get(realOne.toLowerCase()); 181 | } 182 | if (data == null) { 183 | GroupManager.logger.finest("Requested world " + worldName + " not found or badly mirrored. Returning default world..."); 184 | data = getDefaultWorld(); 185 | } 186 | return data; 187 | } 188 | 189 | /** 190 | * Do a matching of playerName, if it s found only one player, do 191 | * getWorldData(player) 192 | * @param playerName 193 | * @return null if matching returned no player, or more than one. 194 | */ 195 | public OverloadedWorldHolder getWorldDataByPlayerName(String playerName) { 196 | List matchPlayer = plugin.getServer().matchPlayer(playerName); 197 | if (matchPlayer.size() == 1) { 198 | return getWorldData(matchPlayer.get(0)); 199 | } 200 | return null; 201 | } 202 | 203 | /** 204 | * Retrieves the field p.getWorld().getName() and do 205 | * getWorld(worldName) 206 | * @param p 207 | * @return 208 | */ 209 | public OverloadedWorldHolder getWorldData(Player p) { 210 | return getWorldData(p.getWorld().getName()); 211 | } 212 | 213 | /** 214 | * It does getWorld(worldName).getPermissionsHandler() 215 | * @param worldName 216 | * @return 217 | */ 218 | public AnjoPermissionsHandler getWorldPermissions(String worldName) { 219 | return getWorldData(worldName).getPermissionsHandler(); 220 | } 221 | 222 | /** 223 | *It does getWorldData(p).getPermission 224 | * @param p 225 | * @return 226 | */ 227 | public AnjoPermissionsHandler getWorldPermissions(Player p) { 228 | return getWorldData(p).getPermissionsHandler(); 229 | } 230 | 231 | /** 232 | * Id does getWorldDataByPlayerName(playerName). 233 | * If it doesnt return null, it will return result.getPermissionsHandler() 234 | * @param playerName 235 | * @return null if the player matching gone wrong. 236 | */ 237 | public AnjoPermissionsHandler getWorldPermissionsByPlayerName(String playerName) { 238 | WorldDataHolder dh = getWorldDataByPlayerName(playerName); 239 | if (dh != null) { 240 | return dh.getPermissionsHandler(); 241 | } 242 | return null; 243 | } 244 | 245 | private void verifyFirstRun() { 246 | worldsFolder = new File(plugin.getDataFolder(), "worlds"); 247 | if (!worldsFolder.exists()) { 248 | worldsFolder.mkdirs(); 249 | } 250 | Properties server = new Properties(); 251 | try { 252 | server.load(new FileInputStream(new File("server.properties"))); 253 | } catch (IOException ex) { 254 | GroupManager.logger.log(Level.SEVERE, null, ex); 255 | } 256 | serverDefaultWorldName = server.getProperty("level-name").toLowerCase(); 257 | File defaultWorldFolder = new File(worldsFolder, serverDefaultWorldName); 258 | if (!defaultWorldFolder.exists()) { 259 | defaultWorldFolder.mkdirs(); 260 | } 261 | if (defaultWorldFolder.exists()) { 262 | File groupsFile = new File(defaultWorldFolder, "groups.yml"); 263 | File usersFile = new File(defaultWorldFolder, "users.yml"); 264 | File oldDataFile = new File(plugin.getDataFolder(), "data.yml"); 265 | if (!groupsFile.exists()) { 266 | if (oldDataFile.exists()) { 267 | try { 268 | Tasks.copy(oldDataFile, groupsFile); 269 | } catch (IOException ex) { 270 | GroupManager.logger.log(Level.SEVERE, null, ex); 271 | } 272 | } else { 273 | InputStream template = plugin.getResourceAsStream("groups.yml"); 274 | try { 275 | Tasks.copy(template, groupsFile); 276 | } catch (IOException ex) { 277 | GroupManager.logger.log(Level.SEVERE, null, ex); 278 | } 279 | } 280 | } 281 | if (!usersFile.exists()) { 282 | if (oldDataFile.exists()) { 283 | try { 284 | Tasks.copy(oldDataFile, usersFile); 285 | } catch (IOException ex) { 286 | GroupManager.logger.log(Level.SEVERE, null, ex); 287 | } 288 | } else { 289 | InputStream template = plugin.getResourceAsStream("users.yml"); 290 | try { 291 | Tasks.copy(template, usersFile); 292 | } catch (IOException ex) { 293 | GroupManager.logger.log(Level.SEVERE, null, ex); 294 | } 295 | } 296 | } 297 | try { 298 | if (oldDataFile.exists()) { 299 | oldDataFile.renameTo(new File(plugin.getDataFolder(), "NOT_USED_ANYMORE_data.yml")); 300 | } 301 | } catch (Exception ex) { 302 | } 303 | } 304 | } 305 | 306 | /** 307 | * Copies the specified world data to another world 308 | * @param fromWorld 309 | * @param toWorld 310 | * @return 311 | */ 312 | public boolean cloneWorld(String fromWorld, String toWorld) { 313 | File fromWorldFolder = new File(worldsFolder, fromWorld); 314 | File toWorldFolder = new File(worldsFolder, toWorld); 315 | if (toWorldFolder.exists() || !fromWorldFolder.exists()) { 316 | return false; 317 | } 318 | File fromWorldGroups = new File(fromWorldFolder, "groups.yml"); 319 | File fromWorldUsers = new File(fromWorldFolder, "users.yml"); 320 | if (!fromWorldGroups.exists() || !fromWorldUsers.exists()) { 321 | return false; 322 | } 323 | File toWorldGroups = new File(toWorldFolder, "groups.yml"); 324 | File toWorldUsers = new File(toWorldFolder, "users.yml"); 325 | toWorldFolder.mkdirs(); 326 | try { 327 | Tasks.copy(fromWorldGroups, toWorldGroups); 328 | Tasks.copy(fromWorldUsers, toWorldUsers); 329 | } catch (IOException ex) { 330 | Logger.getLogger(WorldsHolder.class.getName()).log(Level.SEVERE, null, ex); 331 | return false; 332 | } 333 | return true; 334 | } 335 | 336 | /** 337 | * Load a world from file. 338 | * If it already been loaded, summon reload method from dataHolder. 339 | * @param worldName 340 | */ 341 | public void loadWorld(String worldName) { 342 | if (worldsData.containsKey(worldName.toLowerCase())) { 343 | worldsData.get(worldName.toLowerCase()).reload(); 344 | return; 345 | } 346 | GroupManager.logger.finest("Trying to load world " + worldName + "..."); 347 | File thisWorldFolder = new File(worldsFolder, worldName); 348 | if (thisWorldFolder.exists() && thisWorldFolder.isDirectory()) { 349 | File groupsFile = new File(thisWorldFolder, "groups.yml"); 350 | File usersFile = new File(thisWorldFolder, "users.yml"); 351 | if (!groupsFile.exists()) { 352 | throw new IllegalArgumentException("Groups file for world '" + worldName + "' doesnt exist: " + groupsFile.getPath()); 353 | } 354 | if (!usersFile.exists()) { 355 | throw new IllegalArgumentException("Users file for world '" + worldName + "' doesnt exist: " + usersFile.getPath()); 356 | } 357 | try { 358 | OverloadedWorldHolder thisWorldData = new OverloadedWorldHolder(WorldDataHolder.load(worldName, groupsFile, usersFile)); 359 | if (thisWorldData != null) { 360 | GroupManager.logger.finest("Successful load of world " + worldName + "..."); 361 | worldsData.put(worldName.toLowerCase(), thisWorldData); 362 | return; 363 | } 364 | } catch (FileNotFoundException ex) { 365 | GroupManager.logger.log(Level.SEVERE, null, ex); 366 | return; 367 | } catch (IOException ex) { 368 | GroupManager.logger.log(Level.SEVERE, null, ex); 369 | return; 370 | } 371 | GroupManager.logger.severe("Failed to load world " + worldName + "..."); 372 | } 373 | } 374 | 375 | /** 376 | * Tells if the such world has been mapped. 377 | * 378 | * It will return true if world is a mirror. 379 | * 380 | * @param worldName 381 | * @return true if world is loaded or mirrored. false if not listed 382 | */ 383 | public boolean isInList(String worldName) { 384 | if (worldsData.containsKey(worldName.toLowerCase()) || mirrors.containsKey(worldName.toLowerCase())) { 385 | return true; 386 | } 387 | return false; 388 | } 389 | 390 | /** 391 | * Verify if world has it's own file permissions. 392 | * 393 | * @param worldName 394 | * @return true if it has its own holder. false if not. 395 | */ 396 | public boolean hasOwnData(String worldName) { 397 | if (worldsData.containsKey(worldName.toLowerCase())) { 398 | return true; 399 | } 400 | return false; 401 | } 402 | 403 | /** 404 | * @return the defaultWorld 405 | */ 406 | public OverloadedWorldHolder getDefaultWorld() { 407 | return defaultWorld; 408 | } 409 | 410 | /** 411 | * Returns all physically loaded worlds. 412 | * @return 413 | */ 414 | public ArrayList allWorldsDataList() { 415 | ArrayList list = new ArrayList(); 416 | for (OverloadedWorldHolder data : worldsData.values()) { 417 | if (!list.contains(data)) { 418 | list.add(data); 419 | } 420 | } 421 | return list; 422 | } 423 | } 424 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/permissions/AnjoPermissionsHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.permissions; 6 | 7 | import java.util.ArrayList; 8 | import java.util.LinkedList; 9 | import java.util.List; 10 | import java.util.StringTokenizer; 11 | import org.anjocaido.groupmanager.GroupManager; 12 | import org.anjocaido.groupmanager.data.Group; 13 | import org.anjocaido.groupmanager.dataholder.WorldDataHolder; 14 | import org.anjocaido.groupmanager.data.User; 15 | import org.anjocaido.groupmanager.utils.PermissionCheckResult; 16 | import org.bukkit.entity.Player; 17 | 18 | /** 19 | * Everything here maintains the model created by Nijikokun 20 | * 21 | * But implemented to use GroupManager system. Which provides instant changes, 22 | * without file access. 23 | * 24 | * It holds permissions only for one single world. 25 | * 26 | * @author gabrielcouto 27 | */ 28 | public class AnjoPermissionsHandler extends PermissionsReaderInterface { 29 | 30 | WorldDataHolder ph = null; 31 | 32 | /** 33 | * It needs a WorldDataHolder to work with. 34 | * @param holder 35 | */ 36 | public AnjoPermissionsHandler(WorldDataHolder holder) { 37 | ph = holder; 38 | } 39 | 40 | /** 41 | * A short name method, for permission method. 42 | * @param player 43 | * @param permission 44 | * @return 45 | */ 46 | @Override 47 | public boolean has(Player player, String permission) { 48 | return permission(player, permission); 49 | } 50 | 51 | /** 52 | * Checks if a player can use that permission node. 53 | * @param player 54 | * @param permission 55 | * @return 56 | */ 57 | @Override 58 | public boolean permission(Player player, String permission) { 59 | return checkUserPermission(ph.getUser(player.getName()), permission); 60 | } 61 | 62 | /** 63 | * Returns the name of the group of that player name. 64 | * @param userName 65 | * @return 66 | */ 67 | @Override 68 | public String getGroup(String userName) { 69 | return ph.getUser(userName).getGroup().getName(); 70 | } 71 | 72 | /** 73 | * Verify if player is in suck group. 74 | * It will check it's groups inheritance. 75 | * 76 | * So if you have a group Admin > Moderator 77 | * 78 | * And verify the player 'MyAdmin', which is Admin, it will return true for both 79 | * Admin or Moderator groups. 80 | * 81 | * Mas if you haave a player 'MyModerator', which is Moderator, 82 | * it will give false if you pass Admin in group parameter. 83 | * 84 | * @param name 85 | * @param group 86 | * @return 87 | */ 88 | @Override 89 | public boolean inGroup(String name, String group) { 90 | if (hasGroupInInheritance(ph.getUser(name).getGroup(), group)) { 91 | return true; 92 | } 93 | for (Group subGroup : ph.getUser(name).subGroupListCopy()) { 94 | if (hasGroupInInheritance(subGroup, group)) { 95 | return true; 96 | } 97 | } 98 | return false; 99 | } 100 | 101 | /** 102 | * Returns the String prefix for the given group 103 | * @param groupName 104 | * @return empty string if found none. 105 | */ 106 | @Override 107 | public String getGroupPrefix(String groupName) { 108 | Group g = ph.getGroup(groupName); 109 | if (g == null) { 110 | return null; 111 | } 112 | return g.getVariables().getVarString("prefix"); 113 | } 114 | 115 | /** 116 | * Return the suffix for the given group name 117 | * @param groupName 118 | * @return 119 | */ 120 | @Override 121 | public String getGroupSuffix(String groupName) { 122 | Group g = ph.getGroup(groupName); 123 | if (g == null) { 124 | return null; 125 | } 126 | return g.getVariables().getVarString("suffix"); 127 | } 128 | 129 | /** 130 | * 131 | * @param groupName 132 | * @return 133 | */ 134 | @Override 135 | public boolean canGroupBuild(String groupName) { 136 | Group g = ph.getGroup(groupName); 137 | if (g == null) { 138 | return false; 139 | } 140 | return g.getVariables().getVarBoolean("build"); 141 | } 142 | 143 | /** 144 | * It returns a string variable value, set in the INFO node of the group. 145 | * It will harvest inheritance for value. 146 | * @param groupName 147 | * @param variable 148 | * @return null if no group with that variable is found. 149 | */ 150 | @Override 151 | public String getGroupPermissionString(String groupName, String variable) { 152 | Group start = ph.getGroup(groupName); 153 | if (start == null) { 154 | return null; 155 | } 156 | Group result = nextGroupWithVariable(start, variable); 157 | if (result == null) { 158 | return null; 159 | } 160 | return result.getVariables().getVarString(variable); 161 | } 162 | 163 | /** 164 | * It returns a Integer variable value 165 | * It will harvest inheritance for value. 166 | * @param groupName 167 | * @param variable 168 | * @return -1 if none found or not parseable. 169 | */ 170 | @Override 171 | public int getGroupPermissionInteger(String groupName, String variable) { 172 | Group start = ph.getGroup(groupName); 173 | if (start == null) { 174 | return -1; 175 | } 176 | Group result = nextGroupWithVariable(start, variable); 177 | if (result == null) { 178 | return -1; 179 | } 180 | return result.getVariables().getVarInteger(variable); 181 | } 182 | 183 | /** 184 | * Returns a boolean for given variable in INFO node. 185 | * It will harvest inheritance for value. 186 | * @param group 187 | * @param variable 188 | * @return false if not found/not parseable. 189 | */ 190 | @Override 191 | public boolean getGroupPermissionBoolean(String group, String variable) { 192 | Group start = ph.getGroup(group); 193 | if (start == null) { 194 | return false; 195 | } 196 | Group result = nextGroupWithVariable(start, variable); 197 | if (result == null) { 198 | return false; 199 | } 200 | return result.getVariables().getVarBoolean(variable); 201 | } 202 | 203 | /** 204 | * Returns a double value for the given variable name in INFO node. 205 | * It will harvest inheritance for value. 206 | * @param group 207 | * @param variable 208 | * @return -1 if not found / not parseable. 209 | */ 210 | @Override 211 | public double getGroupPermissionDouble(String group, String variable) { 212 | Group start = ph.getGroup(group); 213 | if (start == null) { 214 | return -1; 215 | } 216 | Group result = nextGroupWithVariable(start, variable); 217 | if (result == null) { 218 | return -1; 219 | } 220 | return result.getVariables().getVarDouble(variable); 221 | } 222 | 223 | /** 224 | * Returns the variable value of the user, in INFO node. 225 | * @param user 226 | * @param variable 227 | * @return 228 | */ 229 | @Override 230 | public String getUserPermissionString(String user, String variable) { 231 | User auser = ph.getUser(user); 232 | if (auser == null) { 233 | return ""; 234 | } 235 | return auser.getVariables().getVarString(variable); 236 | } 237 | 238 | /** 239 | * Returns the variable value of the user, in INFO node. 240 | * @param user 241 | * @param variable 242 | * @return 243 | */ 244 | @Override 245 | public int getUserPermissionInteger(String user, String variable) { 246 | User auser = ph.getUser(user); 247 | if (auser == null) { 248 | return -1; 249 | } 250 | return auser.getVariables().getVarInteger(variable); 251 | } 252 | 253 | /** 254 | * Returns the variable value of the user, in INFO node. 255 | * @param user 256 | * @param variable 257 | * @return 258 | */ 259 | @Override 260 | public boolean getUserPermissionBoolean(String user, String variable) { 261 | User auser = ph.getUser(user); 262 | if (auser == null) { 263 | return false; 264 | } 265 | return auser.getVariables().getVarBoolean(variable); 266 | } 267 | 268 | /** 269 | * Returns the variable value of the user, in INFO node. 270 | * @param user 271 | * @param variable 272 | * @return 273 | */ 274 | @Override 275 | public double getUserPermissionDouble(String user, String variable) { 276 | User auser = ph.getUser(user); 277 | if (auser == null) { 278 | return -1; 279 | } 280 | return auser.getVariables().getVarDouble(variable); 281 | } 282 | 283 | /** 284 | * Returns the variable value of the user, in INFO node. 285 | * If not found, it will search for his Group variables. 286 | * It will harvest the inheritance. 287 | * @param user 288 | * @param variable 289 | * @return empty string if not found 290 | */ 291 | @Override 292 | public String getPermissionString(String user, String variable) { 293 | User auser = ph.getUser(user); 294 | if (auser == null) { 295 | return ""; 296 | } 297 | if (auser.getVariables().hasVar(variable)) { 298 | return auser.getVariables().getVarString(variable); 299 | } 300 | Group start = auser.getGroup(); 301 | if (start == null) { 302 | return ""; 303 | } 304 | Group result = nextGroupWithVariable(start, variable); 305 | if (result == null) { 306 | return ""; 307 | } 308 | return result.getVariables().getVarString(variable); 309 | //return getUserPermissionString(user, variable); 310 | } 311 | 312 | /** 313 | * Returns the variable value of the user, in INFO node. 314 | * If not found, it will search for his Group variables. 315 | * It will harvest the inheritance. 316 | * @param user 317 | * @param variable 318 | * @return -1 if not found 319 | */ 320 | @Override 321 | public int getPermissionInteger(String user, String variable) { 322 | User auser = ph.getUser(user); 323 | if (auser == null) { 324 | return -1; 325 | } 326 | if (auser.getVariables().hasVar(variable)) { 327 | return auser.getVariables().getVarInteger(variable); 328 | } 329 | Group start = auser.getGroup(); 330 | if (start == null) { 331 | return -1; 332 | } 333 | Group result = nextGroupWithVariable(start, variable); 334 | if (result == null) { 335 | return -1; 336 | } 337 | return result.getVariables().getVarInteger(variable); 338 | //return getUserPermissionInteger(string, string1); 339 | } 340 | 341 | /** 342 | * Returns the variable value of the user, in INFO node. 343 | * If not found, it will search for his Group variables. 344 | * It will harvest the inheritance. 345 | * @param user 346 | * @param variable 347 | * @return false if not found or not parseable to true. 348 | */ 349 | @Override 350 | public boolean getPermissionBoolean(String user, String variable) { 351 | User auser = ph.getUser(user); 352 | if (auser == null) { 353 | return false; 354 | } 355 | if (auser.getVariables().hasVar(variable)) { 356 | return auser.getVariables().getVarBoolean(variable); 357 | } 358 | Group start = auser.getGroup(); 359 | if (start == null) { 360 | return false; 361 | } 362 | Group result = nextGroupWithVariable(start, variable); 363 | if (result == null) { 364 | return false; 365 | } 366 | return result.getVariables().getVarBoolean(variable); 367 | //return getUserPermissionBoolean(user, string1); 368 | } 369 | 370 | /** 371 | * Returns the variable value of the user, in INFO node. 372 | * If not found, it will search for his Group variables. 373 | * It will harvest the inheritance. 374 | * @param user 375 | * @param variable 376 | * @return -1 if not found. 377 | */ 378 | @Override 379 | public double getPermissionDouble(String user, String variable) { 380 | User auser = ph.getUser(user); 381 | if (auser == null) { 382 | return -1.0D; 383 | } 384 | if (auser.getVariables().hasVar(variable)) { 385 | return auser.getVariables().getVarDouble(variable); 386 | } 387 | Group start = auser.getGroup(); 388 | if (start == null) { 389 | return -1.0D; 390 | } 391 | Group result = nextGroupWithVariable(start, variable); 392 | if (result == null) { 393 | return -1.0D; 394 | } 395 | return result.getVariables().getVarDouble(variable); 396 | //return getUserPermissionDouble(string, string1); 397 | } 398 | 399 | /** 400 | * Does not include User's group permission 401 | * @param user 402 | * @param permission 403 | * @return 404 | */ 405 | public PermissionCheckResult checkUserOnlyPermission(User user, String permission) { 406 | user.sortPermissions(); 407 | PermissionCheckResult result = new PermissionCheckResult(); 408 | result.askedPermission = permission; 409 | result.owner = user; 410 | for (String access : user.getPermissionList()) { 411 | if (comparePermissionString(access, permission)) { 412 | result.accessLevel = access; 413 | if (access.startsWith("-")) { 414 | result.resultType = PermissionCheckResult.Type.NEGATION; 415 | } else if (access.startsWith("+")) { 416 | result.resultType = PermissionCheckResult.Type.EXCEPTION; 417 | } else { 418 | result.resultType = PermissionCheckResult.Type.FOUND; 419 | } 420 | return result; 421 | } 422 | } 423 | result.resultType = PermissionCheckResult.Type.NOTFOUND; 424 | return result; 425 | } 426 | 427 | /** 428 | * Returns the node responsible for that permission. 429 | * Does not include User's group permission. 430 | * @param group 431 | * @param permission 432 | * @return the node if permission is found. if not found, return null 433 | */ 434 | public PermissionCheckResult checkGroupOnlyPermission(Group group, String permission) { 435 | group.sortPermissions(); 436 | PermissionCheckResult result = new PermissionCheckResult(); 437 | result.owner = group; 438 | result.askedPermission = permission; 439 | for (String access : group.getPermissionList()) { 440 | if (comparePermissionString(access, permission)) { 441 | result.accessLevel = access; 442 | if (access.startsWith("-")) { 443 | result.resultType = PermissionCheckResult.Type.NEGATION; 444 | } else if (access.startsWith("+")) { 445 | result.resultType = PermissionCheckResult.Type.EXCEPTION; 446 | } else { 447 | result.resultType = PermissionCheckResult.Type.FOUND; 448 | } 449 | return result; 450 | } 451 | } 452 | result.resultType = PermissionCheckResult.Type.NOTFOUND; 453 | return result; 454 | } 455 | 456 | /** 457 | * Check permissions, including it's group and inheritance. 458 | * @param user 459 | * @param permission 460 | * @return true if permission was found. false if not, or was negated. 461 | */ 462 | public boolean checkUserPermission(User user, String permission) { 463 | PermissionCheckResult result = checkFullUserPermission(user, permission); 464 | if (result.resultType.equals(PermissionCheckResult.Type.EXCEPTION) 465 | || result.resultType.equals(PermissionCheckResult.Type.FOUND)) { 466 | return true; 467 | } 468 | return false; 469 | } 470 | 471 | /** 472 | * Do what checkUserPermission did before. But now returning a PermissionCheckResult. 473 | * @param user 474 | * @param targetPermission 475 | * @return 476 | */ 477 | public PermissionCheckResult checkFullUserPermission(User user, String targetPermission) { 478 | PermissionCheckResult result = new PermissionCheckResult(); 479 | result.askedPermission = targetPermission; 480 | result.resultType = PermissionCheckResult.Type.NOTFOUND; 481 | 482 | if (user == null || targetPermission == null) { 483 | return result; 484 | } 485 | 486 | PermissionCheckResult resultUser = checkUserOnlyPermission(user, targetPermission); 487 | if (!resultUser.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { 488 | return resultUser; 489 | 490 | } 491 | 492 | //IT ONLY CHECKS GROUPS PERMISSIONS IF RESULT FOR USER IS NOT FOUND 493 | PermissionCheckResult resultGroup = checkGroupPermissionWithInheritance(user.getGroup(), targetPermission); 494 | if (!resultGroup.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { 495 | return resultGroup; 496 | } 497 | 498 | //SUBGROUPS CHECK 499 | for (Group subGroup : user.subGroupListCopy()) { 500 | PermissionCheckResult resultSubGroup = checkGroupPermissionWithInheritance(subGroup, targetPermission); 501 | if (!resultSubGroup.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { 502 | return resultSubGroup; 503 | } 504 | } 505 | 506 | //THEN IT RETURNS A NOT FOUND 507 | return result; 508 | } 509 | 510 | /** 511 | * Verifies if a given group has a variable. Including it's inheritance. 512 | * 513 | * it redirects to the other method now. This one was deprecated, and will 514 | * be gone in a future release. 515 | * 516 | * @param start 517 | * @param variable 518 | * @param alreadyChecked 519 | * @return returns the closest inherited group with the variable. 520 | * @deprecated use now nextGroupWithVariable(Group start, String targetVariable) 521 | */ 522 | @Deprecated 523 | public Group nextGroupWithVariable(Group start, String variable, List alreadyChecked) { 524 | return nextGroupWithVariable(start, variable); 525 | } 526 | 527 | /** 528 | * Returns the next group, including inheritance, which contains that 529 | * variable name. 530 | * 531 | * It does Breadth-first search 532 | * 533 | * @param start the starting group to look for 534 | * @param targetVariable the variable name 535 | * @return The group if found. Null if not. 536 | */ 537 | public Group nextGroupWithVariable(Group start, String targetVariable) { 538 | if (start == null || targetVariable == null) { 539 | return null; 540 | } 541 | LinkedList stack = new LinkedList(); 542 | ArrayList alreadyVisited = new ArrayList(); 543 | stack.push(start); 544 | alreadyVisited.add(start); 545 | while (!stack.isEmpty()) { 546 | Group now = stack.pop(); 547 | if (now.getVariables().hasVar(targetVariable)) { 548 | return now; 549 | } 550 | for (String sonName : now.getInherits()) { 551 | Group son = ph.getGroup(sonName); 552 | if (son != null && !alreadyVisited.contains(son)) { 553 | stack.push(son); 554 | alreadyVisited.add(son); 555 | } 556 | } 557 | } 558 | return null; 559 | } 560 | 561 | /** 562 | * Check if given group inherits another group. 563 | * 564 | * redirected to the other method. this is deprecated now. and will be gone 565 | * in the future releases. 566 | * 567 | * @param start The group to start the search. 568 | * @param askedGroup Name of the group you're looking for 569 | * @param alreadyChecked groups to ignore(pass null on it, please) 570 | * @return true if it inherits the group. 571 | * @deprecated prefer using hasGroupInInheritance(Group start, String askedGroup) 572 | */ 573 | @Deprecated 574 | public boolean searchGroupInInheritance(Group start, String askedGroup, List alreadyChecked) { 575 | return hasGroupInInheritance(start, askedGroup); 576 | } 577 | 578 | /** 579 | * Check if given group inherits another group. 580 | * 581 | * It does Breadth-first search 582 | * 583 | * @param start The group to start the search. 584 | * @param askedGroup Name of the group you're looking for 585 | * @return true if it inherits the group. 586 | */ 587 | public boolean hasGroupInInheritance(Group start, String askedGroup) { 588 | if (start == null || askedGroup == null) { 589 | return false; 590 | } 591 | LinkedList stack = new LinkedList(); 592 | ArrayList alreadyVisited = new ArrayList(); 593 | stack.push(start); 594 | alreadyVisited.add(start); 595 | while (!stack.isEmpty()) { 596 | Group now = stack.pop(); 597 | if (now.getName().equalsIgnoreCase(askedGroup)) { 598 | return true; 599 | } 600 | for (String sonName : now.getInherits()) { 601 | Group son = ph.getGroup(sonName); 602 | if (son != null && !alreadyVisited.contains(son)) { 603 | stack.push(son); 604 | alreadyVisited.add(son); 605 | } 606 | } 607 | } 608 | return false; 609 | } 610 | 611 | /** 612 | * Check if the group has given permission. Including it's inheritance 613 | * @param start 614 | * @param permission 615 | * @param alreadyChecked 616 | * @return true if PermissionCheckResult is EXCEPTION or FOUND 617 | * @deprecated use the other checkGroupPermissionWithInheritance for everything 618 | */ 619 | @Deprecated 620 | public boolean checkGroupPermissionWithInheritance(Group start, String permission, List alreadyChecked) { 621 | PermissionCheckResult result = checkGroupPermissionWithInheritance(start, permission); 622 | if (result.resultType.equals(result.resultType.EXCEPTION) 623 | || result.resultType.equals(result.resultType.FOUND)) { 624 | return true; 625 | } 626 | return false; 627 | } 628 | 629 | /** 630 | * Returns the result of permission check. Including inheritance. 631 | * If found anything, the PermissionCheckResult that retuns will 632 | * include the Group name, and the result type. 633 | * Result types will be EXCEPTION, NEGATION, FOUND. 634 | * 635 | * If returned type NOTFOUND, the owner will be null, 636 | * and ownerType too. 637 | * 638 | * It does Breadth-first search 639 | * 640 | * @param start 641 | * @param targetPermission 642 | * @return 643 | */ 644 | public PermissionCheckResult checkGroupPermissionWithInheritance(Group start, String targetPermission) { 645 | if (start == null || targetPermission == null) { 646 | return null; 647 | } 648 | LinkedList stack = new LinkedList(); 649 | List alreadyVisited = new ArrayList(); 650 | stack.push(start); 651 | alreadyVisited.add(start); 652 | while (!stack.isEmpty()) { 653 | Group now = stack.pop(); 654 | PermissionCheckResult resultNow = checkGroupOnlyPermission(now, targetPermission); 655 | if (!resultNow.resultType.equals(PermissionCheckResult.Type.NOTFOUND)) { 656 | return resultNow; 657 | } 658 | for (String sonName : now.getInherits()) { 659 | Group son = ph.getGroup(sonName); 660 | if (son != null && !alreadyVisited.contains(son)) { 661 | stack.push(son); 662 | alreadyVisited.add(son); 663 | } 664 | } 665 | } 666 | PermissionCheckResult result = new PermissionCheckResult(); 667 | result.askedPermission = targetPermission; 668 | result.resultType = PermissionCheckResult.Type.NOTFOUND; 669 | return result; 670 | } 671 | 672 | /** 673 | * It uses checkGroupPermissionWithInheritance 674 | * and cast the owner to Group type if result type was EXCEPTION or FOUND. 675 | * 676 | * @param start 677 | * @param permission 678 | * @param alreadyChecked 679 | * @return the group that passed on test. null if no group passed. 680 | * @deprecated use checkGroupPermissionWithInheritance for everything now. 681 | */ 682 | @Deprecated 683 | public Group nextGroupWithPermission(Group start, String permission, List alreadyChecked) { 684 | PermissionCheckResult result = checkGroupPermissionWithInheritance(start, permission); 685 | if (result.resultType.equals(result.resultType.EXCEPTION) 686 | || result.resultType.equals(result.resultType.FOUND)) { 687 | return (Group) checkGroupPermissionWithInheritance(start, permission).owner; 688 | } 689 | return null; 690 | } 691 | 692 | /** 693 | * Return whole list of names of groups in a inheritance chain. Including a 694 | * starting group. 695 | * 696 | * it now redirects to the other method. but get away from this one, 697 | * it will disappear in a future release. 698 | * 699 | * @param start 700 | * @param alreadyChecked 701 | * @return the group that passed on test. null if no group passed. 702 | * @deprecated use the other method with same name, instead 703 | */ 704 | @Deprecated 705 | public ArrayList listAllGroupsInherited(Group start, ArrayList alreadyChecked) { 706 | return listAllGroupsInherited(start); 707 | } 708 | 709 | /** 710 | * Return whole list of names of groups in a inheritance chain. Including a 711 | * starting group. 712 | * 713 | * It does Breadth-first search. So closer groups will appear first in list. 714 | * 715 | * @param start 716 | * @return the group that passed on test. null if no group passed. 717 | */ 718 | public ArrayList listAllGroupsInherited(Group start) { 719 | if (start == null) { 720 | return null; 721 | } 722 | LinkedList stack = new LinkedList(); 723 | ArrayList alreadyVisited = new ArrayList(); 724 | stack.push(start); 725 | alreadyVisited.add(start.getName()); 726 | while (!stack.isEmpty()) { 727 | Group now = stack.pop(); 728 | for (String sonName : now.getInherits()) { 729 | Group son = ph.getGroup(sonName); 730 | if (son != null && !alreadyVisited.contains(son.getName())) { 731 | stack.push(son); 732 | alreadyVisited.add(son.getName()); 733 | } 734 | } 735 | } 736 | return alreadyVisited; 737 | } 738 | 739 | /** 740 | * Compare a user permission like 'myplugin.*' against a full plugin 741 | * permission name, like 'myplugin.dosomething'. 742 | * As the example above, will return true. 743 | * 744 | * Please sort permissions before sending them here. So negative tokens 745 | * get priority. 746 | * 747 | * You must test if it start with negative outside this method. It will 748 | * only tell if the nodes are matching or not. 749 | * 750 | * Every '-' or '+' in the beginning is ignored. It will match only 751 | * node names. 752 | * 753 | * @param userAcessLevel 754 | * @param fullPermissionName 755 | * @return true if found a matching token. false if not. 756 | */ 757 | public boolean comparePermissionString(String userAcessLevel, String fullPermissionName) { 758 | if (userAcessLevel == null || fullPermissionName == null) { 759 | return false; 760 | } 761 | GroupManager.logger.finest("COMPARING " + userAcessLevel + " WITH " + fullPermissionName); 762 | 763 | if (userAcessLevel.startsWith("+")) { 764 | userAcessLevel = userAcessLevel.substring(1); 765 | } else if (userAcessLevel.startsWith("-")) { 766 | userAcessLevel = userAcessLevel.substring(1); 767 | } 768 | 769 | if (fullPermissionName.startsWith("+")) { 770 | fullPermissionName = fullPermissionName.substring(1); 771 | } else if (fullPermissionName.startsWith("-")) { 772 | fullPermissionName = fullPermissionName.substring(1); 773 | } 774 | 775 | 776 | StringTokenizer levelATokenizer = new StringTokenizer(userAcessLevel, "."); 777 | StringTokenizer levelBTokenizer = new StringTokenizer(fullPermissionName, "."); 778 | while (levelATokenizer.hasMoreTokens() && levelBTokenizer.hasMoreTokens()) { 779 | String levelA = levelATokenizer.nextToken(); 780 | String levelB = levelBTokenizer.nextToken(); 781 | GroupManager.logger.finest("ROUND " + levelA + " AGAINST " + levelB); 782 | if (levelA.contains("*")) { 783 | GroupManager.logger.finest("WIN"); 784 | return true; 785 | } 786 | if (levelA.equalsIgnoreCase(levelB)) { 787 | if (!levelATokenizer.hasMoreTokens() && !levelBTokenizer.hasMoreTokens()) { 788 | GroupManager.logger.finest("WIN"); 789 | return true; 790 | } 791 | GroupManager.logger.finest("NEXT"); 792 | continue; 793 | } else { 794 | GroupManager.logger.finest("FAIL"); 795 | return false; 796 | } 797 | 798 | } 799 | GroupManager.logger.finest("FAIL"); 800 | return false; 801 | } 802 | 803 | /** 804 | * Returns a list of all groups. 805 | * 806 | * Including subgroups. 807 | * @param userName 808 | * @return 809 | */ 810 | public String[] getGroups(String userName) { 811 | ArrayList allGroups = listAllGroupsInherited(ph.getUser(userName).getGroup()); 812 | for(Group subg: ph.getUser(userName).subGroupListCopy()){ 813 | allGroups.addAll(listAllGroupsInherited(subg)); 814 | } 815 | String[] arr = new String[allGroups.size()]; 816 | return allGroups.toArray(arr); 817 | } 818 | 819 | /** 820 | * A Breadth-first search thru inheritance model. 821 | * 822 | * Just a model to copy and paste. 823 | * This will guarantee the closer groups will be checked first. 824 | * @param start 825 | * @param targerPermission 826 | * @return 827 | */ 828 | private Group breadthFirstSearch(Group start, String targerPermission) { 829 | if (start == null || targerPermission == null) { 830 | return null; 831 | } 832 | LinkedList stack = new LinkedList(); 833 | ArrayList alreadyVisited = new ArrayList(); 834 | stack.push(start); 835 | alreadyVisited.add(start); 836 | while (!stack.isEmpty()) { 837 | Group now = stack.pop(); 838 | PermissionCheckResult resultNow = checkGroupOnlyPermission(now, targerPermission); 839 | if (resultNow.resultType.equals(PermissionCheckResult.Type.EXCEPTION) 840 | || resultNow.resultType.equals(PermissionCheckResult.Type.FOUND)) { 841 | return now; 842 | } 843 | if (resultNow.resultType.equals(PermissionCheckResult.Type.NEGATION)) { 844 | return null; 845 | } 846 | for (String sonName : now.getInherits()) { 847 | Group son = ph.getGroup(sonName); 848 | if (son != null && !alreadyVisited.contains(son)) { 849 | stack.push(son); 850 | alreadyVisited.add(son); 851 | } 852 | } 853 | } 854 | return null; 855 | } 856 | } 857 | -------------------------------------------------------------------------------- /src/main/java/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.anjocaido.groupmanager.dataholder; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.FileNotFoundException; 10 | import java.io.FileWriter; 11 | import java.io.IOException; 12 | import java.util.ArrayList; 13 | import java.util.Collection; 14 | import java.util.HashMap; 15 | import java.util.List; 16 | import java.util.Map; 17 | import java.util.logging.Level; 18 | import java.util.logging.Logger; 19 | import org.anjocaido.groupmanager.GroupManager; 20 | import org.anjocaido.groupmanager.data.Group; 21 | import org.anjocaido.groupmanager.data.User; 22 | import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; 23 | import org.bukkit.Server; 24 | import org.bukkit.plugin.Plugin; 25 | import org.bukkit.plugin.PluginManager; 26 | import org.yaml.snakeyaml.DumperOptions; 27 | import org.yaml.snakeyaml.Yaml; 28 | import org.yaml.snakeyaml.constructor.SafeConstructor; 29 | import org.yaml.snakeyaml.reader.UnicodeReader; 30 | 31 | /** 32 | * 33 | * @author gabrielcouto 34 | */ 35 | public class WorldDataHolder { 36 | 37 | /** 38 | * 39 | */ 40 | protected String name; 41 | /** 42 | * The actual groups holder 43 | */ 44 | protected Map groups = new HashMap(); 45 | /** 46 | * The actual users holder 47 | */ 48 | protected Map users = new HashMap(); 49 | /** 50 | * Points to the default group 51 | */ 52 | protected Group defaultGroup = null; 53 | /** 54 | * The file, which this class loads/save data from/to 55 | * @deprecated 56 | */ 57 | @Deprecated 58 | protected File f; 59 | /** 60 | * 61 | */ 62 | protected AnjoPermissionsHandler permissionsHandler; 63 | /** 64 | * 65 | */ 66 | protected File usersFile; 67 | /** 68 | * 69 | */ 70 | protected File groupsFile; 71 | /** 72 | * 73 | */ 74 | protected boolean haveUsersChanged = false; 75 | /** 76 | * 77 | */ 78 | protected boolean haveGroupsChanged = false; 79 | 80 | /** 81 | * Prevent direct instantiation 82 | * @param worldName 83 | */ 84 | protected WorldDataHolder(String worldName) { 85 | name = worldName; 86 | } 87 | 88 | /** 89 | * The main constructor for a new WorldDataHolder 90 | * Please don't set the default group as null 91 | * @param worldName 92 | * @param defaultGroup the default group. its good to start with one 93 | */ 94 | public WorldDataHolder(String worldName, Group defaultGroup) { 95 | this.name = worldName; 96 | groups.put(defaultGroup.getName().toLowerCase(), defaultGroup); 97 | this.defaultGroup = defaultGroup; 98 | } 99 | 100 | /** 101 | * Search for a user. If it doesn't exist, create a new one with 102 | * default group. 103 | * 104 | * @param userName the name of the user 105 | * @return class that manage that user permission 106 | */ 107 | public User getUser(String userName) { 108 | if (users.containsKey(userName.toLowerCase())) { 109 | return users.get(userName.toLowerCase()); 110 | } 111 | User newUser = createUser(userName); 112 | return newUser; 113 | } 114 | 115 | /** 116 | * Add a user to the list. If it already exists, overwrite the old. 117 | * @param theUser the user you want to add to the permission list 118 | */ 119 | public void addUser(User theUser) { 120 | if (theUser.getDataSource() != this) { 121 | theUser = theUser.clone(this); 122 | } 123 | if (theUser == null) { 124 | return; 125 | } 126 | if ((theUser.getGroup() == null)) { 127 | theUser.setGroup(defaultGroup); 128 | } 129 | removeUser(theUser.getName()); 130 | users.put(theUser.getName().toLowerCase(), theUser); 131 | haveUsersChanged = true; 132 | } 133 | 134 | /** 135 | * Removes the user from the list. (he might become a default user) 136 | * @param userName the username from the user to remove 137 | * @return true if it had something to remove 138 | */ 139 | public boolean removeUser(String userName) { 140 | if (users.containsKey(userName.toLowerCase())) { 141 | users.remove(userName.toLowerCase()); 142 | haveUsersChanged = true; 143 | return true; 144 | } 145 | return false; 146 | } 147 | 148 | /** 149 | * 150 | * @param userName 151 | * @return 152 | */ 153 | public boolean isUserDeclared(String userName) { 154 | return users.containsKey(userName.toLowerCase()); 155 | } 156 | 157 | /** 158 | * Change the default group of the file. 159 | * @param group the group you want make default. 160 | */ 161 | public void setDefaultGroup(Group group) { 162 | if (!groups.containsKey(group.getName().toLowerCase()) || (group.getDataSource() != this)) { 163 | addGroup(group); 164 | } 165 | defaultGroup = this.getGroup(group.getName()); 166 | haveGroupsChanged = true; 167 | } 168 | 169 | /** 170 | * Returns the default group of the file 171 | * @return the default group 172 | */ 173 | public Group getDefaultGroup() { 174 | return defaultGroup; 175 | } 176 | 177 | /** 178 | * Returns a group of the given name 179 | * @param groupName the name of the group 180 | * @return a group if it is found. null if not found. 181 | */ 182 | public Group getGroup(String groupName) { 183 | return groups.get(groupName.toLowerCase()); 184 | } 185 | 186 | /** 187 | * Check if a group exists. 188 | * Its the same of getGroup, but check if it is null. 189 | * @param groupName the name of the group 190 | * @return true if exists. false if not. 191 | */ 192 | public boolean groupExists(String groupName) { 193 | return groups.containsKey(groupName.toLowerCase()); 194 | } 195 | 196 | /** 197 | * Add a group to the list 198 | * @param groupToAdd 199 | */ 200 | public void addGroup(Group groupToAdd) { 201 | if (groupToAdd.getDataSource() != this) { 202 | groupToAdd = groupToAdd.clone(this); 203 | } 204 | removeGroup(groupToAdd.getName()); 205 | groups.put(groupToAdd.getName().toLowerCase(), groupToAdd); 206 | haveGroupsChanged = true; 207 | } 208 | 209 | /** 210 | * Remove the group to the list 211 | * @param groupName 212 | * @return true if had something to remove. false the group was default or non-existant 213 | */ 214 | public boolean removeGroup(String groupName) { 215 | if (defaultGroup != null && groupName.equalsIgnoreCase(defaultGroup.getName())) { 216 | return false; 217 | } 218 | if (groups.containsKey(groupName.toLowerCase())) { 219 | groups.remove(groupName.toLowerCase()); 220 | haveGroupsChanged = true; 221 | return true; 222 | } 223 | return false; 224 | 225 | } 226 | 227 | /** 228 | * Creates a new User with the given name 229 | * and adds it to this holder. 230 | * @param userName the username you want 231 | * @return null if user already exists. or new User 232 | */ 233 | public User createUser(String userName) { 234 | if (this.users.containsKey(userName.toLowerCase())) { 235 | return null; 236 | } 237 | User newUser = new User(this, userName); 238 | newUser.setGroup(defaultGroup); 239 | this.addUser(newUser); 240 | haveUsersChanged = true; 241 | return newUser; 242 | } 243 | 244 | /** 245 | * Creates a new Group with the given name 246 | * and adds it to this holder 247 | * @param groupName the groupname you want 248 | * @return null if group already exists. or new Group 249 | */ 250 | public Group createGroup(String groupName) { 251 | if (this.groups.containsKey(groupName.toLowerCase())) { 252 | return null; 253 | } 254 | Group newGroup = new Group(this, groupName); 255 | this.addGroup(newGroup); 256 | haveGroupsChanged = true; 257 | return newGroup; 258 | } 259 | 260 | /** 261 | * 262 | * @return a collection of the groups 263 | */ 264 | public Collection getGroupList() { 265 | return groups.values(); 266 | } 267 | 268 | /** 269 | * 270 | * @return a collection of the users 271 | */ 272 | public Collection getUserList() { 273 | return users.values(); 274 | } 275 | 276 | /** 277 | * reads the file again 278 | */ 279 | public void reload() { 280 | try { 281 | WorldDataHolder ph = load(this.getName(), getGroupsFile(), getUsersFile()); 282 | this.defaultGroup = ph.defaultGroup; 283 | this.groups = ph.groups; 284 | this.users = ph.users; 285 | } catch (Exception ex) { 286 | Logger.getLogger(WorldDataHolder.class.getName()).log(Level.SEVERE, null, ex); 287 | } 288 | } 289 | 290 | /** 291 | * Save by yourself! 292 | * @deprecated 293 | */ 294 | @Deprecated 295 | public void commit() { 296 | writeGroups(this, getGroupsFile()); 297 | writeUsers(this, getUsersFile()); 298 | } 299 | 300 | /** 301 | * Returns a data holder for the given file 302 | * @param worldName 303 | * @param file 304 | * @return 305 | * @throws Exception 306 | * @deprecated 307 | */ 308 | @Deprecated 309 | public static WorldDataHolder load(String worldName, File file) throws Exception { 310 | WorldDataHolder ph = new WorldDataHolder(worldName); 311 | ph.f = file; 312 | final Yaml yaml = new Yaml(new SafeConstructor()); 313 | Map rootDataNode; 314 | if (!file.exists()) { 315 | throw new Exception("The file which should contain permissions does not exist!\n" + file.getPath()); 316 | } 317 | FileInputStream rx = new FileInputStream(file); 318 | try { 319 | rootDataNode = (Map) yaml.load(new UnicodeReader(rx)); 320 | if (rootDataNode == null) { 321 | throw new NullPointerException(); 322 | } 323 | } catch (Exception ex) { 324 | throw new Exception("The following file couldn't pass on Parser.\n" + file.getPath(), ex); 325 | } finally { 326 | rx.close(); 327 | } 328 | Map> inheritance = new HashMap>(); 329 | try { 330 | Map allGroupsNode = (Map) rootDataNode.get("groups"); 331 | for (String groupKey : allGroupsNode.keySet()) { 332 | Map thisGroupNode = (Map) allGroupsNode.get(groupKey); 333 | Group thisGrp = ph.createGroup(groupKey); 334 | if (thisGrp == null) { 335 | throw new IllegalArgumentException("I think this user was declared more than once: " + groupKey); 336 | } 337 | if (thisGroupNode.get("default") == null) { 338 | thisGroupNode.put("default", false); 339 | } 340 | if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) { 341 | if (ph.getDefaultGroup() != null) { 342 | GroupManager.logger.warning("The group " + thisGrp.getName() + " is declaring be default where" + ph.getDefaultGroup().getName() + " already was."); 343 | GroupManager.logger.warning("Overriding first request."); 344 | } 345 | ph.setDefaultGroup(thisGrp); 346 | } 347 | 348 | //PERMISSIONS NODE 349 | if (thisGroupNode.get("permissions") == null) { 350 | thisGroupNode.put("permissions", new ArrayList()); 351 | } 352 | if (thisGroupNode.get("permissions") instanceof List) { 353 | for (Object o : ((List) thisGroupNode.get("permissions"))) { 354 | thisGrp.addPermission(o.toString()); 355 | } 356 | } else if (thisGroupNode.get("permissions") instanceof String) { 357 | thisGrp.addPermission((String) thisGroupNode.get("permissions")); 358 | } else { 359 | throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List): " + thisGroupNode.get("permissions").getClass().getName()); 360 | } 361 | 362 | //INFO NODE 363 | Map infoNode = (Map) thisGroupNode.get("info"); 364 | if (infoNode != null) { 365 | thisGrp.setVariables(infoNode); 366 | } 367 | 368 | //END INFO NODE 369 | 370 | Object inheritNode = thisGroupNode.get("inheritance"); 371 | if (inheritNode == null) { 372 | thisGroupNode.put("inheritance", new ArrayList()); 373 | } else if (inheritNode instanceof List) { 374 | List groupsInh = (List) inheritNode; 375 | for (String grp : groupsInh) { 376 | if (inheritance.get(groupKey) == null) { 377 | List thisInherits = new ArrayList(); 378 | inheritance.put(groupKey, thisInherits); 379 | } 380 | inheritance.get(groupKey).add(grp); 381 | 382 | } 383 | } 384 | } 385 | } catch (Exception ex) { 386 | ex.printStackTrace(); 387 | throw new Exception("Your Permissions config file is invalid. See console for details."); 388 | } 389 | if (ph.defaultGroup == null) { 390 | throw new IllegalArgumentException("There was no Default Group declared."); 391 | } 392 | for (String groupKey : inheritance.keySet()) { 393 | List inheritedList = inheritance.get(groupKey); 394 | Group thisGroup = ph.getGroup(groupKey); 395 | for (String inheritedKey : inheritedList) { 396 | Group inheritedGroup = ph.getGroup(inheritedKey); 397 | if (thisGroup != null && inheritedGroup != null) { 398 | thisGroup.addInherits(inheritedGroup); 399 | } 400 | } 401 | } 402 | // Process USERS 403 | Map allUsersNode = (Map) rootDataNode.get("users"); 404 | for (String usersKey : allUsersNode.keySet()) { 405 | Map thisUserNode = (Map) allUsersNode.get(usersKey); 406 | User thisUser = ph.createUser(usersKey); 407 | if (thisUser == null) { 408 | throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey); 409 | } 410 | if (thisUserNode.get("permissions") == null) { 411 | thisUserNode.put("permissions", new ArrayList()); 412 | } 413 | if (thisUserNode.get("permissions") instanceof List) { 414 | for (Object o : ((List) thisUserNode.get("permissions"))) { 415 | thisUser.addPermission(o.toString()); 416 | } 417 | } else if (thisUserNode.get("permissions") instanceof String) { 418 | thisUser.addPermission(thisUserNode.get("permissions").toString()); 419 | } 420 | 421 | 422 | //USER INFO NODE - BETA 423 | 424 | //INFO NODE 425 | Map infoNode = (Map) thisUserNode.get("info"); 426 | if (infoNode != null) { 427 | thisUser.setVariables(infoNode); 428 | } 429 | //END INFO NODE - BETA 430 | 431 | if (thisUserNode.get("group") != null) { 432 | Group hisGroup = ph.getGroup(thisUserNode.get("group").toString()); 433 | if (hisGroup == null) { 434 | throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName()); 435 | } 436 | thisUser.setGroup(hisGroup); 437 | } else { 438 | thisUser.setGroup(ph.defaultGroup); 439 | } 440 | } 441 | return ph; 442 | } 443 | 444 | /** 445 | * Returns a data holder for the given file 446 | * @param worldName 447 | * @param groupsFile 448 | * @param usersFile 449 | * @return 450 | * @throws FileNotFoundException 451 | * @throws IOException 452 | */ 453 | public static WorldDataHolder load(String worldName, File groupsFile, File usersFile) throws FileNotFoundException, IOException { 454 | WorldDataHolder ph = new WorldDataHolder(worldName); 455 | ph.groupsFile = groupsFile; 456 | ph.usersFile = usersFile; 457 | 458 | 459 | //READ GROUPS FILE 460 | Yaml yamlGroups = new Yaml(new SafeConstructor()); 461 | Map groupsRootDataNode; 462 | if (!groupsFile.exists()) { 463 | throw new IllegalArgumentException("The file which should contain permissions does not exist!\n" + groupsFile.getPath()); 464 | } 465 | FileInputStream groupsInputStream = new FileInputStream(groupsFile); 466 | try { 467 | groupsRootDataNode = (Map) yamlGroups.load(new UnicodeReader(groupsInputStream)); 468 | if (groupsRootDataNode == null) { 469 | throw new NullPointerException(); 470 | } 471 | } catch (Exception ex) { 472 | throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + groupsFile.getPath(), ex); 473 | } finally { 474 | groupsInputStream.close(); 475 | } 476 | 477 | //PROCESS GROUPS FILE 478 | Map> inheritance = new HashMap>(); 479 | try { 480 | Map allGroupsNode = (Map) groupsRootDataNode.get("groups"); 481 | for (String groupKey : allGroupsNode.keySet()) { 482 | Map thisGroupNode = (Map) allGroupsNode.get(groupKey); 483 | Group thisGrp = ph.createGroup(groupKey); 484 | if (thisGrp == null) { 485 | throw new IllegalArgumentException("I think this user was declared more than once: " + groupKey); 486 | } 487 | if (thisGroupNode.get("default") == null) { 488 | thisGroupNode.put("default", false); 489 | } 490 | if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) { 491 | if (ph.getDefaultGroup() != null) { 492 | GroupManager.logger.warning("The group " + thisGrp.getName() + " is declaring be default where" + ph.getDefaultGroup().getName() + " already was."); 493 | GroupManager.logger.warning("Overriding first request."); 494 | } 495 | ph.setDefaultGroup(thisGrp); 496 | } 497 | 498 | //PERMISSIONS NODE 499 | if (thisGroupNode.get("permissions") == null) { 500 | thisGroupNode.put("permissions", new ArrayList()); 501 | } 502 | if (thisGroupNode.get("permissions") instanceof List) { 503 | for (Object o : ((List) thisGroupNode.get("permissions"))) { 504 | thisGrp.addPermission(o.toString()); 505 | } 506 | } else if (thisGroupNode.get("permissions") instanceof String) { 507 | thisGrp.addPermission((String) thisGroupNode.get("permissions")); 508 | } else { 509 | throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List): " + thisGroupNode.get("permissions").getClass().getName()); 510 | } 511 | 512 | //INFO NODE 513 | Map infoNode = (Map) thisGroupNode.get("info"); 514 | if (infoNode != null) { 515 | thisGrp.setVariables(infoNode); 516 | } 517 | 518 | //END INFO NODE 519 | 520 | Object inheritNode = thisGroupNode.get("inheritance"); 521 | if (inheritNode == null) { 522 | thisGroupNode.put("inheritance", new ArrayList()); 523 | } else if (inheritNode instanceof List) { 524 | List groupsInh = (List) inheritNode; 525 | for (String grp : groupsInh) { 526 | if (inheritance.get(groupKey) == null) { 527 | List thisInherits = new ArrayList(); 528 | inheritance.put(groupKey, thisInherits); 529 | } 530 | inheritance.get(groupKey).add(grp); 531 | 532 | } 533 | } 534 | } 535 | } catch (Exception ex) { 536 | ex.printStackTrace(); 537 | throw new IllegalArgumentException("Your Permissions config file is invalid. See console for details."); 538 | } 539 | if (ph.defaultGroup == null) { 540 | throw new IllegalArgumentException("There was no Default Group declared."); 541 | } 542 | for (String groupKey : inheritance.keySet()) { 543 | List inheritedList = inheritance.get(groupKey); 544 | Group thisGroup = ph.getGroup(groupKey); 545 | for (String inheritedKey : inheritedList) { 546 | Group inheritedGroup = ph.getGroup(inheritedKey); 547 | if (thisGroup != null && inheritedGroup != null) { 548 | thisGroup.addInherits(inheritedGroup); 549 | } 550 | } 551 | } 552 | 553 | 554 | //READ USERS FILE 555 | Yaml yamlUsers = new Yaml(new SafeConstructor()); 556 | Map usersRootDataNode; 557 | if (!groupsFile.exists()) { 558 | throw new IllegalArgumentException("The file which should contain permissions does not exist!\n" + groupsFile.getPath()); 559 | } 560 | FileInputStream usersInputStream = new FileInputStream(usersFile); 561 | try { 562 | usersRootDataNode = (Map) yamlUsers.load(new UnicodeReader(usersInputStream)); 563 | if (usersRootDataNode == null) { 564 | throw new NullPointerException(); 565 | } 566 | } catch (Exception ex) { 567 | throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + groupsFile.getPath(), ex); 568 | } finally { 569 | usersInputStream.close(); 570 | } 571 | 572 | // PROCESS USERS FILE 573 | Map allUsersNode = (Map) usersRootDataNode.get("users"); 574 | for (String usersKey : allUsersNode.keySet()) { 575 | Map thisUserNode = (Map) allUsersNode.get(usersKey); 576 | User thisUser = ph.createUser(usersKey); 577 | if (thisUser == null) { 578 | throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey); 579 | } 580 | if (thisUserNode.get("permissions") == null) { 581 | thisUserNode.put("permissions", new ArrayList()); 582 | } 583 | if (thisUserNode.get("permissions") instanceof List) { 584 | for (Object o : ((List) thisUserNode.get("permissions"))) { 585 | thisUser.addPermission(o.toString()); 586 | } 587 | } else if (thisUserNode.get("permissions") instanceof String) { 588 | thisUser.addPermission(thisUserNode.get("permissions").toString()); 589 | } 590 | 591 | //SUBGROUPS LOADING 592 | if (thisUserNode.get("subgroups") == null) { 593 | thisUserNode.put("subgroups", new ArrayList()); 594 | } 595 | if (thisUserNode.get("subgroups") instanceof List) { 596 | for (Object o : ((List) thisUserNode.get("subgroups"))) { 597 | Group subGrp = ph.getGroup(o.toString()); 598 | if (subGrp != null) { 599 | thisUser.addSubGroup(subGrp); 600 | } else { 601 | GroupManager.logger.warning("Subgroup " + o.toString() + " not found for user " + thisUser.getName() + ". Ignoring entry."); 602 | } 603 | } 604 | } else if (thisUserNode.get("subgroups") instanceof String) { 605 | Group subGrp = ph.getGroup(thisUserNode.get("subgroups").toString()); 606 | if (subGrp != null) { 607 | thisUser.addSubGroup(subGrp); 608 | } else { 609 | GroupManager.logger.warning("Subgroup " + thisUserNode.get("subgroups").toString() + " not found for user " + thisUser.getName() + ". Ignoring entry."); 610 | } 611 | } 612 | 613 | 614 | //USER INFO NODE - BETA 615 | 616 | //INFO NODE 617 | Map infoNode = (Map) thisUserNode.get("info"); 618 | if (infoNode != null) { 619 | thisUser.setVariables(infoNode); 620 | } 621 | //END INFO NODE - BETA 622 | 623 | if (thisUserNode.get("group") != null) { 624 | Group hisGroup = ph.getGroup(thisUserNode.get("group").toString()); 625 | if (hisGroup == null) { 626 | throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName()); 627 | } 628 | thisUser.setGroup(hisGroup); 629 | } else { 630 | thisUser.setGroup(ph.defaultGroup); 631 | } 632 | } 633 | return ph; 634 | } 635 | 636 | /** 637 | * Write a dataHolder in a specified file 638 | * @param ph 639 | * @param file 640 | * @deprecated 641 | */ 642 | @Deprecated 643 | public static void write(WorldDataHolder ph, File file) { 644 | Map root = new HashMap(); 645 | 646 | Map pluginMap = new HashMap(); 647 | root.put("plugin", pluginMap); 648 | 649 | Map permissionsMap = new HashMap(); 650 | pluginMap.put("permissions", permissionsMap); 651 | 652 | permissionsMap.put("system", "default"); 653 | 654 | Map groupsMap = new HashMap(); 655 | root.put("groups", groupsMap); 656 | for (String groupKey : ph.groups.keySet()) { 657 | Group group = ph.groups.get(groupKey); 658 | 659 | Map aGroupMap = new HashMap(); 660 | groupsMap.put(group.getName(), aGroupMap); 661 | 662 | aGroupMap.put("default", group.equals(ph.defaultGroup)); 663 | 664 | Map infoMap = new HashMap(); 665 | aGroupMap.put("info", infoMap); 666 | 667 | for (String infoKey : group.getVariables().getVarKeyList()) { 668 | infoMap.put(infoKey, group.getVariables().getVarObject(infoKey)); 669 | } 670 | 671 | aGroupMap.put("inheritance", group.getInherits()); 672 | 673 | aGroupMap.put("permissions", group.getPermissionList()); 674 | } 675 | 676 | Map usersMap = new HashMap(); 677 | root.put("users", usersMap); 678 | for (String userKey : ph.users.keySet()) { 679 | User user = ph.users.get(userKey); 680 | if ((user.getGroup() == null || user.getGroup().equals(ph.defaultGroup)) && user.getPermissionList().isEmpty()) { 681 | continue; 682 | } 683 | 684 | Map aUserMap = new HashMap(); 685 | usersMap.put(user.getName(), aUserMap); 686 | 687 | if (user.getGroup() == null) { 688 | aUserMap.put("group", ph.defaultGroup.getName()); 689 | } else { 690 | aUserMap.put("group", user.getGroup().getName()); 691 | } 692 | //USER INFO NODE - BETA 693 | if (user.getVariables().getSize() > 0) { 694 | Map infoMap = new HashMap(); 695 | aUserMap.put("info", infoMap); 696 | 697 | for (String infoKey : user.getVariables().getVarKeyList()) { 698 | infoMap.put(infoKey, user.getVariables().getVarObject(infoKey)); 699 | } 700 | } 701 | //END USER INFO NODE - BETA 702 | 703 | aUserMap.put("permissions", user.getPermissionList()); 704 | } 705 | DumperOptions opt = new DumperOptions(); 706 | opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); 707 | final Yaml yaml = new Yaml(opt); 708 | 709 | FileWriter tx = null; 710 | try { 711 | tx = new FileWriter(file, false); 712 | tx.write(yaml.dump(root)); 713 | tx.flush(); 714 | } catch (Exception e) { 715 | } finally { 716 | try { 717 | tx.close(); 718 | } catch (IOException ex) { 719 | } 720 | } 721 | } 722 | 723 | /** 724 | * Write a dataHolder in a specified file 725 | * @param ph 726 | * @param groupsFile 727 | */ 728 | public static void writeGroups(WorldDataHolder ph, File groupsFile) { 729 | Map root = new HashMap(); 730 | 731 | Map groupsMap = new HashMap(); 732 | root.put("groups", groupsMap); 733 | for (String groupKey : ph.groups.keySet()) { 734 | Group group = ph.groups.get(groupKey); 735 | 736 | Map aGroupMap = new HashMap(); 737 | groupsMap.put(group.getName(), aGroupMap); 738 | 739 | if (ph.defaultGroup == null) { 740 | GroupManager.logger.severe("There is no default group for world: " + ph.getName()); 741 | } 742 | aGroupMap.put("default", group.equals(ph.defaultGroup)); 743 | 744 | Map infoMap = new HashMap(); 745 | aGroupMap.put("info", infoMap); 746 | 747 | for (String infoKey : group.getVariables().getVarKeyList()) { 748 | infoMap.put(infoKey, group.getVariables().getVarObject(infoKey)); 749 | } 750 | 751 | aGroupMap.put("inheritance", group.getInherits()); 752 | 753 | aGroupMap.put("permissions", group.getPermissionList()); 754 | } 755 | 756 | DumperOptions opt = new DumperOptions(); 757 | opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); 758 | final Yaml yaml = new Yaml(opt); 759 | 760 | FileWriter tx = null; 761 | try { 762 | tx = new FileWriter(groupsFile, false); 763 | tx.write(yaml.dump(root)); 764 | tx.flush(); 765 | } catch (Exception e) { 766 | } finally { 767 | try { 768 | tx.close(); 769 | } catch (IOException ex) { 770 | } 771 | } 772 | } 773 | 774 | /** 775 | * Write a dataHolder in a specified file 776 | * @param ph 777 | * @param usersFile 778 | */ 779 | public static void writeUsers(WorldDataHolder ph, File usersFile) { 780 | Map root = new HashMap(); 781 | 782 | Map usersMap = new HashMap(); 783 | root.put("users", usersMap); 784 | for (String userKey : ph.users.keySet()) { 785 | User user = ph.users.get(userKey); 786 | if ((user.getGroup() == null || user.getGroup().equals(ph.defaultGroup)) && user.getPermissionList().isEmpty() && user.getVariables().isEmpty() && user.isSubGroupsEmpty()) { 787 | continue; 788 | } 789 | 790 | Map aUserMap = new HashMap(); 791 | usersMap.put(user.getName(), aUserMap); 792 | 793 | if (user.getGroup() == null) { 794 | aUserMap.put("group", ph.defaultGroup.getName()); 795 | } else { 796 | aUserMap.put("group", user.getGroup().getName()); 797 | } 798 | //USER INFO NODE - BETA 799 | if (user.getVariables().getSize() > 0) { 800 | Map infoMap = new HashMap(); 801 | aUserMap.put("info", infoMap); 802 | for (String infoKey : user.getVariables().getVarKeyList()) { 803 | infoMap.put(infoKey, user.getVariables().getVarObject(infoKey)); 804 | } 805 | } 806 | //END USER INFO NODE - BETA 807 | aUserMap.put("permissions", user.getPermissionList()); 808 | 809 | //SUBGROUPS NODE - BETA 810 | aUserMap.put("subgroups", user.subGroupListStringCopy()); 811 | //END SUBGROUPS NODE - BETA 812 | } 813 | DumperOptions opt = new DumperOptions(); 814 | opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); 815 | final Yaml yaml = new Yaml(opt); 816 | 817 | FileWriter tx = null; 818 | try { 819 | tx = new FileWriter(usersFile, false); 820 | tx.write(yaml.dump(root)); 821 | tx.flush(); 822 | } catch (Exception e) { 823 | } finally { 824 | try { 825 | tx.close(); 826 | } catch (IOException ex) { 827 | } 828 | } 829 | } 830 | 831 | /** 832 | * Don't use this. Unless you want to make this plugin to interact with original Nijikokun Permissions 833 | * This method is supposed to make the original one reload the file, and propagate the changes made here. 834 | * 835 | * Prefer to use the AnjoCaido's fake version of Nijikokun's Permission plugin. 836 | * The AnjoCaido's Permission can propagate the changes made on this plugin instantly, 837 | * without need to save the file. 838 | * 839 | * @param server the server that holds the plugin 840 | * @deprecated it is not used anymore... unless if you use original Permissions 841 | */ 842 | @Deprecated 843 | public static void reloadOldPlugins(Server server) { 844 | // Only reload permissions 845 | PluginManager pm = server.getPluginManager(); 846 | Plugin[] plugins = pm.getPlugins(); 847 | for (int i = 0; i < plugins.length; i++) { 848 | plugins[i].getConfiguration().load(); 849 | try { 850 | plugins[i].getClass().getMethod("setupPermissions").invoke(plugins[i]); 851 | } catch (Exception ex) { 852 | continue; 853 | } 854 | } 855 | } 856 | 857 | /** 858 | * @return the permissionsHandler 859 | */ 860 | public AnjoPermissionsHandler getPermissionsHandler() { 861 | if (permissionsHandler == null) { 862 | permissionsHandler = new AnjoPermissionsHandler(this); 863 | } 864 | return permissionsHandler; 865 | } 866 | 867 | /** 868 | * 869 | * @return 870 | */ 871 | public boolean haveUsersChanged() { 872 | if (haveUsersChanged) { 873 | return true; 874 | } 875 | for (User u : users.values()) { 876 | if (u.isChanged()) { 877 | return true; 878 | } 879 | } 880 | return false; 881 | } 882 | 883 | /** 884 | * 885 | * @return 886 | */ 887 | public boolean haveGroupsChanged() { 888 | if (haveGroupsChanged) { 889 | return true; 890 | } 891 | for (Group g : groups.values()) { 892 | if (g.isChanged()) { 893 | return true; 894 | } 895 | } 896 | return false; 897 | } 898 | 899 | /** 900 | * 901 | */ 902 | public void removeUsersChangedFlag() { 903 | haveUsersChanged = false; 904 | for (User u : users.values()) { 905 | u.flagAsSaved(); 906 | } 907 | } 908 | 909 | /** 910 | * 911 | */ 912 | public void removeGroupsChangedFlag() { 913 | haveGroupsChanged = false; 914 | for (Group g : groups.values()) { 915 | g.flagAsSaved(); 916 | } 917 | } 918 | 919 | /** 920 | * @return the usersFile 921 | */ 922 | public File getUsersFile() { 923 | return usersFile; 924 | } 925 | 926 | /** 927 | * @return the groupsFile 928 | */ 929 | public File getGroupsFile() { 930 | return groupsFile; 931 | } 932 | 933 | /** 934 | * @return the name 935 | */ 936 | public String getName() { 937 | return name; 938 | } 939 | } 940 | --------------------------------------------------------------------------------