diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java b/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java index 71bb607c..4721c944 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java @@ -72,11 +72,11 @@ public abstract class CommandManager { private final Map, BiConsumer> exceptionHandlers = Maps.newHashMap(); private final CommandExecutionCoordinator commandExecutionCoordinator; - private final CommandRegistrationHandler commandRegistrationHandler; private final CommandTree commandTree; private CommandSyntaxFormatter commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>(); private CommandSuggestionProcessor commandSuggestionProcessor = new FilteringCommandSuggestionProcessor<>(); + private CommandRegistrationHandler commandRegistrationHandler; /** * Create a new command manager instance @@ -87,7 +87,7 @@ public abstract class CommandManager { public CommandManager( @Nonnull final Function, CommandExecutionCoordinator> commandExecutionCoordinator, @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { - this.commandTree = CommandTree.newTree(this, commandRegistrationHandler); + this.commandTree = CommandTree.newTree(this); this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree); this.commandRegistrationHandler = commandRegistrationHandler; this.servicePipeline.registerServiceType(new TypeToken>() { @@ -388,4 +388,8 @@ public abstract class CommandManager { Optional.ofNullable(this.getExceptionHandler(clazz)).orElse(defaultHandler).accept(sender, exception); } + protected final void setCommandRegistrationHandler(@Nonnull final CommandRegistrationHandler commandRegistrationHandler) { + this.commandRegistrationHandler = commandRegistrationHandler; + } + } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java b/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java index 406d84f3..3a63e436 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java @@ -34,7 +34,6 @@ import com.intellectualsites.commands.exceptions.InvalidSyntaxException; import com.intellectualsites.commands.exceptions.NoCommandInLeafException; import com.intellectualsites.commands.exceptions.NoPermissionException; import com.intellectualsites.commands.exceptions.NoSuchCommandException; -import com.intellectualsites.commands.internal.CommandRegistrationHandler; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -63,27 +62,21 @@ public final class CommandTree { private final Node> internalTree = new Node<>(null); private final CommandManager commandManager; - private final CommandRegistrationHandler commandRegistrationHandler; - private CommandTree(@Nonnull final CommandManager commandManager, - @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { + private CommandTree(@Nonnull final CommandManager commandManager) { this.commandManager = commandManager; - this.commandRegistrationHandler = commandRegistrationHandler; } /** * Create a new command tree instance * * @param commandManager Command manager - * @param commandRegistrationHandler Command registration handler * @param Command sender type * @return New command tree */ @Nonnull - public static CommandTree newTree( - @Nonnull final CommandManager commandManager, - @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { - return new CommandTree<>(commandManager, commandRegistrationHandler); + public static CommandTree newTree(@Nonnull final CommandManager commandManager) { + return new CommandTree<>(commandManager); } /** @@ -432,7 +425,7 @@ public final class CommandTree { throw new NoCommandInLeafException(leaf); } else { final Command owningCommand = leaf.getOwningCommand(); - this.commandRegistrationHandler.registerCommand(owningCommand); + this.commandManager.getCommandRegistrationHandler().registerCommand(owningCommand); } }); diff --git a/cloud-minecraft/cloud-brigadier/src/main/java/com/intellectualsites/commands/brigadier/CloudBrigadierManager.java b/cloud-minecraft/cloud-brigadier/src/main/java/com/intellectualsites/commands/brigadier/CloudBrigadierManager.java index 24ded607..beb48ec9 100644 --- a/cloud-minecraft/cloud-brigadier/src/main/java/com/intellectualsites/commands/brigadier/CloudBrigadierManager.java +++ b/cloud-minecraft/cloud-brigadier/src/main/java/com/intellectualsites/commands/brigadier/CloudBrigadierManager.java @@ -235,19 +235,21 @@ public final class CloudBrigadierManager { /** * Create a new literal command node * + * @param label Command label * @param cloudCommand Cloud command instance * @param permissionChecker Permission checker * @param executor Command executor * @return Literal command node */ - public LiteralCommandNode createLiteralCommandNode(@Nonnull final Command cloudCommand, + public LiteralCommandNode createLiteralCommandNode(@Nonnull final String label, + @Nonnull final Command cloudCommand, @Nonnull final BiPredicate permissionChecker, @Nonnull final com.mojang.brigadier.Command executor) { final CommandTree.Node> node = this.commandManager .getCommandTree().getNamedNode(cloudCommand.getArguments().get(0).getName()); final SuggestionProvider provider = (context, builder) -> this.buildSuggestions(node.getValue(), context, builder); final LiteralArgumentBuilder literalArgumentBuilder = LiteralArgumentBuilder - .literal(cloudCommand.getArguments().get(0).getName()) + .literal(label) .requires(sender -> permissionChecker.test(sender, node.getNodeMeta().getOrDefault("permission", ""))); literalArgumentBuilder.executes(executor); final LiteralCommandNode constructedRoot = literalArgumentBuilder.build(); diff --git a/cloud-minecraft/cloud-bukkit-test/pom.xml b/cloud-minecraft/cloud-bukkit-test/pom.xml index 60c5f59e..f8b1d0de 100644 --- a/cloud-minecraft/cloud-bukkit-test/pom.xml +++ b/cloud-minecraft/cloud-bukkit-test/pom.xml @@ -76,5 +76,10 @@ cloud-annotations 1.0-SNAPSHOT + + me.lucko + commodore + 1.9 + diff --git a/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java b/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java index 2ccce983..d95c8b52 100644 --- a/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java +++ b/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java @@ -37,7 +37,9 @@ import com.intellectualsites.commands.arguments.standard.EnumArgument; import com.intellectualsites.commands.arguments.standard.FloatArgument; import com.intellectualsites.commands.arguments.standard.IntegerArgument; import com.intellectualsites.commands.arguments.standard.StringArgument; +import com.intellectualsites.commands.bukkit.BukkitCommandManager; import com.intellectualsites.commands.bukkit.BukkitCommandMetaBuilder; +import com.intellectualsites.commands.bukkit.CloudBukkitCapabilities; import com.intellectualsites.commands.bukkit.parsers.WorldArgument; import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import com.intellectualsites.commands.meta.SimpleCommandMeta; @@ -55,6 +57,7 @@ import org.bukkit.plugin.java.JavaPlugin; import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; @@ -64,10 +67,12 @@ public final class BukkitTest extends JavaPlugin { private static final int PERC_MIN = 0; private static final int PERC_MAX = 100; + private BukkitCommandManager mgr; + @Override public void onEnable() { try { - final CommandManager mgr = new PaperCommandManager<>( + mgr = new PaperCommandManager<>( this, CommandExecutionCoordinator .simpleCoordinator(), @@ -75,7 +80,11 @@ public final class BukkitTest extends JavaPlugin { Function.identity() ); - ((PaperCommandManager) mgr).registerBrigadier(); + try { + ((PaperCommandManager) mgr).registerBrigadier(); + } catch (final Exception e) { + getLogger().warning("Failed to initialize Brigadier support: " + e.getMessage()); + } final AnnotationParser annotationParser = new AnnotationParser<>(mgr, CommandSender.class, p -> @@ -181,6 +190,17 @@ public final class BukkitTest extends JavaPlugin { player.sendMessage(ChatColor.GOLD + "Your input was: " + ChatColor.AQUA + input + ChatColor.GREEN + " (" + number + ")"); } + @CommandMethod("cloud help") + private void doHelp() { + final Set capabilities = this.mgr.queryCapabilities(); + Bukkit.broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Capabilities"); + for (final CloudBukkitCapabilities capability : capabilities) { + Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "- " + ChatColor.AQUA + capability); + } + Bukkit.broadcastMessage(ChatColor.GRAY + "Using Registration Manager: " + + this.mgr.getCommandRegistrationHandler().getClass().getSimpleName()); + } + @Nonnull private SimpleCommandMeta metaWithDescription(@Nonnull final String description) { return BukkitCommandMetaBuilder.builder().withDescription(description).build(); diff --git a/cloud-minecraft/cloud-bukkit/pom.xml b/cloud-minecraft/cloud-bukkit/pom.xml index 6a69f5c5..ea2eba26 100644 --- a/cloud-minecraft/cloud-bukkit/pom.xml +++ b/cloud-minecraft/cloud-bukkit/pom.xml @@ -57,6 +57,17 @@ cloud-core 1.0-SNAPSHOT + + com.intellectualsites + cloud-brigadier + 1.0-SNAPSHOT + + + me.lucko + commodore + 1.9 + provided + diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java index ee636f9f..266089fe 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java @@ -29,13 +29,18 @@ import com.intellectualsites.commands.CommandTree; import com.intellectualsites.commands.bukkit.parsers.MaterialArgument; import com.intellectualsites.commands.bukkit.parsers.WorldArgument; import com.intellectualsites.commands.execution.CommandExecutionCoordinator; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import javax.annotation.Nonnull; +import java.util.EnumSet; +import java.util.Set; import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Command manager for the Bukkit platform, using {@link BukkitCommandSender} as the @@ -45,7 +50,13 @@ import java.util.function.Function; */ public class BukkitCommandManager extends CommandManager { + private static final int VERSION_RADIX = 10; + private static final int BRIGADIER_MINIMAL_VERSION = 13; + private static final int PAPER_BRIGADIER_VERSION = 15; + private final Plugin owningPlugin; + private final int minecraftVersion; + private final boolean paper; private final Function commandSenderMapper; private final Function backwardsCommandSenderMapper; @@ -77,6 +88,29 @@ public class BukkitCommandManager extends CommandManager { this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldArgument.WorldParser<>()); this.getParserRegistry().registerParserSupplier(TypeToken.of(Material.class), params -> new MaterialArgument.MaterialParser<>()); + + /* Try to determine the Minecraft version */ + int version = -1; + try { + final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)") + .matcher(Bukkit.getVersion()); + if (matcher.find()) { + version = Integer.parseInt(matcher.toMatchResult().group(2), + VERSION_RADIX); + } + } catch (final Exception e) { + this.owningPlugin.getLogger().severe("Failed to determine Minecraft version " + + "for cloud Bukkit capability detection"); + } + this.minecraftVersion = version; + + boolean paper = false; + try { + Class.forName("com.destroystokyo.paper.PaperConfig"); + paper = true; + } catch (final Exception ignored) { + } + this.paper = paper; } /** @@ -123,4 +157,118 @@ public class BukkitCommandManager extends CommandManager { return this.splitAliases; } + protected final void checkBrigadierCompatibility() throws BrigadierFailureException { + if (!this.queryCapability(CloudBukkitCapabilities.BRIGADIER)) { + throw new BrigadierFailureException(BrigadierFailureReason.VERSION_TOO_LOW, + new IllegalArgumentException("Version: " + this.minecraftVersion)); + } + } + + /** + * Query for a specific capability + * + * @param capability Capability + * @return {@code true} if the manager has the given capability, else {@code false} + */ + public final boolean queryCapability(@Nonnull final CloudBukkitCapabilities capability) { + return this.queryCapabilities().contains(capability); + } + + /** + * Check for the platform capabilities + * + * @return A set containing all capabilities of the instance + */ + public final Set queryCapabilities() { + if (this.paper) { + if (this.minecraftVersion >= BRIGADIER_MINIMAL_VERSION) { + if (this.minecraftVersion >= PAPER_BRIGADIER_VERSION) { + return EnumSet.of(CloudBukkitCapabilities.NATIVE_BRIGADIER, + CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION, + CloudBukkitCapabilities.BRIGADIER); + } else { + return EnumSet.of(CloudBukkitCapabilities.COMMODORE_BRIGADIER, + CloudBukkitCapabilities.BRIGADIER); + } + } else { + return EnumSet.of(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION); + } + } else { + if (this.minecraftVersion >= BRIGADIER_MINIMAL_VERSION) { + return EnumSet.of(CloudBukkitCapabilities.COMMODORE_BRIGADIER, + CloudBukkitCapabilities.BRIGADIER); + } + } + return EnumSet.noneOf(CloudBukkitCapabilities.class); + } + + /** + * Attempt to register the Brigadier mapper, and return it. + * + * @throws BrigadierFailureException If Brigadier isn't + * supported by the platform + */ + public void registerBrigadier() throws BrigadierFailureException { + this.checkBrigadierCompatibility(); + try { + final CloudCommodoreManager cloudCommodoreManager = new CloudCommodoreManager<>(this); + cloudCommodoreManager.initialize(this); + this.setCommandRegistrationHandler(cloudCommodoreManager); + } catch (final Throwable e) { + throw new BrigadierFailureException(BrigadierFailureReason.COMMODORE_NOT_PRESENT, e); + } + } + + + /** + * Reasons to explain why Brigadier failed to initialize + */ + public enum BrigadierFailureReason { + COMMODORE_NOT_PRESENT, VERSION_TOO_LOW, PAPER_BRIGADIER_INITIALIZATION_FAILURE + } + + + public static final class BrigadierFailureException extends IllegalStateException { + + private final BrigadierFailureReason reason; + + /** + * Initialize a new Brigadier failure exception + * + * @param reason Reason + */ + public BrigadierFailureException(@Nonnull final BrigadierFailureReason reason) { + this.reason = reason; + } + + /** + * Initialize a new Brigadier failure exception + * + * @param reason Reason + * @param cause Cause + */ + public BrigadierFailureException(@Nonnull final BrigadierFailureReason reason, @Nonnull final Throwable cause) { + super(cause); + this.reason = reason; + } + + /** + * Get the reason for the exception + * + * @return Reason + */ + @Nonnull + public BrigadierFailureReason getReason() { + return this.reason; + } + + @Override + public String getMessage() { + return String.format("Could not initialize Brigadier mappings. Reason: %s (%s)", + this.reason.name().toLowerCase().replace("_", " "), + this.getCause() == null ? "" : this.getCause().getMessage()); + } + + } + } diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java index b66225b6..e14605ec 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java @@ -40,7 +40,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -final class BukkitPluginRegistrationHandler implements CommandRegistrationHandler { +class BukkitPluginRegistrationHandler implements CommandRegistrationHandler { private final Map, org.bukkit.command.Command> registeredCommands = new HashMap<>(); @@ -89,6 +89,7 @@ final class BukkitPluginRegistrationHandler implements CommandRegistrationHan this.registeredCommands.put(commandArgument, bukkitCommand); this.commandMap.register(commandArgument.getName(), this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(), bukkitCommand); + this.registerExternal(commandArgument.getName(), command, bukkitCommand); if (this.bukkitCommandManager.getSplitAliases()) { for (final String alias : aliases) { @@ -102,6 +103,7 @@ final class BukkitPluginRegistrationHandler implements CommandRegistrationHan this.commandMap.register(alias, this.bukkitCommandManager.getOwningPlugin() .getName().toLowerCase(), bukkitCommand); + this.registerExternal(alias, command, aliasCommand); } } } @@ -109,5 +111,9 @@ final class BukkitPluginRegistrationHandler implements CommandRegistrationHan return true; } + protected void registerExternal(@Nonnull final String label, + @Nonnull final Command command, + @Nonnull final BukkitCommand bukkitCommand) { + } } diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/CloudBukkitCapabilities.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/CloudBukkitCapabilities.java new file mode 100644 index 00000000..6a8ebcfb --- /dev/null +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/CloudBukkitCapabilities.java @@ -0,0 +1,31 @@ +// +// 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.bukkit; + +/** + * Capabilities for the Bukkit module + */ +public enum CloudBukkitCapabilities { + BRIGADIER, COMMODORE_BRIGADIER, NATIVE_BRIGADIER, ASYNCHRONOUS_COMPLETION +} diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/CloudCommodoreManager.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/CloudCommodoreManager.java new file mode 100644 index 00000000..b7a8ec54 --- /dev/null +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/CloudCommodoreManager.java @@ -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.bukkit; + +import com.intellectualsites.commands.Command; +import com.intellectualsites.commands.brigadier.CloudBrigadierManager; +import com.intellectualsites.commands.context.CommandContext; +import com.mojang.brigadier.tree.LiteralCommandNode; +import me.lucko.commodore.Commodore; +import me.lucko.commodore.CommodoreProvider; +import org.bukkit.Bukkit; + +import javax.annotation.Nonnull; + +@SuppressWarnings("ALL") +class CloudCommodoreManager extends BukkitPluginRegistrationHandler { + + private final CloudBrigadierManager brigadierManager; + private final Commodore commodore; + + CloudCommodoreManager(@Nonnull final BukkitCommandManager commandManager) + throws BukkitCommandManager.BrigadierFailureException { + if (!CommodoreProvider.isSupported()) { + throw new BukkitCommandManager.BrigadierFailureException(BukkitCommandManager + .BrigadierFailureReason.COMMODORE_NOT_PRESENT); + } + this.commodore = CommodoreProvider.getCommodore(commandManager.getOwningPlugin()); + this.brigadierManager = new CloudBrigadierManager<>(commandManager, () -> + new CommandContext<>(commandManager.getCommandSenderMapper().apply(Bukkit.getConsoleSender()))); + } + + @Override + protected void registerExternal(@Nonnull final String label, + @Nonnull final Command command, + @Nonnull final BukkitCommand bukkitCommand) { + final com.mojang.brigadier.Command cmd = o -> 1; + final LiteralCommandNode literalCommandNode = this.brigadierManager + .createLiteralCommandNode(label, command, (o, p) -> true, cmd); + this.commodore.register(bukkitCommand, literalCommandNode, p -> p.hasPermission(command.getCommandPermission())); + } +} diff --git a/cloud-minecraft/cloud-paper/pom.xml b/cloud-minecraft/cloud-paper/pom.xml index a95c9ed8..437af852 100644 --- a/cloud-minecraft/cloud-paper/pom.xml +++ b/cloud-minecraft/cloud-paper/pom.xml @@ -51,11 +51,6 @@ cloud-bukkit 1.0-SNAPSHOT - - com.intellectualsites - cloud-brigadier - 1.0-SNAPSHOT - com.destroystokyo.paper paper-api diff --git a/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java b/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java index 0c78a9aa..4c2140d4 100644 --- a/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java +++ b/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java @@ -25,13 +25,13 @@ package com.intellectualsites.commands.paper; import com.intellectualsites.commands.CommandTree; import com.intellectualsites.commands.bukkit.BukkitCommandManager; +import com.intellectualsites.commands.bukkit.CloudBukkitCapabilities; import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.function.Function; /** @@ -39,8 +39,7 @@ import java.util.function.Function; * * @param Command sender type */ -public class PaperCommandManager - extends BukkitCommandManager { +public class PaperCommandManager extends BukkitCommandManager { /** * Construct a new Paper command manager @@ -58,27 +57,29 @@ public class PaperCommandManager @Nonnull final Function backwardsCommandSenderMapper) throws Exception { super(owningPlugin, commandExecutionCoordinator, commandSenderMapper, backwardsCommandSenderMapper); + } /** - * Attempt to register the Brigadier mapper, and return it. + * Register Brigadier mappings using the native paper events * - * @return {@link PaperBrigadierListener} instance, if it could be created. If it cannot - * be created {@code null} is returned + * @throws BrigadierFailureException Exception thrown if the mappings cannot be registered */ - @Nullable - public PaperBrigadierListener registerBrigadier() { - try { - final PaperBrigadierListener brigadierListener = new PaperBrigadierListener<>(this); - Bukkit.getPluginManager().registerEvents(brigadierListener, - this.getOwningPlugin()); - this.setSplitAliases(true); - return brigadierListener; - } catch (final Throwable e) { - this.getOwningPlugin().getLogger().severe("Failed to register Brigadier listener"); - e.printStackTrace(); + @Override + public void registerBrigadier() throws BrigadierFailureException { + this.checkBrigadierCompatibility(); + if (!this.queryCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) { + super.registerBrigadier(); + } else { + try { + final PaperBrigadierListener brigadierListener = new PaperBrigadierListener<>(this); + Bukkit.getPluginManager().registerEvents(brigadierListener, + this.getOwningPlugin()); + this.setSplitAliases(true); + } catch (final Throwable e) { + throw new BrigadierFailureException(BrigadierFailureReason.PAPER_BRIGADIER_INITIALIZATION_FAILURE, e); + } } - return null; } } diff --git a/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityPluginRegistrationHandler.java b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityPluginRegistrationHandler.java index 64cd5de4..b3687777 100644 --- a/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityPluginRegistrationHandler.java +++ b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityPluginRegistrationHandler.java @@ -74,7 +74,7 @@ final class VelocityPluginRegistrationHandler implements CommandRegistrationH } final List aliases = ((StaticArgument) argument).getAlternativeAliases(); final BrigadierCommand brigadierCommand = new BrigadierCommand( - this.brigadierManager.createLiteralCommandNode((Command) command, + this.brigadierManager.createLiteralCommandNode(command.getArguments().get(0).getName(), (Command) command, (c, p) -> this.manager.hasPermission( this.manager.getCommandSenderMapper() .apply(c), p),