aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0075-AsyncTabCompleteEvent.patch
blob: b8062adfcabf6ff6c0a14df7d5a7a0a0673a319f (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Sun, 26 Nov 2017 13:17:09 -0500
Subject: [PATCH] AsyncTabCompleteEvent

Let plugins be able to control tab completion of commands and chat async.

This will be useful for frameworks like ACF so we can define async safe completion handlers,
and avoid going to main for tab completions.

Especially useful if you need to query a database in order to obtain the results for tab
completion, such as offline players.

Co-authored-by: Aikar <aikar@aikar.co>

diff --git a/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java b/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..9be64a95c2345433b6142d611077dedadcef9f5d
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java
@@ -0,0 +1,328 @@
+/*
+ * Copyright (c) 2017 Daniel Ennis (Aikar) MIT License
+ *
+ *  Permission is hereby granted, free of charge, to any person obtaining
+ *  a copy of this software and associated documentation files (the
+ *  "Software"), to deal in the Software without restriction, including
+ *  without limitation the rights to use, copy, modify, merge, publish,
+ *  distribute, sublicense, and/or sell copies of the Software, and to
+ *  permit persons to whom the Software is furnished to do so, subject to
+ *  the following conditions:
+ *
+ *  The above copyright notice and this permission notice shall be
+ *  included in all copies or substantial portions of the Software.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package com.destroystokyo.paper.event.server;
+
+import com.google.common.base.Preconditions;
+import io.papermc.paper.util.TransformingRandomAccessList;
+import net.kyori.adventure.text.Component;
+import net.kyori.examination.Examinable;
+import net.kyori.examination.ExaminableProperty;
+import net.kyori.examination.string.StringExaminer;
+import org.bukkit.Location;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Allows plugins to compute tab completion results asynchronously. If this event provides completions, then the standard synchronous process will not be fired to populate the results. However, the synchronous TabCompleteEvent will fire with the Async results.
+ *
+ * Only 1 process will be allowed to provide completions, the Async Event, or the standard process.
+ */
+public class AsyncTabCompleteEvent extends Event implements Cancellable {
+    @NotNull private final CommandSender sender;
+    @NotNull private final String buffer;
+    private final boolean isCommand;
+    @Nullable
+    private final Location loc;
+    private final List<Completion> completions = new ArrayList<>();
+    private final List<String> stringCompletions = new TransformingRandomAccessList<>(
+        this.completions,
+        Completion::suggestion,
+        Completion::completion
+    );
+    private boolean cancelled;
+    private boolean handled = false;
+    private boolean fireSyncHandler = true;
+
+    public AsyncTabCompleteEvent(@NotNull CommandSender sender, @NotNull String buffer, boolean isCommand, @Nullable Location loc) {
+        super(true);
+        this.sender = sender;
+        this.buffer = buffer;
+        this.isCommand = isCommand;
+        this.loc = loc;
+    }
+
+    @Deprecated
+    public AsyncTabCompleteEvent(@NotNull CommandSender sender, @NotNull List<String> completions, @NotNull String buffer, boolean isCommand, @Nullable Location loc) {
+        super(true);
+        this.sender = sender;
+        this.completions.addAll(fromStrings(completions));
+        this.buffer = buffer;
+        this.isCommand = isCommand;
+        this.loc = loc;
+    }
+
+    /**
+     * Get the sender completing this command.
+     *
+     * @return the {@link CommandSender} instance
+     */
+    @NotNull
+    public CommandSender getSender() {
+        return sender;
+    }
+
+    /**
+     * The list of completions which will be offered to the sender, in order.
+     * This list is mutable and reflects what will be offered.
+     *
+     * If this collection is not empty after the event is fired, then
+     * the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
+     * or current player names will not be called.
+     *
+     * @return a list of offered completions
+     */
+    @NotNull
+    public List<String> getCompletions() {
+        return this.stringCompletions;
+    }
+
+    /**
+     * Set the completions offered, overriding any already set.
+     * If this collection is not empty after the event is fired, then
+     * the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
+     * or current player names will not be called.
+     *
+     * The passed collection will be cloned to a new List. You must call {{@link #getCompletions()}} to mutate from here
+     *
+     * @param completions the new completions
+     */
+    public void setCompletions(@NotNull List<String> completions) {
+        if (completions == this.stringCompletions) {
+            return;
+        }
+        Preconditions.checkNotNull(completions);
+        this.completions.clear();
+        this.completions.addAll(fromStrings(completions));
+    }
+
+    /**
+     * The list of {@link Completion completions} which will be offered to the sender, in order.
+     * This list is mutable and reflects what will be offered.
+     * <p>
+     * If this collection is not empty after the event is fired, then
+     * the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
+     * or current player names will not be called.
+     *
+     * @return a list of offered completions
+     */
+    public @NotNull List<Completion> completions() {
+        return this.completions;
+    }
+
+    /**
+     * Set the {@link Completion completions} offered, overriding any already set.
+     * If this collection is not empty after the event is fired, then
+     * the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
+     * or current player names will not be called.
+     * <p>
+     * The passed collection will be cloned to a new List. You must call {{@link #completions()}} to mutate from here
+     *
+     * @param newCompletions the new completions
+     */
+    public void completions(final @NotNull List<Completion> newCompletions) {
+        Preconditions.checkNotNull(newCompletions, "new completions");
+        this.completions.clear();
+        this.completions.addAll(newCompletions);
+    }
+
+    /**
+     * Return the entire buffer which formed the basis of this completion.
+     *
+     * @return command buffer, as entered
+     */
+    @NotNull
+    public String getBuffer() {
+        return buffer;
+    }
+
+    /**
+     * @return True if it is a command being tab completed, false if it is a chat message.
+     */
+    public boolean isCommand() {
+        return isCommand;
+    }
+
+    /**
+     * @return The position looked at by the sender, or null if none
+     */
+    @Nullable
+    public Location getLocation() {
+        return loc;
+    }
+
+    /**
+     * If true, the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
+     * or current player names will not be called.
+     *
+     * @return Is completions considered handled. Always true if completions is not empty.
+     */
+    public boolean isHandled() {
+        return !completions.isEmpty() || handled;
+    }
+
+    /**
+     * Sets whether or not to consider the completion request handled.
+     * If true, the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
+     * or current player names will not be called.
+     *
+     * @param handled if this completion should be marked as being handled
+     */
+    public void setHandled(boolean handled) {
+        this.handled = handled;
+    }
+
+    private static final HandlerList handlers = new HandlerList();
+
+
+    @Override
+    public boolean isCancelled() {
+        return cancelled;
+    }
+
+    /**
+     * Will provide no completions, and will not fire the synchronous process
+     * @param cancelled true if you wish to cancel this event
+     */
+    @Override
+    public void setCancelled(boolean cancelled) {
+        this.cancelled = cancelled;
+    }
+
+    @NotNull
+    public HandlerList getHandlers() {
+        return handlers;
+    }
+
+    @NotNull
+    public static HandlerList getHandlerList() {
+        return handlers;
+    }
+
+    private static @NotNull List<Completion> fromStrings(final @NotNull List<String> strings) {
+        final List<Completion> list = new ArrayList<>();
+        for (final String it : strings) {
+            list.add(new CompletionImpl(it, null));
+        }
+        return list;
+    }
+
+    /**
+     * A rich tab completion, consisting of a string suggestion, and a nullable {@link Component} tooltip.
+     */
+    public interface Completion extends Examinable {
+        /**
+         * Get the suggestion string for this {@link Completion}.
+         *
+         * @return suggestion string
+         */
+        @NotNull String suggestion();
+
+        /**
+         * Get the suggestion tooltip for this {@link Completion}.
+         *
+         * @return tooltip component
+         */
+        @Nullable Component tooltip();
+
+        @Override
+        default @NotNull Stream<? extends ExaminableProperty> examinableProperties() {
+            return Stream.of(ExaminableProperty.of("suggestion", this.suggestion()), ExaminableProperty.of("tooltip", this.tooltip()));
+        }
+
+        /**
+         * Create a new {@link Completion} from a suggestion string.
+         *
+         * @param suggestion suggestion string
+         * @return new completion instance
+         */
+        static @NotNull Completion completion(final @NotNull String suggestion) {
+            return new CompletionImpl(suggestion, null);
+        }
+
+        /**
+         * Create a new {@link Completion} from a suggestion string and a tooltip {@link Component}.
+         *
+         * <p>If the provided component is null, the suggestion will not have a tooltip.</p>
+         *
+         * @param suggestion suggestion string
+         * @param tooltip    tooltip component, or null
+         * @return new completion instance
+         */
+        static @NotNull Completion completion(final @NotNull String suggestion, final @Nullable Component tooltip) {
+            return new CompletionImpl(suggestion, tooltip);
+        }
+    }
+
+    static final class CompletionImpl implements Completion {
+        private final String suggestion;
+        private final Component tooltip;
+
+        CompletionImpl(final @NotNull String suggestion, final @Nullable Component tooltip) {
+            this.suggestion = suggestion;
+            this.tooltip = tooltip;
+        }
+
+        @Override
+        public @NotNull String suggestion() {
+            return this.suggestion;
+        }
+
+        @Override
+        public @Nullable Component tooltip() {
+            return this.tooltip;
+        }
+
+        @Override
+        public boolean equals(final @Nullable Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || this.getClass() != o.getClass()) {
+                return false;
+            }
+            final CompletionImpl that = (CompletionImpl) o;
+            return this.suggestion.equals(that.suggestion)
+                && java.util.Objects.equals(this.tooltip, that.tooltip);
+        }
+
+        @Override
+        public int hashCode() {
+            return java.util.Objects.hash(this.suggestion, this.tooltip);
+        }
+
+        @Override
+        public @NotNull String toString() {
+            return StringExaminer.simpleEscaping().examine(this);
+        }
+    }
+}
diff --git a/src/main/java/io/papermc/paper/util/TransformingRandomAccessList.java b/src/main/java/io/papermc/paper/util/TransformingRandomAccessList.java
new file mode 100644
index 0000000000000000000000000000000000000000..6f560a51277ccbd46a9142cfa057d276118c1c7b
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/TransformingRandomAccessList.java
@@ -0,0 +1,169 @@
+package io.papermc.paper.util;
+
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.AbstractList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.RandomAccess;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Modified version of the Guava class with the same name to support add operations.
+ *
+ * @param <F> backing list element type
+ * @param <T> transformed list element type
+ */
+public final class TransformingRandomAccessList<F, T> extends AbstractList<T> implements RandomAccess {
+    final List<F> fromList;
+    final Function<? super F, ? extends T> toFunction;
+    final Function<? super T, ? extends F> fromFunction;
+
+    /**
+     * Create a new {@link TransformingRandomAccessList}.
+     *
+     * @param fromList     backing list
+     * @param toFunction   function mapping backing list element type to transformed list element type
+     * @param fromFunction function mapping transformed list element type to backing list element type
+     */
+    public TransformingRandomAccessList(
+        final @NonNull List<F> fromList,
+        final @NonNull Function<? super F, ? extends T> toFunction,
+        final @NonNull Function<? super T, ? extends F> fromFunction
+    ) {
+        this.fromList = checkNotNull(fromList);
+        this.toFunction = checkNotNull(toFunction);
+        this.fromFunction = checkNotNull(fromFunction);
+    }
+
+    @Override
+    public void clear() {
+        this.fromList.clear();
+    }
+
+    @Override
+    public T get(int index) {
+        return this.toFunction.apply(this.fromList.get(index));
+    }
+
+    @Override
+    public @NotNull Iterator<T> iterator() {
+        return this.listIterator();
+    }
+
+    @Override
+    public @NotNull ListIterator<T> listIterator(int index) {
+        return new TransformedListIterator<F, T>(this.fromList.listIterator(index)) {
+            @Override
+            T transform(F from) {
+                return TransformingRandomAccessList.this.toFunction.apply(from);
+            }
+
+            @Override
+            F transformBack(T from) {
+                return TransformingRandomAccessList.this.fromFunction.apply(from);
+            }
+        };
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return this.fromList.isEmpty();
+    }
+
+    @Override
+    public boolean removeIf(Predicate<? super T> filter) {
+        checkNotNull(filter);
+        return this.fromList.removeIf(element -> filter.test(this.toFunction.apply(element)));
+    }
+
+    @Override
+    public T remove(int index) {
+        return this.toFunction.apply(this.fromList.remove(index));
+    }
+
+    @Override
+    public int size() {
+        return this.fromList.size();
+    }
+
+    @Override
+    public T set(int i, T t) {
+        return this.toFunction.apply(this.fromList.set(i, this.fromFunction.apply(t)));
+    }
+
+    @Override
+    public void add(int i, T t) {
+        this.fromList.add(i, this.fromFunction.apply(t));
+    }
+
+    static abstract class TransformedListIterator<F, T> implements ListIterator<T>, Iterator<T> {
+        final Iterator<F> backingIterator;
+
+        TransformedListIterator(ListIterator<F> backingIterator) {
+            this.backingIterator = checkNotNull((Iterator<F>) backingIterator);
+        }
+
+        private ListIterator<F> backingIterator() {
+            return cast(this.backingIterator);
+        }
+
+        static <A> ListIterator<A> cast(Iterator<A> iterator) {
+            return (ListIterator<A>) iterator;
+        }
+
+        @Override
+        public final boolean hasPrevious() {
+            return this.backingIterator().hasPrevious();
+        }
+
+        @Override
+        public final T previous() {
+            return this.transform(this.backingIterator().previous());
+        }
+
+        @Override
+        public final int nextIndex() {
+            return this.backingIterator().nextIndex();
+        }
+
+        @Override
+        public final int previousIndex() {
+            return this.backingIterator().previousIndex();
+        }
+
+        @Override
+        public void set(T element) {
+            this.backingIterator().set(this.transformBack(element));
+        }
+
+        @Override
+        public void add(T element) {
+            this.backingIterator().add(this.transformBack(element));
+        }
+
+        abstract T transform(F from);
+
+        abstract F transformBack(T to);
+
+        @Override
+        public final boolean hasNext() {
+            return this.backingIterator.hasNext();
+        }
+
+        @Override
+        public final T next() {
+            return this.transform(this.backingIterator.next());
+        }
+
+        @Override
+        public final void remove() {
+            this.backingIterator.remove();
+        }
+    }
+}
diff --git a/src/main/java/org/bukkit/event/server/TabCompleteEvent.java b/src/main/java/org/bukkit/event/server/TabCompleteEvent.java
index 270e6d8ad4358baa256cee5f16cff281f063ce3b..4a3451af454295ac3e1b688e6665cad9fc594c82 100644
--- a/src/main/java/org/bukkit/event/server/TabCompleteEvent.java
+++ b/src/main/java/org/bukkit/event/server/TabCompleteEvent.java
@@ -29,6 +29,13 @@ public class TabCompleteEvent extends Event implements Cancellable {
     private boolean cancelled;
 
     public TabCompleteEvent(@NotNull CommandSender sender, @NotNull String buffer, @NotNull List<String> completions) {
+        // Paper start
+        this(sender, buffer, completions, sender instanceof org.bukkit.command.ConsoleCommandSender || buffer.startsWith("/"), null);
+    }
+    public TabCompleteEvent(@NotNull CommandSender sender, @NotNull String buffer, @NotNull List<String> completions, boolean isCommand, @org.jetbrains.annotations.Nullable org.bukkit.Location location) {
+        this.isCommand = isCommand;
+        this.loc = location;
+        // Paper end
         Preconditions.checkArgument(sender != null, "sender");
         Preconditions.checkArgument(buffer != null, "buffer");
         Preconditions.checkArgument(completions != null, "completions");
@@ -69,14 +76,35 @@ public class TabCompleteEvent extends Event implements Cancellable {
         return completions;
     }
 
+    // Paper start
+    private final boolean isCommand;
+    private final org.bukkit.Location loc;
+    /**
+     * @return True if it is a command being tab completed, false if it is a chat message.
+     */
+    public boolean isCommand() {
+        return isCommand;
+    }
+
+    /**
+     * @return The position looked at by the sender, or null if none
+     */
+    @org.jetbrains.annotations.Nullable
+    public org.bukkit.Location getLocation() {
+        return loc;
+    }
+    // Paper end
+
     /**
      * Set the completions offered, overriding any already set.
      *
+     * The passed collection will be cloned to a new List. You must call {{@link #getCompletions()}} to mutate from here
+     *
      * @param completions the new completions
      */
     public void setCompletions(@NotNull List<String> completions) {
         Preconditions.checkArgument(completions != null);
-        this.completions = completions;
+        this.completions = new java.util.ArrayList<>(completions); // Paper
     }
 
     @Override
diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java
index c26ea217927ba77611e6ae93f8df50a83bceb3dd..06e96e5c98f1a7a68c8b4b5e527314c1aa774e38 100644
--- a/src/test/java/org/bukkit/AnnotationTest.java
+++ b/src/test/java/org/bukkit/AnnotationTest.java
@@ -48,6 +48,8 @@ public class AnnotationTest {
         // Generic functional interface
         "org/bukkit/util/Consumer",
         // Paper start
+        "io/papermc/paper/util/TransformingRandomAccessList",
+        "io/papermc/paper/util/TransformingRandomAccessList$TransformedListIterator",
         // Timings history is broken in terms of nullability due to guavas Function defining that the param is NonNull
         "co/aikar/timings/TimingHistory$2",
         "co/aikar/timings/TimingHistory$2$1",