Reworked Updatable system

This commit is contained in:
Auxilor 2020-12-29 14:00:39 +00:00
parent 1865ee6ac7
commit aa3816a21d
18 changed files with 72 additions and 140 deletions

View File

@ -87,6 +87,7 @@ clean.doLast {
shadowJar {
relocate('org.bstats.bukkit', 'com.willfp.eco.util.shaded.bstats')
relocate('org.apache.maven', 'com.willfp.eco.util.shaded.maven')
relocate('org.reflections', 'com.willfp.eco.util.shaded.reflections')
archiveFileName = findProperty("plugin-name") + " v" + findProperty("version") + ".jar"
}

View File

@ -6,25 +6,17 @@ 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;
import com.willfp.ecoenchants.command.commands.CommandEcoreload;
import com.willfp.ecoenchants.command.commands.CommandEnchantinfo;
import com.willfp.ecoenchants.command.tabcompleters.TabCompleterEnchantinfo;
import com.willfp.ecoenchants.config.EcoEnchantsConfigs;
import com.willfp.ecoenchants.display.EnchantDisplay;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.display.packets.PacketChat;
import com.willfp.ecoenchants.display.packets.PacketOpenWindowMerchant;
import com.willfp.ecoenchants.display.packets.PacketSetCreativeSlot;
import com.willfp.ecoenchants.display.packets.PacketSetSlot;
import com.willfp.ecoenchants.display.packets.PacketWindowItems;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.enchantments.support.merging.anvil.AnvilListeners;
import com.willfp.ecoenchants.enchantments.support.merging.grindstone.GrindstoneListeners;
import com.willfp.ecoenchants.enchantments.support.obtaining.EnchantingListeners;
@ -202,18 +194,4 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin {
new VillagerListeners()
);
}
@Override
public List<Class<? extends Updatable>> getUpdatableClasses() {
return Arrays.asList(
EcoEnchantsConfigs.class,
EnchantmentCache.class,
EnchantmentRarity.class,
EnchantmentTarget.class,
EcoEnchants.class,
EnchantDisplay.class,
TabCompleterEnchantinfo.class,
EnchantmentType.class
);
}
}

View File

@ -3,8 +3,7 @@ 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.eco.util.config.annotations.Updatable;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import org.bukkit.Bukkit;
@ -18,7 +17,8 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class TabCompleterEnchantinfo extends AbstractTabCompleter implements Updatable {
@Updatable
public class TabCompleterEnchantinfo extends AbstractTabCompleter {
/**
* The cached enchantment names.
*/
@ -34,8 +34,7 @@ public class TabCompleterEnchantinfo extends AbstractTabCompleter implements Upd
/**
* Called on /ecoreload.
*/
@ConfigUpdater
public static void reload() {
public static void update() {
ENCHANT_NAMES.clear();
ENCHANT_NAMES.addAll(EcoEnchants.values().stream().filter(EcoEnchant::isEnabled).map(EcoEnchant::getName).collect(Collectors.toList()));
}

View File

@ -1,7 +1,6 @@
package com.willfp.ecoenchants.config;
import com.willfp.eco.util.config.annotations.ConfigUpdater;
import com.willfp.eco.util.interfaces.Updatable;
import com.willfp.eco.util.config.annotations.Updatable;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.config.configs.Rarity;
import com.willfp.ecoenchants.config.configs.Target;
@ -12,8 +11,9 @@ import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
@Updatable(methodName = "updateConfigs")
@UtilityClass
public class EcoEnchantsConfigs implements Updatable {
public class EcoEnchantsConfigs {
/**
* target.yml.
*/
@ -33,7 +33,6 @@ public class EcoEnchantsConfigs implements Updatable {
/**
* Update all configs.
*/
@ConfigUpdater
public void updateConfigs() {
TARGET.update();
RARITY.update();

View File

@ -2,8 +2,7 @@ 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.config.annotations.Updatable;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import com.willfp.ecoenchants.display.options.DisplayOptions;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
@ -24,12 +23,9 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
/**
* All methods and fields pertaining to showing players the enchantments on their items.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Updatable
@UtilityClass
public class EnchantDisplay implements Updatable {
public class EnchantDisplay {
/**
* Instance of EcoEnchants.
*/
@ -62,7 +58,6 @@ public class EnchantDisplay implements Updatable {
/**
* Update config values.
*/
@ConfigUpdater
public static void update() {
OPTIONS.update();
EnchantmentCache.update();

View File

@ -2,8 +2,7 @@ package com.willfp.ecoenchants.display;
import com.google.common.collect.ImmutableSet;
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.config.annotations.Updatable;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
@ -22,9 +21,10 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
@UtilityClass
@SuppressWarnings("deprecation")
public class EnchantmentCache implements Updatable {
@Updatable
@UtilityClass
public class EnchantmentCache {
/**
* The physical cache.
*/
@ -64,7 +64,6 @@ public class EnchantmentCache implements Updatable {
/**
* Update the cache.
*/
@ConfigUpdater
public static void update() {
CACHE.clear();
Arrays.asList(Enchantment.values()).parallelStream().forEach(enchantment -> {

View File

@ -4,8 +4,7 @@ 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.eco.util.config.annotations.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,9 +238,10 @@ import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
@Updatable
@UtilityClass
@SuppressWarnings({"unused", "checkstyle:JavadocVariable"})
public class EcoEnchants implements Updatable {
public class EcoEnchants {
public static final String CONFIG_LOCATION = "config.";
public static final String OBTAINING_LOCATION = "obtaining.";
public static final String GENERAL_LOCATION = "general-config.";
@ -550,7 +550,6 @@ public class EcoEnchants implements Updatable {
/**
* Update all {@link EcoEnchant}s.
*/
@ConfigUpdater
public static void update() {
for (EcoEnchant ecoEnchant : new HashSet<>(values())) {
ecoEnchant.update();

View File

@ -2,11 +2,10 @@ 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.config.annotations.Updatable;
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 lombok.Getter;
import org.jetbrains.annotations.NotNull;
@ -16,7 +15,8 @@ import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
public class EnchantmentRarity implements Registerable, Updatable {
@Updatable
public class EnchantmentRarity implements Registerable {
/**
* All registered rarities.
*/
@ -134,7 +134,6 @@ public class EnchantmentRarity implements Registerable, Updatable {
/**
* Update all rarities.
*/
@ConfigUpdater
public static void update() {
Set<String> raritiesNames = EcoEnchantsConfigs.RARITY.getRarities();
raritiesNames.forEach(rarity -> {

View File

@ -1,9 +1,8 @@
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.config.annotations.Updatable;
import com.willfp.eco.util.interfaces.Registerable;
import com.willfp.eco.util.interfaces.Updatable;
import com.willfp.ecoenchants.config.EcoEnchantsConfigs;
import lombok.Getter;
import org.bukkit.Material;
@ -14,7 +13,8 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
public class EnchantmentTarget implements Registerable, Updatable {
@Updatable
public class EnchantmentTarget implements Registerable {
/**
* All registered targets.
*/
@ -77,7 +77,6 @@ public class EnchantmentTarget implements Registerable, Updatable {
/**
* Update all targets.
*/
@ConfigUpdater
public static void update() {
Set<String> targetNames = EcoEnchantsConfigs.TARGET.getTargets();
ALL.materials.clear();

View File

@ -2,8 +2,7 @@ package com.willfp.ecoenchants.enchantments.meta;
import com.google.common.collect.ImmutableList;
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.config.annotations.Updatable;
import com.willfp.eco.util.lambda.ObjectCallable;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.itemtypes.Artifact;
@ -15,7 +14,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class EnchantmentType implements Updatable {
@Updatable
public class EnchantmentType {
/**
* All registered types.
*/
@ -204,7 +204,6 @@ public class EnchantmentType implements Updatable {
/**
* Update callables of all types.
*/
@ConfigUpdater
public static void update() {
REGISTERED.forEach(EnchantmentType::refresh);
}

View File

@ -8,6 +8,7 @@ repositories {
dependencies {
implementation 'org.apache.maven:maven-artifact:3.0.3'
implementation 'org.bstats:bstats-bukkit:1.7'
implementation 'org.reflections:reflections:0.9.12'
compileOnly 'org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT'
compileOnly 'me.clip:placeholderapi:2.10.9'
compileOnly 'com.comphenix.protocol:ProtocolLib:4.6.0-SNAPSHOT'
@ -19,5 +20,5 @@ dependencies {
compileOnly 'com.github.angeschossen:LandsAPI:4.7.3'
compileOnly 'fr.neatmonster:nocheatplus:3.16.1-SNAPSHOT'
compileOnly 'com.github.jiangdashao:matrix-api-repo:317d4635fd'
compileOnly fileTree(dir: '/../lib', include: ['*.jar'])
compileOnly fileTree(dir: '../lib', include: ['*.jar'])
}

View File

@ -1,26 +1,18 @@
package com.willfp.eco.util.config;
import com.willfp.eco.util.config.annotations.ConfigUpdater;
import com.willfp.eco.util.config.annotations.Updatable;
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 org.reflections.Reflections;
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<Class<? extends Updatable>> updatableClasses = new HashSet<>();
/**
* Creates a new config handler and links it to an {@link AbstractEcoPlugin}.
*
@ -34,43 +26,35 @@ public class ConfigHandler extends PluginDependent {
* 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.");
for (Class<?> clazz : new Reflections("com.willfp").getTypesAnnotatedWith(Updatable.class)) {
boolean valid = false;
for (Method method : clazz.getDeclaredMethods()) {
if (!method.isAnnotationPresent(Updatable.class)) {
continue;
}
if (!Modifier.isStatic(method.getModifiers())) {
throw new InvalidUpdateMethodException("Update method must be static.");
continue;
}
if (method.getParameterTypes().length != 0) {
continue;
}
try {
method.invoke(null);
valid = true;
break;
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
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<? extends Updatable> 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 (!valid) {
throw new InvalidUpdatableClassException("No valid update method found! (Must be static, have no parameters)");
}
}
if (!isValid) {
throw new InvalidUpdatableClassException("Registered updatable class " + updatableClass + " must have an annotated static method with no modifiers.");
}
updatableClasses.add(updatableClass);
}
}

View File

@ -1,13 +1,13 @@
package com.willfp.eco.util.config;
import com.willfp.eco.util.config.annotations.ConfigUpdater;
import com.willfp.eco.util.config.annotations.Updatable;
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;
@Updatable
@UtilityClass
public final class Configs implements Updatable {
public final class Configs {
/**
* The {@link BaseConfig} implementation for lang.yml.
*/
@ -23,7 +23,6 @@ public final class Configs implements Updatable {
*
* @see BaseConfig
*/
@ConfigUpdater
public void update() {
LANG.update();
CONFIG.update();

View File

@ -1,11 +0,0 @@
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 {
}

View File

@ -0,0 +1,19 @@
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.TYPE)
public @interface Updatable {
/**
* The static, no-parameter method to be called on config update.
* <p>
* Default is <b>update</b>.
*
* @return The method name.
*/
String methodName() default "update";
}

View File

@ -1,13 +1,13 @@
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 com.willfp.eco.util.config.annotations.Updatable;
import lombok.Getter;
import lombok.experimental.UtilityClass;
@Updatable
@UtilityClass
public final class DropManager implements Updatable {
public final class DropManager {
/**
* The currently used type, or implementation, of {@link AbstractDropQueue}.
* <p>
@ -21,7 +21,6 @@ public final class DropManager implements Updatable {
*
* @see DropQueueType
*/
@ConfigUpdater
public void update() {
type = Configs.CONFIG.getBool("drops.collate") ? DropQueueType.COLLATED : DropQueueType.STANDARD;
}

View File

@ -1,4 +0,0 @@
package com.willfp.eco.util.interfaces;
public interface Updatable {
}

View File

@ -12,8 +12,6 @@ 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;
import com.willfp.eco.util.drops.telekinesis.EcoTelekinesisTests;
import com.willfp.eco.util.drops.telekinesis.TelekinesisTests;
@ -39,7 +37,6 @@ 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;
@ -89,11 +86,6 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
*/
private final List<IntegrationLoader> integrations = new ArrayList<>();
/**
* Set of external plugin integrations.
*/
private final List<Class<? extends Updatable>> updatableClasses = new ArrayList<>();
/**
* The internal plugin logger.
*/
@ -240,19 +232,12 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
}
});
updatableClasses.add(Configs.class);
updatableClasses.add(DropManager.class);
updatableClasses.addAll(this.getUpdatableClasses());
this.getListeners().forEach(listener -> this.getEventManager().registerListener(listener));
this.getCommands().forEach(AbstractCommand::register);
this.getScheduler().runLater(this::afterLoad, 1);
this.updatableClasses.forEach(clazz -> this.getConfigHandler().registerUpdatableClass(clazz));
this.enable();
}
@ -401,11 +386,4 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
* @return A list of all listeners.
*/
public abstract List<Listener> getListeners();
/**
* All updatable classes.
*
* @return A list of all updatable classes.
*/
public abstract List<Class<? extends Updatable>> getUpdatableClasses();
}