2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2022-08-01 21:56:28 +02:00
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
2021-06-11 14:02:28 +02:00
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.
2024-08-25 22:28:32 +02:00
Also Enforces mutability of the existing TabCompleteEvent.
2022-08-01 21:56:28 +02:00
Co-authored-by: Aikar <aikar@aikar.co>
2021-06-11 14:02:28 +02:00
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
2024-09-30 01:48:34 +02:00
index 0000000000000000000000000000000000000000..0482ecf5b84ba8e0260679049f384f3449bbe7b5
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/AsyncTabCompleteEvent.java
2024-09-30 01:48:34 +02:00
@@ -0,0 +1,333 @@
2021-06-11 14:02:28 +02:00
+/*
+ * 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;
+
2022-06-07 22:31:10 +02:00
+import com.google.common.base.Preconditions;
2022-08-01 21:56:28 +02:00
+import io.papermc.paper.util.TransformingRandomAccessList;
2024-09-29 21:52:13 +02:00
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
2022-08-01 21:56:28 +02:00
+import net.kyori.adventure.text.Component;
+import net.kyori.examination.Examinable;
+import net.kyori.examination.ExaminableProperty;
+import net.kyori.examination.string.StringExaminer;
2021-06-11 14:02:28 +02:00
+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;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2024-09-29 21:52:13 +02:00
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
2021-06-11 14:02:28 +02:00
+
+/**
2024-02-01 10:15:57 +01:00
+ * Allows plugins to compute tab completion results asynchronously.
+ * <p>
+ * 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.
+ * <p>
2021-06-11 14:02:28 +02:00
+ * Only 1 process will be allowed to provide completions, the Async Event, or the standard process.
+ */
2024-09-29 21:52:13 +02:00
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class AsyncTabCompleteEvent extends Event implements Cancellable {
2024-02-01 10:15:57 +01:00
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2024-09-29 21:52:13 +02:00
+ private final CommandSender sender;
+ private final String buffer;
2021-06-11 14:02:28 +02:00
+ private final boolean isCommand;
2024-09-29 21:52:13 +02:00
+ private final @Nullable Location location;
2022-08-01 21:56:28 +02:00
+ private final List<Completion> completions = new ArrayList<>();
+ private final List<String> stringCompletions = new TransformingRandomAccessList<>(
+ this.completions,
+ Completion::suggestion,
+ Completion::completion
+ );
2024-02-01 10:15:57 +01:00
+ private boolean handled;
2021-06-11 14:02:28 +02:00
+ private boolean cancelled;
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2024-09-29 21:52:13 +02:00
+ public AsyncTabCompleteEvent(final CommandSender sender, final String buffer, final boolean isCommand, final @Nullable Location loc) {
2022-08-01 21:56:28 +02:00
+ super(true);
+ this.sender = sender;
+ this.buffer = buffer;
+ this.isCommand = isCommand;
2024-02-01 10:15:57 +01:00
+ this.location = loc;
2022-08-01 21:56:28 +02:00
+ }
+
+ @Deprecated
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2024-09-29 21:52:13 +02:00
+ public AsyncTabCompleteEvent(final CommandSender sender, final List<String> completions, final String buffer, final boolean isCommand, final @Nullable Location loc) {
2021-06-11 14:02:28 +02:00
+ super(true);
+ this.sender = sender;
2022-08-01 21:56:28 +02:00
+ this.completions.addAll(fromStrings(completions));
2021-06-11 14:02:28 +02:00
+ this.buffer = buffer;
+ this.isCommand = isCommand;
2024-02-01 10:15:57 +01:00
+ this.location = loc;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get the sender completing this command.
+ *
+ * @return the {@link CommandSender} instance
+ */
+ public CommandSender getSender() {
2024-02-01 10:15:57 +01:00
+ return this.sender;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * The list of completions which will be offered to the sender, in order.
+ * This list is mutable and reflects what will be offered.
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * 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 List<String> getCompletions() {
2022-08-01 21:56:28 +02:00
+ return this.stringCompletions;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * The passed collection will be cloned to a new {@code List}. You must call {{@link #getCompletions()}} to mutate from here
2021-06-11 14:02:28 +02:00
+ *
+ * @param completions the new completions
+ */
2024-09-29 21:52:13 +02:00
+ public void setCompletions(final List<String> completions) {
2024-02-01 10:15:57 +01:00
+ Preconditions.checkArgument(completions != null, "Completions list cannot be null");
2022-08-01 21:56:28 +02:00
+ if (completions == this.stringCompletions) {
+ return;
+ }
+ 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
+ */
2024-09-29 21:52:13 +02:00
+ public List<Completion> completions() {
2022-08-01 21:56:28 +02:00
+ 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>
2024-02-01 10:15:57 +01:00
+ * The passed collection will be cloned to a new {@code List}. You must call {@link #completions()} to mutate from here
2022-08-01 21:56:28 +02:00
+ *
+ * @param newCompletions the new completions
+ */
2024-09-29 21:52:13 +02:00
+ public void completions(final List<Completion> newCompletions) {
2024-02-01 10:15:57 +01:00
+ Preconditions.checkArgument(newCompletions != null, "new completions cannot be null");
2022-08-01 21:56:28 +02:00
+ this.completions.clear();
+ this.completions.addAll(newCompletions);
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Return the entire buffer which formed the basis of this completion.
+ *
+ * @return command buffer, as entered
+ */
+ public String getBuffer() {
2024-02-01 10:15:57 +01:00
+ return this.buffer;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * @return {@code true} if it is a command being tab completed, {@code false} if it is a chat message.
2021-06-11 14:02:28 +02:00
+ */
+ public boolean isCommand() {
2024-02-01 10:15:57 +01:00
+ return this.isCommand;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * @return The position looked at by the sender, or {@code null} if none
2021-06-11 14:02:28 +02:00
+ */
2024-09-29 21:52:13 +02:00
+ public @Nullable Location getLocation() {
2024-03-20 21:42:29 +01:00
+ return this.location != null ? this.location.clone() : null;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * If {@code true}, the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
2021-06-11 14:02:28 +02:00
+ * or current player names will not be called.
+ *
2024-02-01 10:15:57 +01:00
+ * @return Is completions considered handled. Always {@code true} if completions is not empty.
2021-06-11 14:02:28 +02:00
+ */
+ public boolean isHandled() {
2024-02-01 10:15:57 +01:00
+ return !this.completions.isEmpty() || this.handled;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Sets whether to consider the completion request handled.
+ * If {@code true}, the standard process of calling {@link Command#tabComplete(CommandSender, String, String[])}
2021-06-11 14:02:28 +02:00
+ * or current player names will not be called.
+ *
+ * @param handled if this completion should be marked as being handled
+ */
2024-09-29 21:52:13 +02:00
+ public void setHandled(final boolean handled) {
2021-06-11 14:02:28 +02:00
+ this.handled = handled;
+ }
+
+ @Override
+ public boolean isCancelled() {
2024-02-01 10:15:57 +01:00
+ return this.cancelled;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * {@inheritDoc}
+ * <br>
2021-06-11 14:02:28 +02:00
+ * Will provide no completions, and will not fire the synchronous process
+ */
+ @Override
2024-09-29 21:52:13 +02:00
+ public void setCancelled(final boolean cancel) {
2024-02-01 10:15:57 +01:00
+ this.cancelled = cancel;
2021-06-11 14:02:28 +02:00
+ }
+
2024-09-30 01:48:34 +02:00
+ @Override
2021-06-11 14:02:28 +02:00
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
2022-08-01 21:56:28 +02:00
+
2024-09-29 21:52:13 +02:00
+ private static List<Completion> fromStrings(final List<String> suggestions) {
2024-02-01 10:15:57 +01:00
+ final List<Completion> list = new ArrayList<>(suggestions.size());
+ for (final String suggestion : suggestions) {
+ list.add(new CompletionImpl(suggestion, null));
2022-08-01 21:56:28 +02:00
+ }
+ return list;
+ }
+
+ /**
+ * A rich tab completion, consisting of a string suggestion, and a nullable {@link Component} tooltip.
+ */
+ public interface Completion extends Examinable {
2024-02-01 10:15:57 +01:00
+
2022-08-01 21:56:28 +02:00
+ /**
+ * Get the suggestion string for this {@link Completion}.
+ *
+ * @return suggestion string
+ */
2024-09-29 21:52:13 +02:00
+ String suggestion();
2022-08-01 21:56:28 +02:00
+
+ /**
+ * Get the suggestion tooltip for this {@link Completion}.
+ *
+ * @return tooltip component
+ */
+ @Nullable Component tooltip();
+
+ @Override
2024-09-29 21:52:13 +02:00
+ default Stream<? extends ExaminableProperty> examinableProperties() {
2022-08-01 21:56:28 +02:00
+ 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
+ */
2024-09-29 21:52:13 +02:00
+ static Completion completion(final String suggestion) {
2022-08-01 21:56:28 +02:00
+ return new CompletionImpl(suggestion, null);
+ }
+
+ /**
+ * Create a new {@link Completion} from a suggestion string and a tooltip {@link Component}.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * If the provided component is {@code null}, the suggestion will not have a tooltip.
2022-08-01 21:56:28 +02:00
+ *
+ * @param suggestion suggestion string
2024-02-01 10:15:57 +01:00
+ * @param tooltip tooltip component, or {@code null}
2022-08-01 21:56:28 +02:00
+ * @return new completion instance
+ */
2024-09-29 21:52:13 +02:00
+ static Completion completion(final String suggestion, final @Nullable Component tooltip) {
2022-08-01 21:56:28 +02:00
+ return new CompletionImpl(suggestion, tooltip);
+ }
+ }
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2022-08-01 21:56:28 +02:00
+ static final class CompletionImpl implements Completion {
2024-02-01 10:15:57 +01:00
+
2022-08-01 21:56:28 +02:00
+ private final String suggestion;
2024-09-29 21:52:13 +02:00
+ private final @Nullable Component tooltip;
2022-08-01 21:56:28 +02:00
+
2024-09-29 21:52:13 +02:00
+ CompletionImpl(final String suggestion, final @Nullable Component tooltip) {
2022-08-01 21:56:28 +02:00
+ this.suggestion = suggestion;
+ this.tooltip = tooltip;
+ }
+
+ @Override
2024-09-29 21:52:13 +02:00
+ public String suggestion() {
2022-08-01 21:56:28 +02:00
+ 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)
2024-02-01 10:15:57 +01:00
+ && Objects.equals(this.tooltip, that.tooltip);
2022-08-01 21:56:28 +02:00
+ }
+
+ @Override
+ public int hashCode() {
2024-02-01 10:15:57 +01:00
+ return Objects.hash(this.suggestion, this.tooltip);
2022-08-01 21:56:28 +02:00
+ }
+
+ @Override
2024-09-29 21:52:13 +02:00
+ public String toString() {
2022-08-01 21:56:28 +02:00
+ 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
2024-09-30 20:44:36 +02:00
index 0000000000000000000000000000000000000000..488250fdcdc93ca1aba5042f63fb6286db518014
2022-08-01 21:56:28 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/TransformingRandomAccessList.java
2024-09-30 20:44:36 +02:00
@@ -0,0 +1,172 @@
2022-08-01 21:56:28 +02:00
+package io.papermc.paper.util;
+
+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;
2024-09-30 20:44:36 +02:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2022-08-01 21:56:28 +02:00
+
+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
+ */
2024-09-30 20:44:36 +02:00
+@NullMarked
+@ApiStatus.Internal
2022-08-01 21:56:28 +02:00
+public final class TransformingRandomAccessList<F, T> extends AbstractList<T> implements RandomAccess {
2024-09-30 20:44:36 +02:00
+
2022-08-01 21:56:28 +02:00
+ final List<F> fromList;
+ final Function<? super F, ? extends T> toFunction;
+ final Function<? super T, ? extends F> fromFunction;
+
+ /**
+ * Create a new {@link TransformingRandomAccessList}.
+ *
2024-09-30 20:44:36 +02:00
+ * @param fromList backing list
+ * @param toFunction function mapping backing list element type to transformed list element type
2022-08-01 21:56:28 +02:00
+ * @param fromFunction function mapping transformed list element type to backing list element type
+ */
+ public TransformingRandomAccessList(
2024-09-30 20:44:36 +02:00
+ final List<F> fromList,
+ final Function<? super F, ? extends T> toFunction,
+ final Function<? super T, ? extends F> fromFunction
2022-08-01 21:56:28 +02:00
+ ) {
+ this.fromList = checkNotNull(fromList);
+ this.toFunction = checkNotNull(toFunction);
+ this.fromFunction = checkNotNull(fromFunction);
+ }
+
+ @Override
+ public void clear() {
+ this.fromList.clear();
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public T get(final int index) {
2022-08-01 21:56:28 +02:00
+ return this.toFunction.apply(this.fromList.get(index));
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public Iterator<T> iterator() {
2022-08-01 21:56:28 +02:00
+ return this.listIterator();
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public ListIterator<T> listIterator(final int index) {
+ return new TransformedListIterator<>(this.fromList.listIterator(index)) {
2022-08-01 21:56:28 +02:00
+ @Override
2024-09-30 20:44:36 +02:00
+ T transform(final F from) {
2022-08-01 21:56:28 +02:00
+ return TransformingRandomAccessList.this.toFunction.apply(from);
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ F transformBack(final T from) {
2022-08-01 21:56:28 +02:00
+ return TransformingRandomAccessList.this.fromFunction.apply(from);
+ }
+ };
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return this.fromList.isEmpty();
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public boolean removeIf(final Predicate<? super T> filter) {
2022-08-01 21:56:28 +02:00
+ checkNotNull(filter);
+ return this.fromList.removeIf(element -> filter.test(this.toFunction.apply(element)));
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public T remove(final int index) {
2022-08-01 21:56:28 +02:00
+ return this.toFunction.apply(this.fromList.remove(index));
+ }
+
+ @Override
+ public int size() {
+ return this.fromList.size();
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public T set(final int i, final T t) {
2022-08-01 21:56:28 +02:00
+ return this.toFunction.apply(this.fromList.set(i, this.fromFunction.apply(t)));
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public void add(final int i, final T t) {
2022-08-01 21:56:28 +02:00
+ this.fromList.add(i, this.fromFunction.apply(t));
+ }
+
2024-09-30 20:44:36 +02:00
+ abstract static class TransformedListIterator<F, T> implements ListIterator<T>, Iterator<T> {
+
2022-08-01 21:56:28 +02:00
+ final Iterator<F> backingIterator;
+
2024-09-30 20:44:36 +02:00
+ TransformedListIterator(final ListIterator<F> backingIterator) {
2022-08-01 21:56:28 +02:00
+ this.backingIterator = checkNotNull((Iterator<F>) backingIterator);
+ }
+
+ private ListIterator<F> backingIterator() {
+ return cast(this.backingIterator);
+ }
+
2024-09-30 20:44:36 +02:00
+ static <A> ListIterator<A> cast(final Iterator<A> iterator) {
2022-08-01 21:56:28 +02:00
+ 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
2024-09-30 20:44:36 +02:00
+ public void set(final T element) {
2022-08-01 21:56:28 +02:00
+ this.backingIterator().set(this.transformBack(element));
+ }
+
+ @Override
2024-09-30 20:44:36 +02:00
+ public void add(final T element) {
2022-08-01 21:56:28 +02:00
+ 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();
+ }
+ }
2021-06-11 14:02:28 +02:00
+}
diff --git a/src/main/java/org/bukkit/event/server/TabCompleteEvent.java b/src/main/java/org/bukkit/event/server/TabCompleteEvent.java
2024-08-25 22:28:32 +02:00
index 270e6d8ad4358baa256cee5f16cff281f063ce3b..6465e290c090d82986352d5ab7ba5dc65bd3dc17 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/event/server/TabCompleteEvent.java
+++ b/src/main/java/org/bukkit/event/server/TabCompleteEvent.java
2024-08-25 22:28:32 +02:00
@@ -29,13 +29,20 @@ public class TabCompleteEvent extends Event implements Cancellable {
2021-06-11 14:02:28 +02:00
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);
+ }
2022-06-07 19:20:30 +02:00
+ public TabCompleteEvent(@NotNull CommandSender sender, @NotNull String buffer, @NotNull List<String> completions, boolean isCommand, @org.jetbrains.annotations.Nullable org.bukkit.Location location) {
2021-06-11 14:02:28 +02:00
+ this.isCommand = isCommand;
+ this.loc = location;
+ // Paper end
2022-06-07 19:20:30 +02:00
Preconditions.checkArgument(sender != null, "sender");
Preconditions.checkArgument(buffer != null, "buffer");
Preconditions.checkArgument(completions != null, "completions");
2024-08-25 22:28:32 +02:00
this.sender = sender;
this.buffer = buffer;
- this.completions = completions;
+ this.completions = new java.util.ArrayList<>(completions); // Paper - Completions must be mutable
}
/**
2022-06-07 19:20:30 +02:00
@@ -69,14 +76,35 @@ public class TabCompleteEvent extends Event implements Cancellable {
2021-06-11 14:02:28 +02:00
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
+ */
2022-06-07 19:20:30 +02:00
+ @org.jetbrains.annotations.Nullable
2021-06-11 14:02:28 +02:00
+ public org.bukkit.Location getLocation() {
2024-03-20 21:42:29 +01:00
+ return this.loc != null ? this.loc.clone() : null;
2021-06-11 14:02:28 +02:00
+ }
+ // 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) {
2022-06-07 19:20:30 +02:00
Preconditions.checkArgument(completions != null);
2021-06-11 14:02:28 +02:00
- this.completions = completions;
2024-08-25 22:28:32 +02:00
+ this.completions = new java.util.ArrayList<>(completions); // Paper - completions must be mutable
2021-06-11 14:02:28 +02:00
}
@Override
2022-08-01 21:56:28 +02:00
diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java
2024-10-22 18:23:19 +02:00
index f8b8969ee7a0b6f7b3224ff081e35c14a398c9d0..f9e4b16a21d6cc6c9cbbe06d20c8af25e72e3ddb 100644
2022-08-01 21:56:28 +02:00
--- 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",