From 3b260e2e5d1ff8a4b29ebb5b5476bf70ee236390 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 12 Dec 2021 12:00:09 +0000 Subject: [PATCH] Improved command system --- .../willfp/eco/core/command/CommandBase.java | 35 +++++++++ .../eco/core/command/CommandHandler.java | 1 + .../eco/core/command/TabCompleteHandler.java | 1 + .../eco/core/command/impl/HandledCommand.java | 72 +++++++++---------- .../willfp/eco/internal/spigot/EcoHandler.kt | 2 +- 5 files changed, 74 insertions(+), 37 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java b/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java index 639e5a64..cd86e395 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java +++ b/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java @@ -1,7 +1,11 @@ package com.willfp.eco.core.command; +import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; + /** * Interface for all command implementations. */ @@ -39,27 +43,58 @@ public interface CommandBase { * Get the handler. * * @return The handler. + * @deprecated Use {@link CommandBase#onExecute(CommandSender, List)} instead. */ + @Deprecated CommandHandler getHandler(); /** * Set the handler. * * @param handler The handler. + * @deprecated Handlers have been deprecated. */ + @Deprecated void setHandler(@NotNull CommandHandler handler); /** * Get the tab completer. * * @return The tab completer. + * @deprecated Use {@link CommandBase#tabComplete(CommandSender, List)} instead. */ + @Deprecated TabCompleteHandler getTabCompleter(); /** * Set the tab completer. * * @param handler The handler. + * @deprecated Handlers have been deprecated. */ + @Deprecated void setTabCompleter(@NotNull TabCompleteHandler handler); + + /** + * Handle command execution. + * + * @param sender The sender. + * @param args The args. + */ + default void onExecute(@NotNull CommandSender sender, + @NotNull List args) { + // Do nothing. + } + + /** + * Handle tab completion. + * + * @param sender The sender. + * @param args The args. + * @return The results. + */ + default List tabComplete(@NotNull CommandSender sender, + @NotNull List args) { + return new ArrayList<>(); + } } diff --git a/eco-api/src/main/java/com/willfp/eco/core/command/CommandHandler.java b/eco-api/src/main/java/com/willfp/eco/core/command/CommandHandler.java index 9116ae21..516af345 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/command/CommandHandler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/command/CommandHandler.java @@ -13,6 +13,7 @@ import java.util.List; * @see CommandBase */ @FunctionalInterface +@Deprecated public interface CommandHandler { /** * The code to be called on execution. diff --git a/eco-api/src/main/java/com/willfp/eco/core/command/TabCompleteHandler.java b/eco-api/src/main/java/com/willfp/eco/core/command/TabCompleteHandler.java index b2b746ae..4caa8567 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/command/TabCompleteHandler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/command/TabCompleteHandler.java @@ -13,6 +13,7 @@ import java.util.List; * @see CommandBase */ @FunctionalInterface +@Deprecated public interface TabCompleteHandler { /** * Handle Tab Completion. diff --git a/eco-api/src/main/java/com/willfp/eco/core/command/impl/HandledCommand.java b/eco-api/src/main/java/com/willfp/eco/core/command/impl/HandledCommand.java index c23eb82e..752992e7 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/command/impl/HandledCommand.java +++ b/eco-api/src/main/java/com/willfp/eco/core/command/impl/HandledCommand.java @@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.util.StringUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Arrays; @@ -23,6 +24,7 @@ import java.util.stream.Collectors; * in order to execute the command-specific code. It's essentially an internal * layer, hence why it's a package-private class. */ +@SuppressWarnings({"DeprecatedIsStillUsed", "deprecation"}) abstract class HandledCommand extends PluginDependent implements CommandBase { /** * The name of the command. @@ -46,14 +48,16 @@ abstract class HandledCommand extends PluginDependent implements Comm /** * The actual code to be executed in the command. */ - private CommandHandler handler = (sender, args) -> { - // Do nothing by default - }; + @Deprecated + @Nullable + private CommandHandler handler = null; /** * The tab completion code to be executed in the command. */ - private TabCompleteHandler tabCompleter = (sender, args) -> new ArrayList<>(); + @Deprecated + @Nullable + private TabCompleteHandler tabCompleter = null; /** * All subcommands for the command. @@ -120,7 +124,11 @@ abstract class HandledCommand extends PluginDependent implements Comm } } - this.getHandler().onExecute(sender, Arrays.asList(args)); + if (this.getHandler() != null) { + this.getHandler().onExecute(sender, Arrays.asList(args)); + } else { + this.onExecute(sender, Arrays.asList(args)); + } } /** @@ -167,7 +175,11 @@ abstract class HandledCommand extends PluginDependent implements Comm } } - return this.getTabCompleter().tabComplete(sender, Arrays.asList(args)); + if (this.getTabCompleter() != null) { + return this.getTabCompleter().tabComplete(sender, Arrays.asList(args)); + } else { + return this.tabComplete(sender, Arrays.asList(args)); + } } /** @@ -221,24 +233,6 @@ abstract class HandledCommand extends PluginDependent implements Comm return this.playersOnly; } - /** - * Get the actual code to be executed in the command. - * - * @return The code. - */ - public CommandHandler getHandler() { - return this.handler; - } - - /** - * Get the tab completion code to be executed in the command. - * - * @return The code. - */ - public TabCompleteHandler getTabCompleter() { - return this.tabCompleter; - } - /** * Get the subcommands of the command. * @@ -248,21 +242,27 @@ abstract class HandledCommand extends PluginDependent implements Comm return this.subcommands; } - /** - * Set the command handler. - * - * @param handler The handler. - */ - public void setHandler(@NotNull final CommandHandler handler) { + @Deprecated + @Override + public @Nullable CommandHandler getHandler() { + return this.handler; + } + + @Deprecated + @Override + public @Nullable TabCompleteHandler getTabCompleter() { + return this.tabCompleter; + } + + @Deprecated + @Override + public void setHandler(@Nullable final CommandHandler handler) { this.handler = handler; } - /** - * Set the tab completer. - * - * @param tabCompleter The tab completer. - */ - public void setTabCompleter(@NotNull final TabCompleteHandler tabCompleter) { + @Deprecated + @Override + public void setTabCompleter(@Nullable final TabCompleteHandler tabCompleter) { this.tabCompleter = tabCompleter; } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt index d9f2d7e6..822b74f2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt @@ -31,7 +31,7 @@ import org.bukkit.inventory.ItemStack import java.util.logging.Logger @Suppress("UNUSED") -class EcoHandler : com.willfp.eco.internal.spigot.EcoSpigotPlugin(), Handler { +class EcoHandler : EcoSpigotPlugin(), Handler { private val cleaner = EcoCleaner() private val requirementFactory = EcoRequirementFactory() private var adventure: BukkitAudiences? = null