ChildConfig.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.gmail.nossr50.config;
  2. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  3. import com.gmail.nossr50.skills.child.FamilyTree;
  4. import com.gmail.nossr50.util.StringUtils;
  5. import org.bukkit.configuration.file.YamlConfiguration;
  6. import java.util.EnumSet;
  7. public class ChildConfig extends ConfigCollection {
  8. public ChildConfig() {
  9. super("child.yml");
  10. }
  11. @Override
  12. protected void register() {
  13. config.setDefaults(YamlConfiguration.loadConfiguration(plugin.getResourceAsReader("child.yml")));
  14. FamilyTree.clearRegistrations(); // when reloading, need to clear statics
  15. for (PrimarySkillType skill : PrimarySkillType.CHILD_SKILLS) {
  16. plugin.debug("Finding parents of " + skill.name());
  17. EnumSet<PrimarySkillType> parentSkills = EnumSet.noneOf(PrimarySkillType.class);
  18. boolean useDefaults = false; // If we had an error we back out and use defaults
  19. for (String name : config.getStringList(StringUtils.getCapitalized(skill.name()))) {
  20. try {
  21. PrimarySkillType parentSkill = PrimarySkillType.valueOf(name.toUpperCase());
  22. FamilyTree.enforceNotChildSkill(parentSkill);
  23. parentSkills.add(parentSkill);
  24. } catch (IllegalArgumentException ex) {
  25. plugin.getLogger().warning(name + " is not a valid skill type, or is a child skill!");
  26. useDefaults = true;
  27. break;
  28. }
  29. }
  30. if (useDefaults) {
  31. parentSkills.clear();
  32. for (String name : config.getDefaults().getStringList(StringUtils.getCapitalized(skill.name()))) {
  33. /* We do less checks in here because it's from inside our jar.
  34. * If they're dedicated enough to have modified it, they can have the errors it may produce.
  35. * 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.
  36. */
  37. parentSkills.add(PrimarySkillType.valueOf(name.toUpperCase()));
  38. }
  39. }
  40. // Register them
  41. for (PrimarySkillType parentSkill : parentSkills) {
  42. plugin.debug("Registering " + parentSkill.name() + " as parent of " + skill.name());
  43. FamilyTree.registerParent(skill, parentSkill);
  44. }
  45. }
  46. FamilyTree.closeRegistration();
  47. }
  48. }