Implement EssentiaFeatures
This commit is contained in:
parent
9fa43c8c65
commit
d4f33b8c2e
|
|
@ -3,11 +3,14 @@ package com.alttd.essentia;
|
||||||
import com.alttd.essentia.commands.EssentiaCommand;
|
import com.alttd.essentia.commands.EssentiaCommand;
|
||||||
import com.alttd.essentia.configuration.Config;
|
import com.alttd.essentia.configuration.Config;
|
||||||
import com.alttd.essentia.api.model.randomteleport.LocationValidator;
|
import com.alttd.essentia.api.model.randomteleport.LocationValidator;
|
||||||
|
import com.alttd.essentia.feature.Features;
|
||||||
|
import com.alttd.essentia.model.annotations.Depends;
|
||||||
import com.alttd.essentia.storage.StorageManager;
|
import com.alttd.essentia.storage.StorageManager;
|
||||||
import com.alttd.essentia.storage.StorageProvider;
|
import com.alttd.essentia.storage.StorageProvider;
|
||||||
import com.alttd.essentia.storage.StorageType;
|
import com.alttd.essentia.storage.StorageType;
|
||||||
import com.alttd.essentia.user.EssentiaUserManager;
|
import com.alttd.essentia.user.EssentiaUserManager;
|
||||||
import com.alttd.essentia.api.user.UserManager;
|
import com.alttd.essentia.api.user.UserManager;
|
||||||
|
import com.alttd.essentia.util.Timer;
|
||||||
import io.papermc.paper.command.brigadier.Commands;
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
||||||
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||||
|
|
@ -36,6 +39,9 @@ public class EssentiaPlugin extends JavaPlugin implements EssentiaAPI {
|
||||||
@Getter
|
@Getter
|
||||||
private StorageProvider storageProvider;
|
private StorageProvider storageProvider;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Features features;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
@ -44,12 +50,15 @@ public class EssentiaPlugin extends JavaPlugin implements EssentiaAPI {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
loadConfiguration();
|
new Timer("onEnable", () -> {
|
||||||
loadCommands();
|
new Timer("loadConfiguration", this::loadConfiguration);
|
||||||
loadEventListeners();
|
new Timer("loadFeatures", this::loadFeatures);
|
||||||
loadManagers();
|
new Timer("loadCommands", this::loadCommands);
|
||||||
loadStorageProvider();
|
new Timer("loadEventListeners", this::loadEventListeners);
|
||||||
loadLocationValidators();
|
new Timer("loadManagers", this::loadManagers);
|
||||||
|
new Timer("loadStorageProvider", this::loadStorageProvider);
|
||||||
|
new Timer("loadLocationValidators", this::loadLocationValidators);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -63,6 +72,11 @@ public class EssentiaPlugin extends JavaPlugin implements EssentiaAPI {
|
||||||
Config.init();
|
Config.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadFeatures() {
|
||||||
|
features = new Features(this);
|
||||||
|
features.registerAll();
|
||||||
|
}
|
||||||
|
|
||||||
void loadCommands() {
|
void loadCommands() {
|
||||||
Reflections reflections = new Reflections("com.alttd.essentia.commands");
|
Reflections reflections = new Reflections("com.alttd.essentia.commands");
|
||||||
Set<Class<?>> subTypes = reflections.get(Scanners.SubTypes.of(EssentiaCommand.class).asClass());
|
Set<Class<?>> subTypes = reflections.get(Scanners.SubTypes.of(EssentiaCommand.class).asClass());
|
||||||
|
|
@ -73,6 +87,13 @@ public class EssentiaPlugin extends JavaPlugin implements EssentiaAPI {
|
||||||
subTypes.forEach(clazz -> {
|
subTypes.forEach(clazz -> {
|
||||||
try {
|
try {
|
||||||
EssentiaCommand essentiaCommand = (EssentiaCommand) clazz.getDeclaredConstructor().newInstance();
|
EssentiaCommand essentiaCommand = (EssentiaCommand) clazz.getDeclaredConstructor().newInstance();
|
||||||
|
final Depends depends = clazz.getAnnotation(Depends.class);
|
||||||
|
if (depends != null) {
|
||||||
|
if (!features.isEnabled(depends.value())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
commands.register(essentiaCommand.command(), essentiaCommand.description(), essentiaCommand.aliases());
|
commands.register(essentiaCommand.command(), essentiaCommand.description(), essentiaCommand.aliases());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
EssentiaPlugin.instance().getLogger().severe("Failed to register command " + clazz.getSimpleName());
|
EssentiaPlugin.instance().getLogger().severe("Failed to register command " + clazz.getSimpleName());
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package com.alttd.essentia.configuration;
|
||||||
|
|
||||||
import com.alttd.essentia.EssentiaPlugin;
|
import com.alttd.essentia.EssentiaPlugin;
|
||||||
import com.google.common.base.Throwables;
|
import com.google.common.base.Throwables;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
@ -274,4 +276,10 @@ public class Config {
|
||||||
MYSQL_PASSWORD = getString("storage.mysql.password", MYSQL_PASSWORD);
|
MYSQL_PASSWORD = getString("storage.mysql.password", MYSQL_PASSWORD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Object2BooleanMap<String> enabledFeatures = new Object2BooleanOpenHashMap<>();
|
||||||
|
private static void features() {
|
||||||
|
enabledFeatures.clear();
|
||||||
|
enabledFeatures.defaultReturnValue(true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.alttd.essentia.feature;
|
||||||
|
|
||||||
|
public interface EssentiaFeature {
|
||||||
|
|
||||||
|
String featureName();
|
||||||
|
|
||||||
|
void register();
|
||||||
|
|
||||||
|
boolean isEnabled();
|
||||||
|
|
||||||
|
default void start() {}
|
||||||
|
|
||||||
|
default void stop() {}
|
||||||
|
|
||||||
|
default void reload() {
|
||||||
|
stop();
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.alttd.essentia.feature;
|
||||||
|
|
||||||
|
import com.alttd.essentia.EssentiaPlugin;
|
||||||
|
import com.alttd.essentia.configuration.Config;
|
||||||
|
import org.reflections.Reflections;
|
||||||
|
import org.reflections.scanners.Scanners;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
public class Features {
|
||||||
|
|
||||||
|
private final EssentiaPlugin plugin;
|
||||||
|
private final Map<String, EssentiaFeature> features;
|
||||||
|
|
||||||
|
public Features(EssentiaPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
features = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled(String featureName) {
|
||||||
|
if (features.containsKey(featureName)) {
|
||||||
|
return features.get(featureName).isEnabled();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerAll() {
|
||||||
|
Reflections reflections = new Reflections("com.alttd.essentia.feature");
|
||||||
|
Set<Class<?>> subTypes = reflections.get(Scanners.SubTypes.of(EssentiaFeature.class).asClass());
|
||||||
|
|
||||||
|
subTypes.forEach(clazz -> {
|
||||||
|
try {
|
||||||
|
EssentiaFeature essentiaFeature = (EssentiaFeature) clazz.getDeclaredConstructor().newInstance();
|
||||||
|
if (!Config.enabledFeatures.getBoolean(essentiaFeature.featureName())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
essentiaFeature.register();
|
||||||
|
essentiaFeature.reload();
|
||||||
|
|
||||||
|
features.putIfAbsent(essentiaFeature.featureName(), essentiaFeature);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log(Level.SEVERE,"Failed to register feature " + clazz.getSimpleName(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void log(Level level, String s) {
|
||||||
|
plugin.getLogger().log(level, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void log(Level level, String s, Throwable thrown) {
|
||||||
|
plugin.getLogger().log(level, s, thrown);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.alttd.essentia.model.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Depends {
|
||||||
|
String value();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user