summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java
blob: a17cfb09b4de513520ea7458744e49debd4ae651 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package io.dico.dicore.command;

import io.dico.dicore.command.parameter.ArgumentBuffer;
import io.dico.dicore.command.registration.BukkitCommand;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;

import java.util.*;

public class RootCommandAddress extends ModifiableCommandAddress implements ICommandDispatcher {
    @Deprecated
    public static final RootCommandAddress INSTANCE = new RootCommandAddress();

    public RootCommandAddress() {
    }

    @Override
    public Command getCommand() {
        return null;
    }

    @Override
    public boolean isRoot() {
        return true;
    }

    @Override
    public List<String> getNames() {
        return Collections.emptyList();
    }

    @Override
    public ModifiableCommandAddress getParent() {
        return null;
    }

    @Override
    public String getMainKey() {
        return null;
    }

    @Override
    public String getAddress() {
        return "";
    }

    @Override
    public void registerToCommandMap(String fallbackPrefix, Map<String, org.bukkit.command.Command> map, EOverridePolicy overridePolicy) {
        Objects.requireNonNull(overridePolicy);
        //debugChildren(this);
        Map<String, ChildCommandAddress> children = this.children;
        Map<ChildCommandAddress, BukkitCommand> wrappers = new IdentityHashMap<>();

        for (ChildCommandAddress address : children.values()) {
            if (!wrappers.containsKey(address)) {
                wrappers.put(address, new BukkitCommand(address));
            }
        }

        for (Map.Entry<String, ChildCommandAddress> entry : children.entrySet()) {
            String key = entry.getKey();
            ChildCommandAddress address = entry.getValue();
            boolean override = overridePolicy == EOverridePolicy.OVERRIDE_ALL;
            if (!override && key.equals(address.getMainKey())) {
                override = overridePolicy == EOverridePolicy.MAIN_KEY_ONLY || overridePolicy == EOverridePolicy.MAIN_AND_FALLBACK;
            }

            registerMember(map, key, wrappers.get(address), override);

            if (fallbackPrefix != null) {
                key = fallbackPrefix + key;
                override = overridePolicy != EOverridePolicy.OVERRIDE_NONE && overridePolicy != EOverridePolicy.MAIN_KEY_ONLY;
                registerMember(map, key, wrappers.get(address), override);
            }
        }

    }

    public static void debugChildren(ModifiableCommandAddress address) {
        Collection<String> keys = address.getChildrenMainKeys();
        for (String key : keys) {
            ChildCommandAddress child = address.getChild(key);
            System.out.println(child.getAddress());
            debugChildren(child);
        }
    }

    private static void registerMember(Map<String, org.bukkit.command.Command> map,
                                       String key, org.bukkit.command.Command value, boolean override) {
        if (override) {
            map.put(key, value);
        } else {
            map.putIfAbsent(key, value);
        }
    }

    @Override
    public void unregisterFromCommandMap(Map<String, org.bukkit.command.Command> map) {
        Set<ICommandAddress> children = new HashSet<>(this.children.values());
        Iterator<Map.Entry<String, org.bukkit.command.Command>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, org.bukkit.command.Command> entry = iterator.next();
            org.bukkit.command.Command cmd = entry.getValue();
            if (cmd instanceof BukkitCommand && children.contains(((BukkitCommand) cmd).getOrigin())) {
                iterator.remove();
            }
        }
    }

    @Override
    public ModifiableCommandAddress getDeepChild(ArgumentBuffer buffer) {
        ModifiableCommandAddress cur = this;
        ChildCommandAddress child;
        while (buffer.hasNext()) {
            child = cur.getChild(buffer.next());
            if (child == null) {
                buffer.rewind();
                return cur;
            }

            cur = child;
        }
        return cur;
    }

    @Override
    public ModifiableCommandAddress getCommandTarget(CommandSender sender, ArgumentBuffer buffer) {
        ModifiableCommandAddress cur = this;
        ChildCommandAddress child;
        while (buffer.hasNext()) {
            child = cur.getChild(buffer.next());
            if (child == null
                    || (child.hasCommand() && !child.getCommand().isVisibleTo(sender))
                    || (cur.hasCommand() && cur.getCommand().takePrecedenceOverSubcommand(buffer.peekPrevious(), buffer.getUnaffectingCopy()))) {
                buffer.rewind();
                break;
            }

            cur = child;
        }

        return cur;
    }

    @Override
    public ModifiableCommandAddress getCommandTarget(ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
        CommandSender sender = context.getSender();
        ModifiableCommandAddress cur = this;
        ChildCommandAddress child;
        while (buffer.hasNext()) {
            int cursor = buffer.getCursor();

            child = cur.getChild(context, buffer);

            if (child == null
                || (context.isTabComplete() && !buffer.hasNext())
                || (child.hasCommand() && !child.getCommand().isVisibleTo(sender))
                || (cur.hasCommand() && cur.getCommand().takePrecedenceOverSubcommand(buffer.peekPrevious(), buffer.getUnaffectingCopy()))) {
                buffer.setCursor(cursor);
                break;
            }

            cur = child;

            context.setAddress(child);
            if (child.hasCommand() && child.isCommandTrailing()) {
                child.getCommand().initializeAndFilterContext(context);
                child.getCommand().execute(context.getSender(), context);
            }
        }

        return cur;
    }

    @Override
    public boolean dispatchCommand(CommandSender sender, String[] command) {
        return dispatchCommand(sender, new ArgumentBuffer(command));
    }

    @Override
    public boolean dispatchCommand(CommandSender sender, String usedLabel, String[] args) {
        return dispatchCommand(sender, new ArgumentBuffer(usedLabel, args));
    }

    @Override
    public boolean dispatchCommand(CommandSender sender, ArgumentBuffer buffer) {
        ExecutionContext context = new ExecutionContext(sender, buffer, false);

        ModifiableCommandAddress targetAddress = null;

        try {
            targetAddress = getCommandTarget(context, buffer);
            Command target = targetAddress.getCommand();

            if (target == null) {
                if (targetAddress.hasHelpCommand()) {
                    target = targetAddress.getHelpCommand().getCommand();
                } else {
                    return false;
                }
            }

            context.setCommand(target);

            if (!targetAddress.isCommandTrailing()) {
                target.initializeAndFilterContext(context);
                String message = target.execute(sender, context);
                if (message != null && !message.isEmpty()) {
                    context.sendMessage(EMessageType.RESULT, message);
                }
            }

        } catch (Throwable t) {
            if (targetAddress == null) {
                targetAddress = this;
            }
            targetAddress.getChatHandler().handleException(sender, context, t);
        }

        return true;
    }

    @Override
    public List<String> getTabCompletions(CommandSender sender, Location location, String[] args) {
        return getTabCompletions(sender, location, new ArgumentBuffer(args));
    }

    @Override
    public List<String> getTabCompletions(CommandSender sender, String usedLabel, Location location, String[] args) {
        return getTabCompletions(sender, location, new ArgumentBuffer(usedLabel, args));
    }

    @Override
    public List<String> getTabCompletions(CommandSender sender, Location location, ArgumentBuffer buffer) {
        ExecutionContext context = new ExecutionContext(sender, buffer, true);
        long start = System.currentTimeMillis();

        try {
            ICommandAddress target = getCommandTarget(context, buffer);

            List<String> out = Collections.emptyList();
            /*if (target.hasCommand()) {
                context.setCommand(target.getCommand());
                target.getCommand().initializeAndFilterContext(context);
                out = target.getCommand().tabComplete(sender, context, location);
            } else {
                out = Collections.emptyList();
            }*/

            int cursor = buffer.getCursor();
            String input;
            if (cursor >= buffer.size()) {
                input = "";
            } else {
                input = buffer.get(cursor).toLowerCase();
            }

            boolean wrapped = false;
            for (String child : target.getChildrenMainKeys()) {
                if (child.toLowerCase().startsWith(input)) {
                    if (!wrapped) {
                        out = new ArrayList<>(out);
                        wrapped = true;
                    }
                    out.add(child);
                }
            }

            return out;

        } catch (CommandException ex) {
            return Collections.emptyList();
        } finally {
            long duration = System.currentTimeMillis() - start;
            if (duration > 2) {
                System.out.println(String.format("Complete took %.3f seconds", duration / 1000.0));
            }
        }

    }
}