Add support for Commodore mappings

This commit is contained in:
Alexander Söderberg 2020-09-20 16:44:30 +02:00
parent b80e33503f
commit 1c831a3bcf
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
13 changed files with 320 additions and 42 deletions

View file

@ -72,11 +72,11 @@ public abstract class CommandManager<C> {
private final Map<Class<? extends Exception>, BiConsumer<C, ? extends Exception>> exceptionHandlers = Maps.newHashMap();
private final CommandExecutionCoordinator<C> commandExecutionCoordinator;
private final CommandRegistrationHandler commandRegistrationHandler;
private final CommandTree<C> commandTree;
private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>();
private CommandSuggestionProcessor<C> commandSuggestionProcessor = new FilteringCommandSuggestionProcessor<>();
private CommandRegistrationHandler commandRegistrationHandler;
/**
* Create a new command manager instance
@ -87,7 +87,7 @@ public abstract class CommandManager<C> {
public CommandManager(
@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> 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<CommandPreprocessor<C>>() {
@ -388,4 +388,8 @@ public abstract class CommandManager<C> {
Optional.ofNullable(this.getExceptionHandler(clazz)).orElse(defaultHandler).accept(sender, exception);
}
protected final void setCommandRegistrationHandler(@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
this.commandRegistrationHandler = commandRegistrationHandler;
}
}

View file

@ -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<C> {
private final Node<CommandArgument<C, ?>> internalTree = new Node<>(null);
private final CommandManager<C> commandManager;
private final CommandRegistrationHandler commandRegistrationHandler;
private CommandTree(@Nonnull final CommandManager<C> commandManager,
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
private CommandTree(@Nonnull final CommandManager<C> commandManager) {
this.commandManager = commandManager;
this.commandRegistrationHandler = commandRegistrationHandler;
}
/**
* Create a new command tree instance
*
* @param commandManager Command manager
* @param commandRegistrationHandler Command registration handler
* @param <C> Command sender type
* @return New command tree
*/
@Nonnull
public static <C> CommandTree<C> newTree(
@Nonnull final CommandManager<C> commandManager,
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
return new CommandTree<>(commandManager, commandRegistrationHandler);
public static <C> CommandTree<C> newTree(@Nonnull final CommandManager<C> commandManager) {
return new CommandTree<>(commandManager);
}
/**
@ -432,7 +425,7 @@ public final class CommandTree<C> {
throw new NoCommandInLeafException(leaf);
} else {
final Command<C> owningCommand = leaf.getOwningCommand();
this.commandRegistrationHandler.registerCommand(owningCommand);
this.commandManager.getCommandRegistrationHandler().registerCommand(owningCommand);
}
});

View file

@ -235,19 +235,21 @@ public final class CloudBrigadierManager<C, S> {
/**
* 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<S> createLiteralCommandNode(@Nonnull final Command<C> cloudCommand,
public LiteralCommandNode<S> createLiteralCommandNode(@Nonnull final String label,
@Nonnull final Command<C> cloudCommand,
@Nonnull final BiPredicate<S, String> permissionChecker,
@Nonnull final com.mojang.brigadier.Command<S> executor) {
final CommandTree.Node<CommandArgument<C, ?>> node = this.commandManager
.getCommandTree().getNamedNode(cloudCommand.getArguments().get(0).getName());
final SuggestionProvider<S> provider = (context, builder) -> this.buildSuggestions(node.getValue(), context, builder);
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder
.<S>literal(cloudCommand.getArguments().get(0).getName())
.<S>literal(label)
.requires(sender -> permissionChecker.test(sender, node.getNodeMeta().getOrDefault("permission", "")));
literalArgumentBuilder.executes(executor);
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();

View file

@ -76,5 +76,10 @@
<artifactId>cloud-annotations</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>me.lucko</groupId>
<artifactId>commodore</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</project>

View file

@ -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<CommandSender> mgr;
@Override
public void onEnable() {
try {
final CommandManager<CommandSender> mgr = new PaperCommandManager<>(
mgr = new PaperCommandManager<>(
this,
CommandExecutionCoordinator
.simpleCoordinator(),
@ -75,7 +80,11 @@ public final class BukkitTest extends JavaPlugin {
Function.identity()
);
try {
((PaperCommandManager<CommandSender>) mgr).registerBrigadier();
} catch (final Exception e) {
getLogger().warning("Failed to initialize Brigadier support: " + e.getMessage());
}
final AnnotationParser<CommandSender> 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<CloudBukkitCapabilities> 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();

View file

@ -57,6 +57,17 @@
<artifactId>cloud-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-brigadier</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>me.lucko</groupId>
<artifactId>commodore</artifactId>
<version>1.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View file

@ -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<C> extends CommandManager<C> {
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<CommandSender, C> commandSenderMapper;
private final Function<C, CommandSender> backwardsCommandSenderMapper;
@ -77,6 +88,29 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
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<C> extends CommandManager<C> {
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<CloudBukkitCapabilities> 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<C> 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());
}
}
}

View file

@ -40,7 +40,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
final class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHandler {
class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHandler {
private final Map<CommandArgument<?, ?>, org.bukkit.command.Command> registeredCommands = new HashMap<>();
@ -89,6 +89,7 @@ final class BukkitPluginRegistrationHandler<C> 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<C> implements CommandRegistrationHan
this.commandMap.register(alias, this.bukkitCommandManager.getOwningPlugin()
.getName().toLowerCase(),
bukkitCommand);
this.registerExternal(alias, command, aliasCommand);
}
}
}
@ -109,5 +111,9 @@ final class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHan
return true;
}
protected void registerExternal(@Nonnull final String label,
@Nonnull final Command<?> command,
@Nonnull final BukkitCommand<C> bukkitCommand) {
}
}

View file

@ -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
}

View file

@ -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<C> extends BukkitPluginRegistrationHandler<C> {
private final CloudBrigadierManager brigadierManager;
private final Commodore commodore;
CloudCommodoreManager(@Nonnull final BukkitCommandManager<C> 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<C> 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()));
}
}

View file

@ -51,11 +51,6 @@
<artifactId>cloud-bukkit</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-brigadier</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>

View file

@ -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 <C> Command sender type
*/
public class PaperCommandManager<C>
extends BukkitCommandManager<C> {
public class PaperCommandManager<C> extends BukkitCommandManager<C> {
/**
* Construct a new Paper command manager
@ -58,27 +57,29 @@ public class PaperCommandManager<C>
@Nonnull final Function<C, CommandSender> 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<C> registerBrigadier() {
@Override
public void registerBrigadier() throws BrigadierFailureException {
this.checkBrigadierCompatibility();
if (!this.queryCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
super.registerBrigadier();
} else {
try {
final PaperBrigadierListener<C> 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();
throw new BrigadierFailureException(BrigadierFailureReason.PAPER_BRIGADIER_INITIALIZATION_FAILURE, e);
}
}
return null;
}
}

View file

@ -74,7 +74,7 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
}
final List<String> aliases = ((StaticArgument<C>) argument).getAlternativeAliases();
final BrigadierCommand brigadierCommand = new BrigadierCommand(
this.brigadierManager.createLiteralCommandNode((Command<C>) command,
this.brigadierManager.createLiteralCommandNode(command.getArguments().get(0).getName(), (Command<C>) command,
(c, p) -> this.manager.hasPermission(
this.manager.getCommandSenderMapper()
.apply(c), p),