Removed the need to register updatable classes - added reflections
This commit is contained in:
@@ -67,11 +67,6 @@ public abstract class EcoPlugin extends JavaPlugin {
|
||||
@Getter
|
||||
private final Set<String> loadedIntegrations = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Set of classes to be processed on config update.
|
||||
*/
|
||||
private final List<Class<?>> updatableClasses = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* The internal plugin scheduler.
|
||||
*/
|
||||
@@ -362,16 +357,12 @@ public abstract class EcoPlugin extends JavaPlugin {
|
||||
}
|
||||
});
|
||||
|
||||
updatableClasses.addAll(this.loadUpdatableClasses());
|
||||
|
||||
this.loadListeners().forEach(listener -> this.getEventManager().registerListener(listener));
|
||||
|
||||
this.loadPluginCommands().forEach(PluginCommand::register);
|
||||
|
||||
this.getScheduler().runLater(this::afterLoad, 1);
|
||||
|
||||
this.updatableClasses.forEach(clazz -> this.getConfigHandler().registerUpdatableClass(clazz));
|
||||
|
||||
if (this.isSupportingExtensions()) {
|
||||
this.getExtensionLoader().loadExtensions();
|
||||
|
||||
|
||||
@@ -9,13 +9,6 @@ public interface ConfigHandler {
|
||||
*/
|
||||
void callUpdate();
|
||||
|
||||
/**
|
||||
* Register an updatable class.
|
||||
*
|
||||
* @param updatableClass The class with an update method.
|
||||
*/
|
||||
void registerUpdatableClass(@NotNull Class<?> updatableClass);
|
||||
|
||||
/**
|
||||
* Save all configs.
|
||||
*/
|
||||
|
||||
@@ -4,4 +4,5 @@ version rootProject.version
|
||||
dependencies {
|
||||
compileOnly 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT'
|
||||
compileOnly 'me.clip:placeholderapi:2.10.9'
|
||||
compileOnly 'org.reflections:reflections:0.9.12'
|
||||
}
|
||||
@@ -2,83 +2,51 @@ package com.willfp.eco.internal.config.updating;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.PluginDependent;
|
||||
import com.willfp.eco.core.config.interfaces.LoadableConfig;
|
||||
import com.willfp.eco.core.config.updating.ConfigHandler;
|
||||
import com.willfp.eco.core.config.updating.ConfigUpdater;
|
||||
import com.willfp.eco.core.config.interfaces.LoadableConfig;
|
||||
import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdatableClassException;
|
||||
import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdateMethodException;
|
||||
import com.willfp.eco.internal.config.yaml.EcoUpdatableYamlConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.MethodAnnotationsScanner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class EcoConfigHandler extends PluginDependent<EcoPlugin> implements ConfigHandler {
|
||||
/**
|
||||
* A set of all configs that can be saved.
|
||||
*/
|
||||
private final List<LoadableConfig> configs = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* A set of all classes that can be updated.
|
||||
*/
|
||||
private final Set<Class<?>> updatableClasses = new HashSet<>();
|
||||
private final Reflections reflections = new Reflections(this.getPlugin().getClass().getPackageName(), new MethodAnnotationsScanner());
|
||||
|
||||
/**
|
||||
* Creates a new config handler and links it to an {@link EcoPlugin}.
|
||||
*
|
||||
* @param plugin The plugin to manage.
|
||||
*/
|
||||
public EcoConfigHandler(@NotNull final EcoPlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callUpdate() {
|
||||
updatableClasses.forEach(clazz -> Arrays.stream(clazz.getDeclaredMethods()).forEach(method -> {
|
||||
if (method.isAnnotationPresent(ConfigUpdater.class)) {
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
throw new InvalidUpdateMethodException("Update method must be static.");
|
||||
}
|
||||
|
||||
try {
|
||||
if (method.getParameterCount() == 0) {
|
||||
method.invoke(null);
|
||||
} else if (method.getParameterCount() == 1) {
|
||||
method.invoke(null, this.getPlugin());
|
||||
} else {
|
||||
throw new InvalidUpdateMethodException("Update method must have 0 parameters or a plugin parameter.");
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
throw new InvalidUpdateMethodException("Update method generated an exception.");
|
||||
}
|
||||
for (Method method : reflections.getMethodsAnnotatedWith(ConfigUpdater.class)) {
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
throw new InvalidUpdateMethodException("Update method must be static.");
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUpdatableClass(@NotNull final Class<?> updatableClass) {
|
||||
boolean isValid = false;
|
||||
for (Method method : updatableClass.getDeclaredMethods()) {
|
||||
if (Modifier.isStatic(method.getModifiers()) && (method.getParameterCount() == 0 || method.getParameterCount() == 1) && method.isAnnotationPresent(ConfigUpdater.class)) {
|
||||
isValid = true;
|
||||
break;
|
||||
try {
|
||||
if (method.getParameterCount() == 0) {
|
||||
method.invoke(null);
|
||||
} else if (method.getParameterCount() == 1) {
|
||||
method.invoke(null, this.getPlugin());
|
||||
} else {
|
||||
throw new InvalidUpdateMethodException("Update method must have 0 parameters or a plugin parameter.");
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
throw new InvalidUpdateMethodException("Update method generated an exception.");
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValid) {
|
||||
throw new InvalidUpdatableClassException("Registered updatable class " + updatableClass + " must have an annotated static method with no modifiers.");
|
||||
}
|
||||
|
||||
updatableClasses.add(updatableClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.willfp.eco.internal.config.updating.exceptions;
|
||||
|
||||
import com.willfp.eco.internal.config.updating.EcoConfigHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InvalidUpdatableClassException extends RuntimeException {
|
||||
/**
|
||||
* Called when an updatable class is registered into an {@link EcoConfigHandler}.
|
||||
*
|
||||
* @param message The error message.
|
||||
*/
|
||||
public InvalidUpdatableClassException(@NotNull final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,6 @@ softdepend:
|
||||
- Spartan
|
||||
- PlaceholderAPI
|
||||
- mcMMO
|
||||
- CombatLogX
|
||||
- CombatLogX
|
||||
libraries:
|
||||
- org.reflections:reflections:0.9.12
|
||||
Reference in New Issue
Block a user