summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java
diff options
context:
space:
mode:
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java')
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java300
1 files changed, 249 insertions, 51 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java
index 4a9f806d8..4140cbfea 100644
--- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java
@@ -45,20 +45,23 @@ public class WorldDataHolder {
* The actual groups holder
*/
protected Map<String, Group> groups = new HashMap<String, Group>();
- /**
+ /**
* The actual users holder
*/
protected Map<String, User> users = new HashMap<String, User>();
- /**
+
+ /**
* Points to the default group
*/
protected Group defaultGroup = null;
+
/**
* The file, which this class loads/save data from/to
* @deprecated
*/
@Deprecated
protected File f;
+
/**
*
*/
@@ -79,8 +82,17 @@ public class WorldDataHolder {
*
*/
protected boolean haveGroupsChanged = false;
-
/**
+ *
+ */
+ protected long timeStampGroups = 0;
+ /**
+ *
+ */
+ protected long timeStampUsers = 0;
+
+
+ /**
* Prevent direct instantiation
* @param worldName
*/
@@ -151,7 +163,7 @@ public class WorldDataHolder {
/**
*
* @param userName
- * @return
+ * @return true if we have data for this player.
*/
public boolean isUserDeclared(String userName) {
return users.containsKey(userName.toLowerCase());
@@ -183,7 +195,10 @@ public class WorldDataHolder {
* @return a group if it is found. null if not found.
*/
public Group getGroup(String groupName) {
- return groups.get(groupName.toLowerCase());
+ if (groupName.startsWith("g:"))
+ return GroupManager.getGlobalGroups().getGroup(groupName);
+ else
+ return groups.get(groupName.toLowerCase());
}
/**
@@ -193,7 +208,10 @@ public class WorldDataHolder {
* @return true if exists. false if not.
*/
public boolean groupExists(String groupName) {
- return groups.containsKey(groupName.toLowerCase());
+ if (groupName.startsWith("g:"))
+ return GroupManager.getGlobalGroups().hasGroup(groupName);
+ else
+ return groups.containsKey(groupName.toLowerCase());
}
/**
@@ -201,6 +219,11 @@ public class WorldDataHolder {
* @param groupToAdd
*/
public void addGroup(Group groupToAdd) {
+ if (groupToAdd.getName().startsWith("g:")) {
+ GroupManager.getGlobalGroups().addGroup(groupToAdd);
+ return;
+ }
+
if (groupToAdd.getDataSource() != this) {
groupToAdd = groupToAdd.clone(this);
}
@@ -215,6 +238,10 @@ public class WorldDataHolder {
* @return true if had something to remove. false the group was default or non-existant
*/
public boolean removeGroup(String groupName) {
+ if (groupName.startsWith("g:")) {
+ return GroupManager.getGlobalGroups().removeGroup(groupName);
+ }
+
if (defaultGroup != null && groupName.equalsIgnoreCase(defaultGroup.getName())) {
return false;
}
@@ -251,10 +278,16 @@ public class WorldDataHolder {
* @return null if group already exists. or new Group
*/
public Group createGroup(String groupName) {
- if (this.groups.containsKey(groupName.toLowerCase())) {
+ if (groupName.startsWith("g:")) {
+ Group newGroup = new Group(groupName);
+ return GroupManager.getGlobalGroups().newGroup(newGroup);
+ }
+
+ if (this.groups.containsKey(groupName.toLowerCase())) {
return null;
}
- Group newGroup = new Group(this, groupName);
+
+ Group newGroup = new Group(this, groupName);
this.addGroup(newGroup);
haveGroupsChanged = true;
return newGroup;
@@ -281,14 +314,34 @@ public class WorldDataHolder {
*/
public void reload() {
try {
- WorldDataHolder ph = load(this.getName(), getGroupsFile(), getUsersFile());
- this.defaultGroup = ph.defaultGroup;
- this.groups = ph.groups;
- this.users = ph.users;
+ reloadGroups();
+ reloadUsers();
} catch (Exception ex) {
Logger.getLogger(WorldDataHolder.class.getName()).log(Level.SEVERE, null, ex);
}
}
+
+ public void reloadGroups() {
+ GroupManager.setLoaded(false);
+ try {
+ resetGroups();
+ loadGroups(this, getGroupsFile());
+ } catch (Exception ex) {
+ Logger.getLogger(WorldDataHolder.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ GroupManager.setLoaded(true);
+ }
+
+ public void reloadUsers() {
+ GroupManager.setLoaded(false);
+ try {
+ resetUsers();
+ loadUsers(this, getUsersFile());
+ } catch (Exception ex) {
+ Logger.getLogger(WorldDataHolder.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ GroupManager.setLoaded(true);
+ }
/**
* Save by yourself!
@@ -302,9 +355,11 @@ public class WorldDataHolder {
/**
* Returns a data holder for the given file
+ *
* @param worldName
* @param file
- * @return
+ * @return a new WorldDataHolder
+ *
* @throws Exception
* @deprecated
*/
@@ -448,26 +503,66 @@ public class WorldDataHolder {
}
/**
- * Returns a data holder for the given file
+ * Returns a NEW data holder containing data read from the files
+ *
* @param worldName
* @param groupsFile
* @param usersFile
- * @return
+ *
* @throws FileNotFoundException
* @throws IOException
*/
- @SuppressWarnings({"rawtypes", "unchecked"})
public static WorldDataHolder load(String worldName, File groupsFile, File usersFile) throws FileNotFoundException, IOException {
- WorldDataHolder ph = new WorldDataHolder(worldName);
- ph.groupsFile = groupsFile;
- ph.usersFile = usersFile;
+ WorldDataHolder ph = new WorldDataHolder(worldName);
+
+ GroupManager.setLoaded(false);
+ loadGroups(ph, groupsFile);
+ loadUsers(ph, usersFile);
+ GroupManager.setLoaded(true);
+
+ return ph;
+ }
+
+ /**
+ * Updates the WorldDataHolder from the files
+ *
+ * @param ph
+ * @param groupsFile
+ * @param usersFile
+ *
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ public static WorldDataHolder Update(WorldDataHolder ph, File groupsFile, File usersFile) throws FileNotFoundException, IOException {
+ GroupManager.setLoaded(false);
+ ph.resetGroups();
+ loadGroups(ph, groupsFile);
+
+ ph.resetUsers();
+ loadUsers(ph, usersFile);
+ GroupManager.setLoaded(true);
+
+ return ph;
+ }
+
+ /**
+ * Updates the WorldDataHolder from the Groups file
+ *
+ * @param ph
+ * @param groupsFile
+ *
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ protected static void loadGroups(WorldDataHolder ph, File groupsFile) throws FileNotFoundException, IOException {
//READ GROUPS FILE
Yaml yamlGroups = new Yaml(new SafeConstructor());
Map<String, Object> groupsRootDataNode;
if (!groupsFile.exists()) {
- throw new IllegalArgumentException("The file which should contain permissions does not exist!\n" + groupsFile.getPath());
+ throw new IllegalArgumentException("The file which should contain groups does not exist!\n" + groupsFile.getPath());
}
FileInputStream groupsInputStream = new FileInputStream(groupsFile);
try {
@@ -483,7 +578,7 @@ public class WorldDataHolder {
//PROCESS GROUPS FILE
Map<String, List<String>> inheritance = new HashMap<String, List<String>>();
- try {
+ //try {
Map<String, Object> allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups");
for (String groupKey : allGroupsNode.keySet()) {
Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey);
@@ -513,36 +608,43 @@ public class WorldDataHolder {
} else if (thisGroupNode.get("permissions") instanceof String) {
thisGrp.addPermission((String) thisGroupNode.get("permissions"));
} else {
- throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>): " + thisGroupNode.get("permissions").getClass().getName());
+ throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>) for group: " + thisGrp.getName());
}
//INFO NODE
- Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info");
- if (infoNode != null) {
- thisGrp.setVariables(infoNode);
- }
+ if (thisGroupNode.get("info") instanceof Map) {
+ Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info");
+ if (infoNode != null) {
+ thisGrp.setVariables(infoNode);
+ }
+ } else
+ throw new IllegalArgumentException("Unknown entry found in Info section for group: " + thisGrp.getName());
+
//END INFO NODE
- Object inheritNode = thisGroupNode.get("inheritance");
- if (inheritNode == null) {
- thisGroupNode.put("inheritance", new ArrayList<String>());
- } else if (inheritNode instanceof List) {
- List<String> groupsInh = (List<String>) inheritNode;
- for (String grp : groupsInh) {
- if (inheritance.get(groupKey) == null) {
- List<String> thisInherits = new ArrayList<String>();
- inheritance.put(groupKey, thisInherits);
- }
- inheritance.get(groupKey).add(grp);
-
- }
- }
+ if (thisGroupNode.get("inheritance") == null || thisGroupNode.get("inheritance") instanceof List) {
+ Object inheritNode = thisGroupNode.get("inheritance");
+ if (inheritNode == null) {
+ thisGroupNode.put("inheritance", new ArrayList<String>());
+ } else if (inheritNode instanceof List) {
+ List<String> groupsInh = (List<String>) inheritNode;
+ for (String grp : groupsInh) {
+ if (inheritance.get(groupKey) == null) {
+ List<String> thisInherits = new ArrayList<String>();
+ inheritance.put(groupKey, thisInherits);
+ }
+ inheritance.get(groupKey).add(grp);
+
+ }
+ }
+ }else
+ throw new IllegalArgumentException("Unknown entry found in inheritance section for group: " + thisGrp.getName());
}
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new IllegalArgumentException("Your Permissions config file is invalid. See console for details.");
- }
+ //} catch (Exception ex) {
+ // ex.printStackTrace();
+ // throw new IllegalArgumentException("Your Permissions config file is invalid. See console for details.");
+ //}
if (ph.defaultGroup == null) {
throw new IllegalArgumentException("There was no Default Group declared.");
}
@@ -556,13 +658,32 @@ public class WorldDataHolder {
}
}
}
+
+ ph.removeGroupsChangedFlag();
+ // Update the LastModified time.
+ ph.groupsFile = groupsFile;
+ ph.setTimeStamps();
-
+ //return ph;
+ }
+
+ /**
+ * Updates the WorldDataHolder from the Users file
+ *
+ * @param ph
+ * @param usersFile
+ *
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ protected static void loadUsers(WorldDataHolder ph, File usersFile) throws FileNotFoundException, IOException {
+
//READ USERS FILE
Yaml yamlUsers = new Yaml(new SafeConstructor());
Map<String, Object> usersRootDataNode;
- if (!groupsFile.exists()) {
- throw new IllegalArgumentException("The file which should contain permissions does not exist!\n" + usersFile.getPath());
+ if (!usersFile.exists()) {
+ throw new IllegalArgumentException("The file which should contain users does not exist!\n" + usersFile.getPath());
}
FileInputStream usersInputStream = new FileInputStream(usersFile);
try {
@@ -581,7 +702,7 @@ public class WorldDataHolder {
// Stop loading if the file is empty
if (allUsersNode == null)
- return ph;
+ return ;
for (String usersKey : allUsersNode.keySet()) {
Map<String, Object> thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
@@ -644,7 +765,13 @@ public class WorldDataHolder {
thisUser.setGroup(ph.defaultGroup);
}
}
- return ph;
+
+ ph.removeUsersChangedFlag();
+ // Update the LastModified time.
+ ph.usersFile = usersFile;
+ ph.setTimeStamps();
+
+ //return ph;
}
/**
@@ -777,6 +904,11 @@ public class WorldDataHolder {
} catch (FileNotFoundException ex) {
}
}
+
+ // Update the LastModified time.
+ ph.groupsFile = groupsFile;
+ ph.setTimeStampGroups(groupsFile.lastModified());
+ ph.removeGroupsChangedFlag();
/*FileWriter tx = null;
try {
@@ -842,6 +974,12 @@ public class WorldDataHolder {
} catch (FileNotFoundException ex) {
}
}
+
+ // Update the LastModified time.
+ ph.usersFile = usersFile;
+ ph.setTimeStampUsers(usersFile.lastModified());
+ ph.removeUsersChangedFlag();
+
/*FileWriter tx = null;
try {
tx = new FileWriter(usersFile, false);
@@ -894,7 +1032,7 @@ public class WorldDataHolder {
/**
*
- * @return
+ * @return true if any user data has changed
*/
public boolean haveUsersChanged() {
if (haveUsersChanged) {
@@ -910,7 +1048,7 @@ public class WorldDataHolder {
/**
*
- * @return
+ * @return true if any group data has changed.
*/
public boolean haveGroupsChanged() {
if (haveGroupsChanged) {
@@ -964,4 +1102,64 @@ public class WorldDataHolder {
public String getName() {
return name;
}
+
+ /**
+ * Resets Groups.
+ */
+ public void resetGroups() {
+ this.defaultGroup = null;
+ this.groups = new HashMap<String, Group>();
+ }
+ /**
+ * Resets Users
+ */
+ public void resetUsers() {
+ this.users = new HashMap<String, User>();
+ }
+
+ /**
+ * @return the groups
+ */
+ public Map<String, Group> getGroups() {
+ return groups;
+ }
+ /**
+ * @return the users
+ */
+ public Map<String, User> getUsers() {
+ return users;
+ }
+
+ /**
+ * @return the timeStampGroups
+ */
+ public long getTimeStampGroups() {
+ return timeStampGroups;
+ }
+ /**
+ * @return the timeStampUsers
+ */
+ public long getTimeStampUsers() {
+ return timeStampUsers;
+ }
+
+ /**
+ * @param timeStampGroups the timeStampGroups to set
+ */
+ protected void setTimeStampGroups(long timeStampGroups) {
+ this.timeStampGroups = timeStampGroups;
+ }
+ /**
+ * @param timeStampUsers the timeStampUsers to set
+ */
+ protected void setTimeStampUsers(long timeStampUsers) {
+ this.timeStampUsers = timeStampUsers;
+ }
+
+ public void setTimeStamps() {
+ if (groupsFile != null)
+ setTimeStampGroups(groupsFile.lastModified());
+ if (usersFile != null)
+ setTimeStampUsers(usersFile.lastModified());
+ }
}