From 414fbf4962cafd1286c2cc6c0814ae31ce87ef23 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 4 Mar 2023 01:30:47 +0100 Subject: feat: added AbstractCommand, load commands --- .../java/ftbsc/bscv/commands/AbstractCommand.java | 35 ++++++++++++++++++++++ src/main/java/ftbsc/bscv/system/ModManager.java | 11 +++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ftbsc/bscv/commands/AbstractCommand.java (limited to 'src/main/java/ftbsc') diff --git a/src/main/java/ftbsc/bscv/commands/AbstractCommand.java b/src/main/java/ftbsc/bscv/commands/AbstractCommand.java new file mode 100644 index 0000000..9568237 --- /dev/null +++ b/src/main/java/ftbsc/bscv/commands/AbstractCommand.java @@ -0,0 +1,35 @@ +package ftbsc.bscv.commands; + +import java.util.Collections; +import java.util.List; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import ftbsc.bscv.Boscovicino; +import ftbsc.bscv.ICommons; +import ftbsc.bscv.api.ICommand; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; + +public abstract class AbstractCommand implements ICommand, ICommons { + + public String getName() { + return this.getClass().getSimpleName(); + } + + public CommandDispatcher getDispatcher() { + return Boscovicino.modManager.getDispatcher(); + } + + public List> subcommands() { + return Collections.emptyList(); + } + + abstract LiteralArgumentBuilder register(LiteralArgumentBuilder builder); + + public AbstractCommand() { + LiteralArgumentBuilder builder = Commands.literal(this.getName().toLowerCase()); + this.getDispatcher().register(this.register(builder)); + } +} diff --git a/src/main/java/ftbsc/bscv/system/ModManager.java b/src/main/java/ftbsc/bscv/system/ModManager.java index 974142e..9420d44 100644 --- a/src/main/java/ftbsc/bscv/system/ModManager.java +++ b/src/main/java/ftbsc/bscv/system/ModManager.java @@ -1,6 +1,8 @@ package ftbsc.bscv.system; import com.mojang.brigadier.CommandDispatcher; + +import ftbsc.bscv.api.ICommand; import ftbsc.bscv.api.ILoadable; import ftbsc.bscv.api.IModule; import net.minecraft.command.CommandSource; @@ -15,6 +17,7 @@ public class ModManager { private Builder cfgBuilder; private CommandDispatcher dispatcher; + public final Set commands; public final Set mods; public final Set categories; @@ -22,9 +25,9 @@ public class ModManager { this.cfgBuilder = cfgBuilder; this.dispatcher = dispatcher; this.mods = new HashSet<>(); + this.commands = new HashSet<>(); this.categories = new HashSet<>(); } - @Nullable public IModule get(Class clazz) { @@ -39,11 +42,15 @@ public class ModManager { public void load() { for (ILoadable module : ServiceLoader.load(ILoadable.class)) { if(module instanceof IModule) { - IModule mod = (IModule) module; + IModule mod = (IModule) module; this.mods.add(mod); this.categories.add(mod.getGroup()); this.cfgBuilder.pop(); // TODO } + if(module instanceof ICommand) { // TODO do these belong here? + ICommand cmd = (ICommand) module; + this.commands.add(cmd); + } } } -- cgit v1.2.3-56-ga3b1