mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-07-22 07:14:44 +02:00
new config pt 10 - All configs are now managed by ConfigManager (WIP)
This commit is contained in:
@ -0,0 +1,166 @@
|
||||
package com.gmail.nossr50.config.collectionconfigs;
|
||||
|
||||
import com.gmail.nossr50.config.*;
|
||||
import com.gmail.nossr50.config.mods.ArmorConfigManager;
|
||||
import com.gmail.nossr50.config.mods.BlockConfigManager;
|
||||
import com.gmail.nossr50.config.mods.EntityConfigManager;
|
||||
import com.gmail.nossr50.config.mods.ToolConfigManager;
|
||||
import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
|
||||
import com.gmail.nossr50.config.treasure.TreasureConfig;
|
||||
import com.gmail.nossr50.skills.child.ChildConfig;
|
||||
import com.gmail.nossr50.skills.repair.repairables.Repairable;
|
||||
import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager;
|
||||
import com.gmail.nossr50.skills.salvage.salvageables.Salvageable;
|
||||
import com.gmail.nossr50.skills.salvage.salvageables.SimpleSalvageableManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Config Manager handles initializing, loading, and unloading registers for all configs that mcMMO uses
|
||||
* This makes sure that mcMMO properly loads and unloads its values on reload
|
||||
*
|
||||
* Config Manager also holds all of our MultiConfigContainers
|
||||
*
|
||||
* MultiConfigContainers
|
||||
* Represents a collection of config files that serve a similar purpose
|
||||
* As an example, with Repair you can have an unlimited number of files named repair.*.yml and each one will be treated the same and have its collections registered
|
||||
* The master file is always named x.vanilla.yml, for example "repair.vanilla.yml"
|
||||
* 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 ConfigManager {
|
||||
|
||||
/* UNLOAD REGISTER */
|
||||
|
||||
private ArrayList<Unload> unloadables;
|
||||
|
||||
/* MULTI CONFIG INSTANCES */
|
||||
|
||||
private MultiConfigContainer<Repairable> repairableMultiConfigContainer;
|
||||
private MultiConfigContainer<Salvageable> salvageableMultiConfigContainer;
|
||||
|
||||
/* COLLECTION MANAGERS */
|
||||
|
||||
private SimpleRepairableManager simpleRepairableManager;
|
||||
private SimpleSalvageableManager simpleSalvageableManager;
|
||||
|
||||
/* CONFIG INSTANCES */
|
||||
|
||||
private TreasureConfig treasureConfig;
|
||||
private AdvancedConfig advancedConfig;
|
||||
private PotionConfig potionConfig;
|
||||
private CoreSkillsConfig coreSkillsConfig;
|
||||
private SoundConfig soundConfig;
|
||||
private RankConfig rankConfig;
|
||||
|
||||
public ConfigManager()
|
||||
{
|
||||
unloadables = new ArrayList<>();
|
||||
|
||||
// Load Config Files
|
||||
// I'm pretty these are supposed to be done in a specific order, so don't rearrange them willy nilly
|
||||
treasureConfig = new TreasureConfig();
|
||||
unloadables.add(treasureConfig);
|
||||
|
||||
advancedConfig = new AdvancedConfig();
|
||||
unloadables.add(advancedConfig);
|
||||
|
||||
potionConfig = new PotionConfig();
|
||||
unloadables.add(potionConfig);
|
||||
|
||||
coreSkillsConfig = new CoreSkillsConfig();
|
||||
unloadables.add(coreSkillsConfig);
|
||||
|
||||
soundConfig = new SoundConfig();
|
||||
unloadables.add(soundConfig);
|
||||
|
||||
rankConfig = new RankConfig();
|
||||
unloadables.add(rankConfig);
|
||||
|
||||
//TODO: This config serves no purpose so its getting removed
|
||||
new ChildConfig();
|
||||
|
||||
if (MainConfig.getInstance().getToolModsEnabled()) {
|
||||
new ToolConfigManager();
|
||||
}
|
||||
|
||||
if (MainConfig.getInstance().getArmorModsEnabled()) {
|
||||
new ArmorConfigManager();
|
||||
}
|
||||
|
||||
if (MainConfig.getInstance().getBlockModsEnabled()) {
|
||||
new BlockConfigManager();
|
||||
}
|
||||
|
||||
if (MainConfig.getInstance().getEntityModsEnabled()) {
|
||||
new EntityConfigManager();
|
||||
}
|
||||
|
||||
// Multi Config Containers
|
||||
initMultiConfigContainers();
|
||||
|
||||
// Register Managers
|
||||
initCollectionManagers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes all of our Multi Config Containers
|
||||
*/
|
||||
private void initMultiConfigContainers()
|
||||
{
|
||||
//Repair
|
||||
repairableMultiConfigContainer = new MultiConfigContainer<>("repair", CollectionClassType.REPAIR);
|
||||
unloadables.add(repairableMultiConfigContainer);
|
||||
|
||||
//Salvage
|
||||
salvageableMultiConfigContainer = new MultiConfigContainer<>("salvage", CollectionClassType.SALVAGE);
|
||||
unloadables.add(salvageableMultiConfigContainer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes any managers related to config collections
|
||||
*/
|
||||
private void initCollectionManagers()
|
||||
{
|
||||
// Handles registration of repairables
|
||||
simpleRepairableManager = new SimpleRepairableManager(getRepairables());
|
||||
unloadables.add(simpleRepairableManager);
|
||||
|
||||
// Handles registration of salvageables
|
||||
simpleSalvageableManager = new SimpleSalvageableManager(getSalvageables());
|
||||
unloadables.add(simpleSalvageableManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all loaded repairables (loaded from all repairable configs)
|
||||
* @return the currently loaded repairables
|
||||
*/
|
||||
public ArrayList<Repairable> getRepairables()
|
||||
{
|
||||
return (ArrayList<Repairable>) repairableMultiConfigContainer.getCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all loaded salvageables (loaded from all salvageable configs)
|
||||
* @return the currently loaded salvageables
|
||||
*/
|
||||
public ArrayList<Salvageable> getSalvageables()
|
||||
{
|
||||
return (ArrayList<Salvageable>) salvageableMultiConfigContainer.getCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unloads all config options (prepares for reload)
|
||||
*/
|
||||
public void unloadAllConfigsAndRegisters()
|
||||
{
|
||||
//Unload
|
||||
for(Unload unloadable : unloadables)
|
||||
{
|
||||
unloadable.unload();
|
||||
}
|
||||
|
||||
//Clear
|
||||
unloadables.clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,155 @@
|
||||
package com.gmail.nossr50.config.collectionconfigs;
|
||||
|
||||
import com.gmail.nossr50.config.ConfigCollection;
|
||||
import com.gmail.nossr50.config.Unload;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Represents a type of config collection, these config collections are spread across multiple config files potentially
|
||||
* @param <T>
|
||||
*/
|
||||
public class MultiConfigContainer<T> implements Unload {
|
||||
|
||||
/* CONSTANTS */
|
||||
public static final String DEFAULT_MULTICONFIG_FILENAME_SUFFIX = ".vanilla.yml";
|
||||
|
||||
/* VARS */
|
||||
private final String configPrefix;
|
||||
private Collection<T> collection;
|
||||
public final CollectionClassType collectionClassType;
|
||||
private ConfigCollection vanillaConfig;
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
collection.clear();
|
||||
vanillaConfig.unload();
|
||||
}
|
||||
|
||||
public MultiConfigContainer(String configPrefix, CollectionClassType collectionClassType)
|
||||
{
|
||||
//Define Config Class
|
||||
this.collectionClassType = collectionClassType;
|
||||
|
||||
//Define Config Filename Prefix
|
||||
this.configPrefix = configPrefix;
|
||||
|
||||
//Init Collection
|
||||
collection = new ArrayList<T>();
|
||||
|
||||
//Load Configs
|
||||
|
||||
//Vanilla Config
|
||||
initConfigAndAddCollection(getVanillaConfigName(configPrefix));
|
||||
|
||||
//Custom Configs
|
||||
loadCustomCollections(configPrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add another collection to this collection
|
||||
* @param otherCollection
|
||||
*/
|
||||
private void addCollection(Collection<T> otherCollection)
|
||||
{
|
||||
collection.addAll(otherCollection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the Class to instance for this config collection
|
||||
* @param collectionClassType the type of class
|
||||
* @return the class to instance for this config collection
|
||||
*/
|
||||
private Class getConfigClass(CollectionClassType collectionClassType)
|
||||
{
|
||||
switch(collectionClassType) {
|
||||
case REPAIR:
|
||||
return RepairConfig.class;
|
||||
case SALVAGE:
|
||||
return SalvageConfig.class;
|
||||
default:
|
||||
mcMMO.p.getLogger().severe("[DEBUG] Config Class type is undefined!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the vanilla config which is always present
|
||||
* @param configPrefix the prefix to the filename, for example "repair" or "salvage"
|
||||
* @return the name of the vanilla config file for this collection
|
||||
*/
|
||||
private String getVanillaConfigName(String configPrefix)
|
||||
{
|
||||
return configPrefix+DEFAULT_MULTICONFIG_FILENAME_SUFFIX;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a config and attempts to load add its collection
|
||||
* @param configFileName the filename of the config to load
|
||||
*/
|
||||
private void initConfigAndAddCollection(String configFileName)
|
||||
{
|
||||
mcMMO.p.getLogger().info("Reading from collection config - "+configFileName);
|
||||
ConfigCollection configCollection = null;
|
||||
|
||||
try {
|
||||
configCollection = (ConfigCollection) getConfigClass(collectionClassType).getConstructor(String.class).newInstance(configFileName);
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//Add the collection loaded from this config
|
||||
addCollection(configCollection.getLoadedCollection());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public void loadCustomCollections(String configPrefix)
|
||||
{
|
||||
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;
|
||||
|
||||
//Load and add the collections
|
||||
initConfigAndAddCollection(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<T> getCollection() {
|
||||
return collection;
|
||||
}
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
package com.gmail.nossr50.config.collectionconfigs;
|
||||
|
||||
import com.gmail.nossr50.config.ConfigCollections;
|
||||
import com.gmail.nossr50.config.ConfigCollection;
|
||||
import com.gmail.nossr50.datatypes.skills.MaterialType;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.skills.repair.repairables.Repairable;
|
||||
import com.gmail.nossr50.skills.repair.repairables.RepairableFactory;
|
||||
import com.gmail.nossr50.util.ItemUtils;
|
||||
@ -12,28 +13,16 @@ import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This config
|
||||
*/
|
||||
public class RepairConfig extends ConfigCollections {
|
||||
private List<Repairable> repairables;
|
||||
|
||||
public class RepairConfig extends ConfigCollection {
|
||||
public RepairConfig(String fileName) {
|
||||
super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
repairables = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getLoadedCollection() {
|
||||
return repairables == null ? new ArrayList<Repairable>() : repairables;
|
||||
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
|
||||
super(mcMMO.p.getDataFolder().getAbsoluteFile(), fileName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,9 +36,7 @@ public class RepairConfig extends ConfigCollections {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadKeys() {
|
||||
repairables = new ArrayList<Repairable>();
|
||||
|
||||
public void register() {
|
||||
ConfigurationNode repairablesNode = getUserRootNode().getNode("Repairables");
|
||||
List<? extends ConfigurationNode> repairablesNodeChildrenList = repairablesNode.getChildrenList();
|
||||
Iterator<? extends ConfigurationNode> configIter = repairablesNodeChildrenList.iterator();
|
||||
@ -164,7 +151,7 @@ public class RepairConfig extends ConfigCollections {
|
||||
|
||||
if (noErrorsInRepairable(reason)) {
|
||||
Repairable repairable = RepairableFactory.getRepairable(itemType, repairMaterial, repairMetadata, minimumLevel, minimumQuantity, maximumDurability, repairConfigItemCategory, repairMaterialType, xpMultiplier);
|
||||
repairables.add(repairable);
|
||||
genericCollection.add(repairable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.gmail.nossr50.config.collectionconfigs;
|
||||
|
||||
import com.gmail.nossr50.config.ConfigCollections;
|
||||
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.salvage.salvageables.Salvageable;
|
||||
import com.gmail.nossr50.skills.salvage.salvageables.SalvageableFactory;
|
||||
import com.gmail.nossr50.util.ItemUtils;
|
||||
@ -12,25 +13,14 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class SalvageConfig extends ConfigCollections {
|
||||
private List<Salvageable> salvageables;
|
||||
public class SalvageConfig extends ConfigCollection {
|
||||
|
||||
public SalvageConfig(String fileName) {
|
||||
super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getLoadedCollection() {
|
||||
return salvageables == null ? new ArrayList<Salvageable>() : salvageables;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
salvageables = null;
|
||||
//super(McmmoCore.getDataFolderPath().getAbsoluteFile(), fileName, false);
|
||||
super(mcMMO.p.getDataFolder().getAbsoluteFile(), fileName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,9 +34,7 @@ public class SalvageConfig extends ConfigCollections {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadKeys() {
|
||||
salvageables = new ArrayList<Salvageable>();
|
||||
|
||||
public void register() {
|
||||
ConfigurationSection section = config.getConfigurationSection("Salvageables");
|
||||
Set<String> keys = section.getKeys(false);
|
||||
|
||||
@ -149,7 +137,7 @@ public class SalvageConfig extends ConfigCollections {
|
||||
|
||||
if (noErrorsInSalvageable(reason)) {
|
||||
Salvageable salvageable = SalvageableFactory.getSalvageable(itemMaterial, salvageMaterial, salvageMetadata, minimumLevel, maximumQuantity, maximumDurability, salvageItemType, salvageMaterialType, xpMultiplier);
|
||||
salvageables.add(salvageable);
|
||||
genericCollection.add(salvageable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user