|
| 1 | +package net.earthcomputer.clientcommands.command; |
| 2 | + |
| 3 | +import com.google.common.collect.Iterables; |
| 4 | +import com.mojang.brigadier.CommandDispatcher; |
| 5 | +import com.mojang.brigadier.ParseResults; |
| 6 | +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; |
| 7 | +import com.mojang.brigadier.tree.CommandNode; |
| 8 | +import net.minecraft.network.chat.TextComponent; |
| 9 | +import net.minecraft.network.chat.TranslatableComponent; |
| 10 | +import net.minecraft.server.command.ServerCommandSource; |
| 11 | + |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static com.mojang.brigadier.arguments.StringArgumentType.*; |
| 16 | +import static net.earthcomputer.clientcommands.command.ClientCommandManager.*; |
| 17 | +import static net.minecraft.server.command.CommandManager.*; |
| 18 | + |
| 19 | +public class CHelpCommand { |
| 20 | + |
| 21 | + private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableComponent("commands.help.failed")); |
| 22 | + |
| 23 | + public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { |
| 24 | + addClientSideCommand("chelp"); |
| 25 | + |
| 26 | + dispatcher.register(literal("chelp") |
| 27 | + .executes(ctx -> { |
| 28 | + int cmdCount = 0; |
| 29 | + for (CommandNode<ServerCommandSource> command : dispatcher.getRoot().getChildren()) { |
| 30 | + String cmdName = command.getName(); |
| 31 | + if (isClientSideCommand(cmdName)) { |
| 32 | + Map<CommandNode<ServerCommandSource>, String> usage = dispatcher.getSmartUsage(command, ctx.getSource()); |
| 33 | + for (String u : usage.values()) { |
| 34 | + sendFeedback(new TextComponent("/" + cmdName + " " + u)); |
| 35 | + } |
| 36 | + cmdCount += usage.size(); |
| 37 | + if (usage.size() == 0) { |
| 38 | + sendFeedback(new TextComponent("/" + cmdName)); |
| 39 | + cmdCount++; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return cmdCount; |
| 44 | + }) |
| 45 | + .then(argument("command", greedyString()) |
| 46 | + .executes(ctx -> { |
| 47 | + String cmdName = getString(ctx, "command"); |
| 48 | + if (!isClientSideCommand(cmdName)) |
| 49 | + throw FAILED_EXCEPTION.create(); |
| 50 | + |
| 51 | + ParseResults<ServerCommandSource> parseResults = dispatcher.parse(cmdName, ctx.getSource()); |
| 52 | + if (parseResults.getContext().getNodes().isEmpty()) |
| 53 | + throw FAILED_EXCEPTION.create(); |
| 54 | + |
| 55 | + Map<CommandNode<ServerCommandSource>, String> usage = dispatcher.getSmartUsage(Iterables.getLast(parseResults.getContext().getNodes()).getNode(), ctx.getSource()); |
| 56 | + for (String u : usage.values()) { |
| 57 | + sendFeedback(new TextComponent("/" + cmdName + " " + u)); |
| 58 | + } |
| 59 | + |
| 60 | + return usage.size(); |
| 61 | + }))); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments