package de.rayzs.pat.utils.node; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.*; import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; public class CommandNodeHelper { 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) { if (commandPath.endsWith("_-")) commandPath = commandPath.substring(0, commandPath.length() - 3); String[] args = commandPath.split(" "); add(rootNode, args, 0); return this; } private void add(CommandNode parent, String[] args, int index) { if (index >= args.length) return; String part = args[index]; CommandNode childNode = parent.getChild(part); if (childNode == null) { LiteralCommandNode newNode = LiteralArgumentBuilder.literal(part).build(); parent.addChild(newNode); childNode = newNode; } add(childNode, args, index + 1); } }