summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/Command.java
blob: e4235297d5bba759a59022c6242cad3c78a0ddbb (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package io.dico.dicore.command;

import io.dico.dicore.command.IContextFilter.Priority;
import io.dico.dicore.command.parameter.ArgumentBuffer;
import io.dico.dicore.command.parameter.IArgumentPreProcessor;
import io.dico.dicore.command.parameter.Parameter;
import io.dico.dicore.command.parameter.ParameterList;
import io.dico.dicore.command.parameter.type.ParameterType;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public abstract class Command {
    private static final String[] EMPTY_DESCRIPTION = new String[0];
    private final ParameterList parameterList = new ParameterList();
    private final List<IContextFilter> contextFilters = new ArrayList<>(3);
    private String[] description = EMPTY_DESCRIPTION;
    private String shortDescription;

    public Command addParameter(Parameter<?, ?> parameter) {
        parameterList.addParameter(parameter);
        return this;
    }

    public <TType> Command addParameter(String name, String description, ParameterType<TType, Void> type) {
        return addParameter(new Parameter<>(name, description, type, null, false, null));
    }

    public <TType, TParamInfo> Command addParameter(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) {
        return addParameter(new Parameter<>(name, description, type, paramInfo, false, null));
    }

    public <TType> Command addFlag(String name, String description, ParameterType<TType, Void> type) {
        return addParameter(new Parameter<>('-' + name, description, type, null, true, null));
    }

    public <TType, TParamInfo> Command addFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) {
        return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, null));
    }

    public <TType> Command addAuthorizedFlag(String name, String description, ParameterType<TType, Void> type, String permission) {
        return addParameter(new Parameter<>('-' + name, description, type, null, true, permission));
    }

    public <TType, TParamInfo> Command addAuthorizedFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo, String permission) {
        return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, permission));
    }

    public Command requiredParameters(int requiredParameters) {
        parameterList.setRequiredCount(requiredParameters);
        return this;
    }

    public Command repeatFinalParameter() {
        parameterList.setRepeatFinalParameter(true);
        return this;
    }

    public Command setDescription(String... description) {
        this.description = Objects.requireNonNull(description);
        return this;
    }

    public Command setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
        return this;
    }

    public Command preprocessArguments(IArgumentPreProcessor processor) {
        parameterList.setArgumentPreProcessor(processor);
        return this;
    }

    public final ParameterList getParameterList() {
        return parameterList;
    }

    public final String[] getDescription() {
        return description.length == 0 ? description : description.clone();
    }

    public String getShortDescription() {
        return shortDescription;
    }

    /**
     * ---- CONTEXT FILTERS ----
     * Filter the contexts. For example, if the sender must be a player but it's the console,
     * throw a CommandException describing the problem.
     */
    private transient int postParameterFilterCount = 0;

    public Command addContextFilter(IContextFilter contextFilter) {
        Objects.requireNonNull(contextFilter);
        if (!contextFilters.contains(contextFilter)) {
            contextFilters.add(contextFilter);
            contextFilters.sort(null);

            if (contextFilter.getPriority().compareTo(Priority.POST_PARAMETERS) >= 0) {
                postParameterFilterCount++;
            }
        }
        return this;
    }

    public List<IContextFilter> getContextFilters() {
        return Collections.unmodifiableList(contextFilters);
    }

    public Command removeContextFilter(IContextFilter contextFilter) {
        boolean ret = contextFilters.remove(contextFilter);
        if (ret) {
            if (contextFilter.getPriority().compareTo(Priority.POST_PARAMETERS) >= 0) {
                postParameterFilterCount--;
            }
        }
        return this;
    }

    // ---- CONTROL FLOW IN COMMAND TREES ----

    public boolean isVisibleTo(CommandSender sender) {
        return true;
    }

    public boolean takePrecedenceOverSubcommand(String subCommand, ArgumentBuffer buffer) {
        return false;
    }

    // ---- EXECUTION ----

    public void execute(CommandSender sender, ICommandAddress caller, ArgumentBuffer buffer) {
        ExecutionContext executionContext = new ExecutionContext(sender, caller, this, buffer, false);

        try {
            executeWithContext(executionContext);
        } catch (Throwable t) {
            caller.getChatController().handleException(sender, executionContext, t);
        }
    }

    public void executeWithContext(ExecutionContext context) throws CommandException {
        //System.out.println("In Command.execute(sender, caller, buffer)#try{");
        int i, n;
        for (i = 0, n = contextFilters.size() - postParameterFilterCount; i < n; i++) {
            contextFilters.get(i).filterContext(context);
        }

        context.parseParameters();

        for (n = contextFilters.size(); i < n; i++) {
            contextFilters.get(i).filterContext(context);
        }

        //System.out.println("Post-contextfilters");

        String message = execute(context.getSender(), context);
        context.getAddress().getChatController().sendMessage(context.getSender(), EMessageType.RESULT, message);
    }

    public abstract String execute(CommandSender sender, ExecutionContext context) throws CommandException;

    public List<String> tabComplete(CommandSender sender, ICommandAddress caller, Location location, ArgumentBuffer buffer) {
        ExecutionContext executionContext = new ExecutionContext(sender, caller, this, buffer, true);
        try {
            return tabCompleteWithContext(executionContext, location);
        } catch (CommandException ex) {
            return Collections.emptyList();
        }
    }

    public List<String> tabCompleteWithContext(ExecutionContext context, Location location) throws CommandException {
        int i, n;
        for (i = 0, n = contextFilters.size() - postParameterFilterCount; i < n; i++) {
            contextFilters.get(i).filterContext(context);
        }

        context.parseParametersQuietly();
        return tabComplete(context.getSender(), context, location);
    }

    public List<String> tabComplete(CommandSender sender, ExecutionContext context, Location location) {
        return context.getSuggestedCompletions(location);
    }

}