Add new child.yml config for picking parents of child skills

This commit is contained in:
NuclearW
2013-02-25 16:43:37 -05:00
parent 3607d0b9a1
commit 5026bdcbd4
7 changed files with 138 additions and 14 deletions

View File

@ -0,0 +1,60 @@
package com.gmail.nossr50.skills.child;
import java.util.EnumSet;
import java.util.List;
import org.bukkit.configuration.file.YamlConfiguration;
import com.gmail.nossr50.config.AutoUpdateConfigLoader;
import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.StringUtils;
public class ChildConfig extends AutoUpdateConfigLoader {
public ChildConfig() {
super("child.yml");
loadKeys();
}
@Override
protected void loadKeys() {
config.setDefaults(YamlConfiguration.loadConfiguration(plugin.getResource("child.yml")));
for (SkillType skill : SkillType.values()) {
if (skill.isChildSkill()) {
plugin.debug("Finding parents of " + skill.name());
List<String> parentNames = config.getStringList(StringUtils.getCapitalized(skill.name()));
EnumSet<SkillType> parentSkills = EnumSet.noneOf(SkillType.class);
boolean useDefaults = false; // If we had an error we back out and use defaults
for (String name : parentNames) {
try {
SkillType parentSkill = Enum.valueOf(SkillType.class, name.toUpperCase());
FamilyTree.enforceNotChildSkill(parentSkill);
parentSkills.add(parentSkill);
} catch (IllegalArgumentException ex) {
plugin.getLogger().warning(name + " is not a valid skill type, or is a child skill!");
useDefaults = true;
break;
}
}
if (useDefaults) {
parentSkills.clear();
for (String name : config.getDefaults().getStringList(StringUtils.getCapitalized(skill.name()))) {
/* We do less checks in here because it's from inside our jar.
* If they're dedicated enough to have modified it, they can have the errors it may produce.
* Alternatively, this can be used to allow child skills to be parent skills, provided there are no circular dependencies this is an advanced sort of configuration.
*/
parentSkills.add(SkillType.valueOf(name.toUpperCase()));
}
}
// Register them
for (SkillType parentSkill : parentSkills) {
plugin.debug("Registering " + parentSkill.name() + " as parent of " + skill.name());
FamilyTree.registerParent(skill, parentSkill);
}
}
}
FamilyTree.closeRegistration();
}
}

View File

@ -0,0 +1,49 @@
package com.gmail.nossr50.skills.child;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Set;
import com.gmail.nossr50.skills.utilities.SkillType;
public class FamilyTree {
private static HashMap<SkillType, Set<SkillType>> tree = new HashMap<SkillType, Set<SkillType>>();
public static Set<SkillType> getParents(SkillType childSkill) {
enforceChildSkill(childSkill);
// We do not check if we have the child skill in question, as not having it would mean we did something wrong, and an NPE is desired.
return tree.get(childSkill);
}
protected static void registerParent(SkillType childSkill, SkillType parentSkill) {
enforceChildSkill(childSkill);
enforceNotChildSkill(parentSkill);
if (!tree.containsKey(childSkill)) {
tree.put(childSkill, EnumSet.noneOf(SkillType.class));
}
tree.get(childSkill).add(parentSkill);
}
protected static void closeRegistration() {
for (SkillType childSkill : tree.keySet()) {
Set<SkillType> immutableSet = Collections.unmodifiableSet(tree.get(childSkill));
tree.put(childSkill, immutableSet);
}
}
protected static void enforceChildSkill(SkillType skill) {
if (!skill.isChildSkill()) {
throw new IllegalArgumentException(skill.name() + " is not a child skill!");
}
}
protected static void enforceNotChildSkill(SkillType skill) {
if (skill.isChildSkill()) {
throw new IllegalArgumentException(skill.name() + " is a child skill!");
}
}
}