From 9767eaffa349039a28dfbfeea9c8907abf945c79 Mon Sep 17 00:00:00 2001 From: "dev@ftbsc" Date: Sat, 28 Jan 2023 17:50:58 +0100 Subject: [PATCH] feat: command dispatcher on client --- .../java/co/fantabos/bscv/BoSCoVicino.java | 29 +++++++++++++++---- .../java/co/fantabos/bscv/Fullbright.java | 7 +++-- src/main/java/co/fantabos/bscv/Module.java | 4 +-- 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 mods; + private final CommandDispatcher 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(); - 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 mode; - public Fullbright(ForgeConfigSpec.Builder builder) { - super("Fullbright", Group.VISION, builder); + public Fullbright(ForgeConfigSpec.Builder builder, CommandDispatcher 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 enabled; - protected Module(String name, Group group, ForgeConfigSpec.Builder builder) { + protected Module(String name, Group group, ForgeConfigSpec.Builder builder, CommandDispatcher 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 dispatcher) { dispatcher.register( Commands.literal(this.name.toLowerCase()) .then(