new config pt 9

This commit is contained in:
nossr50
2019-02-16 16:09:48 -08:00
parent 09c91f3b4b
commit 5e1c12d8ec
30 changed files with 221 additions and 224 deletions

View File

@ -0,0 +1,59 @@
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 ConfigCollections {
public ChildConfig() {
super("child.yml");
}
@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<PrimarySkillType> 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();
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.config;
import java.io.File;
/**
* Represents a config file that registers keys after its initialized
*/
public abstract class ConfigCollections extends Config implements RegistersKeys, ConfigCollection {
public ConfigCollections(String pathToParentFolder, String relativePath, boolean mergeNewKeys) {
super(pathToParentFolder, relativePath, mergeNewKeys);
loadKeys();
}
public ConfigCollections(File pathToParentFolder, String relativePath, boolean mergeNewKeys) {
super(pathToParentFolder, relativePath, mergeNewKeys);
loadKeys();
}
}

View File

@ -0,0 +1,11 @@
package com.gmail.nossr50.config;
/**
* A class that registers keys
*/
public interface RegistersKeys {
/**
* Loads up keys
*/
void loadKeys();
}

View File

@ -0,0 +1,119 @@
package com.gmail.nossr50.config.collectionconfigs;
import com.gmail.nossr50.config.ConfigCollection;
import com.gmail.nossr50.config.ConfigCollections;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
import com.gmail.nossr50.skills.salvage.salvageables.Salvageable;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
/**
* Represents a collection of config files that serve a similar purpose
* For example, files named repair.*.yml are all loaded into memory, this lets admins keep their config files clean
*
* To be honest I'm not sure how many people make use of this system, but I'm keeping it since its been in mcMMO for like 6+ years
*/
public final class MultiConfigManager {
public static final String DEFAULT_MULTICONFIG_FILENAME_SUFFIX = ".vanilla.yml";
//Configs
public com.gmail.nossr50.config.collectionconfigs.RepairConfig vanillaRepairConfig; //This is the main config file that mcMMO will copy out
public com.gmail.nossr50.config.collectionconfigs.SalvageConfig vanillaSalvageConfig;
private static List<Repairable> repairables;
private static List<Salvageable> salvageables;
public MultiConfigManager(String fileNamePrefix)
{
//init Collections
repairables = new ArrayList<>();
salvageables = new ArrayList<>();
//init vanilla configs
vanillaRepairConfig = new com.gmail.nossr50.config.collectionconfigs.RepairConfig(getVanillaConfigName("repair"));
vanillaSalvageConfig = new com.gmail.nossr50.config.collectionconfigs.SalvageConfig(getVanillaConfigName("salvage"));
//add valid vanilla collections to main collection
repairables.addAll(vanillaRepairConfig.getLoadedCollection());
salvageables.addAll(vanillaSalvageConfig.getLoadedCollection());
//add valid custom collections to main collection
loadCustomCollections("repair", repairables, com.gmail.nossr50.config.collectionconfigs.RepairConfig.class);
loadCustomCollections("salvage", salvageables, com.gmail.nossr50.config.collectionconfigs.SalvageConfig.class);
}
/**
* mcMMO allows collection config files to be named things like repair.whatevernameyouwanthere.yml and so on,
* these files are treated in the same way as the vanilla file. They serve the purpose of organization
* @param configPrefix the prefix of the file name, for example "repair", "salvage", etc
* @param collection the collection that will be added to
*/
public void loadCustomCollections(String configPrefix, Collection<?> collection, Class<? extends ConfigCollection> configClass)
{
String vanillaConfigFileName = getVanillaConfigName(configPrefix);
//Find other files
Pattern pattern = Pattern.compile(configPrefix+"\\.(?:.+)\\.yml");
//File dataFolder = McmmoCore.getDataFolderPath();
File dataFolder = mcMMO.p.getDataFolder();
for (String fileName : dataFolder.list()) {
//Vanilla Config is already loaded
if(fileName.equalsIgnoreCase(vanillaConfigFileName))
continue;
//Find files that match the pattern
if (!pattern.matcher(fileName).matches()) {
continue;
}
//Init file
File currentFile = new File(dataFolder, fileName);
//Make sure its not a directory (needed?)
if(currentFile.isDirectory())
continue;
try {
//TODO: Valid?
ConfigCollections customConfig = (ConfigCollections) getConfigClass(configPrefix).getConstructor(String.class).newInstance(configPrefix);
collection.addAll(customConfig.getLoadedCollection());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
private String getVanillaConfigName(String configPrefix)
{
return configPrefix+DEFAULT_MULTICONFIG_FILENAME_SUFFIX;
}
private Class getConfigClass(String configPrefix)
{
switch(configPrefix) {
case "repair":
return RepairConfig.class;
case "salvage":
return SalvageConfig.class;
default:
mcMMO.p.getLogger().severe("[DEBUG] Config Class is null!");
return null;
}
}
}

View File

@ -1,181 +0,0 @@
package com.gmail.nossr50.config.collectionconfigs;
import com.gmail.nossr50.config.ConfigCollection;
import com.gmail.nossr50.config.ConfigConstants;
import com.gmail.nossr50.datatypes.skills.ItemMaterialCategory;
import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.mcMMO;
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.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
@ConfigSerializable
public class SalvageConfig extends ConfigCollection {
public static final String SALVAGEABLES = "Salvageables";
public static final String MATERIAL_TYPE = "ItemMaterialCategory";
public static final String SALVAGE_MATERIAL = "SalvageMaterial";
public static final String MAXIMUM_DURABILITY = "MaximumDurability";
public static final String ITEM_TYPE = "ItemType";
public static final String METADATA = "Metadata";
public static final String MINIMUM_LEVEL = "MinimumLevel";
public static final String XP_MULTIPLIER = "XpMultiplier";
public static final String MAXIMUM_QUANTITY = "MaximumQuantity";
public SalvageConfig() {
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
super("salvage", mcMMO.p.getDataFolder().getAbsoluteFile(), ConfigConstants.RELATIVE_PATH_SKILLS_DIR, true, false, true, false);
register();
}
/**
* The version of this config
*
* @return
*/
@Override
public double getConfigVersion() {
return 1;
}
@Override
public void register() {
//Grab the "keys" under the Repairables node
ArrayList<ConfigurationNode> salvageChildrenNodes = new ArrayList<>(getChildren(SALVAGEABLES));
for (ConfigurationNode salvageChildNode : salvageChildrenNodes) {
// Validate all the things!
List<String> errorMessages = new ArrayList<String>();
// ItemStack Material
String salvageChildNodeName = salvageChildNode.getString();
Material itemMaterial = Material.matchMaterial(salvageChildNodeName);
if (itemMaterial == null) {
errorMessages.add("Salvage Config: Invalid material - " + salvageChildNodeName);
continue;
}
// Salvage Material Type
ItemMaterialCategory salvageItemMaterialCategory = ItemMaterialCategory.OTHER;
String salvageMaterialTypeString;
if(hasNode(SALVAGEABLES, salvageChildNodeName, MATERIAL_TYPE))
salvageMaterialTypeString = getStringValue(SALVAGEABLES, salvageChildNodeName, MATERIAL_TYPE);
else
salvageMaterialTypeString = "OTHER";
if (!hasNode(SALVAGEABLES, salvageChildNodeName, MATERIAL_TYPE)) {
ItemStack salvageItem = new ItemStack(itemMaterial);
if (ItemUtils.isWoodTool(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.WOOD;
} else if (ItemUtils.isStoneTool(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.STONE;
} else if (ItemUtils.isStringTool(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.STRING;
} else if (ItemUtils.isLeatherArmor(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.LEATHER;
} else if (ItemUtils.isIronArmor(salvageItem) || ItemUtils.isIronTool(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.IRON;
} else if (ItemUtils.isGoldArmor(salvageItem) || ItemUtils.isGoldTool(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.GOLD;
} else if (ItemUtils.isDiamondArmor(salvageItem) || ItemUtils.isDiamondTool(salvageItem)) {
salvageItemMaterialCategory = ItemMaterialCategory.DIAMOND;
}
} else {
try {
salvageItemMaterialCategory = ItemMaterialCategory.valueOf(salvageMaterialTypeString.replace(" ", "_").toUpperCase());
} catch (IllegalArgumentException ex) {
errorMessages.add("Salvage Config: " + salvageChildNodeName + " has an invalid ItemMaterialCategory of " + salvageMaterialTypeString);
}
}
// Salvage Material
String salvageMaterialName = getStringValue(SALVAGEABLES, salvageChildNodeName, SALVAGE_MATERIAL);
Material salvageMaterial = (salvageMaterialName == null ? salvageItemMaterialCategory.getDefaultMaterial() : Material.matchMaterial(salvageMaterialName));
if (salvageMaterial == null) {
errorMessages.add(salvageChildNodeName + " has an invalid salvage material: " + salvageMaterialName);
continue;
}
// Maximum Durability
short maximumDurability = itemMaterial.getMaxDurability();
// ItemStack Type
ItemType salvageItemType = ItemType.OTHER;
String salvageItemTypeString;
if(hasNode(SALVAGEABLES, salvageChildNodeName, ITEM_TYPE))
salvageItemTypeString = getStringValue(SALVAGEABLES, salvageChildNodeName, ITEM_TYPE);
else
salvageItemTypeString = "OTHER";
if (!hasNode(SALVAGEABLES, salvageChildNodeName, ITEM_TYPE)) {
ItemStack salvageItem = new ItemStack(itemMaterial);
if (ItemUtils.isMinecraftTool(salvageItem)) {
salvageItemType = ItemType.TOOL;
} else if (ItemUtils.isArmor(salvageItem)) {
salvageItemType = ItemType.ARMOR;
}
} else {
try {
salvageItemType = ItemType.valueOf(salvageItemTypeString.replace(" ", "_").toUpperCase());
} catch (IllegalArgumentException ex) {
errorMessages.add("Salvage Config: " + salvageChildNodeName + " has an invalid " + ITEM_TYPE + " of " + salvageItemTypeString);
}
}
byte salvageMetadata = -1;
if(hasNode(SALVAGEABLES, salvageChildNodeName, SALVAGE_MATERIAL, METADATA))
salvageMetadata = (byte) getIntValue(SALVAGEABLES, salvageChildNodeName, SALVAGE_MATERIAL, METADATA);
int minimumLevel = getIntValue(SALVAGEABLES, salvageChildNodeName, MINIMUM_LEVEL);
double xpMultiplier = 1;
if(hasNode(SALVAGEABLES, salvageChildNodeName, XP_MULTIPLIER))
xpMultiplier = getDoubleValue(SALVAGEABLES, salvageChildNodeName, XP_MULTIPLIER);
// Maximum Quantity
int maximumQuantity = SkillUtils.getRepairAndSalvageQuantities(new ItemStack(itemMaterial), salvageMaterial, salvageMetadata);
if(hasNode(SALVAGEABLES, salvageChildNodeName, MAXIMUM_QUANTITY))
maximumQuantity = getIntValue(SALVAGEABLES, salvageChildNodeName, MAXIMUM_QUANTITY);
/*
* VALIDATE
*/
if(minimumLevel < 0)
minimumLevel = 0;
if(maximumQuantity < 0)
maximumQuantity = 1;
if(xpMultiplier < 0)
xpMultiplier = 0;
Salvageable salvageable = SalvageableFactory.getSalvageable(itemMaterial, salvageMaterial, salvageMetadata, minimumLevel, maximumQuantity, maximumDurability, salvageItemType, salvageItemMaterialCategory, xpMultiplier);
genericCollection.add(salvageable);
for (String issue : errorMessages) {
mcMMO.p.getLogger().warning(issue);
}
}
}
}