Java Code Examples for com.mojang.brigadier.tree.CommandNode#getCommand()

The following examples show how to use com.mojang.brigadier.tree.CommandNode#getCommand() . 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: CommandDispatcher.java    From brigadier with MIT License 6 votes vote down vote up
private void getAllUsage(final CommandNode<S> node, final S source, final ArrayList<String> result, final String prefix, final boolean restricted) {
    if (restricted && !node.canUse(source)) {
        return;
    }

    if (node.getCommand() != null) {
        result.add(prefix);
    }

    if (node.getRedirect() != null) {
        final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText();
        result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + redirect : prefix + ARGUMENT_SEPARATOR + redirect);
    } else if (!node.getChildren().isEmpty()) {
        for (final CommandNode<S> child : node.getChildren()) {
            getAllUsage(child, source, result, prefix.isEmpty() ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText(), restricted);
        }
    }
}
 
Example 2
Source File: AvailableCommands.java    From Velocity with MIT License 4 votes vote down vote up
private static void serializeNode(CommandNode<Object> node, ByteBuf buf,
    Object2IntMap<CommandNode<Object>> idMappings) {
  byte flags = 0;
  if (node.getRedirect() != null) {
    flags |= FLAG_IS_REDIRECT;
  }
  if (node.getCommand() != null) {
    flags |= FLAG_EXECUTABLE;
  }

  if (node instanceof RootCommandNode<?>) {
    flags |= NODE_TYPE_ROOT;
  } else if (node instanceof LiteralCommandNode<?>) {
    flags |= NODE_TYPE_LITERAL;
  } else if (node instanceof ArgumentCommandNode<?, ?>) {
    flags |= NODE_TYPE_ARGUMENT;
    if (((ArgumentCommandNode) node).getCustomSuggestions() != null) {
      flags |= FLAG_HAS_SUGGESTIONS;
    }
  } else {
    throw new IllegalArgumentException("Unknown node type " + node.getClass().getName());
  }

  buf.writeByte(flags);
  ProtocolUtils.writeVarInt(buf, node.getChildren().size());
  for (CommandNode<Object> child : node.getChildren()) {
    ProtocolUtils.writeVarInt(buf, idMappings.getInt(child));
  }
  if (node.getRedirect() != null) {
    ProtocolUtils.writeVarInt(buf, idMappings.getInt(node.getRedirect()));
  }

  if (node instanceof ArgumentCommandNode<?, ?>) {
    ProtocolUtils.writeString(buf, node.getName());
    ArgumentPropertyRegistry.serialize(buf, ((ArgumentCommandNode) node).getType());

    if (((ArgumentCommandNode) node).getCustomSuggestions() != null) {
      // The unchecked cast is required, but it is not particularly relevant because we check for
      // a more specific type later. (Even then, we only pull out one field.)
      @SuppressWarnings("unchecked")
      SuggestionProvider<Object> provider = ((ArgumentCommandNode) node).getCustomSuggestions();

      if (!(provider instanceof ProtocolSuggestionProvider)) {
        throw new IllegalArgumentException("Suggestion provider " + provider.getClass().getName()
          + " is not valid.");
      }

      ProtocolUtils.writeString(buf, ((ProtocolSuggestionProvider) provider).name);
    }
  } else if (node instanceof LiteralCommandNode<?>) {
    ProtocolUtils.writeString(buf, node.getName());
  }
}
 
Example 3
Source File: CommandDispatcher.java    From brigadier with MIT License 4 votes vote down vote up
@Override
public boolean test(final CommandNode<S> input) {
    return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
}
 
Example 4
Source File: CommandDispatcher.java    From brigadier with MIT License 3 votes vote down vote up
/**
 * Gets the possible executable commands from a specified node.
 *
 * <p>You may use {@link #getRoot()} as a target to get usage data for the entire command tree.</p>
 *
 * <p>The returned syntax will be in "smart" form: {@code <param>}, {@code literal}, {@code [optional]} and {@code (either|or)}.
 * These forms may be mixed and matched to provide as much information about the child nodes as it can, without being too verbose.
 * For example, a required literal "foo" followed by an optional param "int" can be compressed into one string:</p>
 * <ul>
 *     <li>{@code foo [<int>]}</li>
 * </ul>
 *
 * <p>The path to the specified node will <b>not</b> be prepended to the output, as there can theoretically be many
 * ways to reach a given node. It will only give you paths relative to the specified node, not absolute from root.</p>
 *
 * <p>The returned usage will be restricted to only commands that the provided {@code source} can use.</p>
 *
 * @param node target node to get child usage strings for
 * @param source a custom "source" object, usually representing the originator of this command
 * @return array of full usage strings under the target node
 */
public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
    final Map<CommandNode<S>, String> result = new LinkedHashMap<>();

    final boolean optional = node.getCommand() != null;
    for (final CommandNode<S> child : node.getChildren()) {
        final String usage = getSmartUsage(child, source, optional, false);
        if (usage != null) {
            result.put(child, usage);
        }
    }
    return result;
}