From 89c949f0d82a73c68f2d1e87f6244025fafd552d Mon Sep 17 00:00:00 2001 From: Nathan Blaney Date: Thu, 2 Nov 2023 21:09:36 +1100 Subject: [PATCH] Added basic tab complete --- res/plugin.yml | 11 + .../dev/MobMortalityMessages/CommandBase.java | 98 ++++++++ .../MobMortalityMessages.java | 225 ++++++++++-------- 3 files changed, 234 insertions(+), 100 deletions(-) create mode 100644 src/im/wma/dev/MobMortalityMessages/CommandBase.java diff --git a/res/plugin.yml b/res/plugin.yml index b2afc34..9197702 100644 --- a/res/plugin.yml +++ b/res/plugin.yml @@ -7,5 +7,16 @@ main: im.wma.dev.MobMortalityMessages.MobMortalityMessages prefix: MMM commands: mmm: + description: Returns the plugin version + usage: /mmm + mmm toggle: + description: Toggles the display of Mob Mortality Messages + usage: /mmm toggle + mmm version: + description: Returns the plugin version + usage: /mmm version +permissions: + mmm.messages: + description: Allows users to see Mob Mortality Messages permission: mmm.messages \ No newline at end of file diff --git a/src/im/wma/dev/MobMortalityMessages/CommandBase.java b/src/im/wma/dev/MobMortalityMessages/CommandBase.java new file mode 100644 index 0000000..84a8c08 --- /dev/null +++ b/src/im/wma/dev/MobMortalityMessages/CommandBase.java @@ -0,0 +1,98 @@ +package im.wma.dev.MobMortalityMessages; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.plugin.Plugin; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A simple class for implementing sub commands. + * Source: https://gist.github.com/dumptruckman/6d5fb33b662ce91e2d232b22a19201d3 + * Just register one of these as the executor for your plugin's root command and then register sub commands to this + * CommandBase with {@link #registerSubCommand(String, CommandExecutor)}. + * + * @param

The implementing plugin. + */ +public abstract class CommandBase

implements CommandExecutor, TabExecutor { + + private final Map subCommands = new HashMap<>(); + private final Map subCommandsTab = new HashMap<>(); + private final P plugin; + + /** + * Creates a new CommandBase for the given plugin. + * + * @param plugin The plugin that owns this command. + */ + public CommandBase(P plugin) { + this.plugin = plugin; + } + + /** + * Returns the plugin that owns this command. + */ + public P getPlugin() { + return plugin; + } + + /** + * Registers a sub command to this command. + * + * @param label The label for the sub command. + * @param subCommand The sub command to register which can either be a plain CommandExecutor or another + * CommandBase if further command nesting is desired. + */ + public void registerSubCommand(String label, CommandExecutor subCommand) { + subCommands.put(label.toLowerCase(), subCommand); + } + public void registerSubCommandTab(String label, TabExecutor subCommandTab) { + subCommandsTab.put(label.toLowerCase(), subCommandTab); + } + + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length > 0) { + CommandExecutor child = subCommands.get(args[0].toLowerCase()); + if (child != null) { + label = args[0]; + String[] newArgs = new String[args.length - 1]; + System.arraycopy(args, 1, newArgs, 0, newArgs.length); + return child.onCommand(sender, command, label, newArgs); + } + } + return runCommand(sender, command, label, args); + } + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (args.length > 1) { + TabExecutor child = subCommandsTab.get(args[0].toLowerCase()); + if (child != null) { + label = args[0]; + String[] newArgs = new String[args.length - 1]; + System.arraycopy(args, 1, newArgs, 0, newArgs.length); + return child.onTabComplete(sender, command, label, newArgs); + } + } + return tabCommand(sender, command, label, args); + } + + /** + * Executes the given commands and returns its success. + * + * Note that the success returned may propagate up to the root command. + * + * @param sender Source of the command. + * @param rootCommand The top level command that was executed. + * @param label Alias of the command that was used - the sub command label being used. + * @param args Arguments for the sub command. + * @return true if a valid command, false otherwise. + */ + public abstract boolean runCommand(CommandSender sender, Command rootCommand, String label, String[] args); + public abstract List tabCommand(CommandSender sender, Command rootCommand, String label, String[] args); +} \ No newline at end of file diff --git a/src/im/wma/dev/MobMortalityMessages/MobMortalityMessages.java b/src/im/wma/dev/MobMortalityMessages/MobMortalityMessages.java index fc1907d..b8d4be4 100644 --- a/src/im/wma/dev/MobMortalityMessages/MobMortalityMessages.java +++ b/src/im/wma/dev/MobMortalityMessages/MobMortalityMessages.java @@ -1,6 +1,7 @@ package im.wma.dev.MobMortalityMessages; import java.util.HashMap; +import java.util.List; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -11,104 +12,128 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; -public class MobMortalityMessages extends JavaPlugin -{ - public HashMap enabledMap; - - private PluginDescriptionFile pdf; - private String version; - private String name; - private String author; - private DamageHandler damageHandler; - - @Override - public void onEnable() - { - pdf = this.getDescription(); - version = pdf.getVersion(); - name = pdf.getName(); - author = pdf.getAuthors().get(0); - - Bukkit.getLogger().info(name+" v"+version+" enabled!"); - enabledMap = new HashMap(); - damageHandler = new DamageHandler(this); - - //Register the DamageHandler as a listener for events - Bukkit.getServer().getPluginManager().registerEvents(damageHandler, this); - } - - @Override - public void onDisable() - { - Bukkit.getLogger().info(name+" v"+version+" disabled!"); - enabledMap = null; - pdf = null; - - //Unregister the listener - EntityDamageByEntityEvent.getHandlerList().unregister(this); - } - - @Override - public boolean onCommand(CommandSender aSender, Command aCmd, String aLabel, String[] aArgs) - { - - //Check they've got permission to do this - if (!aSender.hasPermission("mmm.messages")) - { - aSender.sendMessage("You don't have permission for that!"); - return false; - } - - //The command we're interested in - if (aCmd.getName().equalsIgnoreCase("mmm")) - { - if (aArgs.length < 1) - { - aSender.sendMessage("Incorrect number of parameters."); - return false; - } - - if (aArgs[0].equalsIgnoreCase("toggle")) - { - if (aSender instanceof Player) - { - addPlayer((Player) aSender); - return true; - } - else - { - aSender.sendMessage("This command can only be run as a player!"); - return false; - } - } - - if (aArgs[0].equalsIgnoreCase("version")) - { - aSender.sendMessage(name); - aSender.sendMessage(version); - aSender.sendMessage(author); - } - } - - return false; - } - - private void addPlayer(Player aPlayer) - { - //If they're already in the map then toggle the bool - if (enabledMap.containsKey((aPlayer))) - { - Boolean flag = enabledMap.get(aPlayer); - flag = !flag; - enabledMap.put(aPlayer, flag); - - aPlayer.sendMessage("Mob health messages "+(flag ? "enabled." : "disabled.")); - } - else //Otherwise add them and set the bool to true - { - enabledMap.put(aPlayer, true); - - aPlayer.sendMessage("Mob health messages enabled."); - } - } +public class MobMortalityMessages extends JavaPlugin { + public HashMap enabledMap; + + private PluginDescriptionFile pdf; + private String version; + private String name; + private String author; + private DamageHandler damageHandler; + + @Override + public void onEnable() { + pdf = this.getDescription(); + version = pdf.getVersion(); + name = pdf.getName(); + author = pdf.getAuthors().get(0); + + Bukkit.getLogger().info(name + " v" + version + " enabled!"); + enabledMap = new HashMap(); + damageHandler = new DamageHandler(this); + + //Register the DamageHandler as a listener for events + Bukkit.getServer().getPluginManager().registerEvents(damageHandler, this); + + // Anonymous implementation of "/mmm" root command. + CommandBase mmmCommand = new CommandBase(this) { + @Override + public boolean runCommand(CommandSender sender, Command rootCommand, String label, String[] args) { + sender.sendMessage("MobMortalityMessages v" + getPlugin().getDescription().getVersion()); + return true; + } + + @Override + public List tabCommand(CommandSender sender, Command rootCommand, String label, String[] args) { + return null; + } + }; + + CommandToggle commandToggle = new CommandToggle(this); + mmmCommand.registerSubCommand("toggle", commandToggle); + + CommandVersion commandReload = new CommandVersion(this); + mmmCommand.registerSubCommand("version", commandReload); + + + // Register "/check" command executor with Bukkit. + getCommand("mmm").setExecutor(mmmCommand); + } + + @Override + public void onDisable() { + Bukkit.getLogger().info(name + " v" + version + " disabled!"); + enabledMap = null; + pdf = null; + + //Unregister the listener + EntityDamageByEntityEvent.getHandlerList().unregister(this); + } + + public class CommandToggle extends CommandBase { + public CommandToggle(MobMortalityMessages plugin) { + super(plugin); + } + + @Override + public boolean runCommand(CommandSender sender, Command rootCommand, String label, String[] args) { + if (sender.hasPermission("mmm.messages")) { + if (sender instanceof Player) { + addPlayer((Player) sender); + return true; + } else { + sender.sendMessage("This command can only be run as a player!"); + return false; + } + } else { + sender.sendMessage("You don't have permission for that!"); + } + return true; + } + + @Override + public List tabCommand(CommandSender sender, Command rootCommand, String label, String[] args) { + return null; + } + } + + public class CommandVersion extends CommandBase { + public CommandVersion(MobMortalityMessages plugin) { + super(plugin); + } + + @Override + public boolean runCommand(CommandSender sender, Command rootCommand, String label, String[] args) { + if (sender.hasPermission("mmm.messages")) { + sender.sendMessage(name); + sender.sendMessage(version); + sender.sendMessage(author); + } else { + sender.sendMessage("You don't have permission for that!"); + } + return true; + } + + @Override + public List tabCommand(CommandSender sender, Command rootCommand, String label, String[] args) { + return null; + } + } + + private void addPlayer(Player aPlayer) { + //If they're already in the map then toggle the bool + if (enabledMap.containsKey((aPlayer))) { + Boolean flag = enabledMap.get(aPlayer); + flag = !flag; + enabledMap.put(aPlayer, flag); + + aPlayer.sendMessage("Mob health messages " + (flag ? "enabled." : "disabled.")); + } else //Otherwise add them and set the bool to true + { + enabledMap.put(aPlayer, true); + + aPlayer.sendMessage("Mob health messages enabled."); + } + } } +