feat: command dispatcher on client

This commit is contained in:
dev@ftbsc 2023-01-28 17:50:58 +01:00
parent a57f3d017a
commit 9767eaffa3
3 changed files with 30 additions and 10 deletions

View file

@ -3,9 +3,10 @@ package co.fantabos.bscv;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft; 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.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -37,6 +41,8 @@ public class BoSCoVicino {
public static List<Module> mods; public static List<Module> mods;
private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
public BoSCoVicino() { public BoSCoVicino() {
// Register the setup method for modloading // Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
@ -56,7 +62,7 @@ public class BoSCoVicino {
// load and register mods // load and register mods
BoSCoVicino.mods = new ArrayList<Module>(); BoSCoVicino.mods = new ArrayList<Module>();
BoSCoVicino.mods.add(new Fullbright(builder)); BoSCoVicino.mods.add(new Fullbright(builder, this.dispatcher));
ForgeConfigSpec spec = builder.build(); ForgeConfigSpec spec = builder.build();
@ -90,10 +96,23 @@ public class BoSCoVicino {
collect(Collectors.toList())); collect(Collectors.toList()));
} }
// @SubscribeEvent
// public void onRegisterCommand(RegisterCommandsEvent event) {
// for (Module mod : BoSCoVicino.mods) {
// mod.registerCommands(event.getDispatcher());
// }
// }
@SubscribeEvent @SubscribeEvent
public void onRegisterCommand(RegisterCommandsEvent event) { public void onClientChatEvent(ClientChatEvent event) {
for (Module mod : BoSCoVicino.mods) { if (event.getMessage().startsWith("/")) {
mod.registerCommands(event.getDispatcher()); 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());
}
} }
} }

View file

@ -1,5 +1,8 @@
package co.fantabos.bscv; package co.fantabos.bscv;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.command.CommandSource;
import net.minecraft.potion.Effect; import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectInstance; import net.minecraft.potion.EffectInstance;
import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec;
@ -10,8 +13,8 @@ public class Fullbright extends Module {
private final ForgeConfigSpec.ConfigValue<String> mode; private final ForgeConfigSpec.ConfigValue<String> mode;
public Fullbright(ForgeConfigSpec.Builder builder) { public Fullbright(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
super("Fullbright", Group.VISION, builder); super("Fullbright", Group.VISION, builder, dispatcher);
this.mode = builder.comment("either potion or gamma").define("mode", "potion"); this.mode = builder.comment("either potion or gamma").define("mode", "potion");
} }

View file

@ -20,7 +20,7 @@ public abstract class Module {
public final Group group; public final Group group;
public final ForgeConfigSpec.ConfigValue<Boolean> enabled; 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.name = name;
this.group = group; this.group = group;
@ -28,9 +28,7 @@ public abstract class Module {
this.enabled = builder this.enabled = builder
.comment(String.format("Enables %s", this.name)) .comment(String.format("Enables %s", this.name))
.define("enabled", false); .define("enabled", false);
}
public void registerCommands(CommandDispatcher<CommandSource> dispatcher) {
dispatcher.register( dispatcher.register(
Commands.literal(this.name.toLowerCase()) Commands.literal(this.name.toLowerCase())
.then( .then(