Added Player versions of onExecute and tabComplete

This commit is contained in:
Auxilor
2022-10-30 15:58:14 +00:00
parent 86b427c95e
commit 890f85fa56
3 changed files with 56 additions and 9 deletions

View File

@@ -1,7 +1,9 @@
package com.willfp.eco.core.command;
import com.google.common.collect.ImmutableList;
import com.willfp.eco.core.EcoPlugin;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -43,8 +45,6 @@ public interface CommandBase {
/**
* Handle command execution.
* <p>
* Marked as default void with no implementation for backwards compatibility.
*
* @param sender The sender.
* @param args The args.
@@ -54,20 +54,43 @@ public interface CommandBase {
// Do nothing.
}
/**
* Handle command execution from players.
*
* @param sender The sender.
* @param args The args.
*/
default void onExecute(@NotNull Player sender,
@NotNull List<String> args) {
// Do nothing.
}
/**
* Handle tab completion.
* <p>
* Marked as default void with no implementation for backwards compatibility.
*
* @param sender The sender.
* @param args The args.
* @return The results.
*/
@NotNull
default List<String> tabComplete(@NotNull CommandSender sender,
@NotNull List<String> args) {
return new ArrayList<>();
}
/**
* Handle tab completion.
*
* @param sender The sender.
* @param args The args.
* @return The results.
*/
@NotNull
default List<String> tabComplete(@NotNull Player sender,
@NotNull List<String> args) {
return new ArrayList<>();
}
/**
* Get the plugin.
*
@@ -83,7 +106,11 @@ public interface CommandBase {
* @deprecated Use {@link CommandBase#onExecute(CommandSender, List)} instead.
*/
@Deprecated(forRemoval = true)
CommandHandler getHandler();
default CommandHandler getHandler() {
return (a, b) -> {
};
}
/**
* Set the handler.
@@ -93,7 +120,9 @@ public interface CommandBase {
* @deprecated Handlers have been deprecated.
*/
@Deprecated(forRemoval = true)
void setHandler(@NotNull CommandHandler handler);
default void setHandler(@NotNull final CommandHandler handler) {
// Do nothing.
}
/**
* Get the tab completer.
@@ -103,7 +132,9 @@ public interface CommandBase {
* @deprecated Use {@link CommandBase#tabComplete(CommandSender, List)} instead.
*/
@Deprecated(forRemoval = true)
TabCompleteHandler getTabCompleter();
default TabCompleteHandler getTabCompleter() {
return (a, b) -> ImmutableList.of();
}
/**
* Set the tab completer.
@@ -113,5 +144,7 @@ public interface CommandBase {
* @deprecated Handlers have been deprecated.
*/
@Deprecated(forRemoval = true)
void setTabCompleter(@NotNull TabCompleteHandler handler);
default void setTabCompleter(@NotNull final TabCompleteHandler handler) {
// Do nothing.
}
}

View File

@@ -6,6 +6,7 @@ import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -49,4 +50,10 @@ public final class DelegatedBukkitCommand extends Command implements TabComplete
public Plugin getPlugin() {
return this.delegate.getPlugin();
}
@Nullable
@Override
public String getPermission() {
return this.delegate.getPermission();
}
}

View File

@@ -145,6 +145,9 @@ abstract class HandledCommand implements CommandBase {
this.getHandler().onExecute(sender, Arrays.asList(args));
} else {
this.onExecute(sender, Arrays.asList(args));
if (sender instanceof Player player) {
this.onExecute(player, Arrays.asList(args));
}
}
}
@@ -202,7 +205,11 @@ abstract class HandledCommand implements CommandBase {
if (this.getTabCompleter() != null) {
return this.getTabCompleter().tabComplete(sender, Arrays.asList(args));
} else {
return this.tabComplete(sender, Arrays.asList(args));
List<String> completions = this.tabComplete(sender, Arrays.asList(args));
if (sender instanceof Player player) {
completions.addAll(this.tabComplete(player, Arrays.asList(args)));
}
return completions;
}
}