Search in sources :

Example 21 with CommandSourceStack

use of net.minecraft.commands.CommandSourceStack in project MinecraftForge by MinecraftForge.

the class ConsoleCommandCompleter method complete.

@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
    String buffer = line.line();
    boolean prefix;
    if (buffer.isEmpty() || buffer.charAt(0) != '/') {
        buffer = '/' + buffer;
        prefix = false;
    } else {
        prefix = true;
    }
    final String input = buffer;
    // See NetHandlerPlayServer#processTabComplete
    StringReader stringReader = new StringReader(input);
    if (stringReader.canRead() && stringReader.peek() == '/')
        stringReader.skip();
    try {
        ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(stringReader, this.server.createCommandSourceStack());
        Suggestions tabComplete = this.server.getCommands().getDispatcher().getCompletionSuggestions(results).get();
        for (Suggestion suggestion : tabComplete.getList()) {
            String completion = suggestion.getText();
            if (!completion.isEmpty()) {
                boolean hasPrefix = prefix || completion.charAt(0) != '/';
                Candidate candidate = new Candidate(hasPrefix ? completion : completion.substring(1));
                candidates.add(candidate);
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        logger.error("Failed to tab complete", e);
    }
}
Also used : Suggestions(com.mojang.brigadier.suggestion.Suggestions) Candidate(org.jline.reader.Candidate) Suggestion(com.mojang.brigadier.suggestion.Suggestion) StringReader(com.mojang.brigadier.StringReader) ExecutionException(java.util.concurrent.ExecutionException) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

Example 22 with CommandSourceStack

use of net.minecraft.commands.CommandSourceStack 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)

Example 23 with CommandSourceStack

use of net.minecraft.commands.CommandSourceStack in project Tropicraft by Tropicraft.

the class VolcanoGenerator method onServerStarting.

@SubscribeEvent
public static void onServerStarting(FMLServerStartingEvent event) {
    // we don't really have a structure but we fake it
    CommandDispatcher<CommandSourceStack> dispatcher = event.getServer().getCommands().getDispatcher();
    LiteralArgumentBuilder<CommandSourceStack> locate = Commands.literal("locate").requires(source -> source.hasPermission(2));
    dispatcher.register(locate.then(Commands.literal(Constants.MODID + ":volcano").executes(ctx -> {
        CommandSourceStack source = ctx.getSource();
        BlockPos pos = new BlockPos(source.getPosition());
        BlockPos volcanoPos = getVolcanoNear(source.getLevel(), pos.getX() >> 4, pos.getZ() >> 4, 100);
        if (volcanoPos == null) {
            throw new SimpleCommandExceptionType(new TranslatableComponent("commands.locate.failed")).create();
        } else {
            return LocateCommand.showLocateResult(source, "Volcano", pos, volcanoPos, "commands.locate.success");
        }
    })));
}
Also used : TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) BlockPos(net.minecraft.core.BlockPos) SimpleCommandExceptionType(com.mojang.brigadier.exceptions.SimpleCommandExceptionType) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

CommandSourceStack (net.minecraft.commands.CommandSourceStack)23 CommandNode (com.mojang.brigadier.tree.CommandNode)6 ArrayList (java.util.ArrayList)6 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)5 LiteralCommandNode (com.mojang.brigadier.tree.LiteralCommandNode)5 Map (java.util.Map)5 TextComponent (net.minecraft.network.chat.TextComponent)5 Suggestions (com.mojang.brigadier.suggestion.Suggestions)4 HashMap (java.util.HashMap)4 List (java.util.List)4 ServerLevel (net.minecraft.server.level.ServerLevel)4 ResultConsumer (com.mojang.brigadier.ResultConsumer)3 StringReader (com.mojang.brigadier.StringReader)3 CommandContext (com.mojang.brigadier.context.CommandContext)3 Collectors (java.util.stream.Collectors)3 CommandSource (net.minecraft.commands.CommandSource)3 Component (net.minecraft.network.chat.Component)3 Vec2 (net.minecraft.world.phys.Vec2)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 EventContextKeys (org.spongepowered.api.event.EventContextKeys)3