diff --git a/plugin/src/main/java/com/alttd/essentia/EssentiaPlugin.java b/plugin/src/main/java/com/alttd/essentia/EssentiaPlugin.java index 75f5a9c..8193334 100644 --- a/plugin/src/main/java/com/alttd/essentia/EssentiaPlugin.java +++ b/plugin/src/main/java/com/alttd/essentia/EssentiaPlugin.java @@ -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> subTypes = reflections.get(Scanners.SubTypes.of(EssentiaCommand.class).asClass()); + + LifecycleEventManager 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() { diff --git a/plugin/src/main/java/com/alttd/essentia/commands/EssentiaCommand.java b/plugin/src/main/java/com/alttd/essentia/commands/EssentiaCommand.java new file mode 100644 index 0000000..f6c2394 --- /dev/null +++ b/plugin/src/main/java/com/alttd/essentia/commands/EssentiaCommand.java @@ -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 command(); + + default String description() { + return null; + } + + default List aliases() { + return Collections.emptyList(); + } + +}