diff options
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder')
5 files changed, 1041 insertions, 531 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java index eaaaace74..8169df321 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java @@ -6,20 +6,20 @@ import java.util.Map; import org.anjocaido.groupmanager.data.Group;
+
/**
* This container holds all Groups loaded from the relevant groupsFile.
- *
+ *
* @author ElgarL
- *
+ *
*/
-public class GroupsDataHolder {
-
+public class GroupsDataHolder
+{
private WorldDataHolder dataSource;
private Group defaultGroup = null;
private File groupsFile;
private boolean haveGroupsChanged = false;
private long timeStampGroups = 0;
-
/**
* The actual groups holder
*/
@@ -28,22 +28,26 @@ public class GroupsDataHolder { /**
* Constructor
*/
- protected GroupsDataHolder() {
-
+ protected GroupsDataHolder()
+ {
}
- public void setDataSource(WorldDataHolder dataSource) {
+ public void setDataSource(WorldDataHolder dataSource)
+ {
this.dataSource = dataSource;
//push this data source to the users, so they pull the correct groups data.
for (Group group : groups.values())
+ {
group.setDataSource(this.dataSource);
+ }
}
/**
* @return the defaultGroup
*/
- public Group getDefaultGroup() {
+ public Group getDefaultGroup()
+ {
return defaultGroup;
}
@@ -51,7 +55,8 @@ public class GroupsDataHolder { /**
* @param defaultGroup the defaultGroup to set
*/
- public void setDefaultGroup(Group defaultGroup) {
+ public void setDefaultGroup(Group defaultGroup)
+ {
this.defaultGroup = defaultGroup;
}
@@ -59,7 +64,8 @@ public class GroupsDataHolder { /**
* @return the groups
*/
- public Map<String, Group> getGroups() {
+ public Map<String, Group> getGroups()
+ {
return groups;
}
@@ -67,7 +73,8 @@ public class GroupsDataHolder { /**
* @param groups the groups to set
*/
- public void setGroups(Map<String, Group> groups) {
+ public void setGroups(Map<String, Group> groups)
+ {
this.groups = groups;
}
@@ -75,7 +82,8 @@ public class GroupsDataHolder { /**
* @return the groupsFile
*/
- public File getGroupsFile() {
+ public File getGroupsFile()
+ {
return groupsFile;
}
@@ -83,7 +91,8 @@ public class GroupsDataHolder { /**
* @param groupsFile the groupsFile to set
*/
- public void setGroupsFile(File groupsFile) {
+ public void setGroupsFile(File groupsFile)
+ {
this.groupsFile = groupsFile;
}
@@ -91,7 +100,8 @@ public class GroupsDataHolder { /**
* @return the haveGroupsChanged
*/
- public boolean HaveGroupsChanged() {
+ public boolean HaveGroupsChanged()
+ {
return haveGroupsChanged;
}
@@ -99,7 +109,8 @@ public class GroupsDataHolder { /**
* @param haveGroupsChanged the haveGroupsChanged to set
*/
- public void setGroupsChanged(boolean haveGroupsChanged) {
+ public void setGroupsChanged(boolean haveGroupsChanged)
+ {
this.haveGroupsChanged = haveGroupsChanged;
}
@@ -107,7 +118,8 @@ public class GroupsDataHolder { /**
* @return the timeStampGroups
*/
- public long getTimeStampGroups() {
+ public long getTimeStampGroups()
+ {
return timeStampGroups;
}
@@ -115,9 +127,9 @@ public class GroupsDataHolder { /**
* @param timeStampGroups the timeStampGroups to set
*/
- public void setTimeStampGroups(long timeStampGroups) {
+ public void setTimeStampGroups(long timeStampGroups)
+ {
this.timeStampGroups = timeStampGroups;
}
-
}
\ No newline at end of file diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java index 84561b6e5..4bc32cb4e 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java @@ -10,22 +10,24 @@ import java.util.HashMap; import java.util.Map; import org.anjocaido.groupmanager.data.User; + /** - * + * * @author gabrielcouto */ -public class OverloadedWorldHolder extends WorldDataHolder { - +public class OverloadedWorldHolder extends WorldDataHolder +{ /** - * - */ + * + */ protected Map<String, User> overloadedUsers = new HashMap<String, User>(); /** - * + * * @param ph */ - public OverloadedWorldHolder(WorldDataHolder ph) { + public OverloadedWorldHolder(WorldDataHolder ph) + { super(ph.getName()); this.setGroupsFile(ph.getGroupsFile()); @@ -35,20 +37,23 @@ public class OverloadedWorldHolder extends WorldDataHolder { } /** - * + * * @param userName * @return user object or a new user if none exists. */ @Override - public User getUser(String userName) { + public User getUser(String userName) + { //OVERLOADED CODE String userNameLowered = userName.toLowerCase(); - if (overloadedUsers.containsKey(userNameLowered)) { + if (overloadedUsers.containsKey(userNameLowered)) + { return overloadedUsers.get(userNameLowered); } //END CODE - if (getUsers().containsKey(userNameLowered)) { + if (getUsers().containsKey(userNameLowered)) + { return getUsers().get(userNameLowered); } User newUser = createUser(userName); @@ -57,23 +62,28 @@ public class OverloadedWorldHolder extends WorldDataHolder { } /** - * + * * @param theUser */ @Override - public void addUser(User theUser) { + public void addUser(User theUser) + { - if (theUser.getDataSource() != this) { + if (theUser.getDataSource() != this) + { theUser = theUser.clone(this); } - if (theUser == null) { + if (theUser == null) + { return; } - if ((theUser.getGroup() == null) || (!getGroups().containsKey(theUser.getGroupName().toLowerCase()))) { + if ((theUser.getGroup() == null) || (!getGroups().containsKey(theUser.getGroupName().toLowerCase()))) + { theUser.setGroup(getDefaultGroup()); } //OVERLOADED CODE - if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) { + if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) + { overloadedUsers.remove(theUser.getName().toLowerCase()); overloadedUsers.put(theUser.getName().toLowerCase(), theUser); return; @@ -85,20 +95,23 @@ public class OverloadedWorldHolder extends WorldDataHolder { } /** - * + * * @param userName * @return true if removed/false if not found. */ @Override - public boolean removeUser(String userName) { + public boolean removeUser(String userName) + { //OVERLOADED CODE - if (overloadedUsers.containsKey(userName.toLowerCase())) { + if (overloadedUsers.containsKey(userName.toLowerCase())) + { overloadedUsers.remove(userName.toLowerCase()); return true; } //END CODE - if (getUsers().containsKey(userName.toLowerCase())) { + if (getUsers().containsKey(userName.toLowerCase())) + { getUsers().remove(userName.toLowerCase()); setUsersChanged(true); return true; @@ -107,25 +120,33 @@ public class OverloadedWorldHolder extends WorldDataHolder { } @Override - public boolean removeGroup(String groupName) { + public boolean removeGroup(String groupName) + { - if (groupName.equals(getDefaultGroup())) { + if (groupName.equals(getDefaultGroup())) + { return false; } - for (String key : getGroups().keySet()) { - if (groupName.equalsIgnoreCase(key)) { + for (String key : getGroups().keySet()) + { + if (groupName.equalsIgnoreCase(key)) + { getGroups().remove(key); - for (String userKey : getUsers().keySet()) { + for (String userKey : getUsers().keySet()) + { User user = getUsers().get(userKey); - if (user.getGroupName().equalsIgnoreCase(key)) { + if (user.getGroupName().equalsIgnoreCase(key)) + { user.setGroup(getDefaultGroup()); } } //OVERLOADED CODE - for (String userKey : overloadedUsers.keySet()) { + for (String userKey : overloadedUsers.keySet()) + { User user = overloadedUsers.get(userKey); - if (user.getGroupName().equalsIgnoreCase(key)) { + if (user.getGroupName().equalsIgnoreCase(key)) + { user.setGroup(getDefaultGroup()); } @@ -139,18 +160,23 @@ public class OverloadedWorldHolder extends WorldDataHolder { } /** - * + * * @return Collection of all users */ @Override - public Collection<User> getUserList() { + public Collection<User> getUserList() + { Collection<User> overloadedList = new ArrayList<User>(); Collection<User> normalList = getUsers().values(); - for (User u : normalList) { - if (overloadedUsers.containsKey(u.getName().toLowerCase())) { + for (User u : normalList) + { + if (overloadedUsers.containsKey(u.getName().toLowerCase())) + { overloadedList.add(overloadedUsers.get(u.getName().toLowerCase())); - } else { + } + else + { overloadedList.add(u); } } @@ -158,25 +184,29 @@ public class OverloadedWorldHolder extends WorldDataHolder { } /** - * + * * @param userName * @return true if user is overloaded. */ - public boolean isOverloaded(String userName) { + public boolean isOverloaded(String userName) + { return overloadedUsers.containsKey(userName.toLowerCase()); } /** - * + * * @param userName */ - public void overloadUser(String userName) { + public void overloadUser(String userName) + { - if (!isOverloaded(userName)) { + if (!isOverloaded(userName)) + { User theUser = getUser(userName); theUser = theUser.clone(); - if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) { + if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) + { overloadedUsers.remove(theUser.getName().toLowerCase()); } overloadedUsers.put(theUser.getName().toLowerCase(), theUser); @@ -184,28 +214,31 @@ public class OverloadedWorldHolder extends WorldDataHolder { } /** - * + * * @param userName */ - public void removeOverload(String userName) { + public void removeOverload(String userName) + { overloadedUsers.remove(userName.toLowerCase()); } /** - * Gets the user in normal state. Surpassing the overload state. - * It doesn't affect permissions. But it enables plugins change the - * actual user permissions even in overload mode. - * + * Gets the user in normal state. Surpassing the overload state. It doesn't affect permissions. But it enables + * plugins change the actual user permissions even in overload mode. + * * @param userName * @return user object */ - public User surpassOverload(String userName) { + public User surpassOverload(String userName) + { - if (!isOverloaded(userName)) { + if (!isOverloaded(userName)) + { return getUser(userName); } - if (getUsers().containsKey(userName.toLowerCase())) { + if (getUsers().containsKey(userName.toLowerCase())) + { return getUsers().get(userName.toLowerCase()); } User newUser = createUser(userName); diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java index 665fe227d..6d8701fd4 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java @@ -6,19 +6,19 @@ import java.util.Map; import org.anjocaido.groupmanager.data.User;
+
/**
* This container holds all Users loaded from the relevant usersFile.
- *
+ *
* @author ElgarL
- *
+ *
*/
-public class UsersDataHolder {
-
+public class UsersDataHolder
+{
private WorldDataHolder dataSource;
private File usersFile;
private boolean haveUsersChanged = false;
private long timeStampUsers = 0;
-
/**
* The actual groups holder
*/
@@ -27,23 +27,27 @@ public class UsersDataHolder { /**
* Constructor
*/
- protected UsersDataHolder() {
-
+ protected UsersDataHolder()
+ {
}
- public void setDataSource(WorldDataHolder dataSource) {
+ public void setDataSource(WorldDataHolder dataSource)
+ {
this.dataSource = dataSource;
//push this data source to the users, so they pull the correct groups data.
for (User user : users.values())
+ {
user.setDataSource(this.dataSource);
+ }
}
/**
* @return the users
*/
- public Map<String, User> getUsers() {
+ public Map<String, User> getUsers()
+ {
return users;
}
@@ -51,7 +55,8 @@ public class UsersDataHolder { /**
* @param users the users to set
*/
- public void setUsers(Map<String, User> users) {
+ public void setUsers(Map<String, User> users)
+ {
this.users = users;
}
@@ -59,7 +64,8 @@ public class UsersDataHolder { /**
* @return the usersFile
*/
- public File getUsersFile() {
+ public File getUsersFile()
+ {
return usersFile;
}
@@ -67,7 +73,8 @@ public class UsersDataHolder { /**
* @param usersFile the usersFile to set
*/
- public void setUsersFile(File usersFile) {
+ public void setUsersFile(File usersFile)
+ {
this.usersFile = usersFile;
}
@@ -75,7 +82,8 @@ public class UsersDataHolder { /**
* @return the haveUsersChanged
*/
- public boolean HaveUsersChanged() {
+ public boolean HaveUsersChanged()
+ {
return haveUsersChanged;
}
@@ -83,7 +91,8 @@ public class UsersDataHolder { /**
* @param haveUsersChanged the haveUsersChanged to set
*/
- public void setUsersChanged(boolean haveUsersChanged) {
+ public void setUsersChanged(boolean haveUsersChanged)
+ {
this.haveUsersChanged = haveUsersChanged;
}
@@ -91,7 +100,8 @@ public class UsersDataHolder { /**
* @return the timeStampUsers
*/
- public long getTimeStampUsers() {
+ public long getTimeStampUsers()
+ {
return timeStampUsers;
}
@@ -99,9 +109,9 @@ public class UsersDataHolder { /**
* @param timeStampUsers the timeStampUsers to set
*/
- public void setTimeStampUsers(long timeStampUsers) {
+ public void setTimeStampUsers(long timeStampUsers)
+ {
this.timeStampUsers = timeStampUsers;
}
-
}
\ No newline at end of file diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java index fefc698f9..df1c3c078 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java @@ -36,15 +36,15 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.reader.UnicodeReader; + /** - * One instance of this should exist per world/mirror - * it contains all functions to manage these data sets - * and points to the relevant users and groups objects. - * + * One instance of this should exist per world/mirror it contains all functions to manage these data sets and points to + * the relevant users and groups objects. + * * @author gabrielcouto, ElgarL */ -public class WorldDataHolder { - +public class WorldDataHolder +{ /** * World name */ @@ -58,28 +58,30 @@ public class WorldDataHolder { */ protected UsersDataHolder users = new UsersDataHolder(); /** - * - */ + * + */ protected AnjoPermissionsHandler permissionsHandler; /** * Prevent direct instantiation - * + * * @param worldName */ - public WorldDataHolder(String worldName) { + public WorldDataHolder(String worldName) + { name = worldName; } /** * The main constructor for a new WorldDataHolder - * + * * @param worldName * @param groups * @param users */ - public WorldDataHolder(String worldName, GroupsDataHolder groups, UsersDataHolder users) { + public WorldDataHolder(String worldName, GroupsDataHolder groups, UsersDataHolder users) + { this.name = worldName; this.groups = groups; @@ -90,25 +92,27 @@ public class WorldDataHolder { /** * update the dataSource to point to this object. - * + * * This should be called whenever a set of world data is fetched. */ - public void updateDataSource() { + public void updateDataSource() + { this.groups.setDataSource(this); this.users.setDataSource(this); } /** - * Search for a user. If it doesn't exist, create a new one with - * default group. - * + * Search for a user. If it doesn't exist, create a new one with default group. + * * @param userName the name of the user * @return class that manage that user permission */ - public User getUser(String userName) { + public User getUser(String userName) + { - if (getUsers().containsKey(userName.toLowerCase())) { + if (getUsers().containsKey(userName.toLowerCase())) + { return getUsers().get(userName.toLowerCase()); } User newUser = createUser(userName); @@ -117,154 +121,189 @@ public class WorldDataHolder { /** * Add a user to the list. If it already exists, overwrite the old. - * + * * @param theUser the user you want to add to the permission list */ - public void addUser(User theUser) { + public void addUser(User theUser) + { - if (theUser.getDataSource() != this) { + if (theUser.getDataSource() != this) + { theUser = theUser.clone(this); } - if (theUser == null) { + if (theUser == null) + { return; } - if ((theUser.getGroup() == null)) { + if ((theUser.getGroup() == null)) + { theUser.setGroup(groups.getDefaultGroup()); } removeUser(theUser.getName()); getUsers().put(theUser.getName().toLowerCase(), theUser); setUsersChanged(true); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(theUser, Action.USER_ADDED); + } } /** * Removes the user from the list. (he might become a default user) - * + * * @param userName the username from the user to remove * @return true if it had something to remove */ - public boolean removeUser(String userName) { + public boolean removeUser(String userName) + { - if (getUsers().containsKey(userName.toLowerCase())) { + if (getUsers().containsKey(userName.toLowerCase())) + { getUsers().remove(userName.toLowerCase()); setUsersChanged(true); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(userName, GMUserEvent.Action.USER_REMOVED); + } return true; } return false; } /** - * + * * @param userName * @return true if we have data for this player. */ - public boolean isUserDeclared(String userName) { + public boolean isUserDeclared(String userName) + { return getUsers().containsKey(userName.toLowerCase()); } /** * Change the default group of the file. - * + * * @param group the group you want make default. */ - public void setDefaultGroup(Group group) { + public void setDefaultGroup(Group group) + { - if (!getGroups().containsKey(group.getName().toLowerCase()) || (group.getDataSource() != this)) { + if (!getGroups().containsKey(group.getName().toLowerCase()) || (group.getDataSource() != this)) + { addGroup(group); } groups.setDefaultGroup(getGroup(group.getName())); setGroupsChanged(true); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(GMSystemEvent.Action.DEFAULT_GROUP_CHANGED); + } } /** * Returns the default group of the file - * + * * @return the default group */ - public Group getDefaultGroup() { + public Group getDefaultGroup() + { return groups.getDefaultGroup(); } /** * Returns a group of the given name - * + * * @param groupName the name of the group * @return a group if it is found. null if not found. */ - public Group getGroup(String groupName) { + public Group getGroup(String groupName) + { if (groupName.toLowerCase().startsWith("g:")) + { return GroupManager.getGlobalGroups().getGroup(groupName); + } else + { return getGroups().get(groupName.toLowerCase()); + } } /** - * Check if a group exists. - * Its the same of getGroup, but check if it is null. - * + * Check if a group exists. Its the same of getGroup, but check if it is null. + * * @param groupName the name of the group * @return true if exists. false if not. */ - public boolean groupExists(String groupName) { + public boolean groupExists(String groupName) + { if (groupName.toLowerCase().startsWith("g:")) + { return GroupManager.getGlobalGroups().hasGroup(groupName); + } else + { return getGroups().containsKey(groupName.toLowerCase()); + } } /** * Add a group to the list - * + * * @param groupToAdd */ - public void addGroup(Group groupToAdd) { + public void addGroup(Group groupToAdd) + { - if (groupToAdd.getName().toLowerCase().startsWith("g:")) { + if (groupToAdd.getName().toLowerCase().startsWith("g:")) + { GroupManager.getGlobalGroups().addGroup(groupToAdd); GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED); return; } - if (groupToAdd.getDataSource() != this) { + if (groupToAdd.getDataSource() != this) + { groupToAdd = groupToAdd.clone(this); } removeGroup(groupToAdd.getName()); getGroups().put(groupToAdd.getName().toLowerCase(), groupToAdd); setGroupsChanged(true); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED); + } } /** * Remove the group from the list - * + * * @param groupName - * @return true if had something to remove. false the group was default or - * non-existant + * @return true if had something to remove. false the group was default or non-existant */ - public boolean removeGroup(String groupName) { + public boolean removeGroup(String groupName) + { - if (groupName.toLowerCase().startsWith("g:")) { + if (groupName.toLowerCase().startsWith("g:")) + { return GroupManager.getGlobalGroups().removeGroup(groupName); } - if (getDefaultGroup() != null && groupName.equalsIgnoreCase(getDefaultGroup().getName())) { + if (getDefaultGroup() != null && groupName.equalsIgnoreCase(getDefaultGroup().getName())) + { return false; } - if (getGroups().containsKey(groupName.toLowerCase())) { + if (getGroups().containsKey(groupName.toLowerCase())) + { getGroups().remove(groupName.toLowerCase()); setGroupsChanged(true); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(groupName.toLowerCase(), GMGroupEvent.Action.GROUP_REMOVED); + } return true; } return false; @@ -272,15 +311,16 @@ public class WorldDataHolder { } /** - * Creates a new User with the given name - * and adds it to this holder. - * + * Creates a new User with the given name and adds it to this holder. + * * @param userName the username you want * @return null if user already exists. or new User */ - public User createUser(String userName) { + public User createUser(String userName) + { - if (getUsers().containsKey(userName.toLowerCase())) { + if (getUsers().containsKey(userName.toLowerCase())) + { return null; } User newUser = new User(this, userName); @@ -291,20 +331,22 @@ public class WorldDataHolder { } /** - * Creates a new Group with the given name - * and adds it to this holder - * + * Creates a new Group with the given name and adds it to this holder + * * @param groupName the groupname you want * @return null if group already exists. or new Group */ - public Group createGroup(String groupName) { + public Group createGroup(String groupName) + { - if (groupName.toLowerCase().startsWith("g:")) { + if (groupName.toLowerCase().startsWith("g:")) + { Group newGroup = new Group(groupName); return GroupManager.getGlobalGroups().newGroup(newGroup); } - if (getGroups().containsKey(groupName.toLowerCase())) { + if (getGroups().containsKey(groupName.toLowerCase())) + { return null; } @@ -315,19 +357,21 @@ public class WorldDataHolder { } /** - * + * * @return a collection of the groups */ - public Collection<Group> getGroupList() { + public Collection<Group> getGroupList() + { return getGroups().values(); } /** - * + * * @return a collection of the users */ - public Collection<User> getUserList() { + public Collection<User> getUserList() + { return getUsers().values(); } @@ -335,12 +379,16 @@ public class WorldDataHolder { /** * reads the file again */ - public void reload() { + public void reload() + { - try { + try + { reloadGroups(); reloadUsers(); - } catch (Exception ex) { + } + catch (Exception ex) + { Logger.getLogger(WorldDataHolder.class.getName()).log(Level.SEVERE, null, ex); } } @@ -348,17 +396,20 @@ public class WorldDataHolder { /** * Refresh Group data from file */ - public void reloadGroups() { + public void reloadGroups() + { GroupManager.setLoaded(false); - try { + try + { // temporary holder in case the load fails. WorldDataHolder ph = new WorldDataHolder(this.getName()); loadGroups(ph, getGroupsFile()); // transfer new data resetGroups(); - for (Group tempGroup : ph.getGroupList()) { + for (Group tempGroup : ph.getGroupList()) + { tempGroup.clone(this); } this.setDefaultGroup(getGroup(ph.getDefaultGroup().getName())); @@ -366,7 +417,9 @@ public class WorldDataHolder { this.setTimeStampGroups(getGroupsFile().lastModified()); ph = null; - } catch (Exception ex) { + } + catch (Exception ex) + { Logger.getLogger(WorldDataHolder.class.getName()).log(Level.WARNING, null, ex); } GroupManager.setLoaded(true); @@ -376,14 +429,17 @@ public class WorldDataHolder { /** * Refresh Users data from file */ - public void reloadUsers() { + public void reloadUsers() + { GroupManager.setLoaded(false); - try { + try + { // temporary holder in case the load fails. WorldDataHolder ph = new WorldDataHolder(this.getName()); // copy groups for reference - for (Group tempGroup : this.getGroupList()) { + for (Group tempGroup : this.getGroupList()) + { tempGroup.clone(ph); } // setup the default group before loading user data. @@ -391,30 +447,39 @@ public class WorldDataHolder { loadUsers(ph, getUsersFile()); // transfer new data resetUsers(); - for (User tempUser : ph.getUserList()) { + for (User tempUser : ph.getUserList()) + { tempUser.clone(this); } this.removeUsersChangedFlag(); this.setTimeStampUsers(getUsersFile().lastModified()); ph = null; - } catch (Exception ex) { + } + catch (Exception ex) + { Logger.getLogger(WorldDataHolder.class.getName()).log(Level.WARNING, null, ex); } GroupManager.setLoaded(true); GroupManagerEventHandler.callEvent(GMSystemEvent.Action.RELOADED); } - public void loadGroups(File groupsFile) { + public void loadGroups(File groupsFile) + { GroupManager.setLoaded(false); - try { + try + { setGroupsFile(groupsFile); loadGroups(this, groupsFile); - } catch (FileNotFoundException e) { + } + catch (FileNotFoundException e) + { e.printStackTrace(); throw new IllegalArgumentException("The file which should contain groups does not exist!\n" + groupsFile.getPath()); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); throw new IllegalArgumentException("Error access the groups file!\n" + groupsFile.getPath()); } @@ -422,16 +487,22 @@ public class WorldDataHolder { GroupManager.setLoaded(true); } - public void loadUsers(File usersFile) { + public void loadUsers(File usersFile) + { GroupManager.setLoaded(false); - try { + try + { setUsersFile(usersFile); loadUsers(this, usersFile); - } catch (FileNotFoundException e) { + } + catch (FileNotFoundException e) + { e.printStackTrace(); throw new IllegalArgumentException("The file which should contain users does not exist!\n" + usersFile.getPath()); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); throw new IllegalArgumentException("Error access the users file!\n" + usersFile.getPath()); } @@ -441,23 +512,28 @@ public class WorldDataHolder { /** * Returns a NEW data holder containing data read from the files - * + * * @param worldName * @param groupsFile * @param usersFile - * + * * @throws FileNotFoundException * @throws IOException */ - public static WorldDataHolder load(String worldName, File groupsFile, File usersFile) throws FileNotFoundException, IOException { + public static WorldDataHolder load(String worldName, File groupsFile, File usersFile) throws FileNotFoundException, IOException + { WorldDataHolder ph = new WorldDataHolder(worldName); GroupManager.setLoaded(false); if (groupsFile != null) + { loadGroups(ph, groupsFile); + } if (usersFile != null) + { loadUsers(ph, usersFile); + } GroupManager.setLoaded(true); return ph; @@ -465,33 +541,44 @@ public class WorldDataHolder { /** * 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 { + @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()) { + if (!groupsFile.exists()) + { throw new IllegalArgumentException("The file which should contain groups does not exist!\n" + groupsFile.getPath()); } FileInputStream groupsInputStream = new FileInputStream(groupsFile); - try { - groupsRootDataNode = (Map<String, Object>) yamlGroups.load(new UnicodeReader(groupsInputStream)); - if (groupsRootDataNode == null) { + try + { + groupsRootDataNode = (Map<String, Object>)yamlGroups.load(new UnicodeReader(groupsInputStream)); + if (groupsRootDataNode == null) + { throw new NullPointerException(); } - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + groupsFile.getPath(), ex); - } finally { + } + finally + { groupsInputStream.close(); } @@ -503,9 +590,12 @@ public class WorldDataHolder { /* * Fetch all groups under the 'groups' entry. */ - try { - allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups"); - } catch (Exception ex) { + try + { + allGroupsNode = (Map<String, Object>)groupsRootDataNode.get("groups"); + } + catch (Exception ex) + { throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details.", ex); } @@ -517,13 +607,17 @@ public class WorldDataHolder { * loop each group entry * and process it's data. */ - while (groupItr.hasNext()) { + while (groupItr.hasNext()) + { - try { + try + { groupCount++; // Attempt to fetch the next group name. groupKey = groupItr.next(); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Invalid group name for group entry (" + groupCount + ") in file: " + groupsFile.getPath(), ex); } @@ -532,9 +626,12 @@ public class WorldDataHolder { */ Map<String, Object> thisGroupNode = null; - try { - thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey); - } catch (Exception ex) { + try + { + thisGroupNode = (Map<String, Object>)allGroupsNode.get(groupKey); + } + catch (Exception ex) + { throw new IllegalArgumentException("Invalid child nodes for group '" + groupKey + "' in file: " + groupsFile.getPath(), ex); } @@ -544,29 +641,37 @@ public class WorldDataHolder { */ Group thisGrp = ph.createGroup(groupKey); - if (thisGrp == null) { + if (thisGrp == null) + { throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath()); } // DEFAULT NODE Object nodeData = null; - try { + try + { nodeData = thisGroupNode.get("default"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'permissions' for group: " + groupKey + " in file: " + groupsFile.getPath()); } - if (nodeData == null) { + if (nodeData == null) + { /* * If no 'default' node is found do nothing. */ - } else if ((Boolean.parseBoolean(nodeData.toString()))) { + } + else if ((Boolean.parseBoolean(nodeData.toString()))) + { /* * Set this as the default group. * Warn if some other group has already claimed that position. */ - if (ph.getDefaultGroup() != null) { + if (ph.getDefaultGroup() != null) + { GroupManager.logger.warning("The group '" + thisGrp.getName() + "' is claiming to be default where '" + ph.getDefaultGroup().getName() + "' already was."); GroupManager.logger.warning("Overriding first default request in file: " + groupsFile.getPath()); } @@ -576,51 +681,73 @@ public class WorldDataHolder { // PERMISSIONS NODE nodeData = null; - try { + try + { nodeData = thisGroupNode.get("permissions"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'permissions' for '" + groupKey + "' in file: " + groupsFile.getPath()); } - if (nodeData == null) { + if (nodeData == null) + { /* * If no permissions node is found, or it's empty * do nothing. */ - } else { + } + else + { /* * There is a permission list Which seems to hold some data */ - if (nodeData instanceof List) { + if (nodeData instanceof List) + { /* * Check each entry and add it as a new permission. */ - try { - for (Object o : ((List) nodeData)) { - try { + try + { + for (Object o : ((List)nodeData)) + { + try + { /* * Only add this permission if it's not empty. */ if (!o.toString().isEmpty()) + { thisGrp.addPermission(o.toString()); + } - } catch (NullPointerException ex) { + } + catch (NullPointerException ex) + { // Ignore this entry as it's null. It can be // safely dropped } } - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Invalid formatting found in 'permissions' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex); } - } else if (nodeData instanceof String) { + } + else if (nodeData instanceof String) + { /* * Only add this permission if it's not empty. */ if (!nodeData.toString().isEmpty()) - thisGrp.addPermission((String) nodeData); + { + thisGrp.addPermission((String)nodeData); + } - } else { + } + else + { throw new IllegalArgumentException("Unknown type of 'permissions' node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); } /* @@ -633,13 +760,17 @@ public class WorldDataHolder { // INFO NODE nodeData = null; - try { + try + { nodeData = thisGroupNode.get("info"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'info' section for group: " + groupKey + " in file: " + groupsFile.getPath()); } - if (nodeData == null) { + if (nodeData == null) + { /* * No info section was found, so leave all variables as * defaults. @@ -647,75 +778,109 @@ public class WorldDataHolder { GroupManager.logger.warning("The group '" + thisGrp.getName() + "' has no 'info' section!"); GroupManager.logger.warning("Using default values: " + groupsFile.getPath()); - } else if (nodeData instanceof Map) { - try { - if (nodeData != null) { - thisGrp.setVariables((Map<String, Object>) nodeData); + } + else if (nodeData instanceof Map) + { + try + { + if (nodeData != null) + { + thisGrp.setVariables((Map<String, Object>)nodeData); } - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Invalid formatting found in 'info' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex); } - } else + } + else + { throw new IllegalArgumentException("Unknown entry found in 'info' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); + } // INHERITANCE NODE nodeData = null; - try { + try + { nodeData = thisGroupNode.get("inheritance"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'inheritance' section for group: " + groupKey + " in file: " + groupsFile.getPath()); } - if (nodeData == null || nodeData instanceof List) { - if (nodeData == null) { + if (nodeData == null || nodeData instanceof List) + { + if (nodeData == null) + { /* * If no inheritance node is found, or it's empty * do nothing. */ - } else if (nodeData instanceof List) { - - try { - for (String grp : (List<String>) nodeData) { - if (inheritance.get(groupKey) == null) { + } + else if (nodeData instanceof List) + { + + try + { + for (String grp : (List<String>)nodeData) + { + if (inheritance.get(groupKey) == null) + { inheritance.put(groupKey, new ArrayList<String>()); } inheritance.get(groupKey).add(grp); } - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Invalid formatting found in 'inheritance' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex); } } - } else + } + else + { throw new IllegalArgumentException("Unknown entry found in 'inheritance' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); + } // END GROUP } - if (ph.getDefaultGroup() == null) { + if (ph.getDefaultGroup() == null) + { throw new IllegalArgumentException("There was no Default Group declared in file: " + groupsFile.getPath()); } /* * Build the inheritance map and recored any errors */ - for (String group : inheritance.keySet()) { + for (String group : inheritance.keySet()) + { List<String> inheritedList = inheritance.get(group); Group thisGroup = ph.getGroup(group); if (thisGroup != null) - for (String inheritedKey : inheritedList) { - if (inheritedKey != null) { + { + for (String inheritedKey : inheritedList) + { + if (inheritedKey != null) + { Group inheritedGroup = ph.getGroup(inheritedKey); - if (inheritedGroup != null) { + if (inheritedGroup != null) + { thisGroup.addInherits(inheritedGroup); - } else + } + else + { GroupManager.logger.warning("Inherited group '" + inheritedKey + "' not found for group " + thisGroup.getName() + ". Ignoring entry in file: " + groupsFile.getPath()); + } } } + } } ph.removeGroupsChangedFlag(); @@ -728,31 +893,42 @@ public class WorldDataHolder { /** * 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 { + @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 (!usersFile.exists()) { + if (!usersFile.exists()) + { throw new IllegalArgumentException("The file which should contain users does not exist!\n" + usersFile.getPath()); } FileInputStream usersInputStream = new FileInputStream(usersFile); - try { - usersRootDataNode = (Map<String, Object>) yamlUsers.load(new UnicodeReader(usersInputStream)); - if (usersRootDataNode == null) { + try + { + usersRootDataNode = (Map<String, Object>)yamlUsers.load(new UnicodeReader(usersInputStream)); + if (usersRootDataNode == null) + { throw new NullPointerException(); } - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + usersFile.getPath(), ex); - } finally { + } + finally + { usersInputStream.close(); } @@ -763,72 +939,101 @@ public class WorldDataHolder { /* * Fetch all child nodes under the 'users' entry. */ - try { - allUsersNode = (Map<String, Object>) usersRootDataNode.get("users"); - } catch (Exception ex) { + try + { + allUsersNode = (Map<String, Object>)usersRootDataNode.get("users"); + } + catch (Exception ex) + { throw new IllegalArgumentException("Your " + usersFile.getPath() + " file is invalid. See console for details.", ex); } // Load users if the file is NOT empty - if (allUsersNode != null) { + if (allUsersNode != null) + { Iterator<String> usersItr = allUsersNode.keySet().iterator(); String usersKey; Integer userCount = 0; - while (usersItr.hasNext()) { - try { + while (usersItr.hasNext()) + { + try + { userCount++; // Attempt to fetch the next user name. usersKey = usersItr.next(); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Invalid node type for user entry (" + userCount + ") in file: " + usersFile.getPath(), ex); } Map<String, Object> thisUserNode = null; - try { - thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey); - } catch (Exception ex) { + try + { + thisUserNode = (Map<String, Object>)allUsersNode.get(usersKey); + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found for user: " + usersKey + " in file: " + usersFile.getPath()); } User thisUser = ph.createUser(usersKey); - if (thisUser == null) { + if (thisUser == null) + { throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath()); } // USER PERMISSIONS NODES Object nodeData = null; - try { + try + { nodeData = thisUserNode.get("permissions"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'permissions' for user: " + usersKey + " in file: " + usersFile.getPath()); } - if (nodeData == null) { + if (nodeData == null) + { /* * If no permissions node is found, or it's empty * do nothing. */ - } else { - if (nodeData instanceof List) { - for (Object o : ((List) nodeData)) { + } + else + { + if (nodeData instanceof List) + { + for (Object o : ((List)nodeData)) + { /* * Only add this permission if it's not empty */ if (!o.toString().isEmpty()) + { thisUser.addPermission(o.toString()); + } } - } else if (nodeData instanceof String) { - try { + } + else if (nodeData instanceof String) + { + try + { /* * Only add this permission if it's not empty */ if (!nodeData.toString().isEmpty()) + { thisUser.addPermission(nodeData.toString()); - } catch (NullPointerException e) { + } + } + catch (NullPointerException e) + { // Ignore this entry as it's null. } } @@ -838,31 +1043,46 @@ public class WorldDataHolder { // SUBGROUPS NODES nodeData = null; - try { + try + { nodeData = thisUserNode.get("subgroups"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'subgroups' for user: " + usersKey + " in file: " + usersFile.getPath()); } - if (nodeData == null) { + if (nodeData == null) + { /* * If no subgroups node is found, or it's empty * do nothing. */ - } else if (nodeData instanceof List) { - for (Object o : ((List) nodeData)) { + } + else if (nodeData instanceof List) + { + for (Object o : ((List)nodeData)) + { Group subGrp = ph.getGroup(o.toString()); - if (subGrp != null) { + if (subGrp != null) + { thisUser.addSubGroup(subGrp); - } else { + } + else + { GroupManager.logger.warning("Subgroup '" + o.toString() + "' not found for user: " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath()); } } - } else if (nodeData instanceof String) { + } + else if (nodeData instanceof String) + { Group subGrp = ph.getGroup(nodeData.toString()); - if (subGrp != null) { + if (subGrp != null) + { thisUser.addSubGroup(subGrp); - } else { + } + else + { GroupManager.logger.warning("Subgroup '" + nodeData.toString() + "' not found for user: " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath()); } } @@ -870,42 +1090,58 @@ public class WorldDataHolder { // USER INFO NODE nodeData = null; - try { + try + { nodeData = thisUserNode.get("info"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'info' section for user: " + usersKey + " in file: " + usersFile.getPath()); } - if (nodeData == null) { + if (nodeData == null) + { /* * If no info node is found, or it's empty * do nothing. */ - } else if (nodeData instanceof Map) { - thisUser.setVariables((Map<String, Object>) nodeData); + } + else if (nodeData instanceof Map) + { + thisUser.setVariables((Map<String, Object>)nodeData); - } else + } + else + { throw new IllegalArgumentException("Unknown entry found in 'info' section for user: " + thisUser.getName() + " in file: " + usersFile.getPath()); + } // END INFO NODE // PRIMARY GROUP nodeData = null; - try { + try + { nodeData = thisUserNode.get("group"); - } catch (Exception ex) { + } + catch (Exception ex) + { throw new IllegalArgumentException("Bad format found in 'group' section for user: " + usersKey + " in file: " + usersFile.getPath()); } - if (nodeData != null) { + if (nodeData != null) + { Group hisGroup = ph.getGroup(nodeData.toString()); - if (hisGroup == null) { + if (hisGroup == null) + { GroupManager.logger.warning("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName() + ": Set to '" + ph.getDefaultGroup().getName() + "' for file: " + usersFile.getPath()); hisGroup = ph.getDefaultGroup(); } thisUser.setGroup(hisGroup); - } else { + } + else + { thisUser.setGroup(ph.getDefaultGroup()); } } @@ -919,24 +1155,27 @@ public class WorldDataHolder { /** * Write a dataHolder in a specified file - * + * * @param ph * @param groupsFile */ - public static void writeGroups(WorldDataHolder ph, File groupsFile) { + public static void writeGroups(WorldDataHolder ph, File groupsFile) + { Map<String, Object> root = new HashMap<String, Object>(); Map<String, Object> groupsMap = new HashMap<String, Object>(); root.put("groups", groupsMap); - for (String groupKey : ph.getGroups().keySet()) { + for (String groupKey : ph.getGroups().keySet()) + { Group group = ph.getGroups().get(groupKey); Map<String, Object> aGroupMap = new HashMap<String, Object>(); groupsMap.put(group.getName(), aGroupMap); - if (ph.getDefaultGroup() == null) { + if (ph.getDefaultGroup() == null) + { GroupManager.logger.severe("There is no default group for world: " + ph.getName()); } aGroupMap.put("default", group.equals(ph.getDefaultGroup())); @@ -944,7 +1183,8 @@ public class WorldDataHolder { Map<String, Object> infoMap = new HashMap<String, Object>(); aGroupMap.put("info", infoMap); - for (String infoKey : group.getVariables().getVarKeyList()) { + for (String infoKey : group.getVariables().getVarKeyList()) + { infoMap.put(infoKey, group.getVariables().getVarObject(infoKey)); } @@ -953,11 +1193,13 @@ public class WorldDataHolder { aGroupMap.put("permissions", group.getPermissionList()); } - if (!root.isEmpty()) { + if (!root.isEmpty()) + { DumperOptions opt = new DumperOptions(); opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); final Yaml yaml = new Yaml(opt); - try { + try + { OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(groupsFile), "UTF-8"); String newLine = System.getProperty("line.separator"); @@ -975,9 +1217,15 @@ public class WorldDataHolder { yaml.dump(root, out); out.close(); - } catch (UnsupportedEncodingException ex) { - } catch (FileNotFoundException ex) { - } catch (IOException e) { + } + catch (UnsupportedEncodingException ex) + { + } + catch (FileNotFoundException ex) + { + } + catch (IOException e) + { } } @@ -987,7 +1235,9 @@ public class WorldDataHolder { ph.removeGroupsChangedFlag(); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(GMSystemEvent.Action.SAVED); + } /* * FileWriter tx = null; @@ -1007,35 +1257,43 @@ public class WorldDataHolder { /** * Write a dataHolder in a specified file - * + * * @param ph * @param usersFile */ - public static void writeUsers(WorldDataHolder ph, File usersFile) { + public static void writeUsers(WorldDataHolder ph, File usersFile) + { Map<String, Object> root = new HashMap<String, Object>(); Map<String, Object> usersMap = new HashMap<String, Object>(); root.put("users", usersMap); - for (String userKey : ph.getUsers().keySet()) { + for (String userKey : ph.getUsers().keySet()) + { User user = ph.getUsers().get(userKey); - if ((user.getGroup() == null || user.getGroup().equals(ph.getDefaultGroup())) && user.getPermissionList().isEmpty() && user.getVariables().isEmpty() && user.isSubGroupsEmpty()) { + if ((user.getGroup() == null || user.getGroup().equals(ph.getDefaultGroup())) && user.getPermissionList().isEmpty() && user.getVariables().isEmpty() && user.isSubGroupsEmpty()) + { continue; } Map<String, Object> aUserMap = new HashMap<String, Object>(); usersMap.put(user.getName(), aUserMap); - if (user.getGroup() == null) { + if (user.getGroup() == null) + { aUserMap.put("group", ph.getDefaultGroup().getName()); - } else { + } + else + { aUserMap.put("group", user.getGroup().getName()); } // USER INFO NODE - BETA - if (user.getVariables().getSize() > 0) { + if (user.getVariables().getSize() > 0) + { Map<String, Object> infoMap = new HashMap<String, Object>(); aUserMap.put("info", infoMap); - for (String infoKey : user.getVariables().getVarKeyList()) { + for (String infoKey : user.getVariables().getVarKeyList()) + { infoMap.put(infoKey, user.getVariables().getVarObject(infoKey)); } } @@ -1047,17 +1305,25 @@ public class WorldDataHolder { // END SUBGROUPS NODE - BETA } - if (!root.isEmpty()) { + if (!root.isEmpty()) + { DumperOptions opt = new DumperOptions(); opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); final Yaml yaml = new Yaml(opt); - try { + try + { OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(usersFile), "UTF-8"); yaml.dump(root, out); out.close(); - } catch (UnsupportedEncodingException ex) { - } catch (FileNotFoundException ex) { - } catch (IOException e) { + } + catch (UnsupportedEncodingException ex) + { + } + catch (FileNotFoundException ex) + { + } + catch (IOException e) + { } } @@ -1067,7 +1333,9 @@ public class WorldDataHolder { ph.removeUsersChangedFlag(); if (GroupManager.isLoaded()) + { GroupManagerEventHandler.callEvent(GMSystemEvent.Action.SAVED); + } /* * FileWriter tx = null; @@ -1086,32 +1354,31 @@ public class WorldDataHolder { } /** - * Don't use this. Unless you want to make this plugin to interact with - * original Nijikokun Permissions - * This method is supposed to make the original one reload the file, and - * propagate the changes made here. - * - * Prefer to use the AnjoCaido's fake version of Nijikokun's Permission - * plugin. - * The AnjoCaido's Permission can propagate the changes made on this plugin - * instantly, - * without need to save the file. - * + * Don't use this. Unless you want to make this plugin to interact with original Nijikokun Permissions This method + * is supposed to make the original one reload the file, and propagate the changes made here. + * + * Prefer to use the AnjoCaido's fake version of Nijikokun's Permission plugin. The AnjoCaido's Permission can + * propagate the changes made on this plugin instantly, without need to save the file. + * * @param server the server that holds the plugin - * @deprecated it is not used anymore... unless if you use original - * Permissions + * @deprecated it is not used anymore... unless if you use original Permissions */ @Deprecated - public static void reloadOldPlugins(Server server) { + public static void reloadOldPlugins(Server server) + { // Only reload permissions PluginManager pm = server.getPluginManager(); Plugin[] plugins = pm.getPlugins(); - for (int i = 0; i < plugins.length; i++) { + for (int i = 0; i < plugins.length; i++) + { // plugins[i].getConfiguration().load(); - try { + try + { plugins[i].getClass().getMethod("setupPermissions").invoke(plugins[i]); - } catch (Exception ex) { + } + catch (Exception ex) + { continue; } } @@ -1120,9 +1387,11 @@ public class WorldDataHolder { /** * @return the permissionsHandler */ - public AnjoPermissionsHandler getPermissionsHandler() { + public AnjoPermissionsHandler getPermissionsHandler() + { - if (permissionsHandler == null) { + if (permissionsHandler == null) + { permissionsHandler = new AnjoPermissionsHandler(this); } return permissionsHandler; @@ -1131,22 +1400,27 @@ public class WorldDataHolder { /** * @param haveUsersChanged the haveUsersChanged to set */ - public void setUsersChanged(boolean haveUsersChanged) { + public void setUsersChanged(boolean haveUsersChanged) + { users.setUsersChanged(haveUsersChanged); } /** - * + * * @return true if any user data has changed */ - public boolean haveUsersChanged() { + public boolean haveUsersChanged() + { - if (users.HaveUsersChanged()) { + if (users.HaveUsersChanged()) + { return true; } - for (User u : users.getUsers().values()) { - if (u.isChanged()) { + for (User u : users.getUsers().values()) + { + if (u.isChanged()) + { return true; } } @@ -1156,22 +1430,27 @@ public class WorldDataHolder { /** * @param setGroupsChanged the haveGroupsChanged to set */ - public void setGroupsChanged(boolean setGroupsChanged) { + public void setGroupsChanged(boolean setGroupsChanged) + { groups.setGroupsChanged(setGroupsChanged); } /** - * + * * @return true if any group data has changed. */ - public boolean haveGroupsChanged() { + public boolean haveGroupsChanged() + { - if (groups.HaveGroupsChanged()) { + if (groups.HaveGroupsChanged()) + { return true; } - for (Group g : groups.getGroups().values()) { - if (g.isChanged()) { + for (Group g : groups.getGroups().values()) + { + if (g.isChanged()) + { return true; } } @@ -1179,23 +1458,27 @@ public class WorldDataHolder { } /** - * - */ - public void removeUsersChangedFlag() { + * + */ + public void removeUsersChangedFlag() + { setUsersChanged(false); - for (User u : getUsers().values()) { + for (User u : getUsers().values()) + { u.flagAsSaved(); } } /** - * - */ - public void removeGroupsChangedFlag() { + * + */ + public void removeGroupsChangedFlag() + { setGroupsChanged(false); - for (Group g : getGroups().values()) { + for (Group g : getGroups().values()) + { g.flagAsSaved(); } } @@ -1203,7 +1486,8 @@ public class WorldDataHolder { /** * @return the usersFile */ - public File getUsersFile() { + public File getUsersFile() + { return users.getUsersFile(); } @@ -1211,7 +1495,8 @@ public class WorldDataHolder { /** * @param file the usersFile to set */ - public void setUsersFile(File file) { + public void setUsersFile(File file) + { users.setUsersFile(file); } @@ -1219,7 +1504,8 @@ public class WorldDataHolder { /** * @return the groupsFile */ - public File getGroupsFile() { + public File getGroupsFile() + { return groups.getGroupsFile(); } @@ -1227,7 +1513,8 @@ public class WorldDataHolder { /** * @param file the groupsFile to set */ - public void setGroupsFile(File file) { + public void setGroupsFile(File file) + { groups.setGroupsFile(file); } @@ -1235,7 +1522,8 @@ public class WorldDataHolder { /** * @return the name */ - public String getName() { + public String getName() + { return name; } @@ -1243,7 +1531,8 @@ public class WorldDataHolder { /** * Resets Groups. */ - public void resetGroups() { + public void resetGroups() + { // setDefaultGroup(null); groups.setGroups(new HashMap<String, Group>()); @@ -1252,7 +1541,8 @@ public class WorldDataHolder { /** * Resets Users */ - public void resetUsers() { + public void resetUsers() + { users.setUsers(new HashMap<String, User>()); } @@ -1260,7 +1550,8 @@ public class WorldDataHolder { /** * @return the groups */ - public Map<String, Group> getGroups() { + public Map<String, Group> getGroups() + { return groups.getGroups(); } @@ -1268,7 +1559,8 @@ public class WorldDataHolder { /** * @return the users */ - public Map<String, User> getUsers() { + public Map<String, User> getUsers() + { return users.getUsers(); } @@ -1276,7 +1568,8 @@ public class WorldDataHolder { /** * @return the groups */ - public GroupsDataHolder getGroupsObject() { + public GroupsDataHolder getGroupsObject() + { return groups; } @@ -1284,7 +1577,8 @@ public class WorldDataHolder { /** * @param groupsDataHolder the GroupsDataHolder to set */ - public void setGroupsObject(GroupsDataHolder groupsDataHolder) { + public void setGroupsObject(GroupsDataHolder groupsDataHolder) + { groups = groupsDataHolder; } @@ -1292,7 +1586,8 @@ public class WorldDataHolder { /** * @return the users */ - public UsersDataHolder getUsersObject() { + public UsersDataHolder getUsersObject() + { return users; } @@ -1300,7 +1595,8 @@ public class WorldDataHolder { /** * @param usersDataHolder the UsersDataHolder to set */ - public void setUsersObject(UsersDataHolder usersDataHolder) { + public void setUsersObject(UsersDataHolder usersDataHolder) + { users = usersDataHolder; } @@ -1308,7 +1604,8 @@ public class WorldDataHolder { /** * @return the timeStampGroups */ - public long getTimeStampGroups() { + public long getTimeStampGroups() + { return groups.getTimeStampGroups(); } @@ -1316,7 +1613,8 @@ public class WorldDataHolder { /** * @return the timeStampUsers */ - public long getTimeStampUsers() { + public long getTimeStampUsers() + { return users.getTimeStampUsers(); } @@ -1324,7 +1622,8 @@ public class WorldDataHolder { /** * @param timeStampGroups the timeStampGroups to set */ - protected void setTimeStampGroups(long timeStampGroups) { + protected void setTimeStampGroups(long timeStampGroups) + { groups.setTimeStampGroups(timeStampGroups); } @@ -1332,17 +1631,22 @@ public class WorldDataHolder { /** * @param timeStampUsers the timeStampUsers to set */ - protected void setTimeStampUsers(long timeStampUsers) { + protected void setTimeStampUsers(long timeStampUsers) + { users.setTimeStampUsers(timeStampUsers); } - public void setTimeStamps() { + public void setTimeStamps() + { if (getGroupsFile() != null) + { setTimeStampGroups(getGroupsFile().lastModified()); + } if (getUsersFile() != null) + { setTimeStampUsers(getUsersFile().lastModified()); + } } - } diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java index 27a7d9a59..b5238c058 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/worlds/WorldsHolder.java @@ -25,55 +25,58 @@ import org.bukkit.World; import org.bukkit.configuration.MemorySection; import org.bukkit.entity.Player; + /** - * + * * @author gabrielcouto */ -public class WorldsHolder { - +public class WorldsHolder +{ /** * Map with instances of loaded worlds. */ private Map<String, OverloadedWorldHolder> worldsData = new HashMap<String, OverloadedWorldHolder>(); - /** - * Map of mirrors: <nonExistingWorldName, existingAndLoadedWorldName> - * The key is the mirror. - * The object is the mirrored. - * + * Map of mirrors: <nonExistingWorldName, existingAndLoadedWorldName> The key is the mirror. The object is the + * mirrored. + * * Mirror shows the same data of mirrored. */ private Map<String, String> mirrorsGroup = new HashMap<String, String>(); private Map<String, String> mirrorsUser = new HashMap<String, String>(); - private String serverDefaultWorldName; private GroupManager plugin; private File worldsFolder; /** - * + * * @param plugin */ - public WorldsHolder(GroupManager plugin) { + public WorldsHolder(GroupManager plugin) + { this.plugin = plugin; resetWorldsHolder(); } - - public void resetWorldsHolder() { - + + public void resetWorldsHolder() + { + worldsData = new HashMap<String, OverloadedWorldHolder>(); mirrorsGroup = new HashMap<String, String>(); mirrorsUser = new HashMap<String, String>(); - + // Setup folders and check files exist for the primary world verifyFirstRun(); initialLoad(); if (serverDefaultWorldName == null) + { throw new IllegalStateException("There is no default group! OMG!"); + } } - private void initialLoad() { + private void initialLoad() + { // load the initial world initialWorldLoading(); @@ -83,14 +86,16 @@ public class WorldsHolder { loadAllSearchedWorlds(); } - private void initialWorldLoading() { + private void initialWorldLoading() + { //Load the default world loadWorld(serverDefaultWorldName); //defaultWorld = getUpdatedWorldData(serverDefaultWorldName); } - private void loadAllSearchedWorlds() { + private void loadAllSearchedWorlds() + { /* * Read all known worlds from Bukkit @@ -98,21 +103,28 @@ public class WorldsHolder { * and they are not mirrored. */ for (World world : plugin.getServer().getWorlds()) + { if ((!worldsData.containsKey(world.getName().toLowerCase())) && ((!mirrorsGroup.containsKey(world.getName().toLowerCase())) || (!mirrorsUser.containsKey(world.getName().toLowerCase())))) + { setupWorldFolder(world.getName()); + } + } /* * Loop over all folders within the worlds folder * and attempt to load the world data */ - for (File folder : worldsFolder.listFiles()) { - if (folder.isDirectory()) { + for (File folder : worldsFolder.listFiles()) + { + if (folder.isDirectory()) + { GroupManager.logger.info("World Found: " + folder.getName()); /* * don't load any worlds which are already loaded * or fully mirrored worlds that don't need data. */ - if (!worldsData.containsKey(folder.getName().toLowerCase()) && ((!mirrorsGroup.containsKey(folder.getName().toLowerCase())) || (!mirrorsUser.containsKey(folder.getName().toLowerCase())))) { + if (!worldsData.containsKey(folder.getName().toLowerCase()) && ((!mirrorsGroup.containsKey(folder.getName().toLowerCase())) || (!mirrorsUser.containsKey(folder.getName().toLowerCase())))) + { /* * Call setupWorldFolder to check case sensitivity * and convert to lower case, before we attempt to load this @@ -127,7 +139,8 @@ public class WorldsHolder { } @SuppressWarnings("rawtypes") - public void mirrorSetUp() { + public void mirrorSetUp() + { mirrorsGroup.clear(); mirrorsUser.clear(); @@ -135,25 +148,35 @@ public class WorldsHolder { HashSet<String> mirroredWorlds = new HashSet<String>(); - if (mirrorsMap != null) { - for (String source : mirrorsMap.keySet()) { + if (mirrorsMap != null) + { + for (String source : mirrorsMap.keySet()) + { // Make sure all non mirrored worlds have a set of data files. setupWorldFolder(source); // Load the world data if (!worldsData.containsKey(source.toLowerCase())) + { loadWorld(source); + } - if (mirrorsMap.get(source) instanceof ArrayList) { - ArrayList mirrorList = (ArrayList) mirrorsMap.get(source); + if (mirrorsMap.get(source) instanceof ArrayList) + { + ArrayList mirrorList = (ArrayList)mirrorsMap.get(source); // These worlds fully mirror their parent - for (Object o : mirrorList) { + for (Object o : mirrorList) + { String world = o.toString().toLowerCase(); - if (world != serverDefaultWorldName) { - try { + if (world != serverDefaultWorldName) + { + try + { mirrorsGroup.remove(world); mirrorsUser.remove(world); - } catch (Exception e) { + } + catch (Exception e) + { } mirrorsGroup.put(world, getWorldData(source).getName()); mirrorsUser.put(world, getWorldData(source).getName()); @@ -161,45 +184,70 @@ public class WorldsHolder { // Track this world so we can create a datasource for it later mirroredWorlds.add(o.toString()); - } else + } + else + { GroupManager.logger.log(Level.WARNING, "Mirroring error with " + o.toString() + ". Recursive loop detected!"); + } } - } else if (mirrorsMap.get(source) instanceof MemorySection) { - MemorySection subSection = (MemorySection) mirrorsMap.get(source); + } + else if (mirrorsMap.get(source) instanceof MemorySection) + { + MemorySection subSection = (MemorySection)mirrorsMap.get(source); - for (String key : subSection.getKeys(true)) { + for (String key : subSection.getKeys(true)) + { - if (key.toLowerCase() != serverDefaultWorldName) { + if (key.toLowerCase() != serverDefaultWorldName) + { - if (subSection.get(key) instanceof ArrayList) { - ArrayList mirrorList = (ArrayList) subSection.get(key); + if (subSection.get(key) instanceof ArrayList) + { + ArrayList mirrorList = (ArrayList)subSection.get(key); // These worlds have defined mirroring - for (Object o : mirrorList) { + for (Object o : mirrorList) + { String type = o.toString().toLowerCase(); - try { + try + { if (type.equals("groups")) + { mirrorsGroup.remove(key.toLowerCase()); + } if (type.equals("users")) + { mirrorsUser.remove(key.toLowerCase()); + } - } catch (Exception e) { + } + catch (Exception e) + { } if (type.equals("groups")) + { mirrorsGroup.put(key.toLowerCase(), getWorldData(source).getName()); + } if (type.equals("users")) + { mirrorsUser.put(key.toLowerCase(), getWorldData(source).getName()); + } } // Track this world so we can create a datasource for it later mirroredWorlds.add(key); - } else + } + else + { GroupManager.logger.log(Level.WARNING, "Mirroring error with " + key + ". Recursive loop detected!"); + } - } else { + } + else + { throw new IllegalStateException("Unknown mirroring format for " + key); } @@ -208,8 +256,10 @@ public class WorldsHolder { } // Create a datasource for any worlds not already loaded - for (String world : mirroredWorlds) { - if (!worldsData.containsKey(world.toLowerCase())) { + for (String world : mirroredWorlds) + { + if (!worldsData.containsKey(world.toLowerCase())) + { setupWorldFolder(world); loadWorld(world, true); } @@ -218,22 +268,29 @@ public class WorldsHolder { } /** - * - */ - public void reloadAll() { + * + */ + public void reloadAll() + { // Load global groups GroupManager.getGlobalGroups().load(); ArrayList<WorldDataHolder> alreadyDone = new ArrayList<WorldDataHolder>(); - for (WorldDataHolder w : worldsData.values()) { - if (alreadyDone.contains(w)) { + for (WorldDataHolder w : worldsData.values()) + { + if (alreadyDone.contains(w)) + { continue; } if (!mirrorsGroup.containsKey(w.getName().toLowerCase())) + { w.reloadGroups(); + } if (!mirrorsUser.containsKey(w.getName().toLowerCase())) + { w.reloadUsers(); + } alreadyDone.add(w); } @@ -241,134 +298,170 @@ public class WorldsHolder { } /** - * + * * @param worldName */ - public void reloadWorld(String worldName) { + public void reloadWorld(String worldName) + { if (!mirrorsGroup.containsKey(worldName.toLowerCase())) + { getWorldData(worldName).reloadGroups(); + } if (!mirrorsUser.containsKey(worldName.toLowerCase())) + { getWorldData(worldName).reloadUsers(); + } } /** - * Wrapper to retain backwards compatibility - * (call this function to auto overwrite files) + * Wrapper to retain backwards compatibility (call this function to auto overwrite files) */ - public void saveChanges() { + public void saveChanges() + { saveChanges(true); } /** - * - */ - public void saveChanges(boolean overwrite) { + * + */ + public void saveChanges(boolean overwrite) + { ArrayList<WorldDataHolder> alreadyDone = new ArrayList<WorldDataHolder>(); Tasks.removeOldFiles(plugin, plugin.getBackupFolder()); // Write Global Groups - if (GroupManager.getGlobalGroups().haveGroupsChanged()) { + if (GroupManager.getGlobalGroups().haveGroupsChanged()) + { GroupManager.getGlobalGroups().writeGroups(overwrite); - } else { - if (GroupManager.getGlobalGroups().getTimeStampGroups() < GroupManager.getGlobalGroups().getGlobalGroupsFile().lastModified()) { + } + else + { + if (GroupManager.getGlobalGroups().getTimeStampGroups() < GroupManager.getGlobalGroups().getGlobalGroupsFile().lastModified()) + { System.out.print("Newer GlobalGroups file found (Loading changes)!"); GroupManager.getGlobalGroups().load(); } } - for (OverloadedWorldHolder w : worldsData.values()) { - if (alreadyDone.contains(w)) { + for (OverloadedWorldHolder w : worldsData.values()) + { + if (alreadyDone.contains(w)) + { continue; } - if (w == null) { + if (w == null) + { GroupManager.logger.severe("WHAT HAPPENED?"); continue; } if (!mirrorsGroup.containsKey(w.getName().toLowerCase())) - if (w.haveGroupsChanged()) { - if (overwrite || (!overwrite && (w.getTimeStampGroups() >= w.getGroupsFile().lastModified()))) { + { + if (w.haveGroupsChanged()) + { + if (overwrite || (!overwrite && (w.getTimeStampGroups() >= w.getGroupsFile().lastModified()))) + { // Backup Groups file backupFile(w, true); WorldDataHolder.writeGroups(w, w.getGroupsFile()); //w.removeGroupsChangedFlag(); - } else { + } + else + { // Newer file found. GroupManager.logger.log(Level.WARNING, "Newer Groups file found for " + w.getName() + ", but we have local changes!"); throw new IllegalStateException("Unable to save unless you issue a '/mansave force'"); } - } else { + } + else + { //Check for newer file as no local changes. - if (w.getTimeStampGroups() < w.getGroupsFile().lastModified()) { + if (w.getTimeStampGroups() < w.getGroupsFile().lastModified()) + { System.out.print("Newer Groups file found (Loading changes)!"); // Backup Groups file backupFile(w, true); w.reloadGroups(); } } + } if (!mirrorsUser.containsKey(w.getName().toLowerCase())) - if (w.haveUsersChanged()) { - if (overwrite || (!overwrite && (w.getTimeStampUsers() >= w.getUsersFile().lastModified()))) { + { + if (w.haveUsersChanged()) + { + if (overwrite || (!overwrite && (w.getTimeStampUsers() >= w.getUsersFile().lastModified()))) + { // Backup Users file backupFile(w, false); WorldDataHolder.writeUsers(w, w.getUsersFile()); //w.removeUsersChangedFlag(); - } else { + } + else + { // Newer file found. GroupManager.logger.log(Level.WARNING, "Newer Users file found for " + w.getName() + ", but we have local changes!"); throw new IllegalStateException("Unable to save unless you issue a '/mansave force'"); } - } else { + } + else + { //Check for newer file as no local changes. - if (w.getTimeStampUsers() < w.getUsersFile().lastModified()) { + if (w.getTimeStampUsers() < w.getUsersFile().lastModified()) + { System.out.print("Newer Users file found (Loading changes)!"); // Backup Users file backupFile(w, false); w.reloadUsers(); } } + } alreadyDone.add(w); } } /** * Backup the Groups/Users file - * + * * @param w * @param groups */ - private void backupFile(OverloadedWorldHolder w, Boolean groups) { + private void backupFile(OverloadedWorldHolder w, Boolean groups) + { File backupFile = new File(plugin.getBackupFolder(), "bkp_" + w.getName() + (groups ? "_g_" : "_u_") + Tasks.getDateString() + ".yml"); - try { + try + { Tasks.copy((groups ? w.getGroupsFile() : w.getUsersFile()), backupFile); - } catch (IOException ex) { + } + catch (IOException ex) + { GroupManager.logger.log(Level.SEVERE, null, ex); } } /** - * Returns the dataHolder for the given world. - * If the world is not on the worlds list, returns the default world + * Returns the dataHolder for the given world. If the world is not on the worlds list, returns the default world * holder. - * - * Mirrors return their parent world data. - * If no mirroring data it returns the default world. - * + * + * Mirrors return their parent world data. If no mirroring data it returns the default world. + * * @param worldName * @return OverloadedWorldHolder */ - public OverloadedWorldHolder getWorldData(String worldName) { + public OverloadedWorldHolder getWorldData(String worldName) + { String worldNameLowered = worldName.toLowerCase(); // Find this worlds data if (worldsData.containsKey(worldNameLowered)) + { return getUpdatedWorldData(worldNameLowered); + } // Oddly no data source was found for this world so return the default. GroupManager.logger.finest("Requested world " + worldName + " not found or badly mirrored. Returning default world..."); @@ -376,17 +469,18 @@ public class WorldsHolder { } /** - * Get the requested world data and update it's dataSource to be relevant - * for this world - * + * Get the requested world data and update it's dataSource to be relevant for this world + * * @param worldName * @return updated world holder */ - private OverloadedWorldHolder getUpdatedWorldData(String worldName) { + private OverloadedWorldHolder getUpdatedWorldData(String worldName) + { String worldNameLowered = worldName.toLowerCase(); - if (worldsData.containsKey(worldNameLowered)) { + if (worldsData.containsKey(worldNameLowered)) + { OverloadedWorldHolder data = worldsData.get(worldNameLowered); data.updateDataSource(); return data; @@ -396,134 +490,160 @@ public class WorldsHolder { } /** - * Do a matching of playerName, if its found only one player, do - * getWorldData(player) - * + * Do a matching of playerName, if its found only one player, do getWorldData(player) + * * @param playerName * @return null if matching returned no player, or more than one. */ - public OverloadedWorldHolder getWorldDataByPlayerName(String playerName) { + public OverloadedWorldHolder getWorldDataByPlayerName(String playerName) + { List<Player> matchPlayer = plugin.getServer().matchPlayer(playerName); - if (matchPlayer.size() == 1) { + if (matchPlayer.size() == 1) + { return getWorldData(matchPlayer.get(0)); } return null; } /** - * Retrieves the field player.getWorld().getName() and do - * getWorld(worldName) - * + * Retrieves the field player.getWorld().getName() and do getWorld(worldName) + * * @param player * @return OverloadedWorldHolder */ - public OverloadedWorldHolder getWorldData(Player player) { + public OverloadedWorldHolder getWorldData(Player player) + { return getWorldData(player.getWorld().getName()); } /** * It does getWorld(worldName).getPermissionsHandler() - * + * * @param worldName * @return AnjoPermissionsHandler */ - public AnjoPermissionsHandler getWorldPermissions(String worldName) { + public AnjoPermissionsHandler getWorldPermissions(String worldName) + { return getWorldData(worldName).getPermissionsHandler(); } /** * Returns the PermissionsHandler for this player data - * + * * @param player * @return AnjoPermissionsHandler */ - public AnjoPermissionsHandler getWorldPermissions(Player player) { + public AnjoPermissionsHandler getWorldPermissions(Player player) + { return getWorldData(player).getPermissionsHandler(); } /** - * Id does getWorldDataByPlayerName(playerName). - * If it doesnt return null, it will return result.getPermissionsHandler() - * + * Id does getWorldDataByPlayerName(playerName). If it doesnt return null, it will return + * result.getPermissionsHandler() + * * @param playerName * @return null if the player matching gone wrong. */ - public AnjoPermissionsHandler getWorldPermissionsByPlayerName(String playerName) { + public AnjoPermissionsHandler getWorldPermissionsByPlayerName(String playerName) + { WorldDataHolder dh = getWorldDataByPlayerName(playerName); - if (dh != null) { + if (dh != null) + { return dh.getPermissionsHandler(); } return null; } - private void verifyFirstRun() { + private void verifyFirstRun() + { Properties server = new Properties(); - try { + try + { server.load(new FileInputStream(new File("server.properties"))); serverDefaultWorldName = server.getProperty("level-name").toLowerCase(); setupWorldFolder(serverDefaultWorldName); - } catch (IOException ex) { + } + catch (IOException ex) + { GroupManager.logger.log(Level.SEVERE, null, ex); } } - public void setupWorldFolder(String worldName) { + public void setupWorldFolder(String worldName) + { String worldNameLowered = worldName.toLowerCase(); worldsFolder = new File(plugin.getDataFolder(), "worlds"); - if (!worldsFolder.exists()) { + if (!worldsFolder.exists()) + { worldsFolder.mkdirs(); } File defaultWorldFolder = new File(worldsFolder, worldNameLowered); - if ((!defaultWorldFolder.exists()) && ((!mirrorsGroup.containsKey(worldNameLowered))) || (!mirrorsUser.containsKey(worldNameLowered))) { + if ((!defaultWorldFolder.exists()) && ((!mirrorsGroup.containsKey(worldNameLowered))) || (!mirrorsUser.containsKey(worldNameLowered))) + { /* * check and convert all old case sensitive folders to lower case */ File casedWorldFolder = new File(worldsFolder, worldName); - if ((casedWorldFolder.exists()) && (casedWorldFolder.getName().toLowerCase().equals(worldNameLowered))) { + if ((casedWorldFolder.exists()) && (casedWorldFolder.getName().toLowerCase().equals(worldNameLowered))) + { /* * Rename the old folder to the new lower cased format */ casedWorldFolder.renameTo(new File(worldsFolder, worldNameLowered)); - } else { + } + else + { /* * Else we just create the folder */ defaultWorldFolder.mkdirs(); } } - if (defaultWorldFolder.exists()) { - if (!mirrorsGroup.containsKey(worldNameLowered)) { + if (defaultWorldFolder.exists()) + { + if (!mirrorsGroup.containsKey(worldNameLowered)) + { File groupsFile = new File(defaultWorldFolder, "groups.yml"); - if (!groupsFile.exists() || groupsFile.length() == 0) { + if (!groupsFile.exists() || groupsFile.length() == 0) + { InputStream template = plugin.getResourceAsStream("groups.yml"); - try { + try + { Tasks.copy(template, groupsFile); - } catch (IOException ex) { + } + catch (IOException ex) + { GroupManager.logger.log(Level.SEVERE, null, ex); } } } - if (!mirrorsUser.containsKey(worldNameLowered)) { + if (!mirrorsUser.containsKey(worldNameLowered)) + { File usersFile = new File(defaultWorldFolder, "users.yml"); - if (!usersFile.exists() || usersFile.length() == 0) { + if (!usersFile.exists() || usersFile.length() == 0) + { InputStream template = plugin.getResourceAsStream("users.yml"); - try { + try + { Tasks.copy(template, usersFile); - } catch (IOException ex) { + } + catch (IOException ex) + { GroupManager.logger.log(Level.SEVERE, null, ex); } @@ -534,30 +654,36 @@ public class WorldsHolder { /** * Copies the specified world data to another world - * + * * @param fromWorld * @param toWorld * @return true if successfully copied. */ - public boolean cloneWorld(String fromWorld, String toWorld) { + public boolean cloneWorld(String fromWorld, String toWorld) + { File fromWorldFolder = new File(worldsFolder, fromWorld.toLowerCase()); File toWorldFolder = new File(worldsFolder, toWorld.toLowerCase()); - if (toWorldFolder.exists() || !fromWorldFolder.exists()) { + if (toWorldFolder.exists() || !fromWorldFolder.exists()) + { return false; } File fromWorldGroups = new File(fromWorldFolder, "groups.yml"); File fromWorldUsers = new File(fromWorldFolder, "users.yml"); - if (!fromWorldGroups.exists() || !fromWorldUsers.exists()) { + if (!fromWorldGroups.exists() || !fromWorldUsers.exists()) + { return false; } File toWorldGroups = new File(toWorldFolder, "groups.yml"); File toWorldUsers = new File(toWorldFolder, "users.yml"); toWorldFolder.mkdirs(); - try { + try + { Tasks.copy(fromWorldGroups, toWorldGroups); Tasks.copy(fromWorldUsers, toWorldUsers); - } catch (IOException ex) { + } + catch (IOException ex) + { Logger.getLogger(WorldsHolder.class.getName()).log(Level.SEVERE, null, ex); return false; } @@ -566,43 +692,47 @@ public class WorldsHolder { /** * Wrapper for LoadWorld(String,Boolean) for backwards compatibility - * - * Load a world from file. - * If it already been loaded, summon reload method from dataHolder. - * + * + * Load a world from file. If it already been loaded, summon reload method from dataHolder. + * * @param worldName */ - public void loadWorld(String worldName) { + public void loadWorld(String worldName) + { loadWorld(worldName, false); } /** - * Load a world from file. - * If it already been loaded, summon reload method from dataHolder. - * + * Load a world from file. If it already been loaded, summon reload method from dataHolder. + * * @param worldName */ - public void loadWorld(String worldName, Boolean isMirror) { + public void loadWorld(String worldName, Boolean isMirror) + { String worldNameLowered = worldName.toLowerCase(); - if (worldsData.containsKey(worldNameLowered)) { + if (worldsData.containsKey(worldNameLowered)) + { worldsData.get(worldNameLowered).reload(); return; } GroupManager.logger.finest("Trying to load world " + worldName + "..."); File thisWorldFolder = new File(worldsFolder, worldNameLowered); - if ((isMirror) || (thisWorldFolder.exists() && thisWorldFolder.isDirectory())) { + if ((isMirror) || (thisWorldFolder.exists() && thisWorldFolder.isDirectory())) + { // Setup file handles, if not mirrored File groupsFile = (mirrorsGroup.containsKey(worldNameLowered)) ? null : new File(thisWorldFolder, "groups.yml"); File usersFile = (mirrorsUser.containsKey(worldNameLowered)) ? null : new File(thisWorldFolder, "users.yml"); - if ((groupsFile != null) && (!groupsFile.exists())) { + if ((groupsFile != null) && (!groupsFile.exists())) + { throw new IllegalArgumentException("Groups file for world '" + worldName + "' doesnt exist: " + groupsFile.getPath()); } - if ((usersFile != null) && (!usersFile.exists())) { + if ((usersFile != null) && (!usersFile.exists())) + { throw new IllegalArgumentException("Users file for world '" + worldName + "' doesnt exist: " + usersFile.getPath()); } @@ -610,15 +740,23 @@ public class WorldsHolder { // Map the group object for any mirror if (mirrorsGroup.containsKey(worldNameLowered)) + { tempHolder.setGroupsObject(this.getWorldData(mirrorsGroup.get(worldNameLowered)).getGroupsObject()); + } else + { tempHolder.loadGroups(groupsFile); + } // Map the user object for any mirror if (mirrorsUser.containsKey(worldNameLowered)) + { tempHolder.setUsersObject(this.getWorldData(mirrorsUser.get(worldNameLowered)).getUsersObject()); + } else + { tempHolder.loadUsers(usersFile); + } OverloadedWorldHolder thisWorldData = new OverloadedWorldHolder(tempHolder); @@ -628,7 +766,8 @@ public class WorldsHolder { // Set the file TimeStamps as it will be default from the initial load. thisWorldData.setTimeStamps(); - if (thisWorldData != null) { + if (thisWorldData != null) + { GroupManager.logger.finest("Successful load of world " + worldName + "..."); worldsData.put(worldNameLowered, thisWorldData); return; @@ -640,15 +779,17 @@ public class WorldsHolder { /** * Tells if the such world has been mapped. - * + * * It will return true if world is a mirror. - * + * * @param worldName * @return true if world is loaded or mirrored. false if not listed */ - public boolean isInList(String worldName) { + public boolean isInList(String worldName) + { - if (worldsData.containsKey(worldName.toLowerCase()) || mirrorsGroup.containsKey(worldName.toLowerCase()) || mirrorsUser.containsKey(worldName.toLowerCase())) { + if (worldsData.containsKey(worldName.toLowerCase()) || mirrorsGroup.containsKey(worldName.toLowerCase()) || mirrorsUser.containsKey(worldName.toLowerCase())) + { return true; } return false; @@ -656,13 +797,15 @@ public class WorldsHolder { /** * Verify if world has it's own file permissions. - * + * * @param worldName * @return true if it has its own holder. false if not. */ - public boolean hasOwnData(String worldName) { + public boolean hasOwnData(String worldName) + { - if (worldsData.containsKey(worldName.toLowerCase()) && (!mirrorsGroup.containsKey(worldName.toLowerCase()) || !mirrorsUser.containsKey(worldName.toLowerCase()))) { + if (worldsData.containsKey(worldName.toLowerCase()) && (!mirrorsGroup.containsKey(worldName.toLowerCase()) || !mirrorsUser.containsKey(worldName.toLowerCase()))) + { return true; } return false; @@ -671,37 +814,45 @@ public class WorldsHolder { /** * @return the defaultWorld */ - public OverloadedWorldHolder getDefaultWorld() { + public OverloadedWorldHolder getDefaultWorld() + { return getUpdatedWorldData(serverDefaultWorldName); } /** - * Returns all physically loaded worlds which have at least - * one of their own data sets for users or groups. - * + * Returns all physically loaded worlds which have at least one of their own data sets for users or groups. + * * @return ArrayList<OverloadedWorldHolder> of all loaded worlds */ - public ArrayList<OverloadedWorldHolder> allWorldsDataList() { + public ArrayList<OverloadedWorldHolder> allWorldsDataList() + { ArrayList<OverloadedWorldHolder> list = new ArrayList<OverloadedWorldHolder>(); - for (OverloadedWorldHolder data : worldsData.values()) { - if ((!list.contains(data)) && (!mirrorsGroup.containsKey(data.getName().toLowerCase()) || !mirrorsUser.containsKey(data.getName().toLowerCase()))) { + for (OverloadedWorldHolder data : worldsData.values()) + { + if ((!list.contains(data)) && (!mirrorsGroup.containsKey(data.getName().toLowerCase()) || !mirrorsUser.containsKey(data.getName().toLowerCase()))) + { String worldNameLowered = data.getName().toLowerCase(); String usersMirror = mirrorsUser.get(worldNameLowered); String groupsMirror = mirrorsGroup.get(worldNameLowered); // is users mirrored? - if (usersMirror != null) { + if (usersMirror != null) + { // If both are mirrored - if (groupsMirror != null) { + if (groupsMirror != null) + { // if the data sources are the same, return the parent - if (usersMirror == groupsMirror) { + if (usersMirror == groupsMirror) + { if (!list.contains(usersMirror.toLowerCase())) + { list.add(worldsData.get(usersMirror.toLowerCase())); + } continue; } // Both data sources are mirrors, but they are from different parents |