Search in sources :

Example 1 with ParsedCommandNode

use of com.mojang.brigadier.context.ParsedCommandNode in project MinecraftForge by MinecraftForge.

the class CommandEventTest method onCommand.

@SubscribeEvent
public static void onCommand(CommandEvent event) {
    CommandDispatcher<CommandSourceStack> dispatcher = event.getParseResults().getContext().getDispatcher();
    List<ParsedCommandNode<CommandSourceStack>> nodes = event.getParseResults().getContext().getNodes();
    CommandSourceStack source = event.getParseResults().getContext().getSource();
    // test: when the /time command is used with no arguments, automatically add default arguments (/time set day)
    if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("time")) {
        event.setParseResults(dispatcher.parse("time set day", source));
        return;
    }
    // test: whenever a player uses the /give command, let everyone on the server know
    if (nodes.size() > 0 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("give")) {
        String msg = source.getTextName() + " used the give command: " + event.getParseResults().getReader().getString();
        source.getServer().getPlayerList().getPlayers().forEach(player -> player.sendMessage(new TextComponent(msg), player.getUUID()));
        return;
    }
// this is annoying so I disabled it
// test: when the /kill command is used with no arguments, throw a custom exception
// if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("kill"))
// {
// event.setException(new CommandRuntimeException(new TextComponent("You tried to use the /kill command with no arguments")));
// event.setCanceled(true);
// return;
// }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ParsedCommandNode(com.mojang.brigadier.context.ParsedCommandNode) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

ParsedCommandNode (com.mojang.brigadier.context.ParsedCommandNode)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 TextComponent (net.minecraft.network.chat.TextComponent)1 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)1