summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java
blob: def0db195a070ffc5967cfed4489d76a31d6d0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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().getChatHandler().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);
    }

}