Converting repair config to the new system

This commit is contained in:
nossr50 2019-02-20 15:42:46 -08:00
parent 5ce08a2294
commit c14b949b0a
5 changed files with 228 additions and 117 deletions

View File

@ -3,7 +3,6 @@ package com.gmail.nossr50.config;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode; import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader; import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.ObjectMappingException;
@ -33,8 +32,8 @@ public abstract class Config implements VersionedConfig, Unload {
/* LOADERS */ /* LOADERS */
private YAMLConfigurationLoader defaultCopyLoader; private ConfigurationLoader<CommentedConfigurationNode> defaultCopyLoader;
private YAMLConfigurationLoader userCopyLoader; private ConfigurationLoader<CommentedConfigurationNode> userCopyLoader;
/* CONFIG FILES */ /* CONFIG FILES */
@ -43,8 +42,8 @@ public abstract class Config implements VersionedConfig, Unload {
/* ROOT NODES */ /* ROOT NODES */
private ConfigurationNode userRootNode = null; private CommentedConfigurationNode userRootNode = null;
private ConfigurationNode defaultRootNode = null; private CommentedConfigurationNode defaultRootNode = null;
/* CONFIG MANAGER */ /* CONFIG MANAGER */
private ConfigurationLoader<CommentedConfigurationNode> configManager; private ConfigurationLoader<CommentedConfigurationNode> configManager;
@ -111,10 +110,10 @@ public abstract class Config implements VersionedConfig, Unload {
private void loadConfig() private void loadConfig()
{ {
try { try {
final ConfigurationNode defaultConfig = this.defaultCopyLoader.load(); final CommentedConfigurationNode defaultConfig = this.defaultCopyLoader.load();
defaultRootNode = defaultConfig; defaultRootNode = defaultConfig;
final ConfigurationNode userConfig = this.userCopyLoader.load(); final CommentedConfigurationNode userConfig = this.userCopyLoader.load();
userRootNode = userConfig; userRootNode = userConfig;
} catch (IOException e) { } catch (IOException e) {
@ -282,7 +281,7 @@ public abstract class Config implements VersionedConfig, Unload {
if(!removeOldKeys) if(!removeOldKeys)
return; return;
for(ConfigurationNode configurationNode : defaultRootNode.getChildrenList()) for(CommentedConfigurationNode configurationNode : defaultRootNode.getChildrenList())
{ {
} }
@ -311,10 +310,17 @@ public abstract class Config implements VersionedConfig, Unload {
* Returns the root node of this config * Returns the root node of this config
* @return the root node of this config * @return the root node of this config
*/ */
protected ConfigurationNode getUserRootNode() { protected CommentedConfigurationNode getUserRootNode() {
return userRootNode; return userRootNode;
} }
/**
* Gets an int from the config and casts it to short before returning
* @param path the path to the int
* @return the value of the int after being cast to short at the node, null references will zero initialize
*/
public short getShortValue(String... path) { return (short) userRootNode.getNode(path).getInt();}
/** /**
* Grabs an int from the specified node * Grabs an int from the specified node
* @param path * @param path
@ -374,6 +380,12 @@ public abstract class Config implements VersionedConfig, Unload {
return (userRootNode.getNode(path) != null); return (userRootNode.getNode(path) != null);
} }
/**
* Gets a a List of type String from the Configuration file
* @param path path to the node
* @return a list of strings at the node, if null it will most likely zero initialize (empty list)
* @throws ObjectMappingException
*/
public List<String> getStringValueList(String... path) throws ObjectMappingException { public List<String> getStringValueList(String... path) throws ObjectMappingException {
return userRootNode.getList(TypeToken.of(String.class)); return userRootNode.getList(TypeToken.of(String.class));
} }

View File

@ -200,6 +200,7 @@ public class MainConfig extends ConfigValidated {
public static final String XP_AFTER_TELEPORT = "XP_After_Teleport_"; public static final String XP_AFTER_TELEPORT = "XP_After_Teleport_";
public static final String LIGHTNING = "_Lightning"; public static final String LIGHTNING = "_Lightning";
public static final String GOLD_BLOCK = "GOLD_BLOCK"; public static final String GOLD_BLOCK = "GOLD_BLOCK";
public static final String PICKAXE = "_Pickaxe";
public MainConfig() { public MainConfig() {
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), "config.yml", true); //super(McmmoCore.getDataFolderPath().getAbsoluteFile(), "config.yml", true);
@ -246,7 +247,7 @@ public class MainConfig extends ConfigValidated {
/* MySQL Settings */ /* MySQL Settings */
for (SQLDatabaseManager.PoolIdentifier identifier : SQLDatabaseManager.PoolIdentifier.values()) { for (SQLDatabaseManager.PoolIdentifier identifier : SQLDatabaseManager.PoolIdentifier.values()) {
if (getMySQLMaxConnections(identifier) <= 0) { if (getMySQLMaxConnections(identifier) <= 0) {
reason.add(MY_SQL + "." + DATABASE, MAX_CONNECTIONS + "." + StringUtils.getCapitalized(identifier.toString()) + " should be greater than 0!"); reason.add(MY_SQL + "." + DATABASE + "." + MAX_CONNECTIONS + "." + StringUtils.getCapitalized(identifier.toString()) + " should be greater than 0!");
} }
if (getMySQLMaxPoolSize(identifier) <= 0) { if (getMySQLMaxPoolSize(identifier) <= 0) {
reason.add(MY_SQL + "." + DATABASE + "." + MAX_POOL_SIZE + "." + StringUtils.getCapitalized(identifier.toString()) + " should be greater than 0!"); reason.add(MY_SQL + "." + DATABASE + "." + MAX_POOL_SIZE + "." + StringUtils.getCapitalized(identifier.toString()) + " should be greater than 0!");
@ -782,7 +783,7 @@ public class MainConfig extends ConfigValidated {
} }
public boolean getFluxPickaxeSoundEnabled() { public boolean getFluxPickaxeSoundEnabled() {
return getBooleanValue(ITEMS, FLUX + "_Pickaxe." + SOUND + "_" + ENABLED); return getBooleanValue(ITEMS, FLUX + PICKAXE, SOUND + "_" + ENABLED);
} }
/* Particles */ /* Particles */

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.config.collectionconfigs; package com.gmail.nossr50.config.collectionconfigs;
import com.gmail.nossr50.config.ConfigCollection; import com.gmail.nossr50.config.ConfigCollection;
import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.datatypes.skills.MaterialType; import com.gmail.nossr50.datatypes.skills.MaterialType;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.repair.repairables.Repairable; import com.gmail.nossr50.skills.repair.repairables.Repairable;
@ -9,17 +10,29 @@ 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 com.sk89q.worldedit.InvalidItemException;
import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.ConfigurationNode;
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.*;
import java.util.Iterator;
import java.util.List;
/** /**
* This config * This config
*/ */
public class RepairConfig extends ConfigCollection { public class RepairConfig extends ConfigCollection {
public static final String REPAIRABLES = "Repairables";
public static final String ITEM_ID = "ItemId";
public static final String MATERIAL_TYPE = "MaterialType";
public static final String REPAIR_MATERIAL = "RepairMaterial";
public static final String MAXIMUM_DURABILITY = "MaximumDurability";
public static final String ITEM_TYPE = "ItemType";
public static final String METADATA = "Metadata";
public static final String XP_MULTIPLIER = "XpMultiplier";
public static final String MINIMUM_LEVEL = "MinimumLevel";
public static final String MINIMUM_QUANTITY = "MinimumQuantity";
public RepairConfig(String fileName) { public RepairConfig(String fileName) {
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false); //super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
super(mcMMO.p.getDataFolder().getAbsoluteFile(), fileName, false); super(mcMMO.p.getDataFolder().getAbsoluteFile(), fileName, false);
@ -37,125 +50,191 @@ public class RepairConfig extends ConfigCollection {
@Override @Override
public void register() { public void register() {
ConfigurationNode repairablesNode = getUserRootNode().getNode("Repairables"); try {
List<? extends ConfigurationNode> repairablesNodeChildrenList = repairablesNode.getChildrenList(); //Grab the "keys" under the Repairables node
Iterator<? extends ConfigurationNode> configIter = repairablesNodeChildrenList.iterator(); ArrayList<String> keys = new ArrayList<>(getStringValueList(REPAIRABLES));
for(Iterator<? extends ConfigurationNode> i = repairablesNodeChildrenList.iterator(); i.hasNext();) //TODO: Remove Debug
{ if(keys.size() <= 0) {
ConfigurationNode iterNode = i.next(); mcMMO.p.getLogger().severe("DEBUG: Repair MultiConfigContainer key list is empty");
//TODO: Verify that this is getting the key return;
String key = iterNode.getKey().toString(); //Get the String of the node
// Validate all the things!
List<String> reason = new ArrayList<String>();
try {
// ItemStack Material
ConfigItemCategory configItemCategory = ItemUtils.matchItemType(key);
} catch (InvalidItemException e) {
e.printStackTrace();
} }
if (itemType == null) { for (String key : keys) {
reason.add("Invalid material: " + key); // Validate all the things!
} List<String> errorMessages = new ArrayList<String>();
// Repair Material Type /*
MaterialType repairMaterialType = MaterialType.OTHER; * Match the name of the key to a Material constant definition
String repairMaterialTypeString = getStringValue("Repairables." + key + ".MaterialType", "OTHER"); */
Material itemMaterial = Material.matchMaterial(key);
if (!config.contains("Repairables." + key + ".MaterialType") && itemType != null) { if (itemMaterial == null) {
ItemStack repairItem = ItemStack.makeNew(itemType); mcMMO.p.getLogger().severe("Repair Invalid material: " + key);
continue;
if (ItemUtils.isWoodTool(repairItem)) {
repairMaterialType = MaterialType.WOOD;
} else if (ItemUtils.isStoneTool(repairItem)) {
repairMaterialType = MaterialType.STONE;
} else if (ItemUtils.isStringTool(repairItem)) {
repairMaterialType = MaterialType.STRING;
} else if (ItemUtils.isLeatherArmor(repairItem)) {
repairMaterialType = MaterialType.LEATHER;
} else if (ItemUtils.isIronArmor(repairItem) || ItemUtils.isIronTool(repairItem)) {
repairMaterialType = MaterialType.IRON;
} else if (ItemUtils.isGoldArmor(repairItem) || ItemUtils.isGoldTool(repairItem)) {
repairMaterialType = MaterialType.GOLD;
} else if (ItemUtils.isDiamondArmor(repairItem) || ItemUtils.isDiamondTool(repairItem)) {
repairMaterialType = MaterialType.DIAMOND;
} }
} else {
try { /*
repairMaterialType = MaterialType.valueOf(repairMaterialTypeString); * Determine Repair Material Type
} catch (IllegalArgumentException ex) { */
reason.add(key + " has an invalid MaterialType of " + repairMaterialTypeString); MaterialType repairMaterialType = MaterialType.OTHER;
String repairMaterialTypeString = getRepairMaterialTypeString(key);
if (hasNode(REPAIRABLES, key, MATERIAL_TYPE)) {
ItemStack repairItem = new ItemStack(itemMaterial);
if (ItemUtils.isWoodTool(repairItem)) {
repairMaterialType = MaterialType.WOOD;
}
else if (ItemUtils.isStoneTool(repairItem)) {
repairMaterialType = MaterialType.STONE;
}
else if (ItemUtils.isStringTool(repairItem)) {
repairMaterialType = MaterialType.STRING;
}
else if (ItemUtils.isLeatherArmor(repairItem)) {
repairMaterialType = MaterialType.LEATHER;
}
else if (ItemUtils.isIronArmor(repairItem) || ItemUtils.isIronTool(repairItem)) {
repairMaterialType = MaterialType.IRON;
}
else if (ItemUtils.isGoldArmor(repairItem) || ItemUtils.isGoldTool(repairItem)) {
repairMaterialType = MaterialType.GOLD;
}
else if (ItemUtils.isDiamondArmor(repairItem) || ItemUtils.isDiamondTool(repairItem)) {
repairMaterialType = MaterialType.DIAMOND;
}
} }
} else {
//If a material cannot be matched, try matching the material to its repair material type string from the config
// Repair Material try {
String repairMaterialName = getStringValue("Repairables." + key + ".RepairMaterial"); repairMaterialType = MaterialType.valueOf(repairMaterialTypeString);
Material repairMaterial = (repairMaterialName == null ? repairMaterialType.getDefaultMaterial() : Material.matchMaterial(repairMaterialName)); }
catch (IllegalArgumentException ex) {
if (repairMaterial == null) { errorMessages.add(key + " has an invalid " + MATERIAL_TYPE + " of " + repairMaterialTypeString);
reason.add(key + " has an invalid repair material: " + repairMaterialName); continue;
} }
// Maximum Durability
short maximumDurability = (itemType != null ? itemType.getMaxDurability() : (short) getIntValue("Repairables." + key + ".MaximumDurability"));
if (maximumDurability <= 0) {
maximumDurability = (short) getIntValue("Repairables." + key + ".MaximumDurability");
}
if (maximumDurability <= 0) {
reason.add("Maximum durability of " + key + " must be greater than 0!");
}
// ItemStack Type
ConfigItemCategory repairConfigItemCategory = ConfigItemCategory.OTHER;
String repairItemTypeString = getStringValue("Repairables." + key + ".ItemType", "OTHER");
if (!config.contains("Repairables." + key + ".ItemType") && itemType != null) {
ItemStack repairItem = new ItemStack(itemType);
if (ItemUtils.isMinecraftTool(repairItem)) {
repairConfigItemCategory = ConfigItemCategory.TOOL;
} else if (ItemUtils.isArmor(repairItem)) {
repairConfigItemCategory = ConfigItemCategory.ARMOR;
} }
} else {
try { // Repair Material
repairConfigItemCategory = ConfigItemCategory.valueOf(repairItemTypeString); String repairMaterialName = getRepairMaterialStringName(key);
} catch (IllegalArgumentException ex) { Material repairMaterial = (repairMaterialName == null ? repairMaterialType.getDefaultMaterial() : Material.matchMaterial(repairMaterialName));
reason.add(key + " has an invalid ItemType of " + repairItemTypeString);
if (repairMaterial == null) {
errorMessages.add(key + " has an invalid repair material: " + repairMaterialName);
} }
}
byte repairMetadata = (byte) getIntValue("Repairables." + key + ".RepairMaterialMetadata", -1); // Maximum Durability
int minimumLevel = getIntValue("Repairables." + key + ".MinimumLevel"); short maximumDurability = (itemMaterial != null ? itemMaterial.getMaxDurability() : getRepairableMaximumDurability(key));
double xpMultiplier = getDoubleValue("Repairables." + key + ".XpMultiplier", 1);
if (minimumLevel < 0) { if (maximumDurability <= 0) {
reason.add(key + " has an invalid MinimumLevel of " + minimumLevel); maximumDurability = getRepairableMaximumDurability(key);
} }
// Minimum Quantity if (maximumDurability <= 0) {
int minimumQuantity = (itemType != null ? SkillUtils.getRepairAndSalvageQuantities(new ItemStack(itemType), repairMaterial, repairMetadata) : getIntValue("Repairables." + key + ".MinimumQuantity", 2)); errorMessages.add("Maximum durability of " + key + " must be greater than 0!");
}
if (minimumQuantity <= 0 && itemType != null) { // Item Type
minimumQuantity = getIntValue("Repairables." + key + ".MinimumQuantity", 2); ItemType repairItemType = ItemType.OTHER;
} String repairItemTypeString = "";
if (minimumQuantity <= 0) { if(hasNode(REPAIRABLES, key, ITEM_TYPE))
reason.add("Minimum quantity of " + key + " must be greater than 0!"); repairItemTypeString = getStringValue(REPAIRABLES, key, ITEM_TYPE);
} else
repairItemTypeString = "OTHER";
if (noErrorsInRepairable(reason)) { if (!hasNode(REPAIRABLES, key, ITEM_TYPE) && itemMaterial != null) {
Repairable repairable = RepairableFactory.getRepairable(itemType, repairMaterial, repairMetadata, minimumLevel, minimumQuantity, maximumDurability, repairConfigItemCategory, repairMaterialType, xpMultiplier); ItemStack repairItem = new ItemStack(itemMaterial);
if (ItemUtils.isMinecraftTool(repairItem)) {
repairItemType = ItemType.TOOL;
}
else if (ItemUtils.isArmor(repairItem)) {
repairItemType = ItemType.ARMOR;
}
}
else {
try {
repairItemType = ItemType.valueOf(repairItemTypeString);
}
catch (IllegalArgumentException ex) {
errorMessages.add(key + " has an invalid ItemType of " + repairItemTypeString);
}
}
byte repairMetadata = -1;
//Set the metadata byte
if(hasNode(REPAIRABLES, key, REPAIR_MATERIAL, METADATA))
repairMetadata = (byte) getIntValue(REPAIRABLES, key, REPAIR_MATERIAL, METADATA);
int minimumLevel = getIntValue(REPAIRABLES, key, MINIMUM_LEVEL);
double xpMultiplier = 1;
if(hasNode(REPAIRABLES, key, XP_MULTIPLIER))
xpMultiplier = getDoubleValue(REPAIRABLES, key, XP_MULTIPLIER);
// Minimum Quantity
int minimumQuantity = SkillUtils.getRepairAndSalvageQuantities(new ItemStack(itemMaterial), repairMaterial, repairMetadata);
if (minimumQuantity <= 0) {
minimumQuantity = getIntValue(REPAIRABLES, key, MINIMUM_QUANTITY);
}
/*
* VALIDATE
* Just make sure the values we may have just grabbed from the config aren't below 0
*/
//Validate min level
if(minimumLevel < 0)
minimumLevel = 0;
//Validate XP Mult
if(xpMultiplier < 0)
xpMultiplier = 0;
//Validate Minimum Quantity
if (minimumQuantity <= 0) {
minimumQuantity = 2;
errorMessages.add("Minimum quantity for "+key+" in repair config should be above 0");
}
Repairable repairable = RepairableFactory.getRepairable(itemMaterial, repairMaterial, repairMetadata, minimumLevel, minimumQuantity, maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
genericCollection.add(repairable); genericCollection.add(repairable);
for (String error : errorMessages) {
//McmmoCore.getLogger().warning(issue);
mcMMO.p.getLogger().warning(error);
}
} }
} catch (ObjectMappingException e) {
e.printStackTrace();
} }
} }
private String getRepairMaterialTypeString(String key) {
return getStringValue(REPAIRABLES, key, MATERIAL_TYPE);
}
private short getRepairableMaximumDurability(String key) {
return getShortValue(REPAIRABLES, key, MAXIMUM_DURABILITY);
}
/**
* Gets the Repair Material String Name defined in the config
* @param key the key name of the repairable child node under the Repairables parent node
* @return the Repair Material String Name defined in the config
*/
private String getRepairMaterialStringName(String key) {
return getStringValue(REPAIRABLES, key, REPAIR_MATERIAL);
}
/** /**
* Check if there are any errors for this repairable and if there are reports them to console * Check if there are any errors for this repairable and if there are reports them to console
@ -163,9 +242,7 @@ public class RepairConfig extends ConfigCollection {
* @return returns true if there are no errors for this repairable * @return returns true if there are no errors for this repairable
*/ */
private boolean noErrorsInRepairable(List<String> issues) { private boolean noErrorsInRepairable(List<String> issues) {
for (String issue : issues) {
McmmoCore.getLogger().warning(issue);
}
return issues.isEmpty(); return issues.isEmpty();
} }

View File

@ -8,6 +8,7 @@ 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 org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -35,7 +36,7 @@ public class SalvageConfig extends ConfigCollection {
@Override @Override
public void register() { public void register() {
ConfigurationSection section = config.getConfigurationSection("Salvageables"); CommentedConfigurationNode section = getUserRootNode().getNode("Salvageables");
Set<String> keys = section.getKeys(false); Set<String> keys = section.getKeys(false);
for (String key : keys) { for (String key : keys) {

View File

@ -419,6 +419,26 @@ public final class ItemUtils {
} }
} }
/**
* Checks to see if an item is a wooden tool.
*
* @param material Material to check
* @return true if the item is a wooden tool, false otherwise
*/
public static boolean isWoodTool(Material material) {
switch (material) {
case WOODEN_AXE:
case WOODEN_HOE:
case WOODEN_PICKAXE:
case WOODEN_SHOVEL:
case WOODEN_SWORD:
return true;
default:
return false;
}
}
/** /**
* Checks to see if an item is a string tool. * Checks to see if an item is a string tool.
* *