Add asynchronous tab completion to the paper module
This commit is contained in:
parent
9d5f007e37
commit
6f0dba0bf0
14 changed files with 203 additions and 21 deletions
|
|
@ -0,0 +1,62 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// 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.intellectualsites.commands.paper;
|
||||
|
||||
import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class AsyncCommandSuggestionsListener<C> implements Listener {
|
||||
|
||||
private static final long CACHE_EXPIRATION_TIME = 30L;
|
||||
|
||||
private final PaperCommandManager<C> paperCommandManager;
|
||||
|
||||
AsyncCommandSuggestionsListener(@Nonnull final PaperCommandManager<C> paperCommandManager) {
|
||||
this.paperCommandManager = paperCommandManager;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
void onTabCompletion(@Nonnull final AsyncTabCompleteEvent event) throws Exception {
|
||||
if (event.getBuffer().isEmpty() || !event.getBuffer().startsWith("/")) {
|
||||
return;
|
||||
}
|
||||
final String[] arguments = event.getBuffer().substring(1).split(" ");
|
||||
if (paperCommandManager.getCommandTree().getNamedNode(arguments[0]) == null) {
|
||||
return;
|
||||
}
|
||||
final CommandSender sender = event.getSender();
|
||||
final C cloudSender = this.paperCommandManager.getCommandSenderMapper().apply(sender);
|
||||
final List<String> suggestions = new ArrayList<>(this.paperCommandManager.suggest(cloudSender,
|
||||
event.getBuffer().substring(1)));
|
||||
event.setCompletions(suggestions);
|
||||
event.setHandled(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -82,4 +82,12 @@ public class PaperCommandManager<C> extends BukkitCommandManager<C> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register asynchronous completions. This requires all argument parsers to be thread safe, and it
|
||||
* is up to the caller to guarantee that such is the case
|
||||
*/
|
||||
public void registerAsynchronousCompletions() {
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new AsyncCommandSuggestionsListener<>(this), this.getOwningPlugin());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue