|
| 1 | +package com.mt1006.mocap.command; |
| 2 | + |
| 3 | +import com.mojang.brigadier.Command; |
| 4 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 5 | +import com.mojang.brigadier.builder.RequiredArgumentBuilder; |
| 6 | +import com.mojang.brigadier.context.CommandContext; |
| 7 | +import com.mojang.brigadier.context.CommandContextBuilder; |
| 8 | +import com.mojang.brigadier.context.ParsedCommandNode; |
| 9 | +import com.mt1006.mocap.mocap.playing.PlayerData; |
| 10 | +import com.mt1006.mocap.utils.Utils; |
| 11 | +import net.minecraft.commands.CommandSourceStack; |
| 12 | +import net.minecraft.commands.Commands; |
| 13 | +import org.apache.commons.lang3.function.TriFunction; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.function.BiFunction; |
| 18 | +import java.util.function.Function; |
| 19 | + |
| 20 | +public class CommandUtils |
| 21 | +{ |
| 22 | + public static RequiredArgumentBuilder<CommandSourceStack, String> withPlayerArguments(Command<CommandSourceStack> command) |
| 23 | + { |
| 24 | + return Commands.argument("playerName", StringArgumentType.string()).executes(command) |
| 25 | + .then(Commands.literal("from_player").then(Commands.argument("skinPlayerName", StringArgumentType.greedyString()).executes(command))) |
| 26 | + .then(Commands.literal("from_file").then(Commands.argument("skinFilename", StringArgumentType.greedyString()).executes(command))) |
| 27 | + .then(Commands.literal("from_mineskin").then(Commands.argument("mineskinURL", StringArgumentType.greedyString()).executes(command))); |
| 28 | + } |
| 29 | + |
| 30 | + public static @Nullable String getString(CommandContext<?> ctx, String name) |
| 31 | + { |
| 32 | + try { return StringArgumentType.getString(ctx, name); } |
| 33 | + catch (Exception exception) { return null; } |
| 34 | + } |
| 35 | + |
| 36 | + public static PlayerData getPlayerData(CommandContext<?> ctx) |
| 37 | + { |
| 38 | + String playerName = getString(ctx, "playerName"); |
| 39 | + if (playerName == null) { return new PlayerData((String)null); } |
| 40 | + |
| 41 | + String fromPlayer = getString(ctx, "skinPlayerName"); |
| 42 | + if (fromPlayer != null) { return new PlayerData(playerName, PlayerData.SkinSource.FROM_PLAYER, fromPlayer); } |
| 43 | + |
| 44 | + String fromFile = getString(ctx, "skinFilename"); |
| 45 | + if (fromFile != null) { return new PlayerData(playerName, PlayerData.SkinSource.FROM_FILE, fromFile); } |
| 46 | + |
| 47 | + String fromMineskin = getString(ctx, "mineskinURL"); |
| 48 | + if (fromMineskin != null) { return new PlayerData(playerName, PlayerData.SkinSource.FROM_MINESKIN, fromMineskin); } |
| 49 | + |
| 50 | + return new PlayerData(playerName); |
| 51 | + } |
| 52 | + |
| 53 | + public static Command<CommandSourceStack> simpleCommand(Function<CommandSourceStack, Boolean> function) |
| 54 | + { |
| 55 | + return (ctx) -> (function.apply(ctx.getSource()) ? 1 : 0); |
| 56 | + } |
| 57 | + |
| 58 | + public static RequiredArgumentBuilder<CommandSourceStack, String> withStringArgument(BiFunction<CommandSourceStack, String, Boolean> function, String arg) |
| 59 | + { |
| 60 | + return Commands.argument(arg, StringArgumentType.string()).executes((ctx) -> stringCommand(function, ctx, arg)); |
| 61 | + } |
| 62 | + |
| 63 | + public static RequiredArgumentBuilder<CommandSourceStack, String> withTwoStringArguments(TriFunction<CommandSourceStack, String, String, Boolean> function, String arg1, String arg2) |
| 64 | + { |
| 65 | + return Commands.argument(arg1, StringArgumentType.string()) |
| 66 | + .then(Commands.argument(arg2, StringArgumentType.string()) |
| 67 | + .executes((ctx) -> twoStringCommand(function, ctx, arg1, arg2))); |
| 68 | + } |
| 69 | + |
| 70 | + private static int stringCommand(BiFunction<CommandSourceStack, String, Boolean> function, CommandContext<CommandSourceStack> ctx, String arg) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + String name = StringArgumentType.getString(ctx, arg); |
| 75 | + return function.apply(ctx.getSource(), name) ? 1 : 0; |
| 76 | + } |
| 77 | + catch (Exception exception) |
| 78 | + { |
| 79 | + Utils.sendException(exception, ctx.getSource(), "mocap.error.unable_to_get_argument"); |
| 80 | + return 0; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static int twoStringCommand(TriFunction<CommandSourceStack, String, String, Boolean> function, CommandContext<CommandSourceStack> ctx, String arg1, String arg2) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + String name1 = StringArgumentType.getString(ctx, arg1); |
| 89 | + String name2 = StringArgumentType.getString(ctx, arg2); |
| 90 | + return function.apply(ctx.getSource(), name1, name2) ? 1 : 0; |
| 91 | + } |
| 92 | + catch (Exception exception) |
| 93 | + { |
| 94 | + Utils.sendException(exception, ctx.getSource(), "mocap.error.unable_to_get_argument"); |
| 95 | + return 0; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static <T> @Nullable CommandContext<T> getFinalCommandContext(CommandContext<T> ctx) |
| 100 | + { |
| 101 | + while (true) |
| 102 | + { |
| 103 | + String command = getCommandNode(ctx, 0); |
| 104 | + if (command != null && (command.equals("mocap") || command.equals("mocap:mocap"))) { return ctx; } |
| 105 | + |
| 106 | + ctx = ctx.getChild(); |
| 107 | + if (ctx == null) { return null; } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static <T> @Nullable CommandContextBuilder<T> getFinalCommandContext(CommandContextBuilder<T> ctx) |
| 112 | + { |
| 113 | + while (true) |
| 114 | + { |
| 115 | + String command = getCommandNode(ctx, 0); |
| 116 | + if (command != null && (command.equals("mocap") || command.equals("mocap:mocap"))) { return ctx; } |
| 117 | + |
| 118 | + ctx = ctx.getChild(); |
| 119 | + if (ctx == null) { return null; } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static @Nullable String getCommandNode(CommandContext<?> ctx, int pos) |
| 124 | + { |
| 125 | + return getCommandNode(ctx.getNodes(), pos); |
| 126 | + } |
| 127 | + |
| 128 | + public static @Nullable String getCommandNode(CommandContextBuilder<?> ctx, int pos) |
| 129 | + { |
| 130 | + return getCommandNode(ctx.getNodes(), pos); |
| 131 | + } |
| 132 | + |
| 133 | + private static @Nullable String getCommandNode(List<? extends ParsedCommandNode<?>> nodes, int pos) |
| 134 | + { |
| 135 | + int size = nodes.size(); |
| 136 | + if (pos < 0) { pos += size; } |
| 137 | + if (pos >= size || pos < 0) { return null; } |
| 138 | + return nodes.get(pos).getNode().getName(); |
| 139 | + } |
| 140 | +} |
0 commit comments