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.google.common.io.Files;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
@ -33,8 +32,8 @@ public abstract class Config implements VersionedConfig, Unload {
/* LOADERS */
private YAMLConfigurationLoader defaultCopyLoader;
private YAMLConfigurationLoader userCopyLoader;
private ConfigurationLoader<CommentedConfigurationNode> defaultCopyLoader;
private ConfigurationLoader<CommentedConfigurationNode> userCopyLoader;
/* CONFIG FILES */
@ -43,8 +42,8 @@ public abstract class Config implements VersionedConfig, Unload {
/* ROOT NODES */
private ConfigurationNode userRootNode = null;
private ConfigurationNode defaultRootNode = null;
private CommentedConfigurationNode userRootNode = null;
private CommentedConfigurationNode defaultRootNode = null;
/* CONFIG MANAGER */
private ConfigurationLoader<CommentedConfigurationNode> configManager;
@ -111,10 +110,10 @@ public abstract class Config implements VersionedConfig, Unload {
private void loadConfig()
{
try {
final ConfigurationNode defaultConfig = this.defaultCopyLoader.load();
final CommentedConfigurationNode defaultConfig = this.defaultCopyLoader.load();
defaultRootNode = defaultConfig;
final ConfigurationNode userConfig = this.userCopyLoader.load();
final CommentedConfigurationNode userConfig = this.userCopyLoader.load();
userRootNode = userConfig;
} catch (IOException e) {
@ -282,7 +281,7 @@ public abstract class Config implements VersionedConfig, Unload {
if(!removeOldKeys)
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
* @return the root node of this config
*/
protected ConfigurationNode getUserRootNode() {
protected CommentedConfigurationNode getUserRootNode() {
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
* @param path
@ -374,6 +380,12 @@ public abstract class Config implements VersionedConfig, Unload {
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 {
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 LIGHTNING = "_Lightning";
public static final String GOLD_BLOCK = "GOLD_BLOCK";
public static final String PICKAXE = "_Pickaxe";
public MainConfig() {
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), "config.yml", true);
@ -246,7 +247,7 @@ public class MainConfig extends ConfigValidated {
/* MySQL Settings */
for (SQLDatabaseManager.PoolIdentifier identifier : SQLDatabaseManager.PoolIdentifier.values()) {
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) {
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() {
return getBooleanValue(ITEMS, FLUX + "_Pickaxe." + SOUND + "_" + ENABLED);
return getBooleanValue(ITEMS, FLUX + PICKAXE, SOUND + "_" + ENABLED);
}
/* Particles */

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.config.collectionconfigs;
import com.gmail.nossr50.config.ConfigCollection;
import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.datatypes.skills.MaterialType;
import com.gmail.nossr50.mcMMO;
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.sk89q.worldedit.InvalidItemException;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
/**
* This config
*/
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) {
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
super(mcMMO.p.getDataFolder().getAbsoluteFile(), fileName, false);
@ -37,123 +50,189 @@ public class RepairConfig extends ConfigCollection {
@Override
public void register() {
ConfigurationNode repairablesNode = getUserRootNode().getNode("Repairables");
List<? extends ConfigurationNode> repairablesNodeChildrenList = repairablesNode.getChildrenList();
Iterator<? extends ConfigurationNode> configIter = repairablesNodeChildrenList.iterator();
for(Iterator<? extends ConfigurationNode> i = repairablesNodeChildrenList.iterator(); i.hasNext();)
{
ConfigurationNode iterNode = i.next();
//TODO: Verify that this is getting the key
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();
//Grab the "keys" under the Repairables node
ArrayList<String> keys = new ArrayList<>(getStringValueList(REPAIRABLES));
//TODO: Remove Debug
if(keys.size() <= 0) {
mcMMO.p.getLogger().severe("DEBUG: Repair MultiConfigContainer key list is empty");
return;
}
if (itemType == null) {
reason.add("Invalid material: " + key);
for (String key : keys) {
// Validate all the things!
List<String> errorMessages = new ArrayList<String>();
/*
* Match the name of the key to a Material constant definition
*/
Material itemMaterial = Material.matchMaterial(key);
if (itemMaterial == null) {
mcMMO.p.getLogger().severe("Repair Invalid material: " + key);
continue;
}
// Repair Material Type
/*
* Determine Repair Material Type
*/
MaterialType repairMaterialType = MaterialType.OTHER;
String repairMaterialTypeString = getStringValue("Repairables." + key + ".MaterialType", "OTHER");
String repairMaterialTypeString = getRepairMaterialTypeString(key);
if (!config.contains("Repairables." + key + ".MaterialType") && itemType != null) {
ItemStack repairItem = ItemStack.makeNew(itemType);
if (hasNode(REPAIRABLES, key, MATERIAL_TYPE)) {
ItemStack repairItem = new ItemStack(itemMaterial);
if (ItemUtils.isWoodTool(repairItem)) {
repairMaterialType = MaterialType.WOOD;
} else if (ItemUtils.isStoneTool(repairItem)) {
}
else if (ItemUtils.isStoneTool(repairItem)) {
repairMaterialType = MaterialType.STONE;
} else if (ItemUtils.isStringTool(repairItem)) {
}
else if (ItemUtils.isStringTool(repairItem)) {
repairMaterialType = MaterialType.STRING;
} else if (ItemUtils.isLeatherArmor(repairItem)) {
}
else if (ItemUtils.isLeatherArmor(repairItem)) {
repairMaterialType = MaterialType.LEATHER;
} else if (ItemUtils.isIronArmor(repairItem) || ItemUtils.isIronTool(repairItem)) {
}
else if (ItemUtils.isIronArmor(repairItem) || ItemUtils.isIronTool(repairItem)) {
repairMaterialType = MaterialType.IRON;
} else if (ItemUtils.isGoldArmor(repairItem) || ItemUtils.isGoldTool(repairItem)) {
}
else if (ItemUtils.isGoldArmor(repairItem) || ItemUtils.isGoldTool(repairItem)) {
repairMaterialType = MaterialType.GOLD;
} else if (ItemUtils.isDiamondArmor(repairItem) || ItemUtils.isDiamondTool(repairItem)) {
}
else if (ItemUtils.isDiamondArmor(repairItem) || ItemUtils.isDiamondTool(repairItem)) {
repairMaterialType = MaterialType.DIAMOND;
}
} else {
}
else {
//If a material cannot be matched, try matching the material to its repair material type string from the config
try {
repairMaterialType = MaterialType.valueOf(repairMaterialTypeString);
} catch (IllegalArgumentException ex) {
reason.add(key + " has an invalid MaterialType of " + repairMaterialTypeString);
}
catch (IllegalArgumentException ex) {
errorMessages.add(key + " has an invalid " + MATERIAL_TYPE + " of " + repairMaterialTypeString);
continue;
}
}
// Repair Material
String repairMaterialName = getStringValue("Repairables." + key + ".RepairMaterial");
String repairMaterialName = getRepairMaterialStringName(key);
Material repairMaterial = (repairMaterialName == null ? repairMaterialType.getDefaultMaterial() : Material.matchMaterial(repairMaterialName));
if (repairMaterial == null) {
reason.add(key + " has an invalid repair material: " + repairMaterialName);
errorMessages.add(key + " has an invalid repair material: " + repairMaterialName);
}
// Maximum Durability
short maximumDurability = (itemType != null ? itemType.getMaxDurability() : (short) getIntValue("Repairables." + key + ".MaximumDurability"));
short maximumDurability = (itemMaterial != null ? itemMaterial.getMaxDurability() : getRepairableMaximumDurability(key));
if (maximumDurability <= 0) {
maximumDurability = (short) getIntValue("Repairables." + key + ".MaximumDurability");
maximumDurability = getRepairableMaximumDurability(key);
}
if (maximumDurability <= 0) {
reason.add("Maximum durability of " + key + " must be greater than 0!");
errorMessages.add("Maximum durability of " + key + " must be greater than 0!");
}
// ItemStack Type
ConfigItemCategory repairConfigItemCategory = ConfigItemCategory.OTHER;
String repairItemTypeString = getStringValue("Repairables." + key + ".ItemType", "OTHER");
// Item Type
ItemType repairItemType = ItemType.OTHER;
String repairItemTypeString = "";
if (!config.contains("Repairables." + key + ".ItemType") && itemType != null) {
ItemStack repairItem = new ItemStack(itemType);
if(hasNode(REPAIRABLES, key, ITEM_TYPE))
repairItemTypeString = getStringValue(REPAIRABLES, key, ITEM_TYPE);
else
repairItemTypeString = "OTHER";
if (!hasNode(REPAIRABLES, key, ITEM_TYPE) && itemMaterial != null) {
ItemStack repairItem = new ItemStack(itemMaterial);
if (ItemUtils.isMinecraftTool(repairItem)) {
repairConfigItemCategory = ConfigItemCategory.TOOL;
} else if (ItemUtils.isArmor(repairItem)) {
repairConfigItemCategory = ConfigItemCategory.ARMOR;
repairItemType = ItemType.TOOL;
}
} else {
else if (ItemUtils.isArmor(repairItem)) {
repairItemType = ItemType.ARMOR;
}
}
else {
try {
repairConfigItemCategory = ConfigItemCategory.valueOf(repairItemTypeString);
} catch (IllegalArgumentException ex) {
reason.add(key + " has an invalid ItemType of " + repairItemTypeString);
repairItemType = ItemType.valueOf(repairItemTypeString);
}
catch (IllegalArgumentException ex) {
errorMessages.add(key + " has an invalid ItemType of " + repairItemTypeString);
}
}
byte repairMetadata = (byte) getIntValue("Repairables." + key + ".RepairMaterialMetadata", -1);
int minimumLevel = getIntValue("Repairables." + key + ".MinimumLevel");
double xpMultiplier = getDoubleValue("Repairables." + key + ".XpMultiplier", 1);
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);
if (minimumLevel < 0) {
reason.add(key + " has an invalid MinimumLevel of " + minimumLevel);
}
// Minimum Quantity
int minimumQuantity = (itemType != null ? SkillUtils.getRepairAndSalvageQuantities(new ItemStack(itemType), repairMaterial, repairMetadata) : getIntValue("Repairables." + key + ".MinimumQuantity", 2));
if (minimumQuantity <= 0 && itemType != null) {
minimumQuantity = getIntValue("Repairables." + key + ".MinimumQuantity", 2);
}
int minimumQuantity = SkillUtils.getRepairAndSalvageQuantities(new ItemStack(itemMaterial), repairMaterial, repairMetadata);
if (minimumQuantity <= 0) {
reason.add("Minimum quantity of " + key + " must be greater than 0!");
minimumQuantity = getIntValue(REPAIRABLES, key, MINIMUM_QUANTITY);
}
if (noErrorsInRepairable(reason)) {
Repairable repairable = RepairableFactory.getRepairable(itemType, repairMaterial, repairMetadata, minimumLevel, minimumQuantity, maximumDurability, repairConfigItemCategory, repairMaterialType, xpMultiplier);
/*
* 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);
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);
}
@ -163,9 +242,7 @@ public class RepairConfig extends ConfigCollection {
* @return returns true if there are no errors for this repairable
*/
private boolean noErrorsInRepairable(List<String> issues) {
for (String issue : issues) {
McmmoCore.getLogger().warning(issue);
}
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.util.ItemUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
@ -35,7 +36,7 @@ public class SalvageConfig extends ConfigCollection {
@Override
public void register() {
ConfigurationSection section = config.getConfigurationSection("Salvageables");
CommentedConfigurationNode section = getUserRootNode().getNode("Salvageables");
Set<String> keys = section.getKeys(false);
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.
*