Last active
July 15, 2020 17:43
-
-
Save JTK222/4b73f8960d98da11ec2b9fe055dc2c96 to your computer and use it in GitHub Desktop.
A rudimentary implementation of client side commands.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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