diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java index e8951d20..2026f123 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java @@ -6,6 +6,7 @@ import com.willfp.eco.util.command.AbstractCommand; import com.willfp.eco.util.drops.telekinesis.TelekinesisTests; import com.willfp.eco.util.integrations.IntegrationLoader; import com.willfp.eco.util.interfaces.EcoRunnable; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.ecoenchants.command.commands.CommandEcodebug; @@ -118,16 +119,7 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin { * Code executed on /ecoreload. */ @Override - public void reload() { - EcoEnchantsConfigs.updateConfigs(); - EnchantmentCache.update(); - EnchantmentRarity.update(); - EnchantmentTarget.update(); - EcoEnchants.update(); - EnchantDisplay.update(); - TabCompleterEnchantinfo.reload(); - EnchantmentType.update(); - + public void onReload() { EcoEnchants.values().forEach((ecoEnchant -> { HandlerList.unregisterAll(ecoEnchant); @@ -209,4 +201,18 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin { new VillagerListeners() ); } + + @Override + public List> getUpdatableClasses() { + return Arrays.asList( + EcoEnchantsConfigs.class, + EnchantmentCache.class, + EnchantmentRarity.class, + EnchantmentTarget.class, + EcoEnchants.class, + EnchantDisplay.class, + TabCompleterEnchantinfo.class, + EnchantmentType.class + ); + } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterEnchantinfo.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterEnchantinfo.java index d633d39c..5d8301da 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterEnchantinfo.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/tabcompleters/TabCompleterEnchantinfo.java @@ -3,6 +3,8 @@ package com.willfp.ecoenchants.command.tabcompleters; import com.willfp.eco.util.StringUtils; import com.willfp.eco.util.command.AbstractCommand; import com.willfp.eco.util.command.AbstractTabCompleter; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import org.bukkit.Bukkit; @@ -16,13 +18,14 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -public class TabCompleterEnchantinfo extends AbstractTabCompleter { +public class TabCompleterEnchantinfo extends AbstractTabCompleter implements Updatable { private static final List enchantsNames = EcoEnchants.values().stream().filter(EcoEnchant::isEnabled).map(EcoEnchant::getName).collect(Collectors.toList()); public TabCompleterEnchantinfo() { super((AbstractCommand) Objects.requireNonNull(Bukkit.getPluginCommand("enchantinfo")).getExecutor()); } + @ConfigUpdater public static void reload() { enchantsNames.clear(); enchantsNames.addAll(EcoEnchants.values().stream().filter(EcoEnchant::isEnabled).map(EcoEnchant::getName).collect(Collectors.toList())); @@ -32,7 +35,7 @@ public class TabCompleterEnchantinfo extends AbstractTabCompleter { public List onTab(@NotNull CommandSender sender, @NotNull List args) { List completions = new ArrayList<>(); - if (args.size() == 0) { + if (args.isEmpty()) { // Currently, this case is not ever reached return enchantsNames; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/EcoEnchantsConfigs.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/EcoEnchantsConfigs.java index e5e1c3aa..ada3f385 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/EcoEnchantsConfigs.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/EcoEnchantsConfigs.java @@ -1,79 +1,62 @@ package com.willfp.ecoenchants.config; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.ecoenchants.config.configs.EnchantmentConfig; import com.willfp.ecoenchants.config.configs.Rarity; import com.willfp.ecoenchants.config.configs.Target; +import lombok.Getter; +import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; -public class EcoEnchantsConfigs { - private static final Target TARGET = new Target(); - private static final Rarity RARITY = new Rarity(); - private static final Set enchantmentConfigs = new HashSet<>(); +@UtilityClass +public class EcoEnchantsConfigs implements Updatable { + /** + * target.yml. + */ + public final Target TARGET = new Target(); /** - * Update all configs - * Called on /ecoreload + * rarity.yml. */ - public static void updateConfigs() { + public final Rarity RARITY = new Rarity(); + + /** + * All enchantment-specific configs. + */ + @Getter + private final Set enchantmentConfigs = new HashSet<>(); + + /** + * Update all configs. + */ + @ConfigUpdater + public void updateConfigs() { TARGET.update(); RARITY.update(); - updateEnchantmentConfigs(); - } - - /** - * Update enchantment configs - */ - public static void updateEnchantmentConfigs() { enchantmentConfigs.forEach((EnchantmentYamlConfig::update)); } /** - * Get all enchantment configs + * Get EnchantmentConfig matching permission name. * - * @return Set of all enchantment configs - */ - public static Set getEnchantmentConfigs() { - return enchantmentConfigs; - } - - /** - * Get EnchantmentConfig matching permission name - * - * @param permissionName The permission name to match - * - * @return The matching {@link EnchantmentConfig} + * @param permissionName The permission name to match. + * @return The matching {@link EnchantmentConfig}. */ @SuppressWarnings("OptionalGetWithoutIsPresent") - public static EnchantmentConfig getEnchantmentConfig(String permissionName) { + public EnchantmentConfig getEnchantmentConfig(@NotNull final String permissionName) { return enchantmentConfigs.stream().filter(config -> config.getName().equalsIgnoreCase(permissionName)).findFirst().get(); } /** - * Adds new enchantment config yml + * Adds new enchantment config yml. * - * @param config The config to add + * @param config The config to add. */ - public static void addEnchantmentConfig(EnchantmentConfig config) { + public void addEnchantmentConfig(@NotNull final EnchantmentConfig config) { enchantmentConfigs.add(config); } - - /** - * Get target.yml - * - * @return target.yml - */ - public static Target getTarget() { - return TARGET; - } - - /** - * Get rarity.yml - * - * @return rarity.yml - */ - public static Rarity getRarity() { - return RARITY; - } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java index 0fa61133..06f30cd4 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -2,6 +2,8 @@ package com.willfp.ecoenchants.display; import com.google.common.collect.Lists; import com.willfp.eco.util.NumberUtils; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.ecoenchants.display.options.DisplayOptions; import com.willfp.ecoenchants.enchantments.EcoEnchant; @@ -24,7 +26,7 @@ import java.util.List; * All methods and fields pertaining to showing players the enchantments on their items. */ @SuppressWarnings("DeprecatedIsStillUsed") -public class EnchantDisplay { +public class EnchantDisplay implements Updatable { private static final AbstractEcoPlugin plugin = AbstractEcoPlugin.getInstance(); /** @@ -58,6 +60,7 @@ public class EnchantDisplay { /** * Update config values */ + @ConfigUpdater public static void update() { OPTIONS.update(); EnchantmentCache.update(); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java index 77a917a5..27174596 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantmentCache.java @@ -1,6 +1,8 @@ package com.willfp.ecoenchants.display; import com.willfp.eco.util.config.Configs; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; @@ -15,8 +17,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; -@SuppressWarnings("deprecation") -public class EnchantmentCache { +public class EnchantmentCache implements Updatable { private static final Set CACHE = new HashSet<>(); @SuppressWarnings("OptionalGetWithoutIsPresent") @@ -29,6 +30,7 @@ public class EnchantmentCache { return new HashSet<>(CACHE); } + @ConfigUpdater public static void update() { CACHE.clear(); Arrays.asList(Enchantment.values()).parallelStream().forEach(enchantment -> { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java index 2bbc323f..860fb531 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java @@ -4,6 +4,8 @@ package com.willfp.ecoenchants.enchantments; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableList; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.ecoenchants.enchantments.ecoenchants.artifact.AshArtifact; import com.willfp.ecoenchants.enchantments.ecoenchants.artifact.CloudsArtifact; import com.willfp.ecoenchants.enchantments.ecoenchants.artifact.CrimsonArtifact; @@ -239,7 +241,7 @@ import java.util.concurrent.atomic.AtomicBoolean; * Contains general methods for EcoEnchants */ @SuppressWarnings("unused") -public class EcoEnchants { +public class EcoEnchants implements Updatable { public static final String CONFIG_LOCATION = "config."; public static final String OBTAINING_LOCATION = "obtaining."; public static final String GENERAL_LOCATION = "general-config."; @@ -555,6 +557,7 @@ public class EcoEnchants { * Update all {@link EcoEnchant}s * Called on /ecoreload */ + @ConfigUpdater public static void update() { for (EcoEnchant ecoEnchant : new HashSet<>(values())) { ecoEnchant.update(); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentRarity.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentRarity.java index 909907c1..22965420 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentRarity.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentRarity.java @@ -2,9 +2,11 @@ package com.willfp.ecoenchants.enchantments.meta; import com.willfp.eco.util.NumberUtils; import com.willfp.eco.util.StringUtils; +import com.willfp.eco.util.config.annotations.ConfigUpdater; import com.willfp.eco.util.integrations.placeholder.PlaceholderEntry; import com.willfp.eco.util.integrations.placeholder.PlaceholderManager; import com.willfp.eco.util.interfaces.Registerable; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.ecoenchants.config.EcoEnchantsConfigs; import java.util.HashSet; @@ -14,7 +16,7 @@ import java.util.Set; /** * Class for storing all enchantment rarities */ -public class EnchantmentRarity implements Registerable { +public class EnchantmentRarity implements Registerable, Updatable { private static final Set rarities = new HashSet<>(); private final String name; @@ -151,17 +153,17 @@ public class EnchantmentRarity implements Registerable { * Update all rarities * Called on /ecoreload */ - + @ConfigUpdater public static void update() { - Set raritiesNames = EcoEnchantsConfigs.getRarity().getRarities(); + Set raritiesNames = EcoEnchantsConfigs.RARITY.getRarities(); raritiesNames.forEach((rarity) -> { - double probability = EcoEnchantsConfigs.getRarity().getDouble("rarities." + rarity + ".table-probability"); - int minimumLevel = EcoEnchantsConfigs.getRarity().getInt("rarities." + rarity + ".minimum-level"); - double villagerProbability = EcoEnchantsConfigs.getRarity().getDouble("rarities." + rarity + ".villager-probability"); - double lootProbability = EcoEnchantsConfigs.getRarity().getDouble("rarities." + rarity + ".loot-probability"); + double probability = EcoEnchantsConfigs.RARITY.getDouble("rarities." + rarity + ".table-probability"); + int minimumLevel = EcoEnchantsConfigs.RARITY.getInt("rarities." + rarity + ".minimum-level"); + double villagerProbability = EcoEnchantsConfigs.RARITY.getDouble("rarities." + rarity + ".villager-probability"); + double lootProbability = EcoEnchantsConfigs.RARITY.getDouble("rarities." + rarity + ".loot-probability"); String customColor = null; - if (EcoEnchantsConfigs.getRarity().getBool("rarities." + rarity + ".custom-color.enabled")) { - customColor = StringUtils.translate(EcoEnchantsConfigs.getRarity().getString("rarities." + rarity + ".custom-color.color")); + if (EcoEnchantsConfigs.RARITY.getBool("rarities." + rarity + ".custom-color.enabled")) { + customColor = StringUtils.translate(EcoEnchantsConfigs.RARITY.getString("rarities." + rarity + ".custom-color.color")); } new EnchantmentRarity(rarity, probability, minimumLevel, villagerProbability, lootProbability, customColor).register(); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentTarget.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentTarget.java index fef17d63..2809b30c 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentTarget.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentTarget.java @@ -1,7 +1,9 @@ package com.willfp.ecoenchants.enchantments.meta; import com.google.common.collect.ImmutableSet; +import com.willfp.eco.util.config.annotations.ConfigUpdater; import com.willfp.eco.util.interfaces.Registerable; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.ecoenchants.config.EcoEnchantsConfigs; import org.bukkit.Material; @@ -13,7 +15,7 @@ import java.util.Set; /** * Class for storing all enchantment rarities */ -public class EnchantmentTarget implements Registerable { +public class EnchantmentTarget implements Registerable, Updatable { private static final Set targets = new HashSet<>(); public static final EnchantmentTarget ALL = new EnchantmentTarget("all", new HashSet<>()); @@ -79,11 +81,12 @@ public class EnchantmentTarget implements Registerable { * Update all targets * Called on /ecoreload */ + @ConfigUpdater public static void update() { - Set targetNames = EcoEnchantsConfigs.getTarget().getTargets(); + Set targetNames = EcoEnchantsConfigs.TARGET.getTargets(); ALL.materials.clear(); targetNames.forEach((name) -> { - Set materials = EcoEnchantsConfigs.getTarget().getTargetMaterials(name); + Set materials = EcoEnchantsConfigs.TARGET.getTargetMaterials(name); new EnchantmentTarget(name, materials).register(); }); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentType.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentType.java index 217604a0..afd4ef76 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentType.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/meta/EnchantmentType.java @@ -1,6 +1,8 @@ package com.willfp.ecoenchants.enchantments.meta; import com.willfp.eco.util.config.Configs; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.eco.util.lambda.ObjectCallable; import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.itemtypes.Artifact; @@ -9,7 +11,7 @@ import com.willfp.ecoenchants.enchantments.itemtypes.Spell; import java.util.ArrayList; import java.util.List; -public class EnchantmentType { +public class EnchantmentType implements Updatable { private static final List values = new ArrayList<>(); public static final EnchantmentType NORMAL = new EnchantmentType("normal", false, () -> Configs.LANG.getString("not-curse-color")); @@ -115,6 +117,7 @@ public class EnchantmentType { return requiredToExtend; } + @ConfigUpdater public static void update() { values.forEach(EnchantmentType::refresh); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/config/ConfigHandler.java b/eco-util/src/main/java/com/willfp/eco/util/config/ConfigHandler.java new file mode 100644 index 00000000..49a8689a --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/config/ConfigHandler.java @@ -0,0 +1,75 @@ +package com.willfp.eco.util.config; + +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.config.annotations.InvalidUpdatableClassException; +import com.willfp.eco.util.config.annotations.InvalidUpdateMethodException; +import com.willfp.eco.util.injection.PluginDependent; +import com.willfp.eco.util.interfaces.Updatable; +import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class ConfigHandler extends PluginDependent { + /** + * A set of all classes that can be updated. + */ + private final Set> updatableClasses = new HashSet<>(); + + /** + * Creates a new config handler and links it to an {@link AbstractEcoPlugin}. + * + * @param plugin The plugin to manage. + */ + public ConfigHandler(@NotNull final AbstractEcoPlugin plugin) { + super(plugin); + } + + /** + * Invoke all update methods. + */ + public void callUpdate() { + updatableClasses.forEach(clazz -> Arrays.stream(clazz.getDeclaredMethods()).forEach(method -> { + if (method.isAnnotationPresent(ConfigUpdater.class)) { + if (method.getParameterTypes().length == 0) { + throw new InvalidUpdateMethodException("Update method must not have parameters."); + } + if (!Modifier.isStatic(method.getModifiers())) { + throw new InvalidUpdateMethodException("Update method must be static."); + } + + try { + method.invoke(null); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new InvalidUpdateMethodException("Update method generated an exception."); + } + } + })); + } + + /** + * Register an updatable class. + * + * @param updatableClass The class with an update method. + */ + public void registerUpdatableClass(@NotNull final Class updatableClass) { + boolean isValid = false; + for (Method method : updatableClass.getDeclaredMethods()) { + if (Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 0 && method.isAnnotationPresent(ConfigUpdater.class)) { + isValid = true; + break; + } + } + + if (!isValid) { + throw new InvalidUpdatableClassException("Registered updatable class " + updatableClass + " must have an annotated static method with no modifiers."); + } + + updatableClasses.add(updatableClass); + } +} diff --git a/eco-util/src/main/java/com/willfp/eco/util/config/Configs.java b/eco-util/src/main/java/com/willfp/eco/util/config/Configs.java index 3c52912b..6ed19530 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/config/Configs.java +++ b/eco-util/src/main/java/com/willfp/eco/util/config/Configs.java @@ -1,11 +1,13 @@ package com.willfp.eco.util.config; +import com.willfp.eco.util.config.annotations.ConfigUpdater; import com.willfp.eco.util.config.configs.Config; import com.willfp.eco.util.config.configs.Lang; +import com.willfp.eco.util.interfaces.Updatable; import lombok.experimental.UtilityClass; @UtilityClass -public final class Configs { +public final class Configs implements Updatable { /** * The {@link BaseConfig} implementation for lang.yml. */ @@ -21,6 +23,7 @@ public final class Configs { * * @see BaseConfig */ + @ConfigUpdater public void update() { LANG.update(); CONFIG.update(); diff --git a/eco-util/src/main/java/com/willfp/eco/util/config/annotations/ConfigUpdater.java b/eco-util/src/main/java/com/willfp/eco/util/config/annotations/ConfigUpdater.java new file mode 100644 index 00000000..40d64862 --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/config/annotations/ConfigUpdater.java @@ -0,0 +1,11 @@ +package com.willfp.eco.util.config.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface ConfigUpdater { +} diff --git a/eco-util/src/main/java/com/willfp/eco/util/config/annotations/InvalidUpdatableClassException.java b/eco-util/src/main/java/com/willfp/eco/util/config/annotations/InvalidUpdatableClassException.java new file mode 100644 index 00000000..e86ff815 --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/config/annotations/InvalidUpdatableClassException.java @@ -0,0 +1,14 @@ +package com.willfp.eco.util.config.annotations; + +import org.jetbrains.annotations.NotNull; + +public class InvalidUpdatableClassException extends RuntimeException { + /** + * Called when an updatable class is registered into an {@link com.willfp.eco.util.config.ConfigHandler}. + * + * @param message The error message. + */ + public InvalidUpdatableClassException(@NotNull final String message) { + super(message); + } +} diff --git a/eco-util/src/main/java/com/willfp/eco/util/config/annotations/InvalidUpdateMethodException.java b/eco-util/src/main/java/com/willfp/eco/util/config/annotations/InvalidUpdateMethodException.java new file mode 100644 index 00000000..679f5642 --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/config/annotations/InvalidUpdateMethodException.java @@ -0,0 +1,18 @@ +package com.willfp.eco.util.config.annotations; + +import org.jetbrains.annotations.NotNull; + +public class InvalidUpdateMethodException extends RuntimeException { + /** + * Throws a new invalid update method exception. + *

+ * Causes include: + * Update method with parameters. + * Update method is not static. + * + * @param message The error message to show. + */ + public InvalidUpdateMethodException(@NotNull final String message) { + super(message); + } +} diff --git a/eco-util/src/main/java/com/willfp/eco/util/drops/internal/DropManager.java b/eco-util/src/main/java/com/willfp/eco/util/drops/internal/DropManager.java index 5252b716..94d0b5b8 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/drops/internal/DropManager.java +++ b/eco-util/src/main/java/com/willfp/eco/util/drops/internal/DropManager.java @@ -1,31 +1,28 @@ package com.willfp.eco.util.drops.internal; import com.willfp.eco.util.config.Configs; +import com.willfp.eco.util.config.annotations.ConfigUpdater; +import com.willfp.eco.util.interfaces.Updatable; import lombok.Getter; +import lombok.experimental.UtilityClass; -public final class DropManager { +@UtilityClass +public final class DropManager implements Updatable { /** * The currently used type, or implementation, of {@link AbstractDropQueue}. *

* Standard by default, used if drops.collate key is not present in config. */ @Getter - private static DropQueueType type = DropQueueType.STANDARD; + private DropQueueType type = DropQueueType.STANDARD; /** * Update the type of drop queue that should be used. * * @see DropQueueType */ - public static void update() { + @ConfigUpdater + public void update() { type = Configs.CONFIG.getBool("drops.collate") ? DropQueueType.COLLATED : DropQueueType.STANDARD; } - - static { - update(); - } - - private DropManager() { - - } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/interfaces/Updatable.java b/eco-util/src/main/java/com/willfp/eco/util/interfaces/Updatable.java new file mode 100644 index 00000000..5f97e64e --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/interfaces/Updatable.java @@ -0,0 +1,4 @@ +package com.willfp.eco.util.interfaces; + +public interface Updatable { +} diff --git a/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java b/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java index 400294f0..6caf4fd2 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java +++ b/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java @@ -11,6 +11,7 @@ import com.willfp.eco.util.bukkit.scheduling.EcoScheduler; import com.willfp.eco.util.bukkit.scheduling.RunnableFactory; import com.willfp.eco.util.bukkit.scheduling.Scheduler; import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.config.ConfigHandler; import com.willfp.eco.util.config.Configs; import com.willfp.eco.util.drops.internal.DropManager; import com.willfp.eco.util.drops.internal.FastCollatedDropQueue; @@ -38,6 +39,7 @@ import com.willfp.eco.util.integrations.antigrief.plugins.AntigriefTowny; import com.willfp.eco.util.integrations.antigrief.plugins.AntigriefWorldGuard; import com.willfp.eco.util.integrations.placeholder.PlaceholderManager; import com.willfp.eco.util.integrations.placeholder.plugins.PlaceholderIntegrationPAPI; +import com.willfp.eco.util.interfaces.Updatable; import com.willfp.eco.util.optional.Prerequisite; import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.updater.UpdateChecker; @@ -87,6 +89,11 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { */ private final List integrations = new ArrayList<>(); + /** + * Set of external plugin integrations. + */ + private final List> updatableClasses = new ArrayList<>(); + /** * The internal plugin logger. */ @@ -131,6 +138,12 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { @Getter private final ExtensionLoader extensionLoader; + /** + * The handler class for updatable classes. + */ + @Getter + private final ConfigHandler configHandler; + /** * If the server is running an outdated version of the plugin. */ @@ -158,6 +171,7 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.metadataValueFactory = new MetadataValueFactory(this); this.runnableFactory = new RunnableFactory(this); this.extensionLoader = new EcoExtensionLoader(this); + this.configHandler = new ConfigHandler(this); if (!Bukkit.getServicesManager().isProvidedFor(TelekinesisTests.class)) { Bukkit.getServicesManager().register(TelekinesisTests.class, new EcoTelekinesisTests(), this, ServicePriority.Normal); @@ -232,6 +246,8 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.getScheduler().runLater(this::afterLoad, 1); + this.getUpdatableClasses().forEach(clazz -> this.getConfigHandler().registerUpdatableClass(clazz)); + this.enable(); } @@ -265,7 +281,9 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { */ public final void afterLoad() { this.getPacketAdapters().forEach(abstractPacketAdapter -> { - if (abstractPacketAdapter.isPostLoad()) abstractPacketAdapter.register(); + if (abstractPacketAdapter.isPostLoad()) { + abstractPacketAdapter.register(); + } }); if (!Prerequisite.HasPaper.isMet()) { @@ -291,9 +309,8 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { /** * Default code to be executed on plugin reload. */ - public final void onReload() { - Configs.update(); - DropManager.update(); + public final void reload() { + this.getConfigHandler().callUpdate(); this.getScheduler().cancelAll(); new FastCollatedDropQueue.CollatedRunnable(this); @@ -325,6 +342,17 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { return integrations; } + /** + * Default updatable classes that exist within internal libraries. + * + * @return The default updatable classes. + */ + public final List> getDefaultUpdatableClasses() { + updatableClasses.add(Configs.class); + updatableClasses.add(DropManager.class); + return updatableClasses; + } + /** * The plugin-specific code to be executed on enable. */ @@ -343,7 +371,7 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { /** * The plugin-specific code to be executed on reload. */ - public abstract void reload(); + public abstract void onReload(); /** * The plugin-specific code to be executed after the server is up. @@ -379,4 +407,11 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { * @return A list of all listeners. */ public abstract List getListeners(); + + /** + * All updatable classes. + * + * @return A list of all updatable classes. + */ + public abstract List> getUpdatableClasses(); }