Catch errors caused by bad indentation in yml's.

This commit is contained in:
ElgarL 2012-03-29 14:02:53 +01:00
parent 1ef8ab70d1
commit 9dbeb1407c
3 changed files with 219 additions and 206 deletions

View file

@ -156,3 +156,4 @@ v 1.9:
- Auto rename all case sensitive world folders to lower case (if possible). - Auto rename all case sensitive world folders to lower case (if possible).
- Update GlobalGroups.yml for new/changed Towny permission nodes. - Update GlobalGroups.yml for new/changed Towny permission nodes.
- Stop attempting to push empty permissions when players edit the yml's incorrectly. - Stop attempting to push empty permissions when players edit the yml's incorrectly.
- Catch errors caused by bad indentation in yml's.

View file

@ -116,42 +116,46 @@ public class GlobalGroups {
// Load each groups permissions list. // Load each groups permissions list.
if (allGroups != null) { if (allGroups != null) {
for (String groupName : allGroups.keySet()) { try {
Group newGroup = new Group(groupName.toLowerCase()); for (String groupName : allGroups.keySet()) {
Object element; Group newGroup = new Group(groupName.toLowerCase());
Object element;
// Permission nodes // Permission nodes
element = GGroups.get("groups." + groupName + ".permissions"); element = GGroups.get("groups." + groupName + ".permissions");
if (element != null) if (element != null)
if (element instanceof List) { if (element instanceof List) {
try { try {
for (String node : (List<String>) element) { for (String node : (List<String>) element) {
newGroup.addPermission(node); newGroup.addPermission(node);
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Invalid permission node for global group: " + groupName);
} }
} catch (ClassCastException e) { } else if (element instanceof String) {
throw new IllegalArgumentException("Invalid permission node for global group: " + groupName); newGroup.addPermission((String) element);
} } else
} else if (element instanceof String) { throw new IllegalArgumentException("Unknown type of permission node for global group: " + groupName);
newGroup.addPermission((String) element);
} else
throw new IllegalArgumentException("Unknown type of permission node for global group: " + groupName);
// Info nodes // Info nodes
element = GGroups.get("groups." + groupName + ".info"); element = GGroups.get("groups." + groupName + ".info");
if (element != null) if (element != null)
if (element instanceof MemorySection) { if (element instanceof MemorySection) {
Map<String, Object> vars = new HashMap<String, Object>(); Map<String, Object> vars = new HashMap<String, Object>();
for (String key : ((MemorySection) element).getKeys(false)) { for (String key : ((MemorySection) element).getKeys(false)) {
vars.put(key, ((MemorySection) element).get(key)); vars.put(key, ((MemorySection) element).get(key));
} }
newGroup.setVariables(vars); newGroup.setVariables(vars);
} else } else
throw new IllegalArgumentException("Unknown type of info node for global group: " + groupName); throw new IllegalArgumentException("Unknown type of info node for global group: " + groupName);
// Push a new group // Push a new group
addGroup(newGroup); addGroup(newGroup);
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type, or bad indentation in GlobalGroups! ");
} }
} }

View file

@ -460,93 +460,97 @@ public class WorldDataHolder {
Map<String, List<String>> inheritance = new HashMap<String, List<String>>(); Map<String, List<String>> inheritance = new HashMap<String, List<String>>();
try { try {
Map<String, Object> allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups"); Map<String, Object> allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups");
for (String groupKey : allGroupsNode.keySet()) { try {
Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey); for (String groupKey : allGroupsNode.keySet()) {
Group thisGrp = ph.createGroup(groupKey); Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey);
if (thisGrp == null) { Group thisGrp = ph.createGroup(groupKey);
throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath()); if (thisGrp == null) {
} throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath());
if (thisGroupNode.get("default") == null) { }
thisGroupNode.put("default", false); if (thisGroupNode.get("default") == null) {
} thisGroupNode.put("default", false);
if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) { }
if (ph.getDefaultGroup() != null) { if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) {
GroupManager.logger.warning("The group " + thisGrp.getName() + " is claiming to be default where" + ph.getDefaultGroup().getName() + " already was."); if (ph.getDefaultGroup() != null) {
GroupManager.logger.warning("Overriding first request for file: " + groupsFile.getPath()); GroupManager.logger.warning("The group " + thisGrp.getName() + " is claiming to be default where" + ph.getDefaultGroup().getName() + " already was.");
} GroupManager.logger.warning("Overriding first request for file: " + groupsFile.getPath());
ph.setDefaultGroup(thisGrp); }
} ph.setDefaultGroup(thisGrp);
//PERMISSIONS NODE
try {
if (thisGroupNode.get("permissions") == null) {
thisGroupNode.put("permissions", new ArrayList<String>());
} else {
if (thisGroupNode.get("permissions") instanceof List) {
for (Object o : ((List) thisGroupNode.get("permissions"))) {
try {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
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) {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
thisGrp.addPermission((String) thisGroupNode.get("permissions"));
} else {
throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
thisGrp.sortPermissions();
} }
} catch (Exception e) {
throw new IllegalArgumentException("Invalid formatting found in permissions section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
//INFO NODE //PERMISSIONS NODE
try { try {
if (thisGroupNode.get("info") instanceof Map) { if (thisGroupNode.get("permissions") == null) {
Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info"); thisGroupNode.put("permissions", new ArrayList<String>());
if (infoNode != null) { } else {
thisGrp.setVariables(infoNode); if (thisGroupNode.get("permissions") instanceof List) {
for (Object o : ((List) thisGroupNode.get("permissions"))) {
try {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
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) {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
thisGrp.addPermission((String) thisGroupNode.get("permissions"));
} else {
throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
thisGrp.sortPermissions();
} }
} else } catch (Exception e) {
throw new IllegalArgumentException("Unknown entry found in Info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); throw new IllegalArgumentException("Invalid formatting found in permissions section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} catch (Exception e1) { }
throw new IllegalArgumentException("Invalid formatting found in info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
//END INFO NODE //INFO NODE
try {
if (thisGroupNode.get("info") instanceof Map) {
Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info");
if (infoNode != null) {
thisGrp.setVariables(infoNode);
}
} else
throw new IllegalArgumentException("Unknown entry found in Info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} catch (Exception e1) {
throw new IllegalArgumentException("Invalid formatting found in info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
try { //END INFO NODE
if (thisGroupNode.get("inheritance") == null || thisGroupNode.get("inheritance") instanceof List) {
Object inheritNode = thisGroupNode.get("inheritance");
if (inheritNode == null) {
thisGroupNode.put("inheritance", new ArrayList<String>());
} else if (inheritNode instanceof List) {
List<String> groupsInh = (List<String>) inheritNode;
for (String grp : groupsInh) {
if (inheritance.get(groupKey) == null) {
List<String> thisInherits = new ArrayList<String>();
inheritance.put(groupKey, thisInherits);
}
inheritance.get(groupKey).add(grp);
} try {
} if (thisGroupNode.get("inheritance") == null || thisGroupNode.get("inheritance") instanceof List) {
}else Object inheritNode = thisGroupNode.get("inheritance");
throw new IllegalArgumentException("Unknown entry found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); if (inheritNode == null) {
} catch (Exception e2) { thisGroupNode.put("inheritance", new ArrayList<String>());
throw new IllegalArgumentException("Invalid formatting found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); } else if (inheritNode instanceof List) {
} List<String> groupsInh = (List<String>) inheritNode;
} for (String grp : groupsInh) {
if (inheritance.get(groupKey) == null) {
List<String> thisInherits = new ArrayList<String>();
inheritance.put(groupKey, thisInherits);
}
inheritance.get(groupKey).add(grp);
}
}
}else
throw new IllegalArgumentException("Unknown entry found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} catch (Exception e2) {
throw new IllegalArgumentException("Invalid formatting found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type, or bad indentation in file: " + groupsFile.getPath());
}
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details."); throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details.");
@ -609,92 +613,96 @@ public class WorldDataHolder {
// Load users if the file is NOT empty // Load users if the file is NOT empty
if (allUsersNode != null) if (allUsersNode != null)
for (String usersKey : allUsersNode.keySet()) { try {
Map<String, Object> thisUserNode = null; for (String usersKey : allUsersNode.keySet()) {
try { Map<String, Object> thisUserNode = null;
thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey); try {
} catch (Exception ex) { thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
throw new IllegalArgumentException("Bad format found in file: " + usersFile.getPath()); } catch (Exception ex) {
} throw new IllegalArgumentException("Bad format found in file: " + usersFile.getPath());
User thisUser = ph.createUser(usersKey); }
if (thisUser == null) { User thisUser = ph.createUser(usersKey);
throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath()); if (thisUser == null) {
} throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath());
if (thisUserNode.get("permissions") == null) { }
thisUserNode.put("permissions", new ArrayList<String>()); if (thisUserNode.get("permissions") == null) {
} else { thisUserNode.put("permissions", new ArrayList<String>());
if (thisUserNode.get("permissions") instanceof List) { } else {
for (Object o : ((List) thisUserNode.get("permissions"))) { if (thisUserNode.get("permissions") instanceof List) {
/* for (Object o : ((List) thisUserNode.get("permissions"))) {
* Only add this permission if it's not empty /*
*/ * Only add this permission if it's not empty
if (!o.toString().isEmpty()) */
thisUser.addPermission(o.toString()); if (!o.toString().isEmpty())
} thisUser.addPermission(o.toString());
} else if (thisUserNode.get("permissions") instanceof String) { }
try { } else if (thisUserNode.get("permissions") instanceof String) {
/* try {
* Only add this permission if it's not empty /*
*/ * Only add this permission if it's not empty
if (!thisUserNode.get("permissions").toString().isEmpty()) */
thisUser.addPermission(thisUserNode.get("permissions").toString()); if (!thisUserNode.get("permissions").toString().isEmpty())
} catch (NullPointerException e) { thisUser.addPermission(thisUserNode.get("permissions").toString());
// Ignore this entry as it's null. } catch (NullPointerException e) {
//throw new IllegalArgumentException("Invalid permission node for user: " + thisUser.getName() + " in file: " + UserFile.getPath()); // Ignore this entry as it's null.
} //throw new IllegalArgumentException("Invalid permission node for user: " + thisUser.getName() + " in file: " + UserFile.getPath());
}
}
thisUser.sortPermissions();
} }
thisUser.sortPermissions();
}
//SUBGROUPS LOADING //SUBGROUPS LOADING
if (thisUserNode.get("subgroups") == null) { if (thisUserNode.get("subgroups") == null) {
thisUserNode.put("subgroups", new ArrayList<String>()); thisUserNode.put("subgroups", new ArrayList<String>());
} }
if (thisUserNode.get("subgroups") instanceof List) { if (thisUserNode.get("subgroups") instanceof List) {
for (Object o : ((List) thisUserNode.get("subgroups"))) { for (Object o : ((List) thisUserNode.get("subgroups"))) {
Group subGrp = ph.getGroup(o.toString()); Group subGrp = ph.getGroup(o.toString());
if (subGrp != null) { if (subGrp != null) {
thisUser.addSubGroup(subGrp); thisUser.addSubGroup(subGrp);
} else { } else {
GroupManager.logger.warning("Subgroup " + o.toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath()); GroupManager.logger.warning("Subgroup " + o.toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
} }
} }
} else if (thisUserNode.get("subgroups") instanceof String) { } else if (thisUserNode.get("subgroups") instanceof String) {
Group subGrp = ph.getGroup(thisUserNode.get("subgroups").toString()); Group subGrp = ph.getGroup(thisUserNode.get("subgroups").toString());
if (subGrp != null) { if (subGrp != null) {
thisUser.addSubGroup(subGrp); thisUser.addSubGroup(subGrp);
} else { } else {
GroupManager.logger.warning("Subgroup " + thisUserNode.get("subgroups").toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath()); GroupManager.logger.warning("Subgroup " + thisUserNode.get("subgroups").toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
} }
} }
//USER INFO NODE //USER INFO NODE
//INFO NODE //INFO NODE
if (thisUserNode.get("info") instanceof Map) { if (thisUserNode.get("info") instanceof Map) {
Map<String, Object> infoNode = (Map<String, Object>) thisUserNode.get("info"); Map<String, Object> infoNode = (Map<String, Object>) thisUserNode.get("info");
if (infoNode != null) { if (infoNode != null) {
thisUser.setVariables(infoNode); thisUser.setVariables(infoNode);
} }
} else if (thisUserNode.get("info") != null) } else if (thisUserNode.get("info") != null)
throw new IllegalArgumentException("Unknown entry found in Info section for user: " + thisUser.getName() + " in file: " + usersFile.getPath()); throw new IllegalArgumentException("Unknown entry found in Info section for user: " + thisUser.getName() + " in file: " + usersFile.getPath());
//END INFO NODE //END INFO NODE
if (thisUserNode.get("group") != null) { if (thisUserNode.get("group") != null) {
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.getDefaultGroup(); 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.getDefaultGroup()); thisUser.setGroup(ph.getDefaultGroup());
} }
} }
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type, or bad indentation in file: " + usersFile.getPath());
}
ph.removeUsersChangedFlag(); ph.removeUsersChangedFlag();
// Update the LastModified time. // Update the LastModified time.