Skip to content

Instantly share code, notes, and snippets.

@JTK222
Last active July 15, 2020 17:43
Show Gist options
  • Save JTK222/0f1fe6c6ec3bfbf4c1604183fb00eaef to your computer and use it in GitHub Desktop.
Save JTK222/0f1fe6c6ec3bfbf4c1604183fb00eaef to your computer and use it in GitHub Desktop.
A rudimentary implementation of client side commands. No auto completion, no error messages
@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