summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/IContextFilter.java
blob: a60c34ebeaf98b9727b05543f903f82163ddc26a (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package io.dico.dicore.command;

import io.dico.dicore.exceptions.checkedfunctions.CheckedConsumer;
import io.dico.dicore.exceptions.checkedfunctions.CheckedRunnable;
import org.bukkit.command.CommandSender;

import java.util.List;
import java.util.Objects;

public interface IContextFilter extends Comparable<IContextFilter> {

    /**
     * Filter the given context by this filter's criteria.
     * If the context does not match the criteria, an exception is thrown describing the problem.
     *
     * @param context the context to match
     * @throws CommandException if it doesn't match
     */
    void filterContext(ExecutionContext context) throws CommandException;

    /**
     * Filter an execution context for a direct or indirect sub command of the command that registered this filter.
     *
     * @param subContext the context for the execution
     * @param path       the path traversed from the command that registered this filter to the executed command
     */
    default void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
        filterContext(subContext);
    }

    /**
     * Get the priority of this context filter.
     * The priorities determine the order in which a command's context filters are executed.
     *
     * @return the priority
     */
    Priority getPriority();

    default boolean allowsContext(ExecutionContext context) {
        try {
            filterContext(context);
            return true;
        } catch (CommandException ex) {
            return false;
        }
    }

    /**
     * Used to sort filters in execution order. That is, filters are ordered by {@link #getPriority()}
     *
     * @param o compared filter
     * @return comparison value
     */
    @Override
    default int compareTo(IContextFilter o) {
        return getPriority().compareTo(o.getPriority());
    }

    default boolean isInheritable() {
        return false;
    }

    default IContextFilter inherit(String... components) {
        if (!isInheritable()) {
            throw new IllegalStateException("This IContextFilter cannot be inherited");
        }

        return this;
    }

    /**
     * IContextFilter priorities. Executes from top to bottom.
     */
    enum Priority {
        /**
         * This priority should have checks on the sender type.
         * Any filters on this priority are tested before permissions are.
         * This is the highest priority.
         */
        VERY_EARLY, // sender type check

        /**
         * This priority is specific to permissions.
         */
        PERMISSION,

        /**
         * Early priority. Post permissions, pre parameter-parsing.
         */
        EARLY,

        /**
         * Normal priority. Post permissions, pre parameter-parsing.
         */
        NORMAL,

        /**
         * Late priority. Post permissions, pre parameter-parsing.
         */
        LATE,

        /**
         * Very late priority. Post permissions, pre parameter-parsing.
         */
        VERY_LATE,

        /**
         * Post parameters priority. Post permissions, post parameter-parsing.
         * This is the lowest priority.
         */
        POST_PARAMETERS;

        /**
         * Get the context filter that inherits context filters from the parent of the same priority.
         * If this filter is also present at the parent, it will do the same for the parent's parent, and so on.
         *
         * @return the inheritor
         */
        public IContextFilter getInheritor() {
            return inheritor;
        }

        private static String[] addParent(String[] path, String parent) {
            String[] out = new String[path.length + 1];
            System.arraycopy(path, 0, out, 0, path.length);
            out[0] = parent;
            return out;
        }

        final IContextFilter inheritor = new IContextFilter() {
            @Override
            public void filterContext(ExecutionContext context) throws CommandException {
                ICommandAddress address = context.getAddress();

                String[] traversedPath = new String[0];
                do {
                    traversedPath = addParent(traversedPath, address.getMainKey());
                    address = address.getParent();

                    if (address != null && address.hasCommand()) {
                        boolean doBreak = true;

                        Command command = address.getCommand();
                        List<IContextFilter> contextFilterList = command.getContextFilters();
                        for (IContextFilter filter : contextFilterList) {
                            if (filter.getPriority() == Priority.this) {
                                if (filter == this) {
                                    // do the same for next parent
                                    // this method is necessary to keep traversedPath information
                                    doBreak = false;
                                } else {
                                    filter.filterSubContext(context, traversedPath);
                                }
                            }
                        }

                        if (doBreak) {
                            break;
                        }
                    }
                } while (address != null);
            }

            @Override
            public Priority getPriority() {
                return Priority.this;
            }
        };

    }

    /**
     * Ensures that only {@link org.bukkit.entity.Player} type senders can execute the command.
     */
    IContextFilter PLAYER_ONLY = filterSender(Priority.VERY_EARLY, Validate::isPlayer);

    /**
     * Ensures that only {@link org.bukkit.command.ConsoleCommandSender} type senders can execute the command.
     */
    IContextFilter CONSOLE_ONLY = filterSender(Priority.VERY_EARLY, Validate::isConsole);

    /**
     * This filter is not working as intended.
     * <p>
     * There is supposed to be a permission filter that takes a base, and appends the command's address to the base, and checks that permission.
     */
    IContextFilter INHERIT_PERMISSIONS = Priority.PERMISSION.getInheritor();

    static IContextFilter fromCheckedRunnable(Priority priority, CheckedRunnable<? extends CommandException> runnable) {
        return new IContextFilter() {
            @Override
            public void filterContext(ExecutionContext context) throws CommandException {
                runnable.checkedRun();
            }

            @Override
            public Priority getPriority() {
                return priority;
            }
        };
    }

    static IContextFilter filterSender(Priority priority, CheckedConsumer<? super CommandSender, ? extends CommandException> consumer) {
        return new IContextFilter() {
            @Override
            public void filterContext(ExecutionContext context) throws CommandException {
                consumer.checkedAccept(context.getSender());
            }

            @Override
            public Priority getPriority() {
                return priority;
            }
        };
    }

    static IContextFilter permission(String permission) {
        Objects.requireNonNull(permission);
        return filterSender(Priority.PERMISSION, sender -> Validate.isAuthorized(sender, permission));
    }

    static IContextFilter permission(String permission, String failMessage) {
        Objects.requireNonNull(permission);
        Objects.requireNonNull(failMessage);
        return filterSender(Priority.PERMISSION, sender -> Validate.isAuthorized(sender, permission, failMessage));
    }

    /**
     * Produce an inheritable permission context filter.
     * A permission component is an element in {@code permission.split("\\.")}
     *
     * @param permission              The permission that is required for the command that this is directly assigned to
     * @param componentInsertionIndex the index where any sub-components are inserted. -1 for "at the end".
     * @param failMessage             the message to send if the permission is not met
     * @return the context filter
     * @throws IllegalArgumentException if componentInsertionIndex is out of range
     */
    static IContextFilter inheritablePermission(String permission, int componentInsertionIndex, String failMessage) {
        Objects.requireNonNull(permission);
        Objects.requireNonNull(failMessage);
        if (componentInsertionIndex > permission.split("\\.").length || componentInsertionIndex < -1) {
            throw new IllegalArgumentException("componentInsertionIndex out of range");
        }


        return new IContextFilter() {
            private String getInheritedPermission(String[] components) {
                int insertedAmount = components.length;
                String[] currentComponents = permission.split("\\.");
                int currentAmount = currentComponents.length;
                String[] targetArray = new String[currentAmount + insertedAmount];

                int insertionIndex;
                //int newInsertionIndex;
                if (componentInsertionIndex == -1) {
                    insertionIndex = currentAmount;
                    //newInsertionIndex = -1;
                } else {
                    insertionIndex = componentInsertionIndex;
                    //newInsertionIndex = insertionIndex + insertedAmount;
                }

                // copy the current components up to insertionIndex
                System.arraycopy(currentComponents, 0, targetArray, 0, insertionIndex);
                // copy the new components into the array at insertionIndex
                System.arraycopy(components, 0, targetArray, insertionIndex, insertedAmount);
                // copy the current components from insertionIndex + inserted amount
                System.arraycopy(currentComponents, insertionIndex, targetArray, insertionIndex + insertedAmount, currentAmount - insertionIndex);

                return String.join(".", targetArray);
            }

            @Override
            public void filterContext(ExecutionContext context) throws CommandException {
                Validate.isAuthorized(context.getSender(), permission, failMessage);
            }

            @Override
            public void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
                Validate.isAuthorized(subContext.getSender(), getInheritedPermission(path), failMessage);
            }

            @Override
            public Priority getPriority() {
                return Priority.PERMISSION;
            }

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

            @Override
            public IContextFilter inherit(String... components) {
                int insertedAmount = components.length;
                String[] currentComponents = permission.split("\\.");
                int currentAmount = currentComponents.length;
                String[] targetArray = new String[currentAmount + insertedAmount];

                int insertionIndex;
                int newInsertionIndex;
                if (componentInsertionIndex == -1) {
                    insertionIndex = currentAmount;
                    newInsertionIndex = -1;
                } else {
                    insertionIndex = componentInsertionIndex;
                    newInsertionIndex = insertionIndex + insertedAmount;
                }

                // copy the current components up to insertionIndex
                System.arraycopy(currentComponents, 0, targetArray, 0, insertionIndex);
                // copy the new components into the array at insertionIndex
                System.arraycopy(components, 0, targetArray, insertionIndex, insertedAmount);
                // copy the current components from insertionIndex + inserted amount
                System.arraycopy(currentComponents, insertionIndex, targetArray, insertionIndex + insertedAmount, currentAmount - insertionIndex);

                return inheritablePermission(String.join(".", targetArray), newInsertionIndex, failMessage);
            }
        };
    }

}