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 f6b4b78f..37ef4e61 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 @@ -43,8 +43,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Command manager for the Bukkit platform, using {@link BukkitCommandSender} as the - * command sender type + * Command manager for the Bukkit platform * * @param Command sender type */ diff --git a/cloud-minecraft/cloud-cloudburst/pom.xml b/cloud-minecraft/cloud-cloudburst/pom.xml new file mode 100644 index 00000000..6e8b236e --- /dev/null +++ b/cloud-minecraft/cloud-cloudburst/pom.xml @@ -0,0 +1,66 @@ + + + + + + cloud + com.intellectualsites + 1.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + cloud-cloudburst + 0.2.0-SNAPSHOT + + + + nukkit-repo + https://repo.nukkitx.com/maven-snapshots + + + jitpack + https://jitpack.io + + + + + + org.cloudburstmc + cloudburst-server + 0.0.1-SNAPSHOT + provided + + + com.intellectualsites + cloud-core + 0.2.0-SNAPSHOT + + + diff --git a/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstCommand.java b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstCommand.java new file mode 100644 index 00000000..602a4d2b --- /dev/null +++ b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstCommand.java @@ -0,0 +1,131 @@ +// +// 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.cloudburst; + +import com.intellectualsites.commands.arguments.CommandArgument; +import com.intellectualsites.commands.exceptions.ArgumentParseException; +import com.intellectualsites.commands.exceptions.InvalidCommandSenderException; +import com.intellectualsites.commands.exceptions.InvalidSyntaxException; +import com.intellectualsites.commands.exceptions.NoPermissionException; +import com.intellectualsites.commands.exceptions.NoSuchCommandException; +import org.cloudburstmc.server.command.CommandSender; +import org.cloudburstmc.server.command.PluginCommand; +import org.cloudburstmc.server.command.data.CommandData; +import org.cloudburstmc.server.plugin.PluginContainer; + +import javax.annotation.Nonnull; +import java.util.List; +import java.util.concurrent.CompletionException; + +final class CloudburstCommand extends PluginCommand { + + private static final String MESSAGE_NO_PERMS = + "I'm sorry, but you do not have permission to perform this command. " + + "Please contact the server administrators if you believe that this is in error."; + private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help."; + + private final CommandArgument command; + private final CloudburstCommandManager manager; + private final com.intellectualsites.commands.Command cloudCommand; + + CloudburstCommand(@Nonnull final String label, + @Nonnull final List aliases, + @Nonnull final com.intellectualsites.commands.Command cloudCommand, + @Nonnull final CommandArgument command, + @Nonnull final CloudburstCommandManager manager) { + super(manager.getOwningPlugin(), null, CommandData.builder(label) + .addAliases(aliases.toArray(new String[0])) + .addPermission(cloudCommand.getCommandPermission()) + .setDescription(cloudCommand.getCommandMeta().getOrDefault("description", "")) + .build()); + this.command = command; + this.manager = manager; + this.cloudCommand = cloudCommand; + } + + @Override + public boolean execute(final CommandSender commandSender, + final String commandLabel, + final String[] strings) { + /* Join input */ + final StringBuilder builder = new StringBuilder(this.command.getName()); + for (final String string : strings) { + builder.append(" ").append(string); + } + final C sender = this.manager.getCommandSenderMapper().apply(commandSender); + this.manager.executeCommand(sender, + builder.toString()) + .whenComplete(((commandResult, throwable) -> { + if (throwable != null) { + if (throwable instanceof CompletionException) { + throwable = throwable.getCause(); + } + final Throwable finalThrowable = throwable; + if (throwable instanceof InvalidSyntaxException) { + this.manager.handleException(sender, + InvalidSyntaxException.class, + (InvalidSyntaxException) throwable, (c, e) -> + commandSender.sendMessage( + "Invalid Command Syntax. " + + "Correct command syntax is: " + + "/" + + ((InvalidSyntaxException) finalThrowable) + .getCorrectSyntax()) + ); + } else if (throwable instanceof InvalidCommandSenderException) { + this.manager.handleException(sender, + InvalidCommandSenderException.class, + (InvalidCommandSenderException) throwable, (c, e) -> + commandSender.sendMessage(finalThrowable.getMessage()) + ); + } else if (throwable instanceof NoPermissionException) { + this.manager.handleException(sender, + NoPermissionException.class, + (NoPermissionException) throwable, (c, e) -> + commandSender.sendMessage(MESSAGE_NO_PERMS) + ); + } else if (throwable instanceof NoSuchCommandException) { + this.manager.handleException(sender, + NoSuchCommandException.class, + (NoSuchCommandException) throwable, (c, e) -> + commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND) + ); + } else if (throwable instanceof ArgumentParseException) { + this.manager.handleException(sender, + ArgumentParseException.class, + (ArgumentParseException) throwable, (c, e) -> + commandSender.sendMessage( + "Invalid Command Argument: " + + finalThrowable.getCause().getMessage()) + ); + } else { + commandSender.sendMessage(throwable.getMessage()); + throwable.printStackTrace(); + } + } + })); + return true; + } + +} diff --git a/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstCommandManager.java b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstCommandManager.java new file mode 100644 index 00000000..62844d2f --- /dev/null +++ b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstCommandManager.java @@ -0,0 +1,97 @@ +// +// 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.cloudburst; + +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 org.cloudburstmc.server.command.CommandSender; +import org.cloudburstmc.server.plugin.PluginContainer; + +import javax.annotation.Nonnull; +import java.util.function.Function; + +/** + * Command manager for the Cloudburst platform + * + * @param Command sender type + */ +public class CloudburstCommandManager extends CommandManager { + + private final Function commandSenderMapper; + private final Function backwardsCommandSenderMapper; + + private final PluginContainer owningPlugin; + + /** + * Construct a new Cloudburst command manager + * + * @param owningPlugin Plugin that is constructing the manager + * @param commandExecutionCoordinator Coordinator provider + * @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type + * @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender} + * @throws Exception If the construction of the manager fails + */ + public CloudburstCommandManager(@Nonnull final PluginContainer owningPlugin, + @Nonnull final Function, + CommandExecutionCoordinator> commandExecutionCoordinator, + @Nonnull final Function commandSenderMapper, + @Nonnull final Function backwardsCommandSenderMapper) throws Exception { + super(commandExecutionCoordinator, new CloudburstPluginRegistrationHandler<>()); + ((CloudburstPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this); + this.commandSenderMapper = commandSenderMapper; + this.backwardsCommandSenderMapper = backwardsCommandSenderMapper; + this.owningPlugin = owningPlugin; + } + + @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.builder().build(); + } + + @Nonnull + final Function getCommandSenderMapper() { + return this.commandSenderMapper; + } + + /** + * Get the plugin that owns the manager + * + * @return Owning plugin + */ + @Nonnull + public final PluginContainer getOwningPlugin() { + return this.owningPlugin; + } + +} diff --git a/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstPluginRegistrationHandler.java b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstPluginRegistrationHandler.java new file mode 100644 index 00000000..0d998802 --- /dev/null +++ b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/CloudburstPluginRegistrationHandler.java @@ -0,0 +1,70 @@ +// +// 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.cloudburst; + +import com.intellectualsites.commands.Command; +import com.intellectualsites.commands.arguments.CommandArgument; +import com.intellectualsites.commands.arguments.StaticArgument; +import com.intellectualsites.commands.internal.CommandRegistrationHandler; +import org.cloudburstmc.server.Server; +import org.cloudburstmc.server.plugin.PluginContainer; + +import javax.annotation.Nonnull; +import java.util.HashMap; +import java.util.Map; + +class CloudburstPluginRegistrationHandler implements CommandRegistrationHandler { + + private final Map, org.cloudburstmc.server.command.Command> registeredCommands = new HashMap<>(); + + private CloudburstCommandManager cloudburstCommandManager;; + + CloudburstPluginRegistrationHandler() { + } + + void initialize(@Nonnull final CloudburstCommandManager cloudburstCommandManager) { + this.cloudburstCommandManager = cloudburstCommandManager; + } + + @Override + public final boolean registerCommand(@Nonnull final Command command) { + /* We only care about the root command argument */ + final CommandArgument commandArgument = command.getArguments().get(0); + if (this.registeredCommands.containsKey(commandArgument)) { + return false; + } + final PluginContainer plugin = this.cloudburstCommandManager.getOwningPlugin(); + final CloudburstCommand cloudburstCommand = new CloudburstCommand<>( + commandArgument.getName(), + ((StaticArgument) commandArgument).getAlternativeAliases(), + (Command) command, + (CommandArgument) commandArgument, + this.cloudburstCommandManager + ); + this.registeredCommands.put(commandArgument, cloudburstCommand); + Server.getInstance().getCommandRegistry().register(plugin, cloudburstCommand); + return true; + } + +} diff --git a/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/package-info.java b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/package-info.java new file mode 100644 index 00000000..02be0f9c --- /dev/null +++ b/cloud-minecraft/cloud-cloudburst/src/main/java/com/intellectualsites/commands/cloudburst/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. +// + +/** + * Cloudburst implementation of Cloud + */ +package com.intellectualsites.commands.cloudburst; diff --git a/pom.xml b/pom.xml index c89a3b1f..23ebf88e 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ cloud-minecraft/cloud-bungee cloud-minecraft/cloud-velocity cloud-minecraft/cloud-minecraft-extras + cloud-minecraft/cloud-cloudburst pom 2020