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
+96 -27

No files matched your search

@@ -7,6 +7,9 @@ import dev.dejvokep.boostedyaml.settings.general.GeneralSettings;
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings; import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings; import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings;
import net.william278.huskhomes.cache.Cache; 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.Locales;
import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.data.Database; import net.william278.huskhomes.data.Database;
@@ -27,6 +30,7 @@ import net.william278.huskhomes.util.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -131,6 +135,15 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes {
// Prepare the teleport manager // Prepare the teleport manager
this.teleportManager = new BukkitTeleportManager(this); 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 @Override
@@ -1,33 +1,69 @@
package net.william278.huskhomes.command; package net.william278.huskhomes.command;
import org.bukkit.command.Command; import net.william278.huskhomes.HuskHomes;
import org.bukkit.command.CommandSender; import net.william278.huskhomes.player.BukkitPlayer;
import org.bukkit.command.TabExecutor; import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Collections;
import java.util.List; 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 @Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
return false; @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()));
} }
}
@Override return true;
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return false;
} }
@Nullable @Nullable
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
return null; @NotNull String alias, @NotNull String[] args) {
if (command instanceof TabCompletable tabCompletable) {
return tabCompletable.onTabComplete(BukkitPlayer.adapt((Player) sender), args);
}
return Collections.emptyList();
} }
} }
@@ -4,8 +4,6 @@ import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.Player; import net.william278.huskhomes.player.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
/** /**
* Represents an abstract cross-platform representation for a plugin command * 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); 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<String> onTabComplete(@NotNull Player player, @NotNull String[] args);
/** /**
* Returns the localised description string of this command * Returns the localised description string of this command
* *
@@ -2,8 +2,16 @@ package net.william278.huskhomes.command;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/**
* Interface providing console execution of commands
*/
public interface ConsoleExecutable { public interface ConsoleExecutable {
/**
* What to do when console executes a command
*
* @param args command argument strings
*/
void onConsoleExecute(@NotNull String[] args); void onConsoleExecute(@NotNull String[] args);
} }
@@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; 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"; private final static String PERMISSION = "huskhomes.command.home";
@@ -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<String> onTabComplete(@NotNull Player player, @NotNull String[] args);
}
@@ -151,3 +151,4 @@ setspawn_command_description: 'Set the spawn position'
tpaall_command_description: 'Request that everyone teleports to you' tpaall_command_description: 'Request that everyone teleports to you'
tpall_command_description: 'Teleport everyone to your position' tpall_command_description: 'Teleport everyone to your position'
huskhomes_command_description: 'View plugin information & reload configs' huskhomes_command_description: 'View plugin information & reload configs'
error_in_game_only: '[Error:](#ff3300) [That command can only be run in-game.](#ff7e5e)'