2019-02-16 16:09:48 -08:00
|
|
|
package com.gmail.nossr50.config.collectionconfigs;
|
|
|
|
|
2019-02-17 08:59:09 -08:00
|
|
|
import com.gmail.nossr50.config.ConfigCollection;
|
2019-02-20 15:42:46 -08:00
|
|
|
import com.gmail.nossr50.datatypes.skills.ItemType;
|
2019-02-16 16:09:48 -08:00
|
|
|
import com.gmail.nossr50.datatypes.skills.MaterialType;
|
2019-02-17 08:59:09 -08:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2019-02-16 16:09:48 -08:00
|
|
|
import com.gmail.nossr50.skills.repair.repairables.Repairable;
|
|
|
|
import com.gmail.nossr50.skills.repair.repairables.RepairableFactory;
|
|
|
|
import com.gmail.nossr50.util.ItemUtils;
|
|
|
|
import com.gmail.nossr50.util.skills.SkillUtils;
|
2019-02-26 16:15:40 -08:00
|
|
|
import ninja.leaping.configurate.ConfigurationNode;
|
2019-02-16 16:09:48 -08:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2019-02-21 17:07:38 -08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2019-02-16 16:09:48 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This config
|
|
|
|
*/
|
2019-02-17 08:59:09 -08:00
|
|
|
public class RepairConfig extends ConfigCollection {
|
2019-02-20 15:42:46 -08:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
2019-02-25 16:39:49 -08:00
|
|
|
public RepairConfig(String fileName, boolean merge, boolean copyDefaults) {
|
2019-02-17 08:59:09 -08:00
|
|
|
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
|
2019-02-25 16:39:49 -08:00
|
|
|
super(mcMMO.p.getDataFolder().getAbsoluteFile(), fileName, merge, copyDefaults, false);
|
2019-02-26 20:57:02 -08:00
|
|
|
register();
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The version of this config
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public double getConfigVersion() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-02-17 08:59:09 -08:00
|
|
|
public void register() {
|
2019-02-26 16:15:40 -08:00
|
|
|
//Grab the "keys" under the Repairables node
|
|
|
|
ArrayList<ConfigurationNode> repairChildrenNodes = new ArrayList<>(getChildren(REPAIRABLES));
|
|
|
|
|
|
|
|
//TODO: Remove Debug
|
|
|
|
if(repairChildrenNodes.size() <= 0) {
|
|
|
|
mcMMO.p.getLogger().severe("DEBUG: Repair MultiConfigContainer key list is empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ConfigurationNode repairNode : repairChildrenNodes) {
|
|
|
|
// Validate all the things!
|
|
|
|
List<String> errorMessages = new ArrayList<String>();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match the name of the key to a Material constant definition
|
|
|
|
*/
|
|
|
|
String repairChildNodeName = repairNode.getString();
|
|
|
|
Material itemMaterial = Material.matchMaterial(repairChildNodeName);
|
|
|
|
|
|
|
|
if (itemMaterial == null) {
|
|
|
|
mcMMO.p.getLogger().severe("Repair Invalid material: " + repairChildNodeName);
|
|
|
|
continue;
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
/*
|
|
|
|
* Determine Repair Material Type
|
|
|
|
*/
|
|
|
|
MaterialType repairMaterialType = MaterialType.OTHER;
|
|
|
|
String repairMaterialTypeString = getRepairMaterialTypeString(repairChildNodeName);
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (hasNode(REPAIRABLES, repairChildNodeName, MATERIAL_TYPE)) {
|
|
|
|
ItemStack repairItem = new ItemStack(itemMaterial);
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (ItemUtils.isWoodTool(repairItem)) {
|
|
|
|
repairMaterialType = MaterialType.WOOD;
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
2019-02-26 16:15:40 -08:00
|
|
|
else if (ItemUtils.isStoneTool(repairItem)) {
|
|
|
|
repairMaterialType = MaterialType.STONE;
|
|
|
|
}
|
|
|
|
else if (ItemUtils.isStringTool(repairItem)) {
|
|
|
|
repairMaterialType = MaterialType.STRING;
|
2019-02-20 15:42:46 -08:00
|
|
|
}
|
2019-02-26 16:15:40 -08:00
|
|
|
else if (ItemUtils.isLeatherArmor(repairItem)) {
|
|
|
|
repairMaterialType = MaterialType.LEATHER;
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
2019-02-26 16:15:40 -08:00
|
|
|
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
|
|
|
|
try {
|
|
|
|
repairMaterialType = MaterialType.valueOf(repairMaterialTypeString.toUpperCase());
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
errorMessages.add("Repair Config: " + repairChildNodeName + " has an invalid " + MATERIAL_TYPE + " of " + repairMaterialTypeString);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
// Repair Material
|
|
|
|
String repairMaterialName = getRepairMaterialStringName(repairChildNodeName);
|
|
|
|
Material repairMaterial = (repairMaterialName == null ? repairMaterialType.getDefaultMaterial() : Material.matchMaterial(repairMaterialName));
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (repairMaterial == null) {
|
|
|
|
errorMessages.add(repairChildNodeName + " has an invalid repair material: " + repairMaterialName);
|
|
|
|
}
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
// Maximum Durability
|
|
|
|
short maximumDurability = (itemMaterial != null ? itemMaterial.getMaxDurability() : getRepairableMaximumDurability(repairChildNodeName));
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (maximumDurability <= 0) {
|
|
|
|
maximumDurability = getRepairableMaximumDurability(repairChildNodeName);
|
|
|
|
}
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (maximumDurability <= 0) {
|
|
|
|
errorMessages.add("Maximum durability of " + repairChildNodeName + " must be greater than 0!");
|
|
|
|
}
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
// Item Type
|
|
|
|
ItemType repairItemType = ItemType.OTHER;
|
|
|
|
String repairItemTypeString = "";
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if(hasNode(REPAIRABLES, repairChildNodeName, ITEM_TYPE))
|
|
|
|
repairItemTypeString = getStringValue(REPAIRABLES, repairChildNodeName, ITEM_TYPE);
|
|
|
|
else
|
|
|
|
repairItemTypeString = "OTHER";
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (!hasNode(REPAIRABLES, repairChildNodeName, ITEM_TYPE) && itemMaterial != null) {
|
|
|
|
ItemStack repairItem = new ItemStack(itemMaterial);
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (ItemUtils.isMinecraftTool(repairItem)) {
|
|
|
|
repairItemType = ItemType.TOOL;
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
2019-02-26 16:15:40 -08:00
|
|
|
else if (ItemUtils.isArmor(repairItem)) {
|
|
|
|
repairItemType = ItemType.ARMOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
repairItemType = ItemType.valueOf(repairItemTypeString);
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
2019-02-26 16:15:40 -08:00
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
errorMessages.add(repairChildNodeName + " has an invalid ItemType of " + repairItemTypeString);
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
byte repairMetadata = -1;
|
2019-02-16 16:09:48 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
//Set the metadata byte
|
|
|
|
if(hasNode(REPAIRABLES, repairChildNodeName, REPAIR_MATERIAL, METADATA))
|
|
|
|
repairMetadata = (byte) getIntValue(REPAIRABLES, repairChildNodeName, REPAIR_MATERIAL, METADATA);
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
int minimumLevel = getIntValue(REPAIRABLES, repairChildNodeName, MINIMUM_LEVEL);
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
double xpMultiplier = 1;
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if(hasNode(REPAIRABLES, repairChildNodeName, XP_MULTIPLIER))
|
|
|
|
xpMultiplier = getDoubleValue(REPAIRABLES, repairChildNodeName, XP_MULTIPLIER);
|
2019-02-16 16:09:48 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
// Minimum Quantity
|
|
|
|
int minimumQuantity = SkillUtils.getRepairAndSalvageQuantities(new ItemStack(itemMaterial), repairMaterial, repairMetadata);
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
if (minimumQuantity <= 0) {
|
|
|
|
minimumQuantity = getIntValue(REPAIRABLES, repairChildNodeName, MINIMUM_QUANTITY);
|
|
|
|
}
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
/*
|
|
|
|
* VALIDATE
|
|
|
|
* Just make sure the values we may have just grabbed from the config aren't below 0
|
|
|
|
*/
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
//Validate min level
|
|
|
|
if(minimumLevel < 0)
|
|
|
|
minimumLevel = 0;
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
//Validate XP Mult
|
|
|
|
if(xpMultiplier < 0)
|
|
|
|
xpMultiplier = 0;
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
//Validate Minimum Quantity
|
|
|
|
if (minimumQuantity <= 0) {
|
|
|
|
minimumQuantity = 2;
|
|
|
|
errorMessages.add("Minimum quantity for "+repairChildNodeName+" in repair config should be above 0");
|
|
|
|
}
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
Repairable repairable = RepairableFactory.getRepairable(itemMaterial, repairMaterial, repairMetadata, minimumLevel, minimumQuantity, maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
|
|
|
|
genericCollection.add(repairable);
|
2019-02-20 15:42:46 -08:00
|
|
|
|
2019-02-26 16:15:40 -08:00
|
|
|
for (String error : errorMessages) {
|
|
|
|
//McmmoCore.getLogger().warning(issue);
|
|
|
|
mcMMO.p.getLogger().warning(error);
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:42:46 -08:00
|
|
|
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);
|
|
|
|
}
|
2019-02-16 16:09:48 -08:00
|
|
|
}
|