Skip to content

Commit 1c33afd

Browse files
committed
/chelp command
1 parent 010e5a9 commit 1c33afd

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/main/java/net/earthcomputer/clientcommands/ClientCommands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
2525
CalcCommand.register(dispatcher);
2626
TempRuleCommand.register(dispatcher);
2727
RenderCommand.register(dispatcher);
28+
CHelpCommand.register(dispatcher);
2829
}
2930
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)