From f1d4e7865da799f43c300c703683c5af0a81facb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 19 Sep 2020 15:50:44 +0200 Subject: [PATCH] Add Velocity module --- README.md | 14 +- .../annotations/AnnotationParser.java | 1 - .../annotations/AnnotationParserTest.java | 2 +- .../commands/CommandTree.java | 6 +- .../parser/StandardParserRegistry.java | 5 +- .../brigadier/CloudBrigadierManager.java | 34 ++- .../commands/BukkitTest.java | 2 +- .../commands/bukkit/BukkitCommand.java | 6 +- cloud-minecraft/cloud-bungee/pom.xml | 1 + .../commands/bungee/BungeeCommandManager.java | 2 +- cloud-minecraft/cloud-velocity-test/pom.xml | 214 ++++++++++++++++++ .../cloudvelocitytest/package-info.java | 28 +++ cloud-minecraft/cloud-velocity/pom.xml | 66 ++++++ .../velocity/VelocityCommandManager.java | 89 ++++++++ .../VelocityPluginRegistrationHandler.java | 86 +++++++ .../commands/velocity/package-info.java | 28 +++ pom.xml | 4 +- 17 files changed, 566 insertions(+), 22 deletions(-) create mode 100644 cloud-minecraft/cloud-velocity-test/pom.xml create mode 100644 cloud-minecraft/cloud-velocity-test/src/main/java/com/intellectualsites/cloudvelocitytest/package-info.java create mode 100644 cloud-minecraft/cloud-velocity/pom.xml create mode 100644 cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityCommandManager.java create mode 100644 cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityPluginRegistrationHandler.java create mode 100644 cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/package-info.java diff --git a/README.md b/README.md index 15ddf6ac..4020687d 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Once the core functionality is present, the framework will offer implementation - Create a Discord implementation (JDA) - Create a Java CLI implementation (JLine3) - + ## nomenclature - **sender**: someone who is able to produce input - **argument**: an argument is something that can be parsed from a string @@ -47,7 +47,17 @@ Once the core functionality is present, the framework will offer implementation - **static argument**: a string literal - **command**: a command is a chain of arguments and a handler that acts on the parsed arguments - **command tree**: structure that contains all commands and is used to parse input into arguments - + +## modules +- **cloud-core**: Core module containing most of the cloud API, and shared implementations +- **cloud-annotations**: Annotation processing code that allows you to use annotated methods rather than builders +- **cloud-jline**: W.I.P JLine3 implementation of cloud +- **cloud-minecraft/cloud-brigadier**: Brigadier mappings for cloud +- **cloud-minecraft/cloud-bukkit**: Bukkit 1.8.8+ implementation of cloud +- **cloud-minecraft/cloud-paper**: Module that extends cloud-bukkit to add special support for Paper 1.8.8+ +- **cloud-minecraft/cloud-bungee**: BungeeCord 1.8.8+ implementation of Cloud +- **cloud-minecraft/cloud-velocity**: Velocity v1.1.0 implementation of cloud + ## links - Discord: https://discord.gg/KxkjDVg diff --git a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java index f0909f3f..baa3d4c0 100644 --- a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java +++ b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java @@ -55,7 +55,6 @@ import java.util.regex.Pattern; * Parser that parses class instances {@link com.intellectualsites.commands.Command commands} * * @param Command sender type - * @param Command meta type */ public final class AnnotationParser { diff --git a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java index b5986211..1825ffd6 100644 --- a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java +++ b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java @@ -38,7 +38,7 @@ import java.util.concurrent.CompletionException; class AnnotationParserTest { private static CommandManager manager; - private static AnnotationParser annotationParser; + private static AnnotationParser annotationParser; @BeforeAll static void setup() { 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 e89aba4c..ce93a975 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java @@ -447,10 +447,8 @@ public final class CommandTree { chain = chain.subList(1, chain.size()); // Go through all nodes from the tail upwards until a collision occurs for (final Node> commandArgumentNode : chain) { - if (commandArgumentNode.nodeMeta.containsKey("permission") && !commandArgumentNode.nodeMeta.get("permission") - .equalsIgnoreCase( - node.nodeMeta - .get("permission"))) { + if (commandArgumentNode.nodeMeta.containsKey("permission") + && !commandArgumentNode.nodeMeta.get("permission").equalsIgnoreCase(node.nodeMeta.get("permission"))) { commandArgumentNode.nodeMeta.put("permission", ""); } else { commandArgumentNode.nodeMeta.put("permission", node.nodeMeta.get("permission")); diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java index 32c908e0..3140d713 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java @@ -148,9 +148,8 @@ public final class StandardParserRegistry implements ParserRegistry { if (producer == null) { /* Give enums special treatment */ if (actualType.isSubtypeOf(Enum.class)) { - @SuppressWarnings("all") final EnumArgument.EnumParser enumArgument = new EnumArgument.EnumParser((Class) - actualType - .getRawType()); + @SuppressWarnings("all") final EnumArgument.EnumParser enumArgument + = new EnumArgument.EnumParser((Class) actualType.getRawType()); // noinspection all return Optional.of(enumArgument); } 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 048959b5..564928e2 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 @@ -239,6 +239,33 @@ public final class CloudBrigadierManager { return new Pair<>(StringArgumentType.string(), false); } + /** + * Create a new literal command node + * + * @param cloudCommand Cloud command instance + * @param permissionChecker Permission checker + * @param executor Command executor + * @return Literal command node + */ + public LiteralCommandNode createLiteralCommandNode(@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()) + .requires(sender -> permissionChecker.test(sender, node.getNodeMeta().getOrDefault("permission", ""))); + if (node.isLeaf() && node.getValue() != null) { + literalArgumentBuilder.executes(executor); + } + final LiteralCommandNode constructedRoot = literalArgumentBuilder.build(); + for (final CommandTree.Node> child : node.getChildren()) { + constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor, provider).build()); + } + return constructedRoot; + } + /** * Create a literal command from Brigadier command info, and a cloud command instance * @@ -297,7 +324,7 @@ public final class CloudBrigadierManager { @Nonnull private CompletableFuture buildSuggestions(@Nonnull final CommandArgument argument, - @Nonnull final CommandContext s, + @Nonnull final com.mojang.brigadier.context.CommandContext s, @Nonnull final SuggestionsBuilder builder) { final CommandContext commandContext = this.dummyContextProvider.get(); final LinkedList inputQueue = new LinkedList<>(Collections.singletonList(builder.getInput())); @@ -313,10 +340,7 @@ public final class CloudBrigadierManager { command = command.substring(1); } final List suggestions = this.commandManager.suggest(commandContext.getSender(), command); - /*argument.getParser().suggestions(commandContext, builder.getInput());*/ - for (final String suggestion : suggestions) { - System.out.printf("- %s\n", suggestion); - } + /* System.out.println("Filtering out with: " + builder.getInput()); final CommandSuggestionProcessor processor = this.commandManager.getCommandSuggestionProcessor(); 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 cef4b288..486830f2 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 @@ -105,7 +105,7 @@ public final class BukkitTest extends JavaPlugin { .newBuilder("perc") .withMin(PERC_MIN).withMax(PERC_MAX).build()) .handler(context -> { - ((Player) context.getSender()).sendMessage(String.format( + context.getSender().sendMessage(String.format( "Kenny sux %d%%", context.get("perc").orElse(PERC_MIN) )); diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java index fc499312..c4f6f93a 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java @@ -76,9 +76,9 @@ final class BukkitCommand extends org.bukkit.command.Command implements Plugi if (throwable != null) { if (throwable instanceof InvalidSyntaxException) { commandSender.sendMessage(ChatColor.RED + "Invalid Command Syntax. " - + "Correct command syntax is: " - + ChatColor.GRAY + "/" - + ((InvalidSyntaxException) throwable).getCorrectSyntax()); + + "Correct command syntax is: " + + ChatColor.GRAY + "/" + + ((InvalidSyntaxException) throwable).getCorrectSyntax()); } else if (throwable instanceof InvalidCommandSenderException) { commandSender.sendMessage(ChatColor.RED + throwable.getMessage()); } else if (throwable instanceof NoPermissionException) { diff --git a/cloud-minecraft/cloud-bungee/pom.xml b/cloud-minecraft/cloud-bungee/pom.xml index 19cf119c..e0559f27 100644 --- a/cloud-minecraft/cloud-bungee/pom.xml +++ b/cloud-minecraft/cloud-bungee/pom.xml @@ -50,6 +50,7 @@ net.md-5 bungeecord-api 1.8-SNAPSHOT + provided com.intellectualsites diff --git a/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java b/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java index 996a2f71..d9bd7d77 100644 --- a/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java +++ b/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java @@ -40,7 +40,7 @@ public class BungeeCommandManager extends CommandManager { private final Function backwardsCommandSenderMapper; /** - * Construct a new Bukkit command manager + * Construct a new Bungee command manager * * @param owningPlugin Plugin that is constructing the manager * @param commandExecutionCoordinator Coordinator provider diff --git a/cloud-minecraft/cloud-velocity-test/pom.xml b/cloud-minecraft/cloud-velocity-test/pom.xml new file mode 100644 index 00000000..c8e8555b --- /dev/null +++ b/cloud-minecraft/cloud-velocity-test/pom.xml @@ -0,0 +1,214 @@ + + + + + + cloud + com.intellectualsites + 1.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + cloud-velocity-test + + + + release + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + + + src/main/resources + true + + + + + + + + + + ${project.basedir}/src/main/resources + true + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.codehaus.mojo + templating-maven-plugin + 1.0.0 + + + filter-src + + filter-sources + + + + + + org.apache.maven.plugins + maven-site-plugin + 3.9.1 + + + net.trajano.wagon + wagon-git + 2.0.4 + + + org.apache.maven.doxia + doxia-module-markdown + 1.9.1 + + + + + org.apache.maven.plugins + maven-release-plugin + 3.0.0-M1 + + true + @{project.version} + [RELEASE] + install deploy site-deploy + + release + + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + + + + + velocitypowered-repo + https://repo.velocitypowered.com/snapshots/ + + + minecraft-libraries + https://libraries.minecraft.net/ + + + spongepowered-repo + https://repo.spongepowered.org/maven + + + + + + com.velocitypowered + velocity-api + 1.1.0-SNAPSHOT + provided + + + com.intellectualsites + cloud-velocity + 1.0-SNAPSHOT + + + diff --git a/cloud-minecraft/cloud-velocity-test/src/main/java/com/intellectualsites/cloudvelocitytest/package-info.java b/cloud-minecraft/cloud-velocity-test/src/main/java/com/intellectualsites/cloudvelocitytest/package-info.java new file mode 100644 index 00000000..a7782d6b --- /dev/null +++ b/cloud-minecraft/cloud-velocity-test/src/main/java/com/intellectualsites/cloudvelocitytest/package-info.java @@ -0,0 +1,28 @@ +// +// 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. +// + +/** + * Velocity test plugin + */ +package com.intellectualsites.cloudvelocitytest; diff --git a/cloud-minecraft/cloud-velocity/pom.xml b/cloud-minecraft/cloud-velocity/pom.xml new file mode 100644 index 00000000..4c698d71 --- /dev/null +++ b/cloud-minecraft/cloud-velocity/pom.xml @@ -0,0 +1,66 @@ + + + + + + cloud + com.intellectualsites + 1.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + cloud-velocity + + + + velocity-snapshosts + https://repo.velocitypowered.com/snapshots/ + + + + + + com.intellectualsites + cloud-core + 1.0-SNAPSHOT + + + com.intellectualsites + cloud-brigadier + 1.0-SNAPSHOT + + + com.velocitypowered + velocity-api + 1.1.0-SNAPSHOT + provided + + + diff --git a/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityCommandManager.java b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityCommandManager.java new file mode 100644 index 00000000..88848ef3 --- /dev/null +++ b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityCommandManager.java @@ -0,0 +1,89 @@ +// +// 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.velocity; + +import com.intellectualsites.commands.CommandManager; +import com.intellectualsites.commands.CommandTree; +import com.intellectualsites.commands.execution.CommandExecutionCoordinator; +import com.intellectualsites.commands.meta.CommandMeta; +import com.intellectualsites.commands.meta.SimpleCommandMeta; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.ProxyServer; + +import javax.annotation.Nonnull; +import java.util.function.Function; + +/** + * {@link CommandManager} implementation for Velocity + * + * @param Command sender type + */ +public class VelocityCommandManager extends CommandManager { + + private final ProxyServer proxyServer; + private final Function commandSenderMapper; + private final Function backwardsCommandSenderMapper; + + /** + * Create a new command manager instance + * + * @param proxyServer ProxyServer instance + * @param commandExecutionCoordinator Coordinator provider + * @param commandSenderMapper Function that maps {@link CommandSource} to the command sender type + * @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSource} + */ + public VelocityCommandManager( + @Nonnull final ProxyServer proxyServer, + @Nonnull final Function, CommandExecutionCoordinator> commandExecutionCoordinator, + @Nonnull final Function commandSenderMapper, + @Nonnull final Function backwardsCommandSenderMapper) { + super(commandExecutionCoordinator, new VelocityPluginRegistrationHandler<>()); + ((VelocityPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this); + this.proxyServer = proxyServer; + this.commandSenderMapper = commandSenderMapper; + this.backwardsCommandSenderMapper = backwardsCommandSenderMapper; + } + + @Override + public final boolean hasPermission(@Nonnull final C sender, @Nonnull final String permission) { + return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission); + } + + @Nonnull + @Override + public final CommandMeta createDefaultCommandMeta() { + return SimpleCommandMeta.empty(); + } + + @Nonnull + final ProxyServer getProxyServer() { + return this.proxyServer; + } + + @Nonnull + final Function getCommandSenderMapper() { + return this.commandSenderMapper; + } + +} 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 new file mode 100644 index 00000000..f20f7a36 --- /dev/null +++ b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/VelocityPluginRegistrationHandler.java @@ -0,0 +1,86 @@ +// +// 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.velocity; + +import com.intellectualsites.commands.Command; +import com.intellectualsites.commands.arguments.CommandArgument; +import com.intellectualsites.commands.arguments.StaticArgument; +import com.intellectualsites.commands.brigadier.CloudBrigadierManager; +import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.internal.CommandRegistrationHandler; +import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.CommandSource; + +import javax.annotation.Nonnull; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +final class VelocityPluginRegistrationHandler implements CommandRegistrationHandler { + + private final Map, BrigadierCommand> registeredCommands = new HashMap<>(); + private CloudBrigadierManager brigadierManager; + private VelocityCommandManager velocityCommandManager; + + void initialize(@Nonnull final VelocityCommandManager velocityCommandManager) { + this.velocityCommandManager = velocityCommandManager; + this.brigadierManager = new CloudBrigadierManager<>(velocityCommandManager, + () -> new CommandContext<>( + velocityCommandManager.getCommandSenderMapper() + .apply(velocityCommandManager.getProxyServer() + .getConsoleCommandSource())) + ); + } + + @Override + public boolean registerCommand(@Nonnull final Command command) { + final CommandArgument argument = command.getArguments().get(0); + if (this.registeredCommands.containsKey(argument)) { + return false; + } + final List aliases = ((StaticArgument) argument).getAlternativeAliases(); + final BrigadierCommand brigadierCommand = new BrigadierCommand( + this.brigadierManager.createLiteralCommandNode((Command) command, + (c, p) -> this.velocityCommandManager.hasPermission( + this.velocityCommandManager.getCommandSenderMapper() + .apply(c), p), + commandContext -> { + final CommandSource source = commandContext.getSource(); + final String input = commandContext.getInput(); + this.velocityCommandManager.executeCommand( + this.velocityCommandManager.getCommandSenderMapper() + .apply(source), input); + return com.mojang.brigadier.Command.SINGLE_SUCCESS; + }) + ); + final CommandMeta commandMeta = this.velocityCommandManager.getProxyServer().getCommandManager() + .metaBuilder(brigadierCommand) + .aliases(aliases.toArray(new String[0])).build(); + this.velocityCommandManager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand); + this.registeredCommands.put(argument, brigadierCommand); + return true; + } + +} diff --git a/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/package-info.java b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/package-info.java new file mode 100644 index 00000000..f7086b3f --- /dev/null +++ b/cloud-minecraft/cloud-velocity/src/main/java/com/intellectualsites/commands/velocity/package-info.java @@ -0,0 +1,28 @@ +// +// 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. +// + +/** + * Velocity implementation of cloud + */ +package com.intellectualsites.commands.velocity; diff --git a/pom.xml b/pom.xml index 29e30dcc..a7ca9884 100644 --- a/pom.xml +++ b/pom.xml @@ -11,13 +11,15 @@ cloud-jline cloud-core cloud-pipeline + cloud-annotations cloud-minecraft/cloud-bukkit-test + cloud-minecraft/cloud-velocity-test cloud-minecraft/cloud-bukkit cloud-minecraft/cloud-paper cloud-minecraft/cloud-brigadier - cloud-annotations cloud-minecraft/cloud-bungee + cloud-minecraft/cloud-velocity pom 2020