All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.jorel.commandapi.Brigadier Maven / Gradle / Ivy

There is a newer version: 9.5.3
Show newest version
/*******************************************************************************
 * Copyright 2018, 2020 Jorel Ali (Skepter) - MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *******************************************************************************/
package dev.jorel.commandapi;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.tree.RootCommandNode;
import dev.jorel.commandapi.arguments.AbstractArgument;
import dev.jorel.commandapi.arguments.Literal;
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;

import java.util.Collections;
import java.util.List;
import java.util.function.BiPredicate;

/**
 * The Brigadier class is used to access some of the internals of the CommandAPI
 * so you can use the CommandAPI alongside Mojang's com.mojang.brigadier package
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public final class Brigadier {

	// Cannot be instantiated
	private Brigadier() {
	}

	/**
	 * Returns the Brigadier CommandDispatcher tree that is used internally by the
	 * CommandAPI. Modifying this CommandDispatcher tree before the server finishes
	 * loading will still keep any changes made to it. For example, adding a new
	 * node to this tree will keep the node once the server has finished loading.
	 * 
	 * @return The CommandAPI's internal CommandDispatcher instance
	 */
	public static CommandDispatcher getCommandDispatcher() {
		return CommandAPIHandler.getInstance().getPlatform().getBrigadierDispatcher();
	}

	/**
	 * Returns the root node of the current CommandDispatcher. This is the
	 * equivalent of running
	 * 
	 * 
	 * Brigadier.getCommandDispatcher().getRoot();
	 * 
	 * 
	 * @return The Brigadier CommandDispatcher's root node
	 */
	public static RootCommandNode getRootNode() {
		return getCommandDispatcher().getRoot();
	}

	/**
	 * Creates a new literal argument builder from a CommandAPI LiteralArgument
	 * 
	 * @param literalArgument the LiteralArgument to convert from
	 * @return a LiteralArgumentBuilder that represents the literal
	 */
	public static >
	LiteralArgumentBuilder fromLiteralArgument(Literal literalArgument) {
		CommandAPIHandler handler = (CommandAPIHandler) CommandAPIHandler.getInstance();
		return handler.getLiteralArgumentBuilderArgument(literalArgument.getLiteral(), literalArgument.instance().getArgumentPermission(), literalArgument.instance().getRequirements());
	}

	/**
	 * Constructs a RedirectModifier from a predicate that uses a command sender and
	 * some arguments. RedirectModifiers can be used with Brigadier's
	 * fork() method to invoke other nodes in the CommandDispatcher
	 * tree. You would use this method as shown:
	 * 
	 * 
	 * Brigadier.fromPredicate((sender, args) -> {
	 *     ...
	 * }, arguments);
	 * 
* * @param predicate the predicate to test * @param args the arguments that the sender has filled in * @return a RedirectModifier that encapsulates the provided predicate */ public static > RedirectModifier fromPredicate(BiPredicate predicate, List args) { return cmdCtx -> { if (predicate.test(getCommandSenderFromContext(cmdCtx), parseArguments(cmdCtx, args))) { return Collections.singleton(cmdCtx.getSource()); } else { return Collections.emptyList(); } }; } /** * Converts a CommandAPICommand into a Brigadier Command * * @param command the command to convert * @return a Brigadier Command object that represents the provided command */ public static , CommandSender> Command fromCommand(AbstractCommandAPICommand command) { // Need to cast base handler to make it realize we're using the same CommandSender class CommandAPIHandler handler = (CommandAPIHandler) CommandAPIHandler.getInstance(); return handler.generateCommand((Argument[]) command.getArguments().toArray(AbstractArgument[]::new), command.getExecutor(), command.isConverted()); } /** * Constructs a RequiredArgumentBuilder from a given argument within a command * declaration. For example: * *
	 * List<Argument> arguments = new ArrayList<>();
	 * arguments.add(new IntegerArgument("hello"));
	 * 
	 * RequiredArgumentBuilder argBuilder = Brigadier.fromArguments(arguments, "hello");
	 * 
* * @param args the List of arguments which you typically declare for * commands * @param argument the argument you want to specify * @return a RequiredArgumentBuilder that represents the provided argument */ public static > RequiredArgumentBuilder fromArgument(List args, Argument argument) { CommandAPIHandler handler = (CommandAPIHandler) CommandAPIHandler.getInstance(); return handler.getRequiredArgumentBuilderDynamic((Argument[]) args.toArray(AbstractArgument[]::new), argument); } /** * Constructs a RequiredArgumentBuilder from a given argument * * @param argument the argument to create a RequiredArgumentBuilder from * @return a RequiredArgumentBuilder that represents the provided argument */ public static > RequiredArgumentBuilder fromArgument(Argument argument) { CommandAPIHandler handler = (CommandAPIHandler) CommandAPIHandler.getInstance(); return handler.getRequiredArgumentBuilderDynamic((Argument[]) new AbstractArgument[] { argument }, argument); } /** * Converts an argument and a list of arguments to a Brigadier * SuggestionProvider * * @param argument the argument to convert to suggestions * @param args the list of arguments * @return a SuggestionProvider that suggests the overridden suggestions for the * specified argument */ public static > SuggestionProvider toSuggestions(Argument argument, List args) { CommandAPIHandler handler = (CommandAPIHandler) CommandAPIHandler.getInstance(); return handler.toSuggestions(argument, (Argument[]) args.toArray(AbstractArgument[]::new), true); } /** * Parses arguments into their respective objects with a given command context. * This method effectively performs the "parse" step in an argument's class and * returns an Object[] which maps directly to the input List with the values * generated via parsing. * * @param cmdCtx the command context used to parse the command arguments * @param args the list of arguments to parse * @return an array of Objects which hold the results of the argument parsing * step * @throws CommandSyntaxException if there was an error during parsing */ public static > Object[] parseArguments(CommandContext cmdCtx, List args) throws CommandSyntaxException { CommandAPIHandler handler = (CommandAPIHandler) CommandAPIHandler.getInstance(); return handler.argsToCommandArgs(cmdCtx, (Argument[]) args.toArray(AbstractArgument[]::new)).args(); } /** * Gets a Brigadier source object (e.g. CommandListenerWrapper or * CommandSourceStack) from a Bukkit CommandSender. This source object is the * same object you would get from a command context. * * @param sender the Bukkit CommandSender to convert into a Brigadier source * object * @return a Brigadier source object representing the provided Bukkit * CommandSender */ public static Object getBrigadierSourceFromCommandSender(CommandSender sender) { CommandAPIPlatform platform = (CommandAPIPlatform) CommandAPIHandler.getInstance().getPlatform(); return platform.getBrigadierSourceFromCommandSender(platform.wrapCommandSender(sender)); } /** * Returns a Bukkit CommandSender from a Brigadier CommandContext * * @param cmdCtx the command context to get the CommandSender from * @return a Bukkit CommandSender from the provided Brigadier CommandContext */ public static CommandSender getCommandSenderFromContext(CommandContext cmdCtx) { CommandAPIPlatform platform = (CommandAPIPlatform) CommandAPIHandler.getInstance().getPlatform(); // For some reason putting this on one line doesn't work - very weird AbstractCommandSender abstractSender = platform.getSenderForCommand(cmdCtx, false); return abstractSender.getSource(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy