Skip to content

Commit

Permalink
Added /help
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Sep 30, 2023
1 parent 479cdaf commit 26cded3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public class CommanderCommandManager {
WeatherCommand.register(DISPATCHER);
SpawnCommand.register(DISPATCHER);
PlaceCommand.register(DISPATCHER);
HelpCommand.register(DISPATCHER);
// GiveCommand.register(DISPATCHER);
// HelpCommand.register(DISPATCHER);
// GenerateCommand.register(DISPATCHER);
// ChunkCommand.register(DISPATCHER);

registerLegacyCommands();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.pedroricardo.commander.content.commands;

import com.google.common.collect.Iterables;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.tree.CommandNode;
import net.minecraft.core.lang.I18n;
import net.pedroricardo.commander.content.CommanderCommandSource;

import java.util.Map;

@SuppressWarnings("unchecked")
public class HelpCommand {
private static final SimpleCommandExceptionType FAILURE = new SimpleCommandExceptionType(() -> I18n.getInstance().translateKey("commands.help.failed"));

public static void register(CommandDispatcher<CommanderCommandSource> commandDispatcher) {
commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("help").executes(c -> {
Map<CommandNode<CommanderCommandSource>, String> map = commandDispatcher.getSmartUsage(commandDispatcher.getRoot(), (CommanderCommandSource)c.getSource());
for (String string : map.values()) {
((CommanderCommandSource)c.getSource()).sendMessage("/" + string);
}
return map.size();
})).then(RequiredArgumentBuilder.argument("command", StringArgumentType.greedyString()).executes(commandContext -> {
ParseResults<CommanderCommandSource> parseResults = commandDispatcher.parse(StringArgumentType.getString(commandContext, "command"), (CommanderCommandSource)commandContext.getSource());
if (parseResults.getContext().getNodes().isEmpty()) {
throw FAILURE.create();
}
Map<CommandNode<CommanderCommandSource>, String> map = commandDispatcher.getSmartUsage(Iterables.getLast(parseResults.getContext().getNodes()).getNode(), (CommanderCommandSource)commandContext.getSource());
for (String string : map.values()) {
((CommanderCommandSource)commandContext.getSource()).sendMessage("/" + parseResults.getReader().getString() + " " + string);
}
return map.size();
})));
}
}

0 comments on commit 26cded3

Please sign in to comment.