From f08456b7898c93fa69a80ced35861a3a29b41c1c Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 18 Feb 2019 09:04:20 -0800 Subject: [PATCH] child.yml was a mistake --- .../com/gmail/nossr50/config/ChildConfig.java | 59 ----------------- .../java/com/gmail/nossr50/config/Config.java | 41 ++++++++---- .../gmail/nossr50/config/ConfigManager.java | 10 ++- .../nossr50/skills/child/ChildConfig.java | 60 ----------------- .../nossr50/skills/child/FamilyTree.java | 66 +++++++------------ src/main/resources/child.yml | 15 ----- 6 files changed, 56 insertions(+), 195 deletions(-) delete mode 100644 src/main/java/com/gmail/nossr50/config/ChildConfig.java delete mode 100644 src/main/java/com/gmail/nossr50/skills/child/ChildConfig.java delete mode 100644 src/main/resources/child.yml diff --git a/src/main/java/com/gmail/nossr50/config/ChildConfig.java b/src/main/java/com/gmail/nossr50/config/ChildConfig.java deleted file mode 100644 index 2220bfe69..000000000 --- a/src/main/java/com/gmail/nossr50/config/ChildConfig.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.gmail.nossr50.config; - -import com.gmail.nossr50.datatypes.skills.PrimarySkillType; -import com.gmail.nossr50.skills.child.FamilyTree; -import com.gmail.nossr50.util.StringUtils; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.util.EnumSet; - -public class ChildConfig extends ConfigCollection { - public ChildConfig() { - super("child.yml"); - } - - @Override - protected void register() { - config.setDefaults(YamlConfiguration.loadConfiguration(plugin.getResourceAsReader("child.yml"))); - - FamilyTree.clearRegistrations(); // when reloading, need to clear statics - - for (PrimarySkillType skill : PrimarySkillType.CHILD_SKILLS) { - plugin.debug("Finding parents of " + skill.name()); - - EnumSet parentSkills = EnumSet.noneOf(PrimarySkillType.class); - boolean useDefaults = false; // If we had an error we back out and use defaults - - for (String name : config.getStringList(StringUtils.getCapitalized(skill.name()))) { - try { - PrimarySkillType parentSkill = PrimarySkillType.valueOf(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(PrimarySkillType.valueOf(name.toUpperCase())); - } - } - - // Register them - for (PrimarySkillType 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/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index ac9ec00d9..eb1ef9402 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.config; +import com.gmail.nossr50.mcMMO; import com.google.common.io.Files; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.commented.CommentedConfigurationNode; @@ -14,11 +15,12 @@ import java.io.InputStream; /** * Handles loading and cacheing configuration settings from a configurable compatible config file */ -//@ConfigSerializable public abstract class Config implements VersionedConfig, Unload { /* SETTINGS */ - private boolean mergeNewKeys; + private boolean mergeNewKeys; //Whether or not to merge keys found in the default config + private boolean removeOldKeys; //Whether or not to remove unused keys form the config + private boolean copyDefaults; //Whether or not to copy the default config when first creating the file /* PATH VARS */ @@ -160,8 +162,9 @@ public abstract class Config implements VersionedConfig, Unload { /* * Gen a Default config from inside the JAR */ - McmmoCore.getLogger().info("Preparing to copy internal resource file (in JAR) - "+FILE_RELATIVE_PATH); - InputStream inputStream = McmmoCore.getResource(FILE_RELATIVE_PATH); + mcMMO.p.getLogger().info("Preparing to copy internal resource file (in JAR) - "+FILE_RELATIVE_PATH); + //InputStream inputStream = McmmoCore.getResource(FILE_RELATIVE_PATH); + InputStream inputStream = mcMMO.p.getResource(FILE_RELATIVE_PATH); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); @@ -172,7 +175,7 @@ public abstract class Config implements VersionedConfig, Unload { //Wipe old default file on disk if (targetFile.exists() && deleteOld) { - McmmoCore.getLogger().info("Updating file " + relativeOutputPath); + mcMMO.p.getLogger().info("Updating file " + relativeOutputPath); targetFile.delete(); //Necessary? } @@ -183,7 +186,7 @@ public abstract class Config implements VersionedConfig, Unload { } Files.write(buffer, targetFile); - McmmoCore.getLogger().info("Created config file - " + relativeOutputPath); + mcMMO.p.getLogger().info("Created config file - " + relativeOutputPath); inputStream.close(); //Close the input stream @@ -225,10 +228,10 @@ public abstract class Config implements VersionedConfig, Unload { * MainConfig will have any missing nodes inserted with their default value */ public void readConfig() { - McmmoCore.getLogger().info("Attempting to read " + FILE_RELATIVE_PATH + "."); + mcMMO.p.getLogger().info("Attempting to read " + FILE_RELATIVE_PATH + "."); int version = this.userRootNode.getNode("ConfigVersion").getInt(); - McmmoCore.getLogger().info(FILE_RELATIVE_PATH + " version is " + version); + mcMMO.p.getLogger().info(FILE_RELATIVE_PATH + " version is " + version); //Update our config updateConfig(); @@ -239,8 +242,8 @@ public abstract class Config implements VersionedConfig, Unload { */ private void updateConfig() { - McmmoCore.getLogger().info(defaultRootNode.getChildrenMap().size() +" items in default children map"); - McmmoCore.getLogger().info(userRootNode.getChildrenMap().size() +" items in default root map"); + mcMMO.p.getLogger().info(defaultRootNode.getChildrenMap().size() +" items in default children map"); + mcMMO.p.getLogger().info(userRootNode.getChildrenMap().size() +" items in default root map"); // Merge Values from default if(mergeNewKeys) @@ -259,13 +262,27 @@ public abstract class Config implements VersionedConfig, Unload { } } + /** + * Finds any keys in the users config that are not present in the default config and removes them + */ + private void removeOldKeys() + { + if(!removeOldKeys) + return; + + for(ConfigurationNode configurationNode : defaultRootNode.getChildrenList()) + { + + } + } + /** * Saves the current state information of the config to the users copy (which they may edit) * @throws IOException */ private void saveUserCopy() throws IOException { - McmmoCore.getLogger().info("Saving new node"); + mcMMO.p.getLogger().info("Saving new node"); userCopyLoader.save(userRootNode); } @@ -275,7 +292,7 @@ public abstract class Config implements VersionedConfig, Unload { private void updateConfigVersion() { // Set a version for our config this.userRootNode.getNode("ConfigVersion").setValue(getConfigVersion()); - McmmoCore.getLogger().info("Updated config to ["+getConfigVersion()+"] - " + FILE_RELATIVE_PATH); + mcMMO.p.getLogger().info("Updated config to ["+getConfigVersion()+"] - " + FILE_RELATIVE_PATH); } /** diff --git a/src/main/java/com/gmail/nossr50/config/ConfigManager.java b/src/main/java/com/gmail/nossr50/config/ConfigManager.java index d0a1a7b62..a806b272b 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigManager.java @@ -1,15 +1,9 @@ package com.gmail.nossr50.config; -import com.gmail.nossr50.config.*; import com.gmail.nossr50.config.collectionconfigs.CollectionClassType; import com.gmail.nossr50.config.collectionconfigs.MultiConfigContainer; -import com.gmail.nossr50.config.mods.ArmorConfigManager; -import com.gmail.nossr50.config.mods.BlockConfigManager; -import com.gmail.nossr50.config.mods.EntityConfigManager; -import com.gmail.nossr50.config.mods.ToolConfigManager; import com.gmail.nossr50.config.skills.alchemy.PotionConfig; import com.gmail.nossr50.config.treasure.TreasureConfig; -import com.gmail.nossr50.skills.child.ChildConfig; import com.gmail.nossr50.skills.repair.repairables.Repairable; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager; import com.gmail.nossr50.skills.salvage.salvageables.Salvageable; @@ -56,6 +50,10 @@ public final class ConfigManager { private SoundConfig soundConfig; private RankConfig rankConfig; + /* CONFIG ERRORS */ + + private ArrayList configErrors; //Collect errors to whine about to server admins + public ConfigManager() { unloadables = 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 deleted file mode 100644 index e350e798d..000000000 --- a/src/main/java/com/gmail/nossr50/skills/child/ChildConfig.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.gmail.nossr50.skills.child; - -import com.gmail.nossr50.datatypes.skills.PrimarySkillType; -import com.gmail.nossr50.util.StringUtils; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.util.EnumSet; - -public class ChildConfig extends AutoUpdateConfigLoader { - public ChildConfig() { - super("child.yml"); - loadKeys(); - } - - @Override - protected void loadKeys() { - config.setDefaults(YamlConfiguration.loadConfiguration(plugin.getResourceAsReader("child.yml"))); - - FamilyTree.clearRegistrations(); // when reloading, need to clear statics - - for (PrimarySkillType skill : PrimarySkillType.CHILD_SKILLS) { - plugin.debug("Finding parents of " + skill.name()); - - EnumSet parentSkills = EnumSet.noneOf(PrimarySkillType.class); - boolean useDefaults = false; // If we had an error we back out and use defaults - - for (String name : config.getStringList(StringUtils.getCapitalized(skill.name()))) { - try { - PrimarySkillType parentSkill = PrimarySkillType.valueOf(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(PrimarySkillType.valueOf(name.toUpperCase())); - } - } - - // Register them - for (PrimarySkillType 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 index 72ecbe530..287c62048 100644 --- a/src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java +++ b/src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java @@ -1,53 +1,33 @@ package com.gmail.nossr50.skills.child; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; -import java.util.Collections; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.Set; +import java.util.*; public class FamilyTree { - private static HashMap> tree = new HashMap>(); + static final ImmutableSet salvageTree; + static final ImmutableSet smeltingTree; + + static { + ArrayList salvageParentsList = new ArrayList<>(); + ArrayList smeltingParentsList = new ArrayList<>(); + + salvageParentsList.add(PrimarySkillType.FISHING); + salvageParentsList.add(PrimarySkillType.REPAIR); + + smeltingParentsList.add(PrimarySkillType.MINING); + smeltingParentsList.add(PrimarySkillType.REPAIR); + + salvageTree = ImmutableSet.copyOf(salvageParentsList); + smeltingTree = ImmutableSet.copyOf(smeltingParentsList); + } public static Set getParents(PrimarySkillType 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(PrimarySkillType childSkill, PrimarySkillType parentSkill) { - enforceChildSkill(childSkill); - enforceNotChildSkill(parentSkill); - - if (!tree.containsKey(childSkill)) { - tree.put(childSkill, EnumSet.noneOf(PrimarySkillType.class)); - } - - tree.get(childSkill).add(parentSkill); - } - - protected static void closeRegistration() { - for (PrimarySkillType childSkill : tree.keySet()) { - Set immutableSet = Collections.unmodifiableSet(tree.get(childSkill)); - tree.put(childSkill, immutableSet); - } - } - - protected static void clearRegistrations() { - tree.clear(); - } - - protected static void enforceChildSkill(PrimarySkillType skill) { - if (!skill.isChildSkill()) { - throw new IllegalArgumentException(skill.name() + " is not a child skill!"); - } - } - - protected static void enforceNotChildSkill(PrimarySkillType skill) { - if (skill.isChildSkill()) { - throw new IllegalArgumentException(skill.name() + " is a child skill!"); - } + if(childSkill == PrimarySkillType.SALVAGE) + return salvageTree; + else + return smeltingTree; } } diff --git a/src/main/resources/child.yml b/src/main/resources/child.yml deleted file mode 100644 index 8261d3a14..000000000 --- a/src/main/resources/child.yml +++ /dev/null @@ -1,15 +0,0 @@ -# -# 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. -# -##### -Salvage: - - Fishing - - Repair -Smelting: - - Mining - - Repair \ No newline at end of file