Skip to content

Instantly share code, notes, and snippets.

@JTK222
Last active July 15, 2020 17:43
Show Gist options
  • Save JTK222/4b73f8960d98da11ec2b9fe055dc2c96 to your computer and use it in GitHub Desktop.
Save JTK222/4b73f8960d98da11ec2b9fe055dc2c96 to your computer and use it in GitHub Desktop.
A rudimentary implementation of client side commands.
@Mod.EventBusSubscriber(modid = MODID, value = Dist.CLIENT)
public class ClientChatListener {
private static CommandDispatcher<CommandSource> commands = new CommandDispatcher();
static{
commands.register(Commands.literal("foo").executes(context -> {
context.getSource().sendFeedback(new StringTextComponent("bar"), false);
return 1;
}));
}
@SubscribeEvent
public static void playerChat(ClientChatEvent event){
if(event.getMessage().startsWith("/")){
try{
ParseResults<CommandSource> parse = commands.parse(event.getMessage().substring(1), Minecraft.getInstance().player.getCommandSource());
if(parse.getContext().getNodes().size() > 0){
event.setCanceled(true);
commands.execute(parse);
}
} catch (CommandSyntaxException e) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment