DefaultKeys refactored to UnsafeValueValidation

This commit is contained in:
nossr50 2019-02-21 17:07:38 -08:00
parent 18d8b84099
commit 83ee9ca92c
8 changed files with 70 additions and 73 deletions

View File

@ -1,15 +1,12 @@
package com.gmail.nossr50.config; package com.gmail.nossr50.config;
import com.gmail.nossr50.mcMMO;
import java.io.File; import java.io.File;
import java.util.List;
/** /**
* This class is used for config files that validate their entries * This class is used for config files that validate their entries
*/ */
public abstract class ConfigValidated extends Config implements DefaultKeys { public abstract class ConfigValidated extends Config implements UnsafeValueValidation {
/** /**
* @param parentFolderPath Path to the "parent" folder on disk * @param parentFolderPath Path to the "parent" folder on disk
* @param relativePath Path to the config relative to the "parent" folder, this should mirror internal structure of resource files * @param relativePath Path to the config relative to the "parent" folder, this should mirror internal structure of resource files
@ -33,25 +30,4 @@ public abstract class ConfigValidated extends Config implements DefaultKeys {
super(parentFolderFile, relativePath, mergeNewKeys, copyDefaults, removeOldKeys); super(parentFolderFile, relativePath, mergeNewKeys, copyDefaults, removeOldKeys);
validateEntries(); validateEntries();
} }
/**
* Prints all errors found when validating the config
*/
private void validateEntries()
{
/*
* Print Errors about Keys
*/
List<String> validKeyErrors = validateKeys(); // Validate Keys
if(validKeyErrors != null && validKeyErrors.size() > 0)
{
for(String error : validKeyErrors)
{
mcMMO.p.getLogger().severe(error);
}
}
}
} }

View File

@ -1,10 +0,0 @@
package com.gmail.nossr50.config;
import java.util.List;
/**
* This is for config validation
*/
public interface DefaultKeys {
List<String> validateKeys();
}

View File

@ -0,0 +1,32 @@
package com.gmail.nossr50.config;
import com.gmail.nossr50.mcMMO;
import java.util.List;
/**
* This is for config validation
*/
public interface UnsafeValueValidation {
List<String> validateKeys();
/**
* Prints all errors found when validating the config
*/
default void validateEntries()
{
/*
* Print Errors about Keys
*/
List<String> validKeyErrors = validateKeys(); // Validate Keys
if(validKeyErrors != null && validKeyErrors.size() > 0)
{
for(String error : validKeyErrors)
{
mcMMO.p.getLogger().severe(error);
}
}
}
}

View File

@ -8,14 +8,12 @@ import com.gmail.nossr50.skills.repair.repairables.Repairable;
import com.gmail.nossr50.skills.repair.repairables.RepairableFactory; import com.gmail.nossr50.skills.repair.repairables.RepairableFactory;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
import com.sk89q.worldedit.InvalidItemException;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.*; import java.util.ArrayList;
import java.util.List;
/** /**
* This config * This config

View File

@ -8,15 +8,12 @@ import com.gmail.nossr50.skills.salvage.salvageables.Salvageable;
import com.gmail.nossr50.skills.salvage.salvageables.SalvageableFactory; import com.gmail.nossr50.skills.salvage.salvageables.SalvageableFactory;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
public class SalvageConfig extends ConfigCollection { public class SalvageConfig extends ConfigCollection {

View File

@ -9,9 +9,11 @@ import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Color; import org.bukkit.Color;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.*; import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;

View File

@ -1,10 +1,13 @@
package com.gmail.nossr50.config.treasure; package com.gmail.nossr50.config.treasure;
import com.gmail.nossr50.config.ConfigCollection; import com.gmail.nossr50.config.ConfigCollection;
import com.gmail.nossr50.config.UnsafeValueValidation;
import com.gmail.nossr50.datatypes.treasure.*; import com.gmail.nossr50.datatypes.treasure.*;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.EnchantmentUtils; import com.gmail.nossr50.util.EnchantmentUtils;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Tag; import org.bukkit.Tag;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -20,9 +23,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
public class TreasureConfig extends ConfigCollection { public class TreasureConfig extends ConfigCollection implements UnsafeValueValidation {
//private static TreasureConfig instance;
public HashMap<String, List<ExcavationTreasure>> excavationMap = new HashMap<String, List<ExcavationTreasure>>(); public HashMap<String, List<ExcavationTreasure>> excavationMap = new HashMap<String, List<ExcavationTreasure>>();
@ -35,6 +36,7 @@ public class TreasureConfig extends ConfigCollection {
public TreasureConfig() { public TreasureConfig() {
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(),"treasures.yml"); //super(McmmoCore.getDataFolderPath().getAbsoluteFile(),"treasures.yml");
super(mcMMO.p.getDataFolder().getAbsoluteFile(), "treasures.yml", false, true, false); super(mcMMO.p.getDataFolder().getAbsoluteFile(), "treasures.yml", false, true, false);
validateEntries();
} }
/** /**
@ -62,37 +64,41 @@ public class TreasureConfig extends ConfigCollection {
@Override @Override
public List<String> validateKeys() { public List<String> validateKeys() {
// Validate all the settings! // Validate all the settings!
List<String> reason = new ArrayList<String>(); List<String> errorMessages = new ArrayList<String>();
for (String tier : config.getConfigurationSection("Enchantment_Drop_Rates").getKeys(false)) { try {
double totalEnchantDropRate = 0; for (String tier : getUserRootNode().getNode("Enchantment_Drop_Rates").getList(TypeToken.of(String.class))) {
double totalItemDropRate = 0; double totalEnchantDropRate = 0;
double totalItemDropRate = 0;
for (Rarity rarity : Rarity.values()) { for (Rarity rarity : Rarity.values()) {
double enchantDropRate = getDoubleValue("Enchantment_Drop_Rates." + tier + "." + rarity.toString()); double enchantDropRate = getDoubleValue("Enchantment_Drop_Rates." + tier + "." + rarity.toString());
double itemDropRate = getDoubleValue("Item_Drop_Rates." + tier + "." + rarity.toString()); double itemDropRate = getDoubleValue("Item_Drop_Rates." + tier + "." + rarity.toString());
if ((enchantDropRate < 0.0 || enchantDropRate > 100.0) && rarity != Rarity.RECORD) { if ((enchantDropRate < 0.0 || enchantDropRate > 100.0) && rarity != Rarity.RECORD) {
reason.add("The enchant drop rate for " + tier + " items that are " + rarity.toString() + "should be between 0.0 and 100.0!"); errorMessages.add("The enchant drop rate for " + tier + " items that are " + rarity.toString() + "should be between 0.0 and 100.0!");
}
if (itemDropRate < 0.0 || itemDropRate > 100.0) {
errorMessages.add("The item drop rate for " + tier + " items that are " + rarity.toString() + "should be between 0.0 and 100.0!");
}
totalEnchantDropRate += enchantDropRate;
totalItemDropRate += itemDropRate;
} }
if (itemDropRate < 0.0 || itemDropRate > 100.0) { if (totalEnchantDropRate < 0 || totalEnchantDropRate > 100.0) {
reason.add("The item drop rate for " + tier + " items that are " + rarity.toString() + "should be between 0.0 and 100.0!"); errorMessages.add("The total enchant drop rate for " + tier + " should be between 0.0 and 100.0!");
} }
totalEnchantDropRate += enchantDropRate; if (totalItemDropRate < 0 || totalItemDropRate > 100.0) {
totalItemDropRate += itemDropRate; errorMessages.add("The total item drop rate for " + tier + " should be between 0.0 and 100.0!");
} }
if (totalEnchantDropRate < 0 || totalEnchantDropRate > 100.0) {
reason.add("The total enchant drop rate for " + tier + " should be between 0.0 and 100.0!");
}
if (totalItemDropRate < 0 || totalItemDropRate > 100.0) {
reason.add("The total item drop rate for " + tier + " should be between 0.0 and 100.0!");
} }
} catch (ObjectMappingException e) {
e.printStackTrace();
} }
return noErrorsInConfig(reason); return errorMessages;
} }
@Override @Override

View File

@ -8,14 +8,12 @@ import com.gmail.nossr50.datatypes.interactions.NotificationType;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.player.NotificationManager; import com.gmail.nossr50.util.player.NotificationManager;
import com.gmail.nossr50.util.player.UserManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -25,8 +23,6 @@ import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;