From 5026bdcbd49a235bdde28dd0aeaa01c646356fd2 Mon Sep 17 00:00:00 2001 From: NuclearW Date: Mon, 25 Feb 2013 16:43:37 -0500 Subject: [PATCH] Add new child.yml config for picking parents of child skills --- Changelog.txt | 1 + .../nossr50/datatypes/PlayerProfile.java | 13 ++-- src/main/java/com/gmail/nossr50/mcMMO.java | 4 +- .../nossr50/skills/child/ChildConfig.java | 60 +++++++++++++++++++ .../nossr50/skills/child/FamilyTree.java | 49 +++++++++++++++ .../smelting/SmeltResourceEventHandler.java | 14 +++-- src/main/resources/child.yml | 11 ++++ 7 files changed, 138 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/skills/child/ChildConfig.java create mode 100644 src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java create mode 100644 src/main/resources/child.yml diff --git a/Changelog.txt b/Changelog.txt index 75bb0f930..a1b4207c3 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -40,6 +40,7 @@ Version 1.4.00-dev + Added config option to make .new config files instead over writing over old ones when updating + Added "Holy Hound" ability to Taming + Added "Shroom Thumb" ability to Herbalism + + Added child.yml config file to choose parents for child skills = Fixed Green Thumb on wheat not working properly at rank 4 = Fixed Green Thumb and Green Terra consuming twice the amount of seed needed = Fixed Green Terra not also checking Green Thumb permissions diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java index b2c62bba0..6243d03bd 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java @@ -6,10 +6,12 @@ import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.HashMap; +import java.util.Set; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.database.Database; +import com.gmail.nossr50.skills.child.FamilyTree; import com.gmail.nossr50.skills.utilities.AbilityType; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.skills.utilities.ToolType; @@ -902,13 +904,12 @@ public class PlayerProfile { } public int getChildSkillLevel(SkillType skillType) { - switch (skillType) { - case SMELTING: - return ((getSkillLevel(SkillType.MINING) / 4) + (getSkillLevel(SkillType.REPAIR) / 4)); //TODO: Make this cleaner somehow - - default: - return 0; + Set parents = FamilyTree.getParents(skillType); + int sum = 0; + for (SkillType parent : parents) { + sum += Math.min(getSkillLevel(parent), 1000); } + return sum / parents.size(); } public int getSkillXpLevel(SkillType skillType) { diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 5fb634214..74972690f 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -44,6 +44,7 @@ import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.runnables.PartiesLoader; import com.gmail.nossr50.party.runnables.PartyAutoKick; import com.gmail.nossr50.runnables.SaveTimer; +import com.gmail.nossr50.skills.child.ChildConfig; import com.gmail.nossr50.skills.repair.RepairManager; import com.gmail.nossr50.skills.repair.RepairManagerFactory; import com.gmail.nossr50.skills.repair.Repairable; @@ -266,8 +267,7 @@ public class mcMMO extends JavaPlugin { TreasuresConfig.getInstance(); HiddenConfig.getInstance(); AdvancedConfig.getInstance(); - - + new ChildConfig(); List repairables = new ArrayList(); diff --git a/src/main/java/com/gmail/nossr50/skills/child/ChildConfig.java b/src/main/java/com/gmail/nossr50/skills/child/ChildConfig.java new file mode 100644 index 000000000..9419946c4 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/child/ChildConfig.java @@ -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 parentNames = config.getStringList(StringUtils.getCapitalized(skill.name())); + EnumSet 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(); + } +} diff --git a/src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java b/src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java new file mode 100644 index 000000000..382619507 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java @@ -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> tree = new HashMap>(); + + public static Set 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 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!"); + } + } +} diff --git a/src/main/java/com/gmail/nossr50/skills/smelting/SmeltResourceEventHandler.java b/src/main/java/com/gmail/nossr50/skills/smelting/SmeltResourceEventHandler.java index 835b3ba0c..c8e6947fb 100644 --- a/src/main/java/com/gmail/nossr50/skills/smelting/SmeltResourceEventHandler.java +++ b/src/main/java/com/gmail/nossr50/skills/smelting/SmeltResourceEventHandler.java @@ -1,5 +1,7 @@ package com.gmail.nossr50.skills.smelting; +import java.util.Set; + import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.FurnaceSmeltEvent; @@ -7,6 +9,7 @@ import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.skills.child.FamilyTree; import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.Permissions; @@ -37,12 +40,11 @@ public class SmeltResourceEventHandler { McMMOPlayer mcMMOPlayer = manager.getMcMMOPlayer(); Player player = mcMMOPlayer.getPlayer(); - if (Permissions.skillEnabled(player, SkillType.MINING)) { - mcMMOPlayer.beginXpGain(SkillType.MINING, xp / 2); - } - - if (Permissions.skillEnabled(player, SkillType.REPAIR)) { - mcMMOPlayer.beginXpGain(SkillType.REPAIR, xp / 2); + Set parentSkills = FamilyTree.getParents(SkillType.SMELTING); + for (SkillType parentSkill : parentSkills) { + if (Permissions.skillEnabled(player, parentSkill)) { + mcMMOPlayer.beginXpGain(parentSkill, xp / parentSkills.size()); + } } } diff --git a/src/main/resources/child.yml b/src/main/resources/child.yml new file mode 100644 index 000000000..da5380a9c --- /dev/null +++ b/src/main/resources/child.yml @@ -0,0 +1,11 @@ +# +# mcMMO child skill configuration +# You do not need to modify this file except to change parents of child skills +# +# If you wish a child skill to be the parent of another child skill, you must also make your changes to the child.yml within the jar +# WARNING: THIS IS NOT SUPPORTED, IF YOU DO SO YOU ARE RESPONSIBLE FOR THE ISSUES THAT MAY ARISE. That said, watch out for circular dependencies, those are bad. +# +##### +Smelting: + - Mining + - Repair \ No newline at end of file