diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:23 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:23 +0100 |
commit | 44587e49ff1840219d9bc44844d4a3a6cd8ac5de (patch) | |
tree | 276ae9625795e9d79fc7db8592dbcb3a1af60928 /dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java | |
parent | 5e168847c2624b767deb9da310ecfdf169e0f43c (diff) |
Add dicore3-command
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java')
-rw-r--r-- | dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java new file mode 100644 index 0000000..cadf4ae --- /dev/null +++ b/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java @@ -0,0 +1,76 @@ +package io.dico.dicore.command.predef; + +import io.dico.dicore.command.*; +import io.dico.dicore.command.annotation.Range; +import io.dico.dicore.command.parameter.ArgumentBuffer; +import io.dico.dicore.command.parameter.Parameter; +import io.dico.dicore.command.parameter.type.NumberParameterType; +import org.bukkit.command.CommandSender; + +/** + * The help command + */ +public class HelpCommand extends PredefinedCommand<HelpCommand> { + private static final Parameter<Integer, Range.Memory> pageParameter; + public static final HelpCommand INSTANCE; + + private HelpCommand(boolean modifiable) { + super(modifiable); + getParameterList().addParameter(pageParameter); + getParameterList().setRequiredCount(0); + setDescription("Shows this help page"); + } + + @Override + protected HelpCommand newModifiableInstance() { + return new HelpCommand(true); + } + + @Override + public String execute(CommandSender sender, ExecutionContext context) throws CommandException { + ICommandAddress target = context.getAddress(); + if (context.getAddress().getCommand() == this) { + target = target.getParent(); + } + + context.getAddress().getChatController().sendHelpMessage(sender, context, target, context.<Integer>get("page") - 1); + return null; + } + + public static void registerAsChild(ICommandAddress address) { + registerAsChild(address, "help"); + } + + public static void registerAsChild(ICommandAddress address, String main, String... aliases) { + ((ModifiableCommandAddress) address).addChild(new ChildCommandAddress(INSTANCE, main, aliases)); + } + + static { + pageParameter = new Parameter<>("page", "the page number", + new NumberParameterType<Integer>(Integer.TYPE) { + @Override + protected Integer parse(String input) throws NumberFormatException { + return Integer.parseInt(input); + } + + @Override + protected Integer select(Number number) { + return number.intValue(); + } + + @Override + public Integer parseForContext(Parameter<Integer, Range.Memory> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException { + if (context.getAddress().getCommand() == null || context.getAddress().getCommand().getClass() != HelpCommand.class) { + // An address was executed with its help command as target + buffer.next(); + return 1; + } + return parse(parameter, context.getSender(), buffer); + } + }, + new Range.Memory(1, Integer.MAX_VALUE, 1)); + + INSTANCE = new HelpCommand(false); + } + +} |