Basic bukkit support

This commit is contained in:
Alexander Söderberg 2020-09-06 17:55:46 +02:00
parent 762bdb7ff4
commit 3ec124aac4
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
27 changed files with 516 additions and 47 deletions

57
commands-bukkit/pom.xml Normal file
View file

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Commands</artifactId>
<groupId>com.intellectualsites</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>commands-bukkit</artifactId>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>commands-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<defaultGoal>clean package</defaultGoal>
<finalName>CommandStuff-${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -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<BukkitCommandSender, ?> command;
private final BukkitCommandManager bukkitCommandManager;
BukkitCommand(@Nonnull final CommandComponent<BukkitCommandSender, ?> 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<String> 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();
}
}

View file

@ -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<BukkitCommandSender> {
private final Plugin owningPlugin;
public BukkitCommandManager(@Nonnull final Plugin owningPlugin,
@Nonnull final Function<CommandTree<BukkitCommandSender>,
CommandExecutionCoordinator<BukkitCommandSender>> commandExecutionCoordinator)
throws Exception {
super(commandExecutionCoordinator, new BukkitPluginRegistrationHandler());
((BukkitPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this);
this.owningPlugin = owningPlugin;
}
@Nonnull public Plugin getOwningPlugin() {
return this.owningPlugin;
}
}

View file

@ -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();
}

View file

@ -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");
}
}

View file

@ -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();
}
}

View file

@ -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<CommandComponent<?, ?>, org.bukkit.command.Command> registeredCommands = new HashMap<>();
private Map<String, org.bukkit.command.Command> 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<String, org.bukkit.command.Command> bukkitCommands = (Map<String, org.bukkit.command.Command>) 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<BukkitCommandSender, ?>) commandComponent, this.bukkitCommandManager));
return true;
}
}

41
commands-core/pom.xml Normal file
View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ 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.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Commands</artifactId>
<groupId>com.intellectualsites</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>commands-core</artifactId>
</project>

View file

@ -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<C extends CommandSender> {
@Nonnull private final CommandComponent<C, ?>[] components;
@Nonnull private final List<CommandComponent<C, ?>> components;
@Nonnull private final CommandExecutionHandler<C> commandExecutionHandler;
@Nullable private final Class<? extends C> senderType;
protected Command(@Nonnull final CommandComponent<C, ?>[] commandComponents,
protected Command(@Nonnull final List<CommandComponent<C, ?>> commandComponents,
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler,
@Nullable final Class<? extends C> 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<C extends CommandSender> {
* @return Copy of the command component array
*/
@Nonnull
@SuppressWarnings("ALL")
public CommandComponent<C, ?>[] getComponents() {
return (CommandComponent<C, ?>[]) Arrays.asList(this.components).toArray();
public List<CommandComponent<C, ?>> getComponents() {
return Collections.unmodifiableList(this.components);
}
/**
@ -121,9 +119,9 @@ public class Command<C extends CommandSender> {
*/
public List<CommandComponent<C, ?>> getSharedComponentChain(@Nonnull final Command<C> other) {
final List<CommandComponent<C, ?>> 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<C extends CommandSender> {
*/
@Nonnull
public Command<C> build() {
return new Command<>(this.commandComponents.toArray(new CommandComponent[0]), this.commandExecutionHandler,
return new Command<>(Collections.unmodifiableList(this.commandComponents),
this.commandExecutionHandler,
this.senderType);
}

View file

@ -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<C extends CommandSender> {
private final CommandExecutionCoordinator<C> commandExecutionCoordinator;
private final CommandRegistrationHandler commandRegistrationHandler;
private final CommandTree<C> commandTree;
private CommandSyntaxFormatter commandSyntaxFormatter = CommandSyntaxFormatter.STANDARD_COMMAND_SYNTAX_FORMATTER;
private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>();
protected CommandManager(@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
this.commandTree = CommandTree.newTree(this, commandRegistrationHandler);
this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree);
this.commandRegistrationHandler = commandRegistrationHandler;
}
public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
@ -82,7 +85,7 @@ public abstract class CommandManager<C extends CommandSender> {
* @param command Command to register
* @return The command manager instance
*/
public CommandManager<C> registerCommand(@Nonnull final Command command) {
public CommandManager<C> registerCommand(@Nonnull final Command<C> command) {
this.commandTree.insertCommand(command);
return this;
}
@ -93,7 +96,7 @@ public abstract class CommandManager<C extends CommandSender> {
* @return Command syntax formatter
*/
@Nonnull
public CommandSyntaxFormatter getCommandSyntaxFormatter() {
public CommandSyntaxFormatter<C> getCommandSyntaxFormatter() {
return this.commandSyntaxFormatter;
}
@ -106,4 +109,8 @@ public abstract class CommandManager<C extends CommandSender> {
this.commandSyntaxFormatter = commandSyntaxFormatter;
}
@Nonnull protected CommandRegistrationHandler getCommandRegistrationHandler() {
return this.commandRegistrationHandler;
}
}

View file

@ -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<C extends CommandSender> {
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<C extends CommandSender> {
} 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<C extends CommandSender> {
} 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<C extends CommandSender> {
chain.add(tail);
tail = tail.getParent();
}
return Lists.reverse(chain);
Collections.reverse(chain);
return chain;
}
@ -390,13 +388,13 @@ public class CommandTree<C extends CommandSender> {
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

View file

@ -153,12 +153,12 @@ public class CommandComponent<C extends CommandSender, T> 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

View file

@ -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<List<CommandComponent<?, ?>>, String> {
CommandSyntaxFormatter STANDARD_COMMAND_SYNTAX_FORMATTER = new StandardCommandSyntaxFormatter();
public interface CommandSyntaxFormatter<C extends CommandSender> extends Function<List<CommandComponent<C, ?>>, String> {
@Override
@Nonnull
String apply(@Nonnull List<CommandComponent<?, ?>> commandComponents);
String apply(@Nonnull List<CommandComponent<C, ?>> commandComponents);
}

View file

@ -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<C extends CommandSender> implements CommandSyntaxFormatter<C> {
public StandardCommandSyntaxFormatter() {
}
@Nonnull
@Override
public String apply(@Nonnull final List<CommandComponent<?, ?>> commandComponents) {
public String apply(@Nonnull final List<CommandComponent<C, ?>> commandComponents) {
final StringBuilder stringBuilder = new StringBuilder();
final Iterator<CommandComponent<?, ?>> iterator = commandComponents.iterator();
final Iterator<CommandComponent<C, ?>> iterator = commandComponents.iterator();
while (iterator.hasNext()) {
final CommandComponent<?, ?> commandComponent = iterator.next();
if (commandComponent instanceof StaticComponent) {

View file

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

View file

@ -8,7 +8,9 @@
<artifactId>Commands</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>commands-jline</module>
<!--<module>commands-jline</module>-->
<module>commands-bukkit</module>
<module>commands-core</module>
</modules>
<packaging>pom</packaging>
<inceptionYear>2020</inceptionYear>
@ -126,10 +128,5 @@
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
</project>