Merge remote branch 'remotes/origin/groupmanager'

This commit is contained in:
KHobbits 2012-01-14 15:52:22 +00:00
commit f0def90301
9 changed files with 634 additions and 534 deletions

View File

@ -98,3 +98,6 @@ v 1.8:
- Changed GlobalGroups to save/load before local groups in the scheduled data saving/loading - Changed GlobalGroups to save/load before local groups in the scheduled data saving/loading
- Fix 'manucheckp' to correctly report if a permission is available from GroupManager or Bukkit. - Fix 'manucheckp' to correctly report if a permission is available from GroupManager or Bukkit.
- Changed over to a reflection method for populating superperms as Bukkit lags when you handle permissions one at a time. - Changed over to a reflection method for populating superperms as Bukkit lags when you handle permissions one at a time.
- Major, MAJOR changes to support partial/full world mirroring.
You can now mirror groups.yml, users.yml or both files between different worlds.
- Catch NullPointerErrors generated by blank permission nodes.

View File

@ -25,14 +25,22 @@ settings:
level: INFO level: INFO
mirrors: mirrors:
# Worlds listed here have their permissions mirrored in their children. # Worlds listed here have their settings mirrored in their children.
# the first element 'world' is the main worlds name # The first element 'world' is the main worlds name
# subsequent elements '- world_nether' are worlds which will use the same # subsequent elements 'world_nether' and 'world_the_end' are worlds which will use
# user/groups permissions as the parent. # the same user/groups files as the parent.
# Each child world can be configured to mirror the 'groups', 'users' or both files from it's parent.
world: world:
- world_nether world_nether:
- world_the_end - users
- world2 - groups
- world3 world_the_end:
# world4: - users
# - world5 - groups
# world2: (World2 would have it's own set of user and groups files)
# world3:
# - users (World3 would use the users.yml from world2, but it's own groups.yml)
# world4:
# - groups (World4 would use the groups.yml from world2, but it's own users.yml)
# world5:
# - world6 (this would cause world6 to mirror both files from world5)

View File

@ -948,7 +948,7 @@ public class GroupManager extends JavaPlugin {
} }
} else { } else {
sender.sendMessage(ChatColor.YELLOW + "The grpup '" + auxGroup.getName() + "' has no specific permissions."); sender.sendMessage(ChatColor.YELLOW + "The group '" + auxGroup.getName() + "' has no specific permissions.");
auxString = ""; auxString = "";
for (String grp : auxGroup.getInherits()) { for (String grp : auxGroup.getInherits()) {
auxString += grp + ", "; auxString += grp + ", ";
@ -1522,6 +1522,7 @@ public class GroupManager extends JavaPlugin {
} }
// WORKING // WORKING
config.load(); config.load();
worldsHolder.mirrorSetUp();
isLoaded = false; isLoaded = false;
@ -1539,7 +1540,6 @@ public class GroupManager extends JavaPlugin {
worldsHolder.reloadAll(); worldsHolder.reloadAll();
sender.sendMessage(ChatColor.YELLOW + " The current world was reloaded."); sender.sendMessage(ChatColor.YELLOW + " The current world was reloaded.");
} }
worldsHolder.mirrorSetUp();
isLoaded = true; isLoaded = true;

View File

@ -0,0 +1,118 @@
package org.anjocaido.groupmanager.dataholder;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.anjocaido.groupmanager.data.Group;
/**
* @author ElgarL
*
*/
public class GroupsDataHolder {
/**
* Root World name this set of groups is associated with.
*/
private String name;
private Group defaultGroup = null;
private File groupsFile;
private boolean haveGroupsChanged = false;
private long timeStampGroups = 0;
/**
* The actual groups holder
*/
private Map<String, Group> groups = new HashMap<String, Group>();
/**
* Constructor
*/
protected GroupsDataHolder() {
}
protected void setWorldName(String worldName) {
name = worldName;
}
/**
* @return the name
*/
public String getWorldName() {
return name;
}
/**
* @return the defaultGroup
*/
public Group getDefaultGroup() {
return defaultGroup;
}
/**
* @param defaultGroup the defaultGroup to set
*/
public void setDefaultGroup(Group defaultGroup) {
this.defaultGroup = defaultGroup;
}
/**
* @return the groups
*/
public Map<String, Group> getGroups() {
return groups;
}
/**
* @param groups the groups to set
*/
public void setGroups(Map<String, Group> groups) {
this.groups = groups;
}
/**
* @return the groupsFile
*/
public File getGroupsFile() {
return groupsFile;
}
/**
* @param groupsFile the groupsFile to set
*/
public void setGroupsFile(File groupsFile) {
this.groupsFile = groupsFile;
}
/**
* @return the haveGroupsChanged
*/
public boolean HaveGroupsChanged() {
return haveGroupsChanged;
}
/**
* @param haveGroupsChanged the haveGroupsChanged to set
*/
public void setGroupsChanged(boolean haveGroupsChanged) {
this.haveGroupsChanged = haveGroupsChanged;
}
/**
* @return the timeStampGroups
*/
public long getTimeStampGroups() {
return timeStampGroups;
}
/**
* @param timeStampGroups the timeStampGroups to set
*/
public void setTimeStampGroups(long timeStampGroups) {
this.timeStampGroups = timeStampGroups;
}
}

View File

@ -25,13 +25,11 @@ public class OverloadedWorldHolder extends WorldDataHolder {
* *
* @param ph * @param ph
*/ */
@SuppressWarnings("deprecation")
public OverloadedWorldHolder(WorldDataHolder ph) { public OverloadedWorldHolder(WorldDataHolder ph) {
super(ph.getName()); super(ph.getName());
this.f = ph.f; this.setGroupsFile(ph.getGroupsFile());
this.groupsFile = ph.groupsFile; this.setUsersFile(ph.getUsersFile());
this.usersFile = ph.usersFile; //this.setDefaultGroup(ph.getDefaultGroup());
this.defaultGroup = ph.defaultGroup;
this.groups = ph.groups; this.groups = ph.groups;
this.users = ph.users; this.users = ph.users;
} }
@ -49,11 +47,11 @@ public class OverloadedWorldHolder extends WorldDataHolder {
return overloadedUsers.get(userNameLowered); return overloadedUsers.get(userNameLowered);
} }
//END CODE //END CODE
if (users.containsKey(userNameLowered)) { if (getUsers().containsKey(userNameLowered)) {
return users.get(userNameLowered); return getUsers().get(userNameLowered);
} }
User newUser = createUser(userName); User newUser = createUser(userName);
haveUsersChanged = true; setUsersChanged(true);
return newUser; return newUser;
} }
@ -69,8 +67,8 @@ public class OverloadedWorldHolder extends WorldDataHolder {
if (theUser == null) { if (theUser == null) {
return; return;
} }
if ((theUser.getGroup() == null) || (!groups.containsKey(theUser.getGroupName().toLowerCase()))) { if ((theUser.getGroup() == null) || (!getGroups().containsKey(theUser.getGroupName().toLowerCase()))) {
theUser.setGroup(defaultGroup); theUser.setGroup(getDefaultGroup());
} }
//OVERLOADED CODE //OVERLOADED CODE
if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) { if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) {
@ -80,8 +78,8 @@ public class OverloadedWorldHolder extends WorldDataHolder {
} }
//END CODE //END CODE
removeUser(theUser.getName()); removeUser(theUser.getName());
users.put(theUser.getName().toLowerCase(), theUser); getUsers().put(theUser.getName().toLowerCase(), theUser);
haveUsersChanged = true; setUsersChanged(true);
} }
/** /**
@ -97,9 +95,9 @@ public class OverloadedWorldHolder extends WorldDataHolder {
return true; return true;
} }
//END CODE //END CODE
if (users.containsKey(userName.toLowerCase())) { if (getUsers().containsKey(userName.toLowerCase())) {
users.remove(userName.toLowerCase()); getUsers().remove(userName.toLowerCase());
haveUsersChanged = true; setUsersChanged(true);
return true; return true;
} }
return false; return false;
@ -107,16 +105,16 @@ public class OverloadedWorldHolder extends WorldDataHolder {
@Override @Override
public boolean removeGroup(String groupName) { public boolean removeGroup(String groupName) {
if (groupName.equals(defaultGroup)) { if (groupName.equals(getDefaultGroup())) {
return false; return false;
} }
for (String key : groups.keySet()) { for (String key : getGroups().keySet()) {
if (groupName.equalsIgnoreCase(key)) { if (groupName.equalsIgnoreCase(key)) {
groups.remove(key); getGroups().remove(key);
for (String userKey : users.keySet()) { for (String userKey : getUsers().keySet()) {
User user = users.get(userKey); User user = getUsers().get(userKey);
if (user.getGroupName().equalsIgnoreCase(key)) { if (user.getGroupName().equalsIgnoreCase(key)) {
user.setGroup(defaultGroup); user.setGroup(getDefaultGroup());
} }
} }
@ -124,12 +122,12 @@ public class OverloadedWorldHolder extends WorldDataHolder {
for (String userKey : overloadedUsers.keySet()) { for (String userKey : overloadedUsers.keySet()) {
User user = overloadedUsers.get(userKey); User user = overloadedUsers.get(userKey);
if (user.getGroupName().equalsIgnoreCase(key)) { if (user.getGroupName().equalsIgnoreCase(key)) {
user.setGroup(defaultGroup); user.setGroup(getDefaultGroup());
} }
} }
//END OVERLOAD //END OVERLOAD
haveGroupsChanged = true; setGroupsChanged(true);
return true; return true;
} }
} }
@ -143,7 +141,7 @@ public class OverloadedWorldHolder extends WorldDataHolder {
@Override @Override
public Collection<User> getUserList() { public Collection<User> getUserList() {
Collection<User> overloadedList = new ArrayList<User>(); Collection<User> overloadedList = new ArrayList<User>();
Collection<User> normalList = users.values(); Collection<User> normalList = getUsers().values();
for (User u : normalList) { for (User u : normalList) {
if (overloadedUsers.containsKey(u.getName().toLowerCase())) { if (overloadedUsers.containsKey(u.getName().toLowerCase())) {
overloadedList.add(overloadedUsers.get(u.getName().toLowerCase())); overloadedList.add(overloadedUsers.get(u.getName().toLowerCase()));
@ -198,8 +196,8 @@ public class OverloadedWorldHolder extends WorldDataHolder {
if (!isOverloaded(userName)) { if (!isOverloaded(userName)) {
return getUser(userName); return getUser(userName);
} }
if (users.containsKey(userName.toLowerCase())) { if (getUsers().containsKey(userName.toLowerCase())) {
return users.get(userName.toLowerCase()); return getUsers().get(userName.toLowerCase());
} }
User newUser = createUser(userName); User newUser = createUser(userName);
return newUser; return newUser;

View File

@ -0,0 +1,106 @@
package org.anjocaido.groupmanager.dataholder;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.anjocaido.groupmanager.data.User;
/**
* @author ElgarL
*
*/
public class UsersDataHolder {
/**
* Root World name this set of groups is associated with.
*/
private String name;
private File usersFile;
private boolean haveUsersChanged = false;
private long timeStampUsers = 0;
/**
* The actual groups holder
*/
private Map<String, User> users = new HashMap<String, User>();
/**
* Constructor
*/
protected UsersDataHolder() {
}
/**
* @param worldName
*/
public void setWorldName(String worldName) {
this.name = worldName;
}
/**
* @return the name
*/
public String getWorldName() {
return this.name;
}
/**
* @return the users
*/
public Map<String, User> getUsers() {
return users;
}
/**
* @param users the users to set
*/
public void setUsers(Map<String, User> users) {
this.users = users;
}
/**
* @return the usersFile
*/
public File getUsersFile() {
return usersFile;
}
/**
* @param usersFile the usersFile to set
*/
public void setUsersFile(File usersFile) {
this.usersFile = usersFile;
}
/**
* @return the haveUsersChanged
*/
public boolean HaveUsersChanged() {
return haveUsersChanged;
}
/**
* @param haveUsersChanged the haveUsersChanged to set
*/
public void setUsersChanged(boolean haveUsersChanged) {
this.haveUsersChanged = haveUsersChanged;
}
/**
* @return the timeStampUsers
*/
public long getTimeStampUsers() {
return timeStampUsers;
}
/**
* @param timeStampUsers the timeStampUsers to set
*/
public void setTimeStampUsers(long timeStampUsers) {
this.timeStampUsers = timeStampUsers;
}
}

View File

@ -8,7 +8,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -49,72 +48,37 @@ public class WorldDataHolder {
/** /**
* The actual groups holder * The actual groups holder
*/ */
protected Map<String, Group> groups = new HashMap<String, Group>(); protected GroupsDataHolder groups = new GroupsDataHolder();
/** /**
* The actual users holder * The actual users holder
*/ */
protected Map<String, User> users = new HashMap<String, User>(); protected UsersDataHolder users = new UsersDataHolder();
/**
* Points to the default group
*/
protected Group defaultGroup = null;
/**
* The file, which this class loads/save data from/to
* @deprecated
*/
@Deprecated
protected File f;
/** /**
* *
*/ */
protected AnjoPermissionsHandler permissionsHandler; protected AnjoPermissionsHandler permissionsHandler;
/**
*
*/
protected File usersFile;
/**
*
*/
protected File groupsFile;
/**
*
*/
protected boolean haveUsersChanged = false;
/**
*
*/
protected boolean haveGroupsChanged = false;
/**
*
*/
protected long timeStampGroups = 0;
/**
*
*/
protected long timeStampUsers = 0;
/** /**
* Prevent direct instantiation * Prevent direct instantiation
* @param worldName * @param worldName
*/ */
protected WorldDataHolder(String worldName) { public WorldDataHolder(String worldName) {
name = worldName; name = worldName;
} }
/** /**
* The main constructor for a new WorldDataHolder * The main constructor for a new WorldDataHolder
* Please don't set the default group as null
* @param worldName * @param worldName
* @param defaultGroup the default group. its good to start with one * @param groups
* @param users
*/ */
public WorldDataHolder(String worldName, Group defaultGroup) { public WorldDataHolder(String worldName, GroupsDataHolder groups, UsersDataHolder users) {
this.name = worldName; this.name = worldName;
groups.put(defaultGroup.getName().toLowerCase(), defaultGroup); this.groups = groups;
this.defaultGroup = defaultGroup; this.users = users;
//this.defaultGroup = defaultGroup;
} }
/** /**
@ -125,8 +89,8 @@ public class WorldDataHolder {
* @return class that manage that user permission * @return class that manage that user permission
*/ */
public User getUser(String userName) { public User getUser(String userName) {
if (users.containsKey(userName.toLowerCase())) { if (getUsers().containsKey(userName.toLowerCase())) {
return users.get(userName.toLowerCase()); return getUsers().get(userName.toLowerCase());
} }
User newUser = createUser(userName); User newUser = createUser(userName);
return newUser; return newUser;
@ -144,11 +108,11 @@ public class WorldDataHolder {
return; return;
} }
if ((theUser.getGroup() == null)) { if ((theUser.getGroup() == null)) {
theUser.setGroup(defaultGroup); theUser.setGroup(groups.getDefaultGroup());
} }
removeUser(theUser.getName()); removeUser(theUser.getName());
users.put(theUser.getName().toLowerCase(), theUser); getUsers().put(theUser.getName().toLowerCase(), theUser);
haveUsersChanged = true; setUsersChanged(true);
if (GroupManager.isLoaded()) if (GroupManager.isLoaded())
GroupManagerEventHandler.callEvent(theUser, Action.USER_ADDED); GroupManagerEventHandler.callEvent(theUser, Action.USER_ADDED);
} }
@ -159,9 +123,9 @@ public class WorldDataHolder {
* @return true if it had something to remove * @return true if it had something to remove
*/ */
public boolean removeUser(String userName) { public boolean removeUser(String userName) {
if (users.containsKey(userName.toLowerCase())) { if (getUsers().containsKey(userName.toLowerCase())) {
users.remove(userName.toLowerCase()); getUsers().remove(userName.toLowerCase());
haveUsersChanged = true; setUsersChanged(true);
if (GroupManager.isLoaded()) if (GroupManager.isLoaded())
GroupManagerEventHandler.callEvent(userName, GMUserEvent.Action.USER_REMOVED); GroupManagerEventHandler.callEvent(userName, GMUserEvent.Action.USER_REMOVED);
return true; return true;
@ -175,7 +139,7 @@ public class WorldDataHolder {
* @return true if we have data for this player. * @return true if we have data for this player.
*/ */
public boolean isUserDeclared(String userName) { public boolean isUserDeclared(String userName) {
return users.containsKey(userName.toLowerCase()); return getUsers().containsKey(userName.toLowerCase());
} }
/** /**
@ -183,11 +147,11 @@ public class WorldDataHolder {
* @param group the group you want make default. * @param group the group you want make default.
*/ */
public void setDefaultGroup(Group group) { public void setDefaultGroup(Group group) {
if (!groups.containsKey(group.getName().toLowerCase()) || (group.getDataSource() != this)) { if (!getGroups().containsKey(group.getName().toLowerCase()) || (group.getDataSource() != this)) {
addGroup(group); addGroup(group);
} }
defaultGroup = this.getGroup(group.getName()); groups.setDefaultGroup(getGroup(group.getName()));
haveGroupsChanged = true; setGroupsChanged(true);
if (GroupManager.isLoaded()) if (GroupManager.isLoaded())
GroupManagerEventHandler.callEvent(GMSystemEvent.Action.DEFAULT_GROUP_CHANGED); GroupManagerEventHandler.callEvent(GMSystemEvent.Action.DEFAULT_GROUP_CHANGED);
} }
@ -197,7 +161,7 @@ public class WorldDataHolder {
* @return the default group * @return the default group
*/ */
public Group getDefaultGroup() { public Group getDefaultGroup() {
return defaultGroup; return groups.getDefaultGroup();
} }
/** /**
@ -209,7 +173,7 @@ public class WorldDataHolder {
if (groupName.toLowerCase().startsWith("g:")) if (groupName.toLowerCase().startsWith("g:"))
return GroupManager.getGlobalGroups().getGroup(groupName); return GroupManager.getGlobalGroups().getGroup(groupName);
else else
return groups.get(groupName.toLowerCase()); return getGroups().get(groupName.toLowerCase());
} }
/** /**
@ -222,7 +186,7 @@ public class WorldDataHolder {
if (groupName.toLowerCase().startsWith("g:")) if (groupName.toLowerCase().startsWith("g:"))
return GroupManager.getGlobalGroups().hasGroup(groupName); return GroupManager.getGlobalGroups().hasGroup(groupName);
else else
return groups.containsKey(groupName.toLowerCase()); return getGroups().containsKey(groupName.toLowerCase());
} }
/** /**
@ -240,8 +204,8 @@ public class WorldDataHolder {
groupToAdd = groupToAdd.clone(this); groupToAdd = groupToAdd.clone(this);
} }
removeGroup(groupToAdd.getName()); removeGroup(groupToAdd.getName());
groups.put(groupToAdd.getName().toLowerCase(), groupToAdd); getGroups().put(groupToAdd.getName().toLowerCase(), groupToAdd);
haveGroupsChanged = true; setGroupsChanged(true);
if (GroupManager.isLoaded()) if (GroupManager.isLoaded())
GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED); GroupManagerEventHandler.callEvent(groupToAdd, GMGroupEvent.Action.GROUP_ADDED);
} }
@ -256,12 +220,12 @@ public class WorldDataHolder {
return GroupManager.getGlobalGroups().removeGroup(groupName); return GroupManager.getGlobalGroups().removeGroup(groupName);
} }
if (defaultGroup != null && groupName.equalsIgnoreCase(defaultGroup.getName())) { if (getDefaultGroup() != null && groupName.equalsIgnoreCase(getDefaultGroup().getName())) {
return false; return false;
} }
if (groups.containsKey(groupName.toLowerCase())) { if (getGroups().containsKey(groupName.toLowerCase())) {
groups.remove(groupName.toLowerCase()); getGroups().remove(groupName.toLowerCase());
haveGroupsChanged = true; setGroupsChanged(true);
if (GroupManager.isLoaded()) if (GroupManager.isLoaded())
GroupManagerEventHandler.callEvent(groupName.toLowerCase(), GMGroupEvent.Action.GROUP_REMOVED); GroupManagerEventHandler.callEvent(groupName.toLowerCase(), GMGroupEvent.Action.GROUP_REMOVED);
return true; return true;
@ -277,13 +241,13 @@ public class WorldDataHolder {
* @return null if user already exists. or new User * @return null if user already exists. or new User
*/ */
public User createUser(String userName) { public User createUser(String userName) {
if (this.users.containsKey(userName.toLowerCase())) { if (getUsers().containsKey(userName.toLowerCase())) {
return null; return null;
} }
User newUser = new User(this, userName); User newUser = new User(this, userName);
newUser.setGroup(defaultGroup); newUser.setGroup(groups.getDefaultGroup());
this.addUser(newUser); addUser(newUser);
haveUsersChanged = true; setUsersChanged(true);
return newUser; return newUser;
} }
@ -299,13 +263,13 @@ public class WorldDataHolder {
return GroupManager.getGlobalGroups().newGroup(newGroup); return GroupManager.getGlobalGroups().newGroup(newGroup);
} }
if (this.groups.containsKey(groupName.toLowerCase())) { if (getGroups().containsKey(groupName.toLowerCase())) {
return null; return null;
} }
Group newGroup = new Group(this, groupName); Group newGroup = new Group(this, groupName);
this.addGroup(newGroup); addGroup(newGroup);
haveGroupsChanged = true; setGroupsChanged(true);
return newGroup; return newGroup;
} }
@ -314,7 +278,7 @@ public class WorldDataHolder {
* @return a collection of the groups * @return a collection of the groups
*/ */
public Collection<Group> getGroupList() { public Collection<Group> getGroupList() {
return groups.values(); return getGroups().values();
} }
/** /**
@ -322,7 +286,7 @@ public class WorldDataHolder {
* @return a collection of the users * @return a collection of the users
*/ */
public Collection<User> getUserList() { public Collection<User> getUserList() {
return users.values(); return getUsers().values();
} }
/** /**
@ -352,9 +316,9 @@ public class WorldDataHolder {
for (Group tempGroup : ph.getGroupList()) { for (Group tempGroup : ph.getGroupList()) {
tempGroup.clone(this); tempGroup.clone(this);
} }
this.setDefaultGroup(this.getGroup(ph.getDefaultGroup().getName())); this.setDefaultGroup(getGroup(ph.getDefaultGroup().getName()));
this.removeGroupsChangedFlag(); this.removeGroupsChangedFlag();
this.timeStampGroups = getGroupsFile().lastModified(); this.setTimeStampGroups(getGroupsFile().lastModified());
ph = null; ph = null;
} catch (Exception ex) { } catch (Exception ex) {
@ -377,7 +341,7 @@ public class WorldDataHolder {
tempGroup.clone(ph); tempGroup.clone(ph);
} }
// setup the default group before loading user data. // setup the default group before loading user data.
ph.setDefaultGroup(ph.getGroup(this.getDefaultGroup().getName())); ph.setDefaultGroup(ph.getGroup(getDefaultGroup().getName()));
loadUsers(ph, getUsersFile()); loadUsers(ph, getUsersFile());
// transfer new data // transfer new data
resetUsers(); resetUsers();
@ -385,7 +349,7 @@ public class WorldDataHolder {
tempUser.clone(this); tempUser.clone(this);
} }
this.removeUsersChangedFlag(); this.removeUsersChangedFlag();
this.timeStampUsers = getUsersFile().lastModified(); this.setTimeStampUsers(getUsersFile().lastModified());
ph = null; ph = null;
} catch (Exception ex) { } catch (Exception ex) {
@ -395,165 +359,39 @@ public class WorldDataHolder {
GroupManagerEventHandler.callEvent(GMSystemEvent.Action.RELOADED); GroupManagerEventHandler.callEvent(GMSystemEvent.Action.RELOADED);
} }
/** public void loadGroups(File groupsFile) {
* Save by yourself!
* @deprecated GroupManager.setLoaded(false);
*/ try {
@Deprecated setGroupsFile(groupsFile);
public void commit() { loadGroups(this, groupsFile);
writeGroups(this, getGroupsFile()); } catch (FileNotFoundException e) {
writeUsers(this, getUsersFile()); e.printStackTrace();
throw new IllegalArgumentException("The file which should contain groups does not exist!\n" + groupsFile.getPath());
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException("Error access the groups file!\n" + groupsFile.getPath());
}
GroupManager.setLoaded(true);
} }
/** public void loadUsers(File usersFile) {
* Returns a data holder for the given file
*
* @param worldName
* @param file
* @return a new WorldDataHolder
*
* @throws Exception
* @deprecated
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Deprecated
public static WorldDataHolder load(String worldName, File file) throws Exception {
WorldDataHolder ph = new WorldDataHolder(worldName);
ph.f = file;
final Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> rootDataNode;
if (!file.exists()) {
throw new Exception("The file which should contain permissions does not exist!\n" + file.getPath());
}
FileInputStream rx = new FileInputStream(file);
try {
rootDataNode = (Map<String, Object>) yaml.load(new UnicodeReader(rx));
if (rootDataNode == null) {
throw new NullPointerException();
}
} catch (Exception ex) {
throw new Exception("The following file couldn't pass on Parser.\n" + file.getPath(), ex);
} finally {
rx.close();
}
Map<String, List<String>> inheritance = new HashMap<String, List<String>>();
try {
Map<String, Object> allGroupsNode = (Map<String, Object>) rootDataNode.get("groups");
for (String groupKey : allGroupsNode.keySet()) {
Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey);
Group thisGrp = ph.createGroup(groupKey);
if (thisGrp == null) {
throw new IllegalArgumentException("I think this group was declared more than once: " + groupKey);
}
if (thisGroupNode.get("default") == null) {
thisGroupNode.put("default", false);
}
if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) {
if (ph.getDefaultGroup() != null) {
GroupManager.logger.warning("The group " + thisGrp.getName() + " is declaring be default where" + ph.getDefaultGroup().getName() + " already was.");
GroupManager.logger.warning("Overriding first request.");
}
ph.setDefaultGroup(thisGrp);
}
//PERMISSIONS NODE GroupManager.setLoaded(false);
if (thisGroupNode.get("permissions") == null) { try {
thisGroupNode.put("permissions", new ArrayList<String>()); setUsersFile(usersFile);
} loadUsers(this, usersFile);
if (thisGroupNode.get("permissions") instanceof List) { } catch (FileNotFoundException e) {
for (Object o : ((List) thisGroupNode.get("permissions"))) { e.printStackTrace();
thisGrp.addPermission(o.toString()); throw new IllegalArgumentException("The file which should contain users does not exist!\n" + usersFile.getPath());
} } catch (IOException e) {
} else if (thisGroupNode.get("permissions") instanceof String) { e.printStackTrace();
thisGrp.addPermission((String) thisGroupNode.get("permissions")); throw new IllegalArgumentException("Error access the users file!\n" + usersFile.getPath());
} else { }
throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>): " + thisGroupNode.get("permissions").getClass().getName());
}
//INFO NODE GroupManager.setLoaded(true);
Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info");
if (infoNode != null) {
thisGrp.setVariables(infoNode);
}
//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);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception("Your Permissions config file is invalid. See console for details.");
}
if (ph.defaultGroup == null) {
throw new IllegalArgumentException("There was no Default Group declared.");
}
for (String groupKey : inheritance.keySet()) {
List<String> inheritedList = inheritance.get(groupKey);
Group thisGroup = ph.getGroup(groupKey);
for (String inheritedKey : inheritedList) {
Group inheritedGroup = ph.getGroup(inheritedKey);
if (thisGroup != null && inheritedGroup != null) {
thisGroup.addInherits(inheritedGroup);
}
}
}
// Process USERS
Map<String, Object> allUsersNode = (Map<String, Object>) rootDataNode.get("users");
for (String usersKey : allUsersNode.keySet()) {
Map<String, Object> thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
User thisUser = ph.createUser(usersKey);
if (thisUser == null) {
GroupManager.logger.warning("I think this user was declared more than once: " + usersKey);
continue;
}
if (thisUserNode.get("permissions") == null) {
thisUserNode.put("permissions", new ArrayList<String>());
}
if (thisUserNode.get("permissions") instanceof List) {
for (Object o : ((List) thisUserNode.get("permissions"))) {
thisUser.addPermission(o.toString());
}
} else if (thisUserNode.get("permissions") instanceof String) {
thisUser.addPermission(thisUserNode.get("permissions").toString());
}
//USER INFO NODE - BETA
//INFO NODE
Map<String, Object> infoNode = (Map<String, Object>) thisUserNode.get("info");
if (infoNode != null) {
thisUser.setVariables(infoNode);
}
//END INFO NODE - BETA
if (thisUserNode.get("group") != null) {
Group hisGroup = ph.getGroup(thisUserNode.get("group").toString());
if (hisGroup == null) {
GroupManager.logger.warning("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName());
thisUser.setGroup(ph.defaultGroup);
}
thisUser.setGroup(hisGroup);
} else {
thisUser.setGroup(ph.defaultGroup);
}
}
return ph;
} }
/** /**
* Returns a NEW data holder containing data read from the files * Returns a NEW data holder containing data read from the files
* *
@ -568,8 +406,8 @@ public class WorldDataHolder {
WorldDataHolder ph = new WorldDataHolder(worldName); WorldDataHolder ph = new WorldDataHolder(worldName);
GroupManager.setLoaded(false); GroupManager.setLoaded(false);
loadGroups(ph, groupsFile); if (groupsFile != null) loadGroups(ph, groupsFile);
loadUsers(ph, usersFile); if (usersFile != null) loadUsers(ph, usersFile);
GroupManager.setLoaded(true); GroupManager.setLoaded(true);
return ph; return ph;
@ -632,7 +470,12 @@ public class WorldDataHolder {
} }
if (thisGroupNode.get("permissions") instanceof List) { if (thisGroupNode.get("permissions") instanceof List) {
for (Object o : ((List) thisGroupNode.get("permissions"))) { for (Object o : ((List) thisGroupNode.get("permissions"))) {
thisGrp.addPermission(o.toString()); try {
thisGrp.addPermission(o.toString());
} catch (NullPointerException e) {
// Ignore this entry as it's null.
//throw new IllegalArgumentException("Invalid permission node in group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
} }
} else if (thisGroupNode.get("permissions") instanceof String) { } else if (thisGroupNode.get("permissions") instanceof String) {
thisGrp.addPermission((String) thisGroupNode.get("permissions")); thisGrp.addPermission((String) thisGroupNode.get("permissions"));
@ -673,7 +516,7 @@ public class WorldDataHolder {
// ex.printStackTrace(); // ex.printStackTrace();
// throw new IllegalArgumentException("Your Permissions config file is invalid. See console for details."); // throw new IllegalArgumentException("Your Permissions config file is invalid. See console for details.");
//} //}
if (ph.defaultGroup == null) { if (ph.getDefaultGroup() == null) {
throw new IllegalArgumentException("There was no Default Group declared in file: " + groupsFile.getPath()); throw new IllegalArgumentException("There was no Default Group declared in file: " + groupsFile.getPath());
} }
for (String groupKey : inheritance.keySet()) { for (String groupKey : inheritance.keySet()) {
@ -689,7 +532,7 @@ public class WorldDataHolder {
ph.removeGroupsChangedFlag(); ph.removeGroupsChangedFlag();
// Update the LastModified time. // Update the LastModified time.
ph.groupsFile = groupsFile; ph.setGroupsFile(groupsFile);
ph.setTimeStampGroups(groupsFile.lastModified()); ph.setTimeStampGroups(groupsFile.lastModified());
//return ph; //return ph;
@ -744,7 +587,12 @@ public class WorldDataHolder {
thisUser.addPermission(o.toString()); thisUser.addPermission(o.toString());
} }
} else if (thisUserNode.get("permissions") instanceof String) { } else if (thisUserNode.get("permissions") instanceof String) {
thisUser.addPermission(thisUserNode.get("permissions").toString()); try {
thisUser.addPermission(thisUserNode.get("permissions").toString());
} catch (NullPointerException e) {
// Ignore this entry as it's null.
//throw new IllegalArgumentException("Invalid permission node for user: " + thisUser.getName() + " in file: " + UserFile.getPath());
}
} }
//SUBGROUPS LOADING //SUBGROUPS LOADING
@ -788,108 +636,21 @@ public class WorldDataHolder {
Group hisGroup = ph.getGroup(thisUserNode.get("group").toString()); Group hisGroup = ph.getGroup(thisUserNode.get("group").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()); 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.defaultGroup; hisGroup = ph.getDefaultGroup();
//throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName()); //throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName());
} }
thisUser.setGroup(hisGroup); thisUser.setGroup(hisGroup);
} else { } else {
thisUser.setGroup(ph.defaultGroup); thisUser.setGroup(ph.getDefaultGroup());
} }
} }
ph.removeUsersChangedFlag(); ph.removeUsersChangedFlag();
// Update the LastModified time. // Update the LastModified time.
ph.usersFile = usersFile; ph.setUsersFile(usersFile);
ph.setTimeStampUsers(usersFile.lastModified()); ph.setTimeStampUsers(usersFile.lastModified());
} }
/**
* Write a dataHolder in a specified file
* @param ph
* @param file
* @deprecated
*/
@Deprecated
public static void write(WorldDataHolder ph, File file) {
Map<String, Object> root = new HashMap<String, Object>();
Map<String, Object> pluginMap = new HashMap<String, Object>();
root.put("plugin", pluginMap);
Map<String, Object> permissionsMap = new HashMap<String, Object>();
pluginMap.put("permissions", permissionsMap);
permissionsMap.put("system", "default");
Map<String, Object> groupsMap = new HashMap<String, Object>();
root.put("groups", groupsMap);
for (String groupKey : ph.groups.keySet()) {
Group group = ph.groups.get(groupKey);
Map<String, Object> aGroupMap = new HashMap<String, Object>();
groupsMap.put(group.getName(), aGroupMap);
aGroupMap.put("default", group.equals(ph.defaultGroup));
Map<String, Object> infoMap = new HashMap<String, Object>();
aGroupMap.put("info", infoMap);
for (String infoKey : group.getVariables().getVarKeyList()) {
infoMap.put(infoKey, group.getVariables().getVarObject(infoKey));
}
aGroupMap.put("inheritance", group.getInherits());
aGroupMap.put("permissions", group.getPermissionList());
}
Map<String, Object> usersMap = new HashMap<String, Object>();
root.put("users", usersMap);
for (String userKey : ph.users.keySet()) {
User user = ph.users.get(userKey);
if ((user.getGroup() == null || user.getGroup().equals(ph.defaultGroup)) && user.getPermissionList().isEmpty()) {
continue;
}
Map<String, Object> aUserMap = new HashMap<String, Object>();
usersMap.put(user.getName(), aUserMap);
if (user.getGroup() == null) {
aUserMap.put("group", ph.defaultGroup.getName());
} else {
aUserMap.put("group", user.getGroup().getName());
}
//USER INFO NODE - BETA
if (user.getVariables().getSize() > 0) {
Map<String, Object> infoMap = new HashMap<String, Object>();
aUserMap.put("info", infoMap);
for (String infoKey : user.getVariables().getVarKeyList()) {
infoMap.put(infoKey, user.getVariables().getVarObject(infoKey));
}
}
//END USER INFO NODE - BETA
aUserMap.put("permissions", user.getPermissionList());
}
DumperOptions opt = new DumperOptions();
opt.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
final Yaml yaml = new Yaml(opt);
FileWriter tx = null;
try {
tx = new FileWriter(file, false);
tx.write(yaml.dump(root));
tx.flush();
} catch (Exception e) {
} finally {
try {
tx.close();
} catch (IOException ex) {
}
}
}
/** /**
* Write a dataHolder in a specified file * Write a dataHolder in a specified file
* @param ph * @param ph
@ -901,16 +662,16 @@ public class WorldDataHolder {
Map<String, Object> groupsMap = new HashMap<String, Object>(); Map<String, Object> groupsMap = new HashMap<String, Object>();
root.put("groups", groupsMap); root.put("groups", groupsMap);
for (String groupKey : ph.groups.keySet()) { for (String groupKey : ph.getGroups().keySet()) {
Group group = ph.groups.get(groupKey); Group group = ph.getGroups().get(groupKey);
Map<String, Object> aGroupMap = new HashMap<String, Object>(); Map<String, Object> aGroupMap = new HashMap<String, Object>();
groupsMap.put(group.getName(), aGroupMap); groupsMap.put(group.getName(), aGroupMap);
if (ph.defaultGroup == null) { if (ph.getDefaultGroup() == null) {
GroupManager.logger.severe("There is no default group for world: " + ph.getName()); GroupManager.logger.severe("There is no default group for world: " + ph.getName());
} }
aGroupMap.put("default", group.equals(ph.defaultGroup)); aGroupMap.put("default", group.equals(ph.getDefaultGroup()));
Map<String, Object> infoMap = new HashMap<String, Object>(); Map<String, Object> infoMap = new HashMap<String, Object>();
aGroupMap.put("info", infoMap); aGroupMap.put("info", infoMap);
@ -951,7 +712,7 @@ public class WorldDataHolder {
} }
// Update the LastModified time. // Update the LastModified time.
ph.groupsFile = groupsFile; ph.setGroupsFile(groupsFile);
ph.setTimeStampGroups(groupsFile.lastModified()); ph.setTimeStampGroups(groupsFile.lastModified());
ph.removeGroupsChangedFlag(); ph.removeGroupsChangedFlag();
@ -982,9 +743,9 @@ public class WorldDataHolder {
Map<String, Object> usersMap = new HashMap<String, Object>(); Map<String, Object> usersMap = new HashMap<String, Object>();
root.put("users", usersMap); root.put("users", usersMap);
for (String userKey : ph.users.keySet()) { for (String userKey : ph.getUsers().keySet()) {
User user = ph.users.get(userKey); User user = ph.getUsers().get(userKey);
if ((user.getGroup() == null || user.getGroup().equals(ph.defaultGroup)) && 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; continue;
} }
@ -992,7 +753,7 @@ public class WorldDataHolder {
usersMap.put(user.getName(), aUserMap); usersMap.put(user.getName(), aUserMap);
if (user.getGroup() == null) { if (user.getGroup() == null) {
aUserMap.put("group", ph.defaultGroup.getName()); aUserMap.put("group", ph.getDefaultGroup().getName());
} else { } else {
aUserMap.put("group", user.getGroup().getName()); aUserMap.put("group", user.getGroup().getName());
} }
@ -1027,7 +788,7 @@ public class WorldDataHolder {
} }
// Update the LastModified time. // Update the LastModified time.
ph.usersFile = usersFile; ph.setUsersFile(usersFile);
ph.setTimeStampUsers(usersFile.lastModified()); ph.setTimeStampUsers(usersFile.lastModified());
ph.removeUsersChangedFlag(); ph.removeUsersChangedFlag();
@ -1084,15 +845,22 @@ public class WorldDataHolder {
return permissionsHandler; return permissionsHandler;
} }
/**
* @param haveUsersChanged the haveUsersChanged to set
*/
public void setUsersChanged(boolean haveUsersChanged) {
users.setUsersChanged(haveUsersChanged);
}
/** /**
* *
* @return true if any user data has changed * @return true if any user data has changed
*/ */
public boolean haveUsersChanged() { public boolean haveUsersChanged() {
if (haveUsersChanged) { if (users.HaveUsersChanged()) {
return true; return true;
} }
for (User u : users.values()) { for (User u : users.getUsers().values()) {
if (u.isChanged()) { if (u.isChanged()) {
return true; return true;
} }
@ -1100,15 +868,22 @@ public class WorldDataHolder {
return false; return false;
} }
/**
* @param setGroupsChanged the haveGroupsChanged to set
*/
public void setGroupsChanged(boolean setGroupsChanged) {
groups.setGroupsChanged(setGroupsChanged);
}
/** /**
* *
* @return true if any group data has changed. * @return true if any group data has changed.
*/ */
public boolean haveGroupsChanged() { public boolean haveGroupsChanged() {
if (haveGroupsChanged) { if (groups.HaveGroupsChanged()) {
return true; return true;
} }
for (Group g : groups.values()) { for (Group g : groups.getGroups().values()) {
if (g.isChanged()) { if (g.isChanged()) {
return true; return true;
} }
@ -1120,8 +895,8 @@ public class WorldDataHolder {
* *
*/ */
public void removeUsersChangedFlag() { public void removeUsersChangedFlag() {
haveUsersChanged = false; setUsersChanged(false);
for (User u : users.values()) { for (User u : getUsers().values()) {
u.flagAsSaved(); u.flagAsSaved();
} }
} }
@ -1130,8 +905,8 @@ public class WorldDataHolder {
* *
*/ */
public void removeGroupsChangedFlag() { public void removeGroupsChangedFlag() {
haveGroupsChanged = false; setGroupsChanged(false);
for (Group g : groups.values()) { for (Group g : getGroups().values()) {
g.flagAsSaved(); g.flagAsSaved();
} }
} }
@ -1140,14 +915,28 @@ public class WorldDataHolder {
* @return the usersFile * @return the usersFile
*/ */
public File getUsersFile() { public File getUsersFile() {
return usersFile; return users.getUsersFile();
}
/**
* @param file the usersFile to set
*/
public void setUsersFile(File file) {
users.setUsersFile(file);
} }
/** /**
* @return the groupsFile * @return the groupsFile
*/ */
public File getGroupsFile() { public File getGroupsFile() {
return groupsFile; return groups.getGroupsFile();
}
/**
* @param file the groupsFile to set
*/
public void setGroupsFile(File file) {
groups.setGroupsFile(file);
} }
/** /**
@ -1161,60 +950,85 @@ public class WorldDataHolder {
* Resets Groups. * Resets Groups.
*/ */
public void resetGroups() { public void resetGroups() {
this.defaultGroup = null; //setDefaultGroup(null);
this.groups = new HashMap<String, Group>(); groups.setGroups(new HashMap<String, Group>());
} }
/** /**
* Resets Users * Resets Users
*/ */
public void resetUsers() { public void resetUsers() {
this.users = new HashMap<String, User>(); users.setUsers(new HashMap<String, User>());
} }
/** /**
* @return the groups * @return the groups
*/ */
public Map<String, Group> getGroups() { public Map<String, Group> getGroups() {
return groups; return groups.getGroups();
} }
/** /**
* @return the users * @return the users
*/ */
public Map<String, User> getUsers() { public Map<String, User> getUsers() {
return users.getUsers();
}
/**
* @return the groups
*/
public GroupsDataHolder getGroupsObject() {
return groups;
}
/**
* @param groupsDataHolder the GroupsDataHolder to set
*/
public void setGroupsObject(GroupsDataHolder groupsDataHolder) {
groups = groupsDataHolder;
}
/**
* @return the users
*/
public UsersDataHolder getUsersObject() {
return users; return users;
} }
/**
* @param usersDataHolder the UsersDataHolder to set
*/
public void setUsersObject(UsersDataHolder usersDataHolder) {
users = usersDataHolder;
}
/** /**
* @return the timeStampGroups * @return the timeStampGroups
*/ */
public long getTimeStampGroups() { public long getTimeStampGroups() {
return timeStampGroups; return groups.getTimeStampGroups();
} }
/** /**
* @return the timeStampUsers * @return the timeStampUsers
*/ */
public long getTimeStampUsers() { public long getTimeStampUsers() {
return timeStampUsers; return users.getTimeStampUsers();
} }
/** /**
* @param timeStampGroups the timeStampGroups to set * @param timeStampGroups the timeStampGroups to set
*/ */
protected void setTimeStampGroups(long timeStampGroups) { protected void setTimeStampGroups(long timeStampGroups) {
this.timeStampGroups = timeStampGroups; groups.setTimeStampGroups(timeStampGroups);
} }
/** /**
* @param timeStampUsers the timeStampUsers to set * @param timeStampUsers the timeStampUsers to set
*/ */
protected void setTimeStampUsers(long timeStampUsers) { protected void setTimeStampUsers(long timeStampUsers) {
this.timeStampUsers = timeStampUsers; users.setTimeStampUsers(timeStampUsers);
} }
public void setTimeStamps() { public void setTimeStamps() {
if (groupsFile != null) if (getGroupsFile() != null)
setTimeStampGroups(groupsFile.lastModified()); setTimeStampGroups(getGroupsFile().lastModified());
if (usersFile != null) if (getUsersFile() != null)
setTimeStampUsers(usersFile.lastModified()); setTimeStampUsers(getUsersFile().lastModified());
} }
} }

View File

@ -6,7 +6,6 @@ package org.anjocaido.groupmanager.dataholder.worlds;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@ -22,6 +21,7 @@ import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder;
import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler;
import org.anjocaido.groupmanager.utils.Tasks; import org.anjocaido.groupmanager.utils.Tasks;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.MemorySection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** /**
@ -34,6 +34,7 @@ public class WorldsHolder {
* Map with instances of loaded worlds. * Map with instances of loaded worlds.
*/ */
private Map<String, OverloadedWorldHolder> worldsData = new HashMap<String, OverloadedWorldHolder>(); private Map<String, OverloadedWorldHolder> worldsData = new HashMap<String, OverloadedWorldHolder>();
/** /**
* Map of mirrors: <nonExistingWorldName, existingAndLoadedWorldName> * Map of mirrors: <nonExistingWorldName, existingAndLoadedWorldName>
* The key is the mirror. * The key is the mirror.
@ -41,7 +42,9 @@ public class WorldsHolder {
* *
* Mirror shows the same data of mirrored. * Mirror shows the same data of mirrored.
*/ */
private Map<String, String> mirrors = new HashMap<String, String>(); private Map<String, String> mirrorsGroup = new HashMap<String, String>();
private Map<String, String> mirrorsUser = new HashMap<String, String>();
private OverloadedWorldHolder defaultWorld; private OverloadedWorldHolder defaultWorld;
private String serverDefaultWorldName; private String serverDefaultWorldName;
private GroupManager plugin; private GroupManager plugin;
@ -85,7 +88,7 @@ public class WorldsHolder {
*/ */
for (World world: plugin.getServer().getWorlds()) for (World world: plugin.getServer().getWorlds())
if ((!worldsData.containsKey(world.getName().toLowerCase())) if ((!worldsData.containsKey(world.getName().toLowerCase()))
&& (!mirrors.containsKey(world.getName().toLowerCase()))) && ((!mirrorsGroup.containsKey(world.getName().toLowerCase())) || (!mirrorsUser.containsKey(world.getName().toLowerCase()))))
setupWorldFolder(world.getName()); setupWorldFolder(world.getName());
/* /*
* Loop over all folders within the worlds folder * Loop over all folders within the worlds folder
@ -97,10 +100,11 @@ public class WorldsHolder {
/* /*
* don't load any worlds which are already loaded * don't load any worlds which are already loaded
* or mirrored worlds that don't need data. * or fully mirrored worlds that don't need data.
*/ */
if (!worldsData.containsKey(folder.getName().toLowerCase()) if (!worldsData.containsKey(folder.getName().toLowerCase())
&& !mirrors.containsKey(folder.getName().toLowerCase())) { && ((!mirrorsGroup.containsKey(folder.getName().toLowerCase()))
|| (!mirrorsUser.containsKey(folder.getName().toLowerCase())))) {
loadWorld(folder.getName()); loadWorld(folder.getName());
} }
@ -110,7 +114,8 @@ public class WorldsHolder {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void mirrorSetUp() { public void mirrorSetUp() {
mirrors.clear(); mirrorsGroup.clear();
mirrorsUser.clear();
Map<String, Object> mirrorsMap = plugin.getGMConfig().getMirrorsMap(); Map<String, Object> mirrorsMap = plugin.getGMConfig().getMirrorsMap();
if (mirrorsMap != null) { if (mirrorsMap != null) {
for (String source : mirrorsMap.keySet()) { for (String source : mirrorsMap.keySet()) {
@ -122,16 +127,52 @@ public class WorldsHolder {
if (mirrorsMap.get(source) instanceof ArrayList) { if (mirrorsMap.get(source) instanceof ArrayList) {
ArrayList mirrorList = (ArrayList) mirrorsMap.get(source); ArrayList mirrorList = (ArrayList) mirrorsMap.get(source);
// These worlds fully mirror their parent
for (Object o : mirrorList) { for (Object o : mirrorList) {
try { try {
mirrors.remove(o.toString().toLowerCase()); mirrorsGroup.remove(o.toString().toLowerCase());
mirrorsUser.remove(o.toString().toLowerCase());
} catch (Exception e) { } catch (Exception e) {
} }
mirrors.put(o.toString().toLowerCase(), getWorldData(source).getName()); mirrorsGroup.put(o.toString().toLowerCase(), getWorldData(source).getName());
mirrorsUser.put(o.toString().toLowerCase(), getWorldData(source).getName());
} }
} else if (mirrorsMap.get(source) instanceof Object) { } else if (mirrorsMap.get(source) instanceof MemorySection) {
String aMirror = mirrorsMap.get(source).toString(); MemorySection subSection = (MemorySection) mirrorsMap.get(source);
mirrors.put(aMirror.toLowerCase(), getWorldData(source).getName());
for (String key : subSection.getKeys(true)) {
//System.out.print("Key - " + key);
if (subSection.get(key) instanceof ArrayList) {
ArrayList mirrorList = (ArrayList) subSection.get(key);
// These worlds have defined mirroring
for (Object o : mirrorList) {
String type = o.toString().toLowerCase();
try {
if (type.equals("groups"))
mirrorsGroup.remove(key.toLowerCase());
if (type.equals("users"))
mirrorsUser.remove(key.toLowerCase());
} 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());
}
} else {
throw new IllegalStateException("Unknown mirroring format for " + key);
}
}
} }
} }
} }
@ -141,16 +182,22 @@ public class WorldsHolder {
* *
*/ */
public void reloadAll() { public void reloadAll() {
// Load global groups
GroupManager.getGlobalGroups().load();
ArrayList<WorldDataHolder> alreadyDone = new ArrayList<WorldDataHolder>(); ArrayList<WorldDataHolder> alreadyDone = new ArrayList<WorldDataHolder>();
for (WorldDataHolder w : worldsData.values()) { for (WorldDataHolder w : worldsData.values()) {
if (alreadyDone.contains(w)) { if (alreadyDone.contains(w)) {
continue; continue;
} }
w.reload(); if (!mirrorsGroup.containsKey(w.getName().toLowerCase()))
w.reloadGroups();
if (!mirrorsUser.containsKey(w.getName().toLowerCase()))
w.reloadUsers();
alreadyDone.add(w); alreadyDone.add(w);
} }
// Load global groups
GroupManager.getGlobalGroups().load();
} }
/** /**
@ -158,7 +205,10 @@ public class WorldsHolder {
* @param worldName * @param worldName
*/ */
public void reloadWorld(String worldName) { public void reloadWorld(String worldName) {
getWorldData(worldName).reload(); if (!mirrorsGroup.containsKey(worldName.toLowerCase()))
getWorldData(worldName).reloadGroups();
if (!mirrorsUser.containsKey(worldName.toLowerCase()))
getWorldData(worldName).reloadUsers();
} }
/** /**
@ -194,48 +244,50 @@ public class WorldsHolder {
GroupManager.logger.severe("WHAT HAPPENED?"); GroupManager.logger.severe("WHAT HAPPENED?");
continue; continue;
} }
if (w.haveGroupsChanged()) { if (!mirrorsGroup.containsKey(w.getName().toLowerCase()))
if (overwrite || (!overwrite && (w.getTimeStampGroups() >= w.getGroupsFile().lastModified()))) { if (w.haveGroupsChanged()) {
// Backup Groups file if (overwrite || (!overwrite && (w.getTimeStampGroups() >= w.getGroupsFile().lastModified()))) {
backupFile(w,true); // Backup Groups file
backupFile(w,true);
WorldDataHolder.writeGroups(w, w.getGroupsFile()); WorldDataHolder.writeGroups(w, w.getGroupsFile());
//w.removeGroupsChangedFlag(); //w.removeGroupsChangedFlag();
} else { } else {
// Newer file found. // Newer file found.
GroupManager.logger.log(Level.WARNING, "Newer Groups file found for " + w.getName() + ", but we have local changes!"); 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'"); throw new IllegalStateException("Unable to save unless you issue a '/mansave force'");
} }
} else { } else {
//Check for newer file as no local changes. //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)!"); System.out.print("Newer Groups file found (Loading changes)!");
// Backup Groups file // Backup Groups file
backupFile(w,true); backupFile(w,true);
w.reloadGroups(); w.reloadGroups();
} }
} }
if (w.haveUsersChanged()) { if (!mirrorsUser.containsKey(w.getName().toLowerCase()))
if (overwrite || (!overwrite && (w.getTimeStampUsers() >= w.getUsersFile().lastModified()))) { if (w.haveUsersChanged()) {
// Backup Users file if (overwrite || (!overwrite && (w.getTimeStampUsers() >= w.getUsersFile().lastModified()))) {
backupFile(w,false); // Backup Users file
backupFile(w,false);
WorldDataHolder.writeUsers(w, w.getUsersFile()); WorldDataHolder.writeUsers(w, w.getUsersFile());
//w.removeUsersChangedFlag(); //w.removeUsersChangedFlag();
} else { } else {
// Newer file found. // Newer file found.
GroupManager.logger.log(Level.WARNING, "Newer Users file found for " + w.getName() + ", but we have local changes!"); 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'"); throw new IllegalStateException("Unable to save unless you issue a '/mansave force'");
} }
} else { } else {
//Check for newer file as no local changes. //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)!"); System.out.print("Newer Users file found (Loading changes)!");
// Backup Users file // Backup Users file
backupFile(w,false); backupFile(w,false);
w.reloadUsers(); w.reloadUsers();
} }
} }
alreadyDone.add(w); alreadyDone.add(w);
} }
} }
@ -270,11 +322,12 @@ public class WorldsHolder {
*/ */
public OverloadedWorldHolder getWorldData(String worldName) { public OverloadedWorldHolder getWorldData(String worldName) {
String worldNameLowered = worldName.toLowerCase(); String worldNameLowered = worldName.toLowerCase();
// If a mirror change to the real world to load.
if (mirrorsGroup.containsKey(worldNameLowered)) {
worldNameLowered = mirrorsGroup.get(worldNameLowered);
}
OverloadedWorldHolder data = worldsData.get(worldNameLowered); OverloadedWorldHolder data = worldsData.get(worldNameLowered);
if (mirrors.containsKey(worldNameLowered)) {
String realOne = mirrors.get(worldNameLowered);
data = worldsData.get(realOne.toLowerCase());
}
if (data == null) { if (data == null) {
GroupManager.logger.finest("Requested world " + worldName + " not found or badly mirrored. Returning default world..."); GroupManager.logger.finest("Requested world " + worldName + " not found or badly mirrored. Returning default world...");
data = getDefaultWorld(); data = getDefaultWorld();
@ -283,8 +336,9 @@ public class WorldsHolder {
} }
/** /**
* Do a matching of playerName, if it s found only one player, do * Do a matching of playerName, if its found only one player, do
* getWorldData(player) * getWorldData(player)
*
* @param playerName * @param playerName
* @return null if matching returned no player, or more than one. * @return null if matching returned no player, or more than one.
*/ */
@ -351,60 +405,45 @@ public class WorldsHolder {
} }
public void setupWorldFolder(String worldName) { public void setupWorldFolder(String worldName) {
worldsFolder = new File(plugin.getDataFolder(), "worlds"); worldsFolder = new File(plugin.getDataFolder(), "worlds");
if (!worldsFolder.exists()) { if (!worldsFolder.exists()) {
worldsFolder.mkdirs(); worldsFolder.mkdirs();
} }
File defaultWorldFolder = new File(worldsFolder, worldName); File defaultWorldFolder = new File(worldsFolder, worldName);
if (!defaultWorldFolder.exists()) { if ((!defaultWorldFolder.exists()) && ((!mirrorsGroup.containsKey(worldName.toLowerCase()))) || (!mirrorsUser.containsKey(worldName.toLowerCase()))) {
defaultWorldFolder.mkdirs(); defaultWorldFolder.mkdirs();
} }
if (defaultWorldFolder.exists()) { if (defaultWorldFolder.exists()) {
File groupsFile = new File(defaultWorldFolder, "groups.yml"); if (!mirrorsGroup.containsKey(worldName.toLowerCase())) {
File usersFile = new File(defaultWorldFolder, "users.yml"); File groupsFile = new File(defaultWorldFolder, "groups.yml");
File oldDataFile = new File(plugin.getDataFolder(), "data.yml"); if (!groupsFile.exists() || groupsFile.length() == 0) {
if (!groupsFile.exists() || groupsFile.length() == 0) {
if (oldDataFile.exists()) { InputStream template = plugin.getResourceAsStream("groups.yml");
try { try {
Tasks.copy(oldDataFile, groupsFile); Tasks.copy(template, groupsFile);
} catch (IOException ex) { } catch (IOException ex) {
GroupManager.logger.log(Level.SEVERE, null, ex); GroupManager.logger.log(Level.SEVERE, null, ex);
} }
} else { }
InputStream template = plugin.getResourceAsStream("groups.yml"); }
try {
Tasks.copy(template, groupsFile); if (!mirrorsUser.containsKey(worldName.toLowerCase())) {
} catch (IOException ex) { File usersFile = new File(defaultWorldFolder, "users.yml");
GroupManager.logger.log(Level.SEVERE, null, ex); if (!usersFile.exists() || usersFile.length() == 0) {
}
} InputStream template = plugin.getResourceAsStream("users.yml");
} try {
if (!usersFile.exists() || usersFile.length() == 0) { Tasks.copy(template, usersFile);
if (oldDataFile.exists()) { } catch (IOException ex) {
try { GroupManager.logger.log(Level.SEVERE, null, ex);
Tasks.copy(oldDataFile, usersFile); }
} catch (IOException ex) {
GroupManager.logger.log(Level.SEVERE, null, ex); }
} }
} else { }
InputStream template = plugin.getResourceAsStream("users.yml"); }
try {
Tasks.copy(template, usersFile);
} catch (IOException ex) {
GroupManager.logger.log(Level.SEVERE, null, ex);
}
}
}
try {
if (oldDataFile.exists()) {
oldDataFile.renameTo(new File(plugin.getDataFolder(), "NOT_USED_ANYMORE_data.yml"));
}
} catch (Exception ex) {
}
}
}
/** /**
* Copies the specified world data to another world * Copies the specified world data to another world
@ -449,16 +488,36 @@ public class WorldsHolder {
GroupManager.logger.finest("Trying to load world " + worldName + "..."); GroupManager.logger.finest("Trying to load world " + worldName + "...");
File thisWorldFolder = new File(worldsFolder, worldName); File thisWorldFolder = new File(worldsFolder, worldName);
if (thisWorldFolder.exists() && thisWorldFolder.isDirectory()) { if (thisWorldFolder.exists() && thisWorldFolder.isDirectory()) {
File groupsFile = new File(thisWorldFolder, "groups.yml");
File usersFile = new File(thisWorldFolder, "users.yml"); // Setup file handles, if not mirrored
if (!groupsFile.exists()) { File groupsFile = (mirrorsGroup.containsKey(worldName.toLowerCase()))? null : new File(thisWorldFolder, "groups.yml");
File usersFile = (mirrorsUser.containsKey(worldName.toLowerCase()))? null : new File(thisWorldFolder, "users.yml");
if ((groupsFile != null) && (!groupsFile.exists())) {
throw new IllegalArgumentException("Groups file for world '" + worldName + "' doesnt exist: " + groupsFile.getPath()); throw new IllegalArgumentException("Groups file for world '" + worldName + "' doesnt exist: " + groupsFile.getPath());
} }
if (!usersFile.exists()) { if ((usersFile != null) && (!usersFile.exists())) {
throw new IllegalArgumentException("Users file for world '" + worldName + "' doesnt exist: " + usersFile.getPath()); throw new IllegalArgumentException("Users file for world '" + worldName + "' doesnt exist: " + usersFile.getPath());
} }
try {
OverloadedWorldHolder thisWorldData = new OverloadedWorldHolder(WorldDataHolder.load(worldName, groupsFile, usersFile)); WorldDataHolder tempHolder = new WorldDataHolder(worldName);
// Map the group object for any mirror
if (mirrorsGroup.containsKey(worldName.toLowerCase()))
tempHolder.setGroupsObject(this.getWorldData(mirrorsGroup.get(worldName.toLowerCase())).getGroupsObject());
else
tempHolder.loadGroups(groupsFile);
// Map the user object for any mirror
if (mirrorsUser.containsKey(worldName.toLowerCase()))
tempHolder.setUsersObject(this.getWorldData(mirrorsUser.get(worldName.toLowerCase())).getUsersObject());
else
tempHolder.loadUsers(usersFile);
OverloadedWorldHolder thisWorldData = new OverloadedWorldHolder(tempHolder);
// null the object so we don't keep file handles open where we shouldn't
tempHolder = null;
// Set the file TimeStamps as it will be default from the initial load. // Set the file TimeStamps as it will be default from the initial load.
thisWorldData.setTimeStamps(); thisWorldData.setTimeStamps();
@ -468,13 +527,7 @@ public class WorldsHolder {
worldsData.put(worldName.toLowerCase(), thisWorldData); worldsData.put(worldName.toLowerCase(), thisWorldData);
return; return;
} }
} catch (FileNotFoundException ex) {
GroupManager.logger.log(Level.SEVERE, null, ex);
return;
} catch (IOException ex) {
GroupManager.logger.log(Level.SEVERE, null, ex);
return;
}
//GroupManager.logger.severe("Failed to load world " + worldName + "..."); //GroupManager.logger.severe("Failed to load world " + worldName + "...");
} }
} }
@ -488,7 +541,7 @@ public class WorldsHolder {
* @return true if world is loaded or mirrored. false if not listed * @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()) || mirrors.containsKey(worldName.toLowerCase())) { if (worldsData.containsKey(worldName.toLowerCase()) || mirrorsGroup.containsKey(worldName.toLowerCase())) {
return true; return true;
} }
return false; return false;

View File

@ -287,7 +287,7 @@ public class BukkitPermissions {
* List all effective permissions for this player. * List all effective permissions for this player.
* *
* @param player * @param player
* @return * @return List<String> of permissions
*/ */
public List<String> listPerms(Player player) { public List<String> listPerms(Player player) {
List<String> perms = new ArrayList<String>(); List<String> perms = new ArrayList<String>();