Command executing

This commit is contained in:
William committed 2022-05-26 23:56:14 +01:00
1 parent e335a48888
commit 83d5a921ee
7 files changed
+98 -29

No files matched your search

@@ -1,33 +1,69 @@
package net.william278.huskhomes.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.BukkitPlayer;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class BukkitCommand extends Command implements TabExecutor {
/**
* Bukkit executor that implements and executes {@link CommandBase}s
*/
public class BukkitCommand implements CommandExecutor, TabExecutor {
protected BukkitCommand(CommandBase command) {
super(command.command, command.getDescription(), command.permission, Arrays.asList(command.aliases));
/**
* The {@link CommandBase} that will be executed
*/
private final CommandBase command;
/**
* The implementing plugin
*/
private final HuskHomes plugin;
public BukkitCommand(@NotNull CommandBase command, @NotNull HuskHomes implementor) {
this.command = command;
this.plugin = implementor;
}
/**
* Registers a {@link PluginCommand} to this implementation
*
* @param pluginCommand {@link PluginCommand} to register
*/
public void register(@NotNull PluginCommand pluginCommand) {
pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this);
pluginCommand.setDescription(pluginCommand.getDescription());
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
return false;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return false;
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
if (sender instanceof Player player) {
this.command.onExecute(BukkitPlayer.adapt(player), args);
} else {
if (command instanceof ConsoleExecutable consoleExecutable) {
consoleExecutable.onConsoleExecute(args);
} else {
plugin.getLocales().getLocale("error_in_game_only").
ifPresent(locale -> sender.spigot().sendMessage(locale.toComponent()));
}
}
return true;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String alias, @NotNull String[] args) {
if (command instanceof TabCompletable tabCompletable) {
return tabCompletable.onTabComplete(BukkitPlayer.adapt((Player) sender), args);
}
return Collections.emptyList();
}
}