summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
author dev@ftbsc <dev@fantabos.co>2023-01-28 17:50:58 +0100
committer dev@ftbsc <dev@fantabos.co>2023-01-28 17:50:58 +0100
commit9767eaffa349039a28dfbfeea9c8907abf945c79 (patch)
tree317265bd4652182dd1d940736cdb5e2020f8abc9 /src/main
parenta57f3d017aae5cd1a60b190a46cdc742a4883998 (diff)
feat: command dispatcher on client
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/co/fantabos/bscv/BoSCoVicino.java29
-rw-r--r--src/main/java/co/fantabos/bscv/Fullbright.java7
-rw-r--r--src/main/java/co/fantabos/bscv/Module.java4
3 files changed, 30 insertions, 10 deletions
diff --git a/src/main/java/co/fantabos/bscv/BoSCoVicino.java b/src/main/java/co/fantabos/bscv/BoSCoVicino.java
index 883d5b6..f139294 100644
--- a/src/main/java/co/fantabos/bscv/BoSCoVicino.java
+++ b/src/main/java/co/fantabos/bscv/BoSCoVicino.java
@@ -3,9 +3,10 @@ package co.fantabos.bscv;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
+import net.minecraft.command.CommandSource;
+import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
-import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
@@ -21,6 +22,9 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+import com.mojang.brigadier.CommandDispatcher;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -37,6 +41,8 @@ public class BoSCoVicino {
public static List<Module> mods;
+ private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
+
public BoSCoVicino() {
// Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
@@ -56,7 +62,7 @@ public class BoSCoVicino {
// load and register mods
BoSCoVicino.mods = new ArrayList<Module>();
- BoSCoVicino.mods.add(new Fullbright(builder));
+ BoSCoVicino.mods.add(new Fullbright(builder, this.dispatcher));
ForgeConfigSpec spec = builder.build();
@@ -90,10 +96,23 @@ public class BoSCoVicino {
collect(Collectors.toList()));
}
+ // @SubscribeEvent
+ // public void onRegisterCommand(RegisterCommandsEvent event) {
+ // for (Module mod : BoSCoVicino.mods) {
+ // mod.registerCommands(event.getDispatcher());
+ // }
+ // }
+
@SubscribeEvent
- public void onRegisterCommand(RegisterCommandsEvent event) {
- for (Module mod : BoSCoVicino.mods) {
- mod.registerCommands(event.getDispatcher());
+ public void onClientChatEvent(ClientChatEvent event) {
+ if (event.getMessage().startsWith("/")) {
+ CommandSource source = BoSCoVicino.minecraft.player.createCommandSourceStack();
+ try {
+ this.dispatcher.execute(event.getMessage(), source);
+ event.setCanceled(true);
+ } catch (CommandSyntaxException e) {
+ LOGGER.error("Syntax error in command : %s", e.toString());
+ }
}
}
diff --git a/src/main/java/co/fantabos/bscv/Fullbright.java b/src/main/java/co/fantabos/bscv/Fullbright.java
index f8aba8d..bd5fe27 100644
--- a/src/main/java/co/fantabos/bscv/Fullbright.java
+++ b/src/main/java/co/fantabos/bscv/Fullbright.java
@@ -1,5 +1,8 @@
package co.fantabos.bscv;
+import com.mojang.brigadier.CommandDispatcher;
+
+import net.minecraft.command.CommandSource;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectInstance;
import net.minecraftforge.common.ForgeConfigSpec;
@@ -10,8 +13,8 @@ public class Fullbright extends Module {
private final ForgeConfigSpec.ConfigValue<String> mode;
- public Fullbright(ForgeConfigSpec.Builder builder) {
- super("Fullbright", Group.VISION, builder);
+ public Fullbright(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
+ super("Fullbright", Group.VISION, builder, dispatcher);
this.mode = builder.comment("either potion or gamma").define("mode", "potion");
}
diff --git a/src/main/java/co/fantabos/bscv/Module.java b/src/main/java/co/fantabos/bscv/Module.java
index d157a6d..295d6a8 100644
--- a/src/main/java/co/fantabos/bscv/Module.java
+++ b/src/main/java/co/fantabos/bscv/Module.java
@@ -20,7 +20,7 @@ public abstract class Module {
public final Group group;
public final ForgeConfigSpec.ConfigValue<Boolean> enabled;
- protected Module(String name, Group group, ForgeConfigSpec.Builder builder) {
+ protected Module(String name, Group group, ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
this.name = name;
this.group = group;
@@ -28,9 +28,7 @@ public abstract class Module {
this.enabled = builder
.comment(String.format("Enables %s", this.name))
.define("enabled", false);
- }
- public void registerCommands(CommandDispatcher<CommandSource> dispatcher) {
dispatcher.register(
Commands.literal(this.name.toLowerCase())
.then(