Use Reflection to load commands.

This commit is contained in:
Len 2024-08-14 20:48:27 +02:00
parent 628e18858a
commit ad7e61ffe8
2 changed files with 41 additions and 22 deletions

View File

@ -1,14 +1,15 @@
package com.alttd.essentia;
import com.alttd.essentia.commands.admin.*;
import com.alttd.essentia.commands.player.*;
import com.alttd.essentia.commands.EssentiaCommand;
import com.alttd.essentia.configuration.Config;
import com.alttd.essentia.listeners.PlayerListener;
import com.alttd.essentia.storage.StorageManager;
import com.alttd.essentia.storage.StorageProvider;
import com.alttd.essentia.storage.StorageType;
import com.alttd.essentia.user.EssentiaUserManager;
import com.alttd.essentia.api.user.UserManager;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import lombok.Getter;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
@ -57,25 +58,21 @@ public class EssentiaPlugin extends JavaPlugin implements EssentiaAPI {
}
void loadCommands() {
getCommand("essentia").setExecutor(new EssentiaCommand(this));
getCommand("teleportaccept").setExecutor(new TeleportAcceptCommand(this));
getCommand("teleportdeny").setExecutor(new TeleportDenyCommand(this));
getCommand("teleportrequest").setExecutor(new TeleportRequestCommand(this));
getCommand("teleportrequesthere").setExecutor(new TeleportRequestHereCommand(this));
getCommand("teleporttoggle").setExecutor(new TeleportToggleCommand(this));
getCommand("clearinventory").setExecutor(new ClearInventoryCommand(this));
getCommand("home").setExecutor(new HomeCommand(this));
getCommand("homes").setExecutor(new HomeListCommand(this));
getCommand("sethome").setExecutor(new SetHomeCommand(this));
getCommand("deletehome").setExecutor(new DelHomeCommand(this));
getCommand("back").setExecutor(new BackCommand(this));
getCommand("deathback").setExecutor(new DeathBackCommand(this));
getCommand("fly").setExecutor(new FlyCommand(this));
getCommand("gamemode").setExecutor(new GamemodeCommand(this));
getCommand("heal").setExecutor(new HealCommand(this));
getCommand("feed").setExecutor(new FeedCommand(this));
getCommand("enchant").setExecutor(new EnchantCommand(this));
getCommand("spawn").setExecutor(new SpawnCommand(this));
Reflections reflections = new Reflections("com.alttd.essentia.commands");
Set<Class<?>> subTypes = reflections.get(Scanners.SubTypes.of(EssentiaCommand.class).asClass());
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
final Commands commands = event.registrar();
subTypes.forEach(clazz -> {
try {
EssentiaCommand essentiaCommand = (EssentiaCommand) clazz.getDeclaredConstructor().newInstance();
commands.register(essentiaCommand.command(), essentiaCommand.description(), essentiaCommand.aliases());
} catch (Exception e) {
EssentiaPlugin.instance().getLogger().severe("Failed to register command " + clazz.getSimpleName());
}
});
});
}
void loadEventListeners() {

View File

@ -0,0 +1,22 @@
package com.alttd.essentia.commands;
import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
// TODO -- add optional -s -silent parameters to commands?
public interface EssentiaCommand {
@NotNull LiteralCommandNode<CommandSourceStack> command();
default String description() {
return null;
}
default List<String> aliases() {
return Collections.emptyList();
}
}