Java Code Examples for com.mojang.brigadier.exceptions.CommandSyntaxException#getCursor()

The following examples show how to use com.mojang.brigadier.exceptions.CommandSyntaxException#getCursor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Exceptions.java    From Chimera with MIT License 5 votes vote down vote up
static void report(CommandSender sender, CommandSyntaxException exception) {
    var listener = from(sender);
    
    listener.sendFailureMessage(ChatComponentUtils.a(exception.getRawMessage()));
    
    var input = exception.getInput();
    if (input != null && exception.getCursor() >= 0) {
        var index = Math.min(input.length(), exception.getCursor());

        var text = (new ChatComponentText("")).a(EnumChatFormat.GRAY).format(modifier -> 
            modifier.setChatClickable(new ChatClickable(ChatClickable.EnumClickAction.SUGGEST_COMMAND, input))
        );
        
        if (index > 10) {
            text.c("...");
        }

        text.c(input.substring(Math.max(0, index - 10), index));
        
        if (index < input.length()) {
            var error = new ChatComponentText(input.substring(index)).a(new EnumChatFormat[]{EnumChatFormat.RED, EnumChatFormat.UNDERLINE});
            text.addSibling(error);
        }
        
        var context = new ChatMessage("command.context.here").a(new EnumChatFormat[]{EnumChatFormat.RED, EnumChatFormat.ITALIC});
        text.addSibling(context);
        
        listener.sendFailureMessage(text);
    }
}
 
Example 2
Source File: ClientInterop.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
public static boolean interceptChatMessage(String message) {
    if (message.startsWith("/bbor:")) {
        ClientPlayNetHandler connection = Minecraft.getInstance().getConnection();
        if (connection != null) {
            CommandDispatcher<ISuggestionProvider> commandDispatcher = connection.func_195515_i();
            CommandSource commandSource = Minecraft.getInstance().player.getCommandSource();
            try {
                commandDispatcher.execute(message.substring(1), commandSource);
            } catch (CommandSyntaxException exception) {
                commandSource.sendErrorMessage(TextComponentUtils.toTextComponent(exception.getRawMessage()));
                if (exception.getInput() != null && exception.getCursor() >= 0) {
                    ITextComponent suggestion = new StringTextComponent("")
                            .applyTextStyle(TextFormatting.GRAY)
                            .applyTextStyle(style -> style.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
                    int textLength = Math.min(exception.getInput().length(), exception.getCursor());
                    if (textLength > 10) {
                        suggestion.appendText("...");
                    }

                    suggestion.appendText(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
                    if (textLength < exception.getInput().length()) {
                        suggestion.appendSibling(new StringTextComponent(exception.getInput().substring(textLength))
                                .applyTextStyles(TextFormatting.RED, TextFormatting.UNDERLINE));
                    }

                    suggestion.appendSibling(new TranslationTextComponent("command.context.here")
                            .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
                    commandSource.sendErrorMessage(suggestion);
                }
            }
        }
        return true;
    }
    return false;
}