| 1 | package net.minecraft.commands.synchronization;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonObject;
|
| 6 | import com.mojang.brigadier.CommandDispatcher;
|
| 7 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 8 | import com.mojang.brigadier.tree.ArgumentCommandNode;
|
| 9 | import com.mojang.brigadier.tree.CommandNode;
|
| 10 | import com.mojang.brigadier.tree.LiteralCommandNode;
|
| 11 | import com.mojang.brigadier.tree.RootCommandNode;
|
| 12 | import com.mojang.logging.LogUtils;
|
| 13 | import com.mojang.serialization.JsonOps;
|
| 14 | import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
|
| 15 | import java.util.Collection;
|
| 16 | import java.util.HashSet;
|
| 17 | import java.util.Set;
|
| 18 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 19 | import net.minecraft.server.permissions.PermissionCheck;
|
| 20 | import net.minecraft.server.permissions.PermissionProviderCheck;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class ArgumentUtils {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private static final byte NUMBER_FLAG_MIN = 1;
|
| 26 | private static final byte NUMBER_FLAG_MAX = 2;
|
| 27 |
|
| 28 | public static int createNumberFlags(final boolean hasMin, final boolean hasMax) {
|
| 29 | int result = 0;
|
| 30 | if (hasMin) {
|
| 31 | result |= 1;
|
| 32 | }
|
| 33 |
|
| 34 | if (hasMax) {
|
| 35 | result |= 2;
|
| 36 | }
|
| 37 |
|
| 38 | return result;
|
| 39 | }
|
| 40 |
|
| 41 | public static boolean numberHasMin(final byte flags) {
|
| 42 | return (flags & 1) != 0;
|
| 43 | }
|
| 44 |
|
| 45 | public static boolean numberHasMax(final byte flags) {
|
| 46 | return (flags & 2) != 0;
|
| 47 | }
|
| 48 |
|
| 49 | private static <A extends ArgumentType<?>, T extends ArgumentTypeInfo.Template<A>> void serializeArgumentCap(
|
| 50 | final JsonObject result, final ArgumentTypeInfo<A, T> info, final ArgumentTypeInfo.Template<A> argumentType
|
| 51 | ) {
|
| 52 | info.serializeToJson((T)argumentType, result);
|
| 53 | }
|
| 54 |
|
| 55 | private static <T extends ArgumentType<?>> void serializeArgumentToJson(final JsonObject result, final T argument) {
|
| 56 | ArgumentTypeInfo.Template<T> template = ArgumentTypeInfos.unpack(argument);
|
| 57 | result.addProperty("type", "argument");
|
| 58 | result.addProperty("parser", String.valueOf(BuiltInRegistries.COMMAND_ARGUMENT_TYPE.getKey(template.type())));
|
| 59 | JsonObject type = new JsonObject();
|
| 60 | serializeArgumentCap(type, template.type(), template);
|
| 61 | if (!type.isEmpty()) {
|
| 62 | result.add("properties", type);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | public static <S> JsonObject serializeNodeToJson(final CommandDispatcher<S> dispatcher, final CommandNode<S> node) {
|
| 67 | JsonObject result = new JsonObject();
|
| 68 | switch (node) {
|
| 69 | case RootCommandNode<S> rootNode:
|
| 70 | result.addProperty("type", "root");
|
| 71 | break;
|
| 72 | case LiteralCommandNode<S> literalNode:
|
| 73 | result.addProperty("type", "literal");
|
| 74 | break;
|
| 75 | case ArgumentCommandNode<S, ?> argumentNode:
|
| 76 | serializeArgumentToJson(result, argumentNode.getType());
|
| 77 | break;
|
| 78 | default:
|
| 79 | LOGGER.error("Could not serialize node {} ({})!", node, node.getClass());
|
| 80 | result.addProperty("type", "unknown");
|
| 81 | }
|
| 82 |
|
| 83 | Collection<CommandNode<S>> children = node.getChildren();
|
| 84 | if (!children.isEmpty()) {
|
| 85 | JsonObject childrenObject = new JsonObject();
|
| 86 |
|
| 87 | for (CommandNode<S> child : children) {
|
| 88 | childrenObject.add(child.getName(), serializeNodeToJson(dispatcher, child));
|
| 89 | }
|
| 90 |
|
| 91 | result.add("children", childrenObject);
|
| 92 | }
|
| 93 |
|
| 94 | if (node.getCommand() != null) {
|
| 95 | result.addProperty("executable", true);
|
| 96 | }
|
| 97 |
|
| 98 | if (node.getRequirement() instanceof PermissionProviderCheck<?> permissionCheck) {
|
| 99 | JsonElement permissions = PermissionCheck.CODEC
|
| 100 | .encodeStart(JsonOps.INSTANCE, permissionCheck.test())
|
| 101 | .getOrThrow(error -> new IllegalStateException("Failed to serialize requirement: " + error));
|
| 102 | result.add("permissions", permissions);
|
| 103 | }
|
| 104 |
|
| 105 | if (node.getRedirect() != null) {
|
| 106 | Collection<String> path = dispatcher.getPath(node.getRedirect());
|
| 107 | if (!path.isEmpty()) {
|
| 108 | JsonArray target = new JsonArray();
|
| 109 |
|
| 110 | for (String piece : path) {
|
| 111 | target.add(piece);
|
| 112 | }
|
| 113 |
|
| 114 | result.add("redirect", target);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | return result;
|
| 119 | }
|
| 120 |
|
| 121 | public static <T> Set<ArgumentType<?>> findUsedArgumentTypes(final CommandNode<T> node) {
|
| 122 | Set<CommandNode<T>> visitedNodes = new ReferenceOpenHashSet<>();
|
| 123 | Set<ArgumentType<?>> result = new HashSet<>();
|
| 124 | findUsedArgumentTypes(node, result, visitedNodes);
|
| 125 | return result;
|
| 126 | }
|
| 127 |
|
| 128 | private static <T> void findUsedArgumentTypes(final CommandNode<T> node, final Set<ArgumentType<?>> output, final Set<CommandNode<T>> visitedNodes) {
|
| 129 | if (visitedNodes.add(node)) {
|
| 130 | if (node instanceof ArgumentCommandNode<T, ?> arg) {
|
| 131 | output.add(arg.getType());
|
| 132 | }
|
| 133 |
|
| 134 | node.getChildren().forEach(child -> findUsedArgumentTypes((CommandNode<T>)child, output, visitedNodes));
|
| 135 | CommandNode<T> redirect = node.getRedirect();
|
| 136 | if (redirect != null) {
|
| 137 | findUsedArgumentTypes(redirect, output, visitedNodes);
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|