diff --git a/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java b/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java index fb786b19..e52d6881 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java +++ b/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java @@ -7,6 +7,9 @@ import dev.dejvokep.boostedyaml.settings.general.GeneralSettings; import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings; import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings; import net.william278.huskhomes.cache.Cache; +import net.william278.huskhomes.command.BukkitCommand; +import net.william278.huskhomes.command.CommandBase; +import net.william278.huskhomes.command.HomeCommand; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.data.Database; @@ -27,6 +30,7 @@ import net.william278.huskhomes.util.Logger; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.command.PluginCommand; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -131,6 +135,15 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { // Prepare the teleport manager this.teleportManager = new BukkitTeleportManager(this); + + // Register commands - todo add all here + final CommandBase[] commands = new CommandBase[]{new HomeCommand(this)}; + for (CommandBase commandBase : commands) { + final PluginCommand pluginCommand = getCommand(commandBase.command); + if (pluginCommand != null) { + new BukkitCommand(commandBase, this).register(pluginCommand); + } + } } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java index 1fe91524..772c6215 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java @@ -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 onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { - return null; + public List 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(); } } diff --git a/common/src/main/java/net/william278/huskhomes/command/CommandBase.java b/common/src/main/java/net/william278/huskhomes/command/CommandBase.java index 33660f45..8edff400 100644 --- a/common/src/main/java/net/william278/huskhomes/command/CommandBase.java +++ b/common/src/main/java/net/william278/huskhomes/command/CommandBase.java @@ -4,8 +4,6 @@ import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.player.Player; import org.jetbrains.annotations.NotNull; -import java.util.List; - /** * Represents an abstract cross-platform representation for a plugin command */ @@ -47,15 +45,6 @@ public abstract class CommandBase { */ public abstract void onExecute(@NotNull Player player, @NotNull String[] args); - /** - * What should be returned when the player attempts to TAB-complete the command - * - * @param player {@link Player} doing the TAB completion - * @param args Current command arguments - * @return List of String arguments to offer TAB suggestions - */ - public abstract List onTabComplete(@NotNull Player player, @NotNull String[] args); - /** * Returns the localised description string of this command * diff --git a/common/src/main/java/net/william278/huskhomes/command/ConsoleExecutable.java b/common/src/main/java/net/william278/huskhomes/command/ConsoleExecutable.java index 8a36dfa9..7e3508c3 100644 --- a/common/src/main/java/net/william278/huskhomes/command/ConsoleExecutable.java +++ b/common/src/main/java/net/william278/huskhomes/command/ConsoleExecutable.java @@ -2,8 +2,16 @@ package net.william278.huskhomes.command; import org.jetbrains.annotations.NotNull; +/** + * Interface providing console execution of commands + */ public interface ConsoleExecutable { + /** + * What to do when console executes a command + * + * @param args command argument strings + */ void onConsoleExecute(@NotNull String[] args); } diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java index 8bd97a5f..7d0736a3 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.stream.Collectors; -public class HomeCommand extends CommandBase { +public class HomeCommand extends CommandBase implements TabCompletable { private final static String PERMISSION = "huskhomes.command.home"; diff --git a/common/src/main/java/net/william278/huskhomes/command/TabCompletable.java b/common/src/main/java/net/william278/huskhomes/command/TabCompletable.java new file mode 100644 index 00000000..22988a6c --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/TabCompletable.java @@ -0,0 +1,22 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.player.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Interface providing tab completions for a command + */ +public interface TabCompletable { + + /** + * What should be returned when the player attempts to TAB-complete the command + * + * @param player {@link Player} doing the TAB completion + * @param args Current command arguments + * @return List of String arguments to offer TAB suggestions + */ + List onTabComplete(@NotNull Player player, @NotNull String[] args); + +} diff --git a/common/src/main/resources/languages/en-gb.yml b/common/src/main/resources/languages/en-gb.yml index b5962741..b8ee4915 100644 --- a/common/src/main/resources/languages/en-gb.yml +++ b/common/src/main/resources/languages/en-gb.yml @@ -150,4 +150,5 @@ spawn_command_description: 'Teleport to spawn' setspawn_command_description: 'Set the spawn position' tpaall_command_description: 'Request that everyone teleports to you' tpall_command_description: 'Teleport everyone to your position' -huskhomes_command_description: 'View plugin information & reload configs' \ No newline at end of file +huskhomes_command_description: 'View plugin information & reload configs' +error_in_game_only: '[Error:](#ff3300) [That command can only be run in-game.](#ff7e5e)' \ No newline at end of file