Expose the Brigadier manager from Brigadier-enabled command managers

This commit is contained in:
jmp 2020-10-24 18:40:05 -07:00 committed by Alexander Söderberg
parent 81e53ed454
commit d0209dc762
5 changed files with 92 additions and 5 deletions

View file

@ -0,0 +1,47 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// 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 cloud.commandframework.brigadier;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* This interface is implemented by command managers capable of registering commands to Brigadier.
*
* @param <C> Command sender type
*/
public interface BrigadierManagerHolder<C> {
/**
* Get the Brigadier manager instance used by this manager. This method being present
* in a command manager means the manager has the capability to register commands
* to Brigadier, but does not necessarily mean that this capability is being used.
* <p>
* In the case that Brigadier isn't used, this method should always return {@code null}.
*
* @return The Brigadier manager instance, if commands are being registered to Brigadier.
* Else, {@code null}
*/
@Nullable CloudBrigadierManager<C, ?> brigadierManager();
}

View file

@ -25,6 +25,8 @@ package cloud.commandframework.bukkit;
import cloud.commandframework.CommandManager;
import cloud.commandframework.CommandTree;
import cloud.commandframework.brigadier.BrigadierManagerHolder;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.arguments.selector.MultipleEntitySelector;
import cloud.commandframework.bukkit.arguments.selector.MultiplePlayerSelector;
import cloud.commandframework.bukkit.arguments.selector.SingleEntitySelector;
@ -53,6 +55,7 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.EnumSet;
import java.util.Set;
@ -65,7 +68,8 @@ import java.util.regex.Pattern;
*
* @param <C> Command sender type
*/
public class BukkitCommandManager<C> extends CommandManager<C> {
@SuppressWarnings("unchecked")
public class BukkitCommandManager<C> extends CommandManager<C> implements BrigadierManagerHolder<C> {
private static final int VERSION_RADIX = 10;
private static final int BRIGADIER_MINIMUM_VERSION = 13;
@ -317,6 +321,17 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
}
}
/**
* {@inheritDoc}
*/
@Override
public @Nullable CloudBrigadierManager<C, ?> brigadierManager() {
if (this.getCommandRegistrationHandler() instanceof CloudCommodoreManager) {
return ((CloudCommodoreManager<C>) this.getCommandRegistrationHandler()).brigadierManager();
}
return null;
}
/**
* Strip the plugin namespace from a plugin namespaced command. This
* will also strip the leading '/' if it's present

View file

@ -71,6 +71,10 @@ class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
this.registerWithCommodore(String.format("%s:%s", bukkitCommand.getPlugin().getName(), label).toLowerCase(), command);
}
protected @NonNull CloudBrigadierManager brigadierManager() {
return this.brigadierManager;
}
private void registerWithCommodore(
final @NonNull String label,
final @NonNull Command<?> command

View file

@ -57,10 +57,16 @@ class PaperBrigadierListener<C> implements Listener {
new BukkitBrigadierMapper<>(this.paperCommandManager, this.brigadierManager);
}
protected @NonNull CloudBrigadierManager<C, BukkitBrigadierCommandSource> brigadierManager() {
return this.brigadierManager;
}
@EventHandler
@SuppressWarnings("deprecation")
public void onCommandRegister(final com.destroystokyo.paper.event.brigadier
.@NonNull CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
public void onCommandRegister(
final com.destroystokyo.paper.event.brigadier.
@NonNull CommandRegisteredEvent<BukkitBrigadierCommandSource> event
) {
if (!(event.getCommand() instanceof PluginIdentifiableCommand)) {
return;
} else if (!((PluginIdentifiableCommand) event.getCommand())

View file

@ -24,6 +24,7 @@
package cloud.commandframework.paper;
import cloud.commandframework.CommandTree;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.BukkitCommandManager;
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.execution.CommandExecutionCoordinator;
@ -31,6 +32,7 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.function.Function;
@ -41,6 +43,8 @@ import java.util.function.Function;
*/
public class PaperCommandManager<C> extends BukkitCommandManager<C> {
private PaperBrigadierListener<C> paperBrigadierListener = null;
/**
* Construct a new Paper command manager
*
@ -96,9 +100,9 @@ public class PaperCommandManager<C> extends BukkitCommandManager<C> {
super.registerBrigadier();
} else {
try {
final PaperBrigadierListener<C> brigadierListener = new PaperBrigadierListener<>(this);
this.paperBrigadierListener = new PaperBrigadierListener<>(this);
Bukkit.getPluginManager().registerEvents(
brigadierListener,
this.paperBrigadierListener,
this.getOwningPlugin()
);
this.setSplitAliases(true);
@ -108,6 +112,17 @@ public class PaperCommandManager<C> extends BukkitCommandManager<C> {
}
}
/**
* {@inheritDoc}
*/
@Override
public @Nullable CloudBrigadierManager<C, ?> brigadierManager() {
if (this.paperBrigadierListener != null) {
return this.paperBrigadierListener.brigadierManager();
}
return super.brigadierManager();
}
/**
* 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