package de.rayzs.pat.utils.node; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.tree.*; import net.md_5.bungee.protocol.packet.Commands; import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; public class CommandNodeHelper { private static final Command DUMMY_COMMAND = (context) -> 0; private static SuggestionProvider DEFAULT_SUGGESTION_PROVIDER; public static void setDefaultSuggestionProvider(SuggestionProvider suggestionProvider) { DEFAULT_SUGGESTION_PROVIDER = suggestionProvider; } public static CommandNode createDummyCommandNode(String command) { return LiteralArgumentBuilder .literal(command) .executes(DUMMY_COMMAND) .then(RequiredArgumentBuilder .argument("args", (ArgumentType) StringArgumentType.greedyString()) .suggests(DEFAULT_SUGGESTION_PROVIDER) .executes(DUMMY_COMMAND)) .build(); } public static CommandNode createEmptyDummyCommandNode(String command) { return LiteralArgumentBuilder .literal(command) .executes(DUMMY_COMMAND) .then(RequiredArgumentBuilder .argument("args", (ArgumentType) StringArgumentType.greedyString()) .executes(DUMMY_COMMAND)) .build(); } private final RootCommandNode rootNode; public CommandNodeHelper(RootCommandNode rootNode) { this.rootNode = rootNode; } public List getChildrenNames() { return rootNode.getChildren().stream().map(CommandNode::getName).collect(Collectors.toList()); } public CommandNodeHelper removeIf(CommandNode node, Predicate predicate) { if (node != null && node.getRedirect() != null) node = node.getRedirect(); List objs = new ArrayList<>(); for (CommandNode n : node.getChildren()) { if (n == null) continue; String name = n.getName(); if (predicate.test(name)) { objs.add(n); } } for (Object obj : objs) { node.getChildren().remove(obj); } return this; } public CommandNodeHelper removeIf(String childName, Predicate predicate) { CommandNode node = rootNode.getChild(childName); return removeIf(node, predicate); } public CommandNodeHelper removeIf(Predicate predicate) { return removeIf(rootNode, predicate); } public CommandNodeHelper clear() { rootNode.getChildren().clear(); return this; } public CommandNodeHelper clear(String childName) { CommandNode child = rootNode.getChild(childName); if (child != null) { child.getChildren().clear(); } return this; } public CommandNodeHelper remove(Object obj) { rootNode.getChildren().remove(obj); return this; } public CommandNodeHelper remove(CommandNode node, Object obj) { node.getChildren().remove(obj); return this; } public CommandNodeHelper add(String commandPath, boolean autoSuggestions) { String[] args = commandPath.split(" "); add(rootNode, args, 0, autoSuggestions); return this; } private void add(CommandNode parent, String[] args, int index, boolean autoSuggestions) { if (index >= args.length) return; String part = args[index]; String next = index + 1 >= args.length ? null : args[index + 1]; CommandNode childNode = parent.getChild(part); if (childNode == null) { LiteralCommandNode newNode; boolean cancel = next != null && (next.equals("_-")); boolean stop = next != null && next.startsWith("%") && next.endsWith("%"); boolean skip = part.equals("-_"); if (index == 0 && part.charAt(0) == '!' && part.length() > 1) { return; } if (skip) { return; } if (!autoSuggestions || cancel || !stop && index != args.length - 1) { newNode = LiteralArgumentBuilder.literal(part) .then(RequiredArgumentBuilder .argument("args", StringArgumentType.string())) .build(); } else { newNode = LiteralArgumentBuilder.literal(part) .then(RequiredArgumentBuilder .argument("args", (ArgumentType) StringArgumentType.greedyString()) .suggests(Commands.SuggestionRegistry.ASK_SERVER)) .build(); } parent.addChild(newNode); if (stop || cancel) { return; } childNode = newNode; } add(childNode, args, index + 1, autoSuggestions); } }