diff --git a/commands-bukkit/pom.xml b/commands-bukkit/pom.xml new file mode 100644 index 00000000..fa1f61fa --- /dev/null +++ b/commands-bukkit/pom.xml @@ -0,0 +1,57 @@ + + + + Commands + com.intellectualsites + 1.0-SNAPSHOT + + + 4.0.0 + commands-bukkit + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + org.bukkit + bukkit + 1.8.8-R0.1-SNAPSHOT + provided + + + com.intellectualsites + commands-core + 1.0-SNAPSHOT + + + + + clean package + CommandStuff-${project.artifactId}-${project.version} + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + diff --git a/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommand.java b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommand.java new file mode 100644 index 00000000..e853a091 --- /dev/null +++ b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommand.java @@ -0,0 +1,75 @@ +// +// MIT License +// +// Copyright (c) 2020 IntellectualSites +// +// 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;import com.intellectualsites.commands.components.CommandComponent; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginIdentifiableCommand; +import org.bukkit.plugin.Plugin; + +import javax.annotation.Nonnull; +import java.util.List; + +public class BukkitCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand { + + private final CommandComponent command; + private final BukkitCommandManager bukkitCommandManager; + + BukkitCommand(@Nonnull final CommandComponent command, @Nonnull final BukkitCommandManager bukkitCommandManager) { + super(command.getName()); + this.command = command; + this.bukkitCommandManager = bukkitCommandManager; + } + + @Override + public boolean execute(final CommandSender commandSender, final String s, final String[] strings) { + /* Join input */ + final StringBuilder builder = new StringBuilder(this.command.getName()); + for (final String string : strings) { + builder.append(" ").append(string); + } + this.bukkitCommandManager.executeCommand(BukkitCommandSender.of(commandSender), builder.toString()).whenComplete(((commandResult, throwable) -> { + if (throwable != null) { + throwable.printStackTrace(); + } else { + // Do something... + commandSender.sendMessage("All good!"); + } + })); + return true; + } + + @Override + public List tabComplete(final CommandSender sender, final String alias, final String[] args) throws IllegalArgumentException { + final StringBuilder builder = new StringBuilder(this.command.getName()); + for (final String string : args) { + builder.append(" ").append(string); + } + return this.bukkitCommandManager.suggest(BukkitCommandSender.of(sender), builder.toString()); + } + + @Override + public Plugin getPlugin() { + return this.bukkitCommandManager.getOwningPlugin(); + } + +} diff --git a/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommandManager.java b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommandManager.java new file mode 100644 index 00000000..484da82b --- /dev/null +++ b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommandManager.java @@ -0,0 +1,47 @@ +// +// MIT License +// +// Copyright (c) 2020 IntellectualSites +// +// 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;import com.intellectualsites.commands.execution.CommandExecutionCoordinator; +import org.bukkit.plugin.Plugin; + +import javax.annotation.Nonnull; +import java.util.function.Function; + +public class BukkitCommandManager extends CommandManager { + + private final Plugin owningPlugin; + + public BukkitCommandManager(@Nonnull final Plugin owningPlugin, + @Nonnull final Function, + CommandExecutionCoordinator> commandExecutionCoordinator) + throws Exception { + super(commandExecutionCoordinator, new BukkitPluginRegistrationHandler()); + ((BukkitPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this); + this.owningPlugin = owningPlugin; + } + + @Nonnull public Plugin getOwningPlugin() { + return this.owningPlugin; + } + +} diff --git a/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommandSender.java b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommandSender.java new file mode 100644 index 00000000..b0cc7d02 --- /dev/null +++ b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitCommandSender.java @@ -0,0 +1,81 @@ +// +// MIT License +// +// Copyright (c) 2020 IntellectualSites +// +// 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;import com.google.common.base.Objects; +import com.intellectualsites.commands.sender.CommandSender; +import org.bukkit.entity.Player; + +import javax.annotation.Nonnull; + +public abstract class BukkitCommandSender implements CommandSender { + + private final org.bukkit.command.CommandSender internalSender; + + BukkitCommandSender(@Nonnull final org.bukkit.command.CommandSender internalSender) { + this.internalSender = internalSender; + } + + @Nonnull + public static BukkitCommandSender player(@Nonnull final Player player) { + return new BukkitPlayerSender(player); + } + + @Nonnull + public static BukkitCommandSender console() { + return new BukkitConsoleSender(); + } + + @Nonnull + public static BukkitCommandSender of(@Nonnull final org.bukkit.command.CommandSender sender) { + if (sender instanceof Player) { + return player((Player) sender); + } + return console(); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final BukkitCommandSender that = (BukkitCommandSender) o; + return Objects.equal(internalSender, that.internalSender); + } + + @Override + public int hashCode() { + return Objects.hashCode(internalSender); + } + + @Nonnull public org.bukkit.command.CommandSender getInternalSender() { + return this.internalSender; + } + + public abstract boolean isPlayer(); + + @Nonnull public abstract Player asPlayer(); + +} diff --git a/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitConsoleSender.java b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitConsoleSender.java new file mode 100644 index 00000000..cbca92cd --- /dev/null +++ b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitConsoleSender.java @@ -0,0 +1,46 @@ +// +// MIT License +// +// Copyright (c) 2020 IntellectualSites +// +// 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;import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import javax.annotation.Nonnull; + +public class BukkitConsoleSender extends BukkitCommandSender { + + BukkitConsoleSender() { + super(Bukkit.getConsoleSender()); + } + + @Override + public boolean isPlayer() { + return false; + } + + @Nonnull + @Override + public Player asPlayer() { + throw new UnsupportedOperationException("Cannot convert console to player"); + } + +} diff --git a/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitPlayerSender.java b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitPlayerSender.java new file mode 100644 index 00000000..6206a50c --- /dev/null +++ b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitPlayerSender.java @@ -0,0 +1,44 @@ +// +// MIT License +// +// Copyright (c) 2020 IntellectualSites +// +// 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;import org.bukkit.entity.Player; + +import javax.annotation.Nonnull; + +public class BukkitPlayerSender extends BukkitCommandSender { + + BukkitPlayerSender(@Nonnull final Player player) { + super(player); + } + + @Override + public boolean isPlayer() { + return true; + } + + @Nonnull + @Override + public Player asPlayer() { + return (Player) this.getInternalSender(); + } +} diff --git a/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitPluginRegistrationHandler.java b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitPluginRegistrationHandler.java new file mode 100644 index 00000000..b5f4bdd1 --- /dev/null +++ b/commands-bukkit/src/main/java/com/intellectualsites/commands/BukkitPluginRegistrationHandler.java @@ -0,0 +1,75 @@ +// +// MIT License +// +// Copyright (c) 2020 IntellectualSites +// +// 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;import com.intellectualsites.commands.components.CommandComponent; +import com.intellectualsites.commands.internal.CommandRegistrationHandler; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; + +import javax.annotation.Nonnull; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class BukkitPluginRegistrationHandler implements CommandRegistrationHandler { + + private final Map, org.bukkit.command.Command> registeredCommands = new HashMap<>(); + + private Map bukkitCommands; + private BukkitCommandManager bukkitCommandManager; + + BukkitPluginRegistrationHandler() { + } + + void initialize(@Nonnull final BukkitCommandManager bukkitCommandManager) throws Exception { + final Method getCommandMap = Bukkit.getServer().getClass().getDeclaredMethod("getCommandMap"); + getCommandMap.setAccessible(true); + final CommandMap commandMap = (CommandMap) getCommandMap.invoke(Bukkit.getServer()); + final Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands"); + knownCommands.setAccessible(true); + @SuppressWarnings("ALL") + final Map bukkitCommands = (Map) knownCommands.get(commandMap); + this.bukkitCommands = bukkitCommands; + this.bukkitCommandManager = bukkitCommandManager; + } + + @Override + public boolean registerCommand(@Nonnull final Command command) { + /* We only care about the root command component */ + final CommandComponent commandComponent = command.getComponents().get(0); + if (this.registeredCommands.containsKey(commandComponent)) { + return false; + } + final String label; + if (bukkitCommands.containsKey(commandComponent.getName())) { + label = String.format("%s:%s", this.bukkitCommandManager.getOwningPlugin().getName(), commandComponent.getName()); + } else { + label = commandComponent.getName(); + } + this.bukkitCommands.put(label, new BukkitCommand((CommandComponent) commandComponent, this.bukkitCommandManager)); + return true; + } + +} diff --git a/commands-core/pom.xml b/commands-core/pom.xml new file mode 100644 index 00000000..97ab9e3c --- /dev/null +++ b/commands-core/pom.xml @@ -0,0 +1,41 @@ + + + + + + Commands + com.intellectualsites + 1.0-SNAPSHOT + + 4.0.0 + + commands-core + + + diff --git a/src/main/java/com/intellectualsites/commands/Command.java b/commands-core/src/main/java/com/intellectualsites/commands/Command.java similarity index 90% rename from src/main/java/com/intellectualsites/commands/Command.java rename to commands-core/src/main/java/com/intellectualsites/commands/Command.java index 5e74dcd9..46a3e7c1 100644 --- a/src/main/java/com/intellectualsites/commands/Command.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/Command.java @@ -30,7 +30,6 @@ import com.intellectualsites.commands.sender.CommandSender; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -44,15 +43,15 @@ import java.util.Optional; */ public class Command { - @Nonnull private final CommandComponent[] components; + @Nonnull private final List> components; @Nonnull private final CommandExecutionHandler commandExecutionHandler; @Nullable private final Class senderType; - protected Command(@Nonnull final CommandComponent[] commandComponents, + protected Command(@Nonnull final List> commandComponents, @Nonnull final CommandExecutionHandler commandExecutionHandler, @Nullable final Class senderType) { this.components = Objects.requireNonNull(commandComponents, "Command components may not be null"); - if (this.components.length == 0) { + if (this.components.size() == 0) { throw new IllegalArgumentException("At least one command component is required"); } // Enforce ordering of command components @@ -88,9 +87,8 @@ public class Command { * @return Copy of the command component array */ @Nonnull - @SuppressWarnings("ALL") - public CommandComponent[] getComponents() { - return (CommandComponent[]) Arrays.asList(this.components).toArray(); + public List> getComponents() { + return Collections.unmodifiableList(this.components); } /** @@ -121,9 +119,9 @@ public class Command { */ public List> getSharedComponentChain(@Nonnull final Command other) { final List> commandComponents = new LinkedList<>(); - for (int i = 0; i < this.components.length && i < other.components.length; i++) { - if (this.components[i].equals(other.components[i])) { - commandComponents.add(this.components[i]); + for (int i = 0; i < this.components.size() && i < other.components.size(); i++) { + if (this.components.get(i).equals(other.components.get(i))) { + commandComponents.add(this.components.get(i)); } else { break; } @@ -189,7 +187,8 @@ public class Command { */ @Nonnull public Command build() { - return new Command<>(this.commandComponents.toArray(new CommandComponent[0]), this.commandExecutionHandler, + return new Command<>(Collections.unmodifiableList(this.commandComponents), + this.commandExecutionHandler, this.senderType); } diff --git a/src/main/java/com/intellectualsites/commands/CommandManager.java b/commands-core/src/main/java/com/intellectualsites/commands/CommandManager.java similarity index 88% rename from src/main/java/com/intellectualsites/commands/CommandManager.java rename to commands-core/src/main/java/com/intellectualsites/commands/CommandManager.java index b2a3996e..103fadb4 100644 --- a/src/main/java/com/intellectualsites/commands/CommandManager.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/CommandManager.java @@ -24,6 +24,7 @@ package com.intellectualsites.commands; import com.intellectualsites.commands.components.CommandSyntaxFormatter; +import com.intellectualsites.commands.components.StandardCommandSyntaxFormatter; import com.intellectualsites.commands.context.CommandContext; import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import com.intellectualsites.commands.execution.CommandResult; @@ -46,14 +47,16 @@ import java.util.function.Function; public abstract class CommandManager { private final CommandExecutionCoordinator commandExecutionCoordinator; + private final CommandRegistrationHandler commandRegistrationHandler; private final CommandTree commandTree; - private CommandSyntaxFormatter commandSyntaxFormatter = CommandSyntaxFormatter.STANDARD_COMMAND_SYNTAX_FORMATTER; + private CommandSyntaxFormatter commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>(); protected CommandManager(@Nonnull final Function, CommandExecutionCoordinator> commandExecutionCoordinator, @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { this.commandTree = CommandTree.newTree(this, commandRegistrationHandler); this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree); + this.commandRegistrationHandler = commandRegistrationHandler; } public CompletableFuture executeCommand(@Nonnull final C commandSender, @Nonnull final String input) { @@ -82,7 +85,7 @@ public abstract class CommandManager { * @param command Command to register * @return The command manager instance */ - public CommandManager registerCommand(@Nonnull final Command command) { + public CommandManager registerCommand(@Nonnull final Command command) { this.commandTree.insertCommand(command); return this; } @@ -93,7 +96,7 @@ public abstract class CommandManager { * @return Command syntax formatter */ @Nonnull - public CommandSyntaxFormatter getCommandSyntaxFormatter() { + public CommandSyntaxFormatter getCommandSyntaxFormatter() { return this.commandSyntaxFormatter; } @@ -106,4 +109,8 @@ public abstract class CommandManager { this.commandSyntaxFormatter = commandSyntaxFormatter; } + @Nonnull protected CommandRegistrationHandler getCommandRegistrationHandler() { + return this.commandRegistrationHandler; + } + } diff --git a/src/main/java/com/intellectualsites/commands/CommandTree.java b/commands-core/src/main/java/com/intellectualsites/commands/CommandTree.java similarity index 95% rename from src/main/java/com/intellectualsites/commands/CommandTree.java rename to commands-core/src/main/java/com/intellectualsites/commands/CommandTree.java index c7385743..277b6fb8 100644 --- a/src/main/java/com/intellectualsites/commands/CommandTree.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/CommandTree.java @@ -23,8 +23,6 @@ // package com.intellectualsites.commands; -import com.google.common.base.Objects; -import com.google.common.collect.Lists; import com.intellectualsites.commands.components.CommandComponent; import com.intellectualsites.commands.components.StaticComponent; import com.intellectualsites.commands.context.CommandContext; @@ -36,12 +34,12 @@ import com.intellectualsites.commands.sender.CommandSender; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Queue; import java.util.stream.Collectors; @@ -95,9 +93,8 @@ public class CommandTree { if (child.isLeaf()) { /* Not enough arguments */ throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter() - .apply(Arrays.asList(child.getValue() - .getOwningCommand() - .getComponents())), + .apply(Objects.requireNonNull( + child.getValue().getOwningCommand()).getComponents()), commandContext.getCommandSender(), this.getChain(root) .stream() .map(Node::getValue) @@ -120,9 +117,9 @@ public class CommandTree { } else { /* Too many arguments. We have a unique path, so we can send the entire context */ throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter() - .apply(Arrays.asList(child.getValue() - .getOwningCommand() - .getComponents())), + .apply(Objects.requireNonNull(child.getValue() + .getOwningCommand()) + .getComponents()), commandContext.getCommandSender(), this.getChain(root) .stream() .map(Node::getValue) @@ -146,9 +143,9 @@ public class CommandTree { } else { /* Too many arguments. We have a unique path, so we can send the entire context */ throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter() - .apply(Arrays.asList(root.getValue() - .getOwningCommand() - .getComponents())), + .apply(root.getValue() + .getOwningCommand() + .getComponents()), commandContext.getCommandSender(), this.getChain(root) .stream() .map(Node::getValue) @@ -337,7 +334,8 @@ public class CommandTree { chain.add(tail); tail = tail.getParent(); } - return Lists.reverse(chain); + Collections.reverse(chain); + return chain; } @@ -390,13 +388,13 @@ public class CommandTree { return false; } final Node node = (Node) o; - return Objects.equal(getChildren(), node.getChildren()) && - Objects.equal(getValue(), node.getValue()); + return Objects.equals(getChildren(), node.getChildren()) && + Objects.equals(getValue(), node.getValue()); } @Override public int hashCode() { - return Objects.hashCode(getChildren(), getValue()); + return Objects.hash(getChildren(), getValue()); } @Nullable diff --git a/src/main/java/com/intellectualsites/commands/components/CommandComponent.java b/commands-core/src/main/java/com/intellectualsites/commands/components/CommandComponent.java similarity index 97% rename from src/main/java/com/intellectualsites/commands/components/CommandComponent.java rename to commands-core/src/main/java/com/intellectualsites/commands/components/CommandComponent.java index 43e904eb..80de965f 100644 --- a/src/main/java/com/intellectualsites/commands/components/CommandComponent.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/components/CommandComponent.java @@ -153,12 +153,12 @@ public class CommandComponent implements Comparable< return false; } final CommandComponent that = (CommandComponent) o; - return isRequired() == that.isRequired() && com.google.common.base.Objects.equal(getName(), that.getName()); + return isRequired() == that.isRequired() && Objects.equals(getName(), that.getName()); } @Override public int hashCode() { - return com.google.common.base.Objects.hashCode(isRequired(), getName()); + return Objects.hash(isRequired(), getName()); } @Override diff --git a/src/main/java/com/intellectualsites/commands/components/CommandSyntaxFormatter.java b/commands-core/src/main/java/com/intellectualsites/commands/components/CommandSyntaxFormatter.java similarity index 83% rename from src/main/java/com/intellectualsites/commands/components/CommandSyntaxFormatter.java rename to commands-core/src/main/java/com/intellectualsites/commands/components/CommandSyntaxFormatter.java index bea60d9f..d360a4a4 100644 --- a/src/main/java/com/intellectualsites/commands/components/CommandSyntaxFormatter.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/components/CommandSyntaxFormatter.java @@ -23,17 +23,17 @@ // package com.intellectualsites.commands.components; +import com.intellectualsites.commands.sender.CommandSender; + import javax.annotation.Nonnull; import java.util.List; import java.util.function.Function; @FunctionalInterface -public interface CommandSyntaxFormatter extends Function>, String> { - - CommandSyntaxFormatter STANDARD_COMMAND_SYNTAX_FORMATTER = new StandardCommandSyntaxFormatter(); +public interface CommandSyntaxFormatter extends Function>, String> { @Override @Nonnull - String apply(@Nonnull List> commandComponents); + String apply(@Nonnull List> commandComponents); } diff --git a/src/main/java/com/intellectualsites/commands/components/StandardCommandSyntaxFormatter.java b/commands-core/src/main/java/com/intellectualsites/commands/components/StandardCommandSyntaxFormatter.java similarity index 88% rename from src/main/java/com/intellectualsites/commands/components/StandardCommandSyntaxFormatter.java rename to commands-core/src/main/java/com/intellectualsites/commands/components/StandardCommandSyntaxFormatter.java index 39c6ab22..cc930cd7 100644 --- a/src/main/java/com/intellectualsites/commands/components/StandardCommandSyntaxFormatter.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/components/StandardCommandSyntaxFormatter.java @@ -23,20 +23,22 @@ // package com.intellectualsites.commands.components; +import com.intellectualsites.commands.sender.CommandSender; + import javax.annotation.Nonnull; import java.util.Iterator; import java.util.List; -public class StandardCommandSyntaxFormatter implements CommandSyntaxFormatter { +public class StandardCommandSyntaxFormatter implements CommandSyntaxFormatter { public StandardCommandSyntaxFormatter() { } @Nonnull @Override - public String apply(@Nonnull final List> commandComponents) { + public String apply(@Nonnull final List> commandComponents) { final StringBuilder stringBuilder = new StringBuilder(); - final Iterator> iterator = commandComponents.iterator(); + final Iterator> iterator = commandComponents.iterator(); while (iterator.hasNext()) { final CommandComponent commandComponent = iterator.next(); if (commandComponent instanceof StaticComponent) { diff --git a/src/main/java/com/intellectualsites/commands/components/StaticComponent.java b/commands-core/src/main/java/com/intellectualsites/commands/components/StaticComponent.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/components/StaticComponent.java rename to commands-core/src/main/java/com/intellectualsites/commands/components/StaticComponent.java diff --git a/src/main/java/com/intellectualsites/commands/context/CommandContext.java b/commands-core/src/main/java/com/intellectualsites/commands/context/CommandContext.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/context/CommandContext.java rename to commands-core/src/main/java/com/intellectualsites/commands/context/CommandContext.java diff --git a/src/main/java/com/intellectualsites/commands/exceptions/CommandParseException.java b/commands-core/src/main/java/com/intellectualsites/commands/exceptions/CommandParseException.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/exceptions/CommandParseException.java rename to commands-core/src/main/java/com/intellectualsites/commands/exceptions/CommandParseException.java diff --git a/src/main/java/com/intellectualsites/commands/exceptions/InvalidSyntaxException.java b/commands-core/src/main/java/com/intellectualsites/commands/exceptions/InvalidSyntaxException.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/exceptions/InvalidSyntaxException.java rename to commands-core/src/main/java/com/intellectualsites/commands/exceptions/InvalidSyntaxException.java diff --git a/src/main/java/com/intellectualsites/commands/exceptions/NoSuchCommandException.java b/commands-core/src/main/java/com/intellectualsites/commands/exceptions/NoSuchCommandException.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/exceptions/NoSuchCommandException.java rename to commands-core/src/main/java/com/intellectualsites/commands/exceptions/NoSuchCommandException.java diff --git a/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java b/commands-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java rename to commands-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java diff --git a/src/main/java/com/intellectualsites/commands/execution/CommandExecutionHandler.java b/commands-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionHandler.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/execution/CommandExecutionHandler.java rename to commands-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionHandler.java diff --git a/src/main/java/com/intellectualsites/commands/execution/CommandResult.java b/commands-core/src/main/java/com/intellectualsites/commands/execution/CommandResult.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/execution/CommandResult.java rename to commands-core/src/main/java/com/intellectualsites/commands/execution/CommandResult.java diff --git a/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java b/commands-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java similarity index 93% rename from src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java rename to commands-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java index c8150bda..e374ae02 100644 --- a/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java +++ b/commands-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java @@ -47,7 +47,7 @@ public interface CommandRegistrationHandler { * @return {@code true} if the command was registered successfully, * else {@code false} */ - boolean registerCommand(@Nonnull final Command command); + boolean registerCommand(@Nonnull final Command command); final class NullCommandRegistrationHandler implements CommandRegistrationHandler { @@ -56,7 +56,7 @@ public interface CommandRegistrationHandler { } @Override - public boolean registerCommand(@Nonnull final Command command) { + public boolean registerCommand(@Nonnull final Command command) { return true; } diff --git a/src/main/java/com/intellectualsites/commands/parser/ComponentParseResult.java b/commands-core/src/main/java/com/intellectualsites/commands/parser/ComponentParseResult.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/parser/ComponentParseResult.java rename to commands-core/src/main/java/com/intellectualsites/commands/parser/ComponentParseResult.java diff --git a/src/main/java/com/intellectualsites/commands/parser/ComponentParser.java b/commands-core/src/main/java/com/intellectualsites/commands/parser/ComponentParser.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/parser/ComponentParser.java rename to commands-core/src/main/java/com/intellectualsites/commands/parser/ComponentParser.java diff --git a/src/main/java/com/intellectualsites/commands/sender/CommandSender.java b/commands-core/src/main/java/com/intellectualsites/commands/sender/CommandSender.java similarity index 100% rename from src/main/java/com/intellectualsites/commands/sender/CommandSender.java rename to commands-core/src/main/java/com/intellectualsites/commands/sender/CommandSender.java diff --git a/pom.xml b/pom.xml index e9bcfc3a..3ad0e2c4 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,9 @@ Commands 1.0-SNAPSHOT - commands-jline + + commands-bukkit + commands-core pom 2020 @@ -126,10 +128,5 @@ jsr305 3.0.2 - - com.google.guava - guava - 29.0-jre -