Search in sources :

Example 1 with QuiltClientCommandSource

use of org.quiltmc.qsl.command.api.client.QuiltClientCommandSource in project quilt-standard-libraries by QuiltMC.

the class ClientCommandInternals method executeCommand.

/**
 * Executes a client-sided command from a message.
 *
 * @param  message the command message
 *
 * @return {@code true} if the message was executed as a command client-side (and therefore should not be sent to the
 *         server), {@code false} otherwise
 */
public static boolean executeCommand(String message) {
    if (message.isEmpty()) {
        // Nothing to process
        return false;
    }
    if (message.charAt(0) != PREFIX) {
        // Incorrect prefix, won't execute anything.
        return false;
    }
    MinecraftClient client = MinecraftClient.getInstance();
    // The interface is implemented on ClientCommandSource with a mixin.
    // noinspection ConstantConditions
    QuiltClientCommandSource commandSource = (QuiltClientCommandSource) client.getNetworkHandler().getCommandSource();
    client.getProfiler().push(message);
    try {
        // Only run client commands if there are no matching server-side commands.
        String command = message.substring(1);
        CommandDispatcher<CommandSource> serverDispatcher = client.getNetworkHandler().getCommandDispatcher();
        ParseResults<CommandSource> serverResults = serverDispatcher.parse(command, client.getNetworkHandler().getCommandSource());
        if (serverResults.getReader().canRead() || isCommandInvalidOrDummy(serverResults)) {
            DISPATCHER.execute(command, commandSource);
            return true;
        } else {
            return false;
        }
    } catch (CommandSyntaxException e) {
        boolean ignored = shouldIgnore(e.getType());
        if (ignored) {
            LOGGER.debug("Syntax exception for client-side command '{}'", message, e);
        } else {
            LOGGER.warn("Syntax exception for client-side command '{}'", message, e);
        }
        if (ignored) {
            return false;
        }
        commandSource.sendError(getErrorMessage(e));
        return true;
    } catch (CommandException e) {
        LOGGER.warn("Error while executing client-side command '{}'", message, e);
        commandSource.sendError(e.getTextMessage());
        return true;
    } catch (RuntimeException e) {
        LOGGER.warn("Error while executing client-side command '{}'", message, e);
        commandSource.sendError(Text.of(e.getMessage()));
        return true;
    } finally {
        client.getProfiler().pop();
    }
}
Also used : QuiltClientCommandSource(org.quiltmc.qsl.command.api.client.QuiltClientCommandSource) MinecraftClient(net.minecraft.client.MinecraftClient) CommandException(net.minecraft.command.CommandException) QuiltClientCommandSource(org.quiltmc.qsl.command.api.client.QuiltClientCommandSource) CommandSource(net.minecraft.command.CommandSource) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Aggregations

CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 MinecraftClient (net.minecraft.client.MinecraftClient)1 CommandException (net.minecraft.command.CommandException)1 CommandSource (net.minecraft.command.CommandSource)1 QuiltClientCommandSource (org.quiltmc.qsl.command.api.client.QuiltClientCommandSource)1