diff --git a/paper-api/src/main/java/org/bukkit/command/Command.java b/paper-api/src/main/java/org/bukkit/command/Command.java new file mode 100644 index 0000000000..3bb4cb690a --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/command/Command.java @@ -0,0 +1,52 @@ +package org.bukkit.command; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.entity.Player; + +public abstract class Command { + private final String name; + private List aliases; + protected String tooltip = ""; + protected String usageMessage; + + public Command(String name) { + this.name = name; + this.aliases = new ArrayList(); + this.usageMessage = "/" + name; + } + + public abstract boolean execute(Player player, String currentAlias, String[] args); + + public String getName() { + return name; + } + + public List getAliases() { + return aliases; + } + + public String getTooltip() { + return tooltip; + } + + public String getUsage() { + return usageMessage; + } + + public Command setAliases(List aliases) { + this.aliases = aliases; + return this; + } + + public Command setTooltip(String tooltip) { + this.tooltip = tooltip; + return this; + } + + public Command setUsage(String usage) { + this.usageMessage = usage; + return this; + } +} \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/command/CommandMap.java b/paper-api/src/main/java/org/bukkit/command/CommandMap.java new file mode 100644 index 0000000000..a60ebd174c --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/command/CommandMap.java @@ -0,0 +1,29 @@ +package org.bukkit.command; + +import java.util.List; +import org.bukkit.entity.Player; + +public interface CommandMap { + /** + * Registers all the commands belonging to a certain plugin. + * @param plugin + * @return + */ + public void registerAll(String fallbackPrefix, List commands); + + /** + * Registers a command. Returns true on success; false if name is already taken and fallback had to be used. + * + * @param a label for this command, without the '/'-prefix. + * @return Returns true if command was registered; false if label was already in use. + */ + public boolean register(String label, String fallbackPrefix, Command command); + + /** Looks for the requested command and executes it if found. + * + * @param cmdLine command + arguments. Example: "/test abc 123" + * @return targetFound returns false if no target is found. + */ + public boolean dispatch(Player sender, String cmdLine); + +} diff --git a/paper-api/src/main/java/org/bukkit/command/PluginCommand.java b/paper-api/src/main/java/org/bukkit/command/PluginCommand.java new file mode 100644 index 0000000000..201645fcbd --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/command/PluginCommand.java @@ -0,0 +1,33 @@ +package org.bukkit.command; + +import java.awt.Color; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +public final class PluginCommand extends Command { + private final Plugin owningPlugin; + + public PluginCommand(String name, Plugin owner) { + super(name); + this.owningPlugin = owner; + this.usageMessage = ""; + } + + public boolean execute(Player player, String commandLabel, String[] args) { + boolean cmdSuccess = owningPlugin.onCommand(player, this, commandLabel, args); + if (!cmdSuccess && usageMessage != "") { + String tmpMsg = usageMessage.replace("", commandLabel); + String[] usageLines = tmpMsg.split("\\n"); + for(String line: usageLines) { + while (line.length() > 0) { + int stripChars = (line.length() > 53 ? 53:line.length()); + player.sendMessage(ChatColor.RED + line.substring(0, stripChars)); + line = line.substring(stripChars); + } + } + } + return cmdSuccess; + } +} \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/command/PluginCommandYamlParser.java b/paper-api/src/main/java/org/bukkit/command/PluginCommandYamlParser.java new file mode 100644 index 0000000000..8aa60d7893 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/command/PluginCommandYamlParser.java @@ -0,0 +1,51 @@ +package org.bukkit.command; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.bukkit.plugin.Plugin; + +public class PluginCommandYamlParser { + + public static List parse(Plugin plugin) { + List pluginCmds = new ArrayList(); + Object object = plugin.getDescription().getCommands(); + if (object == null) + return pluginCmds; + + @SuppressWarnings("unchecked") + Map> map = (Map>)object; + + if (map != null) { + for(Entry> entry : map.entrySet()) { + Command newCmd = new PluginCommand(entry.getKey(),plugin); + Object description = entry.getValue().get("description"); + Object usage = entry.getValue().get("usage"); + Object aliases = entry.getValue().get("aliases"); + + if (description != null) + newCmd.setTooltip(description.toString()); + + if (usage != null) { + newCmd.setUsage(usage.toString()); + } + + if (aliases != null) { + List aliasList = new ArrayList(); + + for(String a : aliases.toString().split(",")) { + aliasList.add(a); + } + + newCmd.setAliases(aliasList); + } + + pluginCmds.add(newCmd); + } + } + return pluginCmds; + } + +} diff --git a/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java b/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java new file mode 100644 index 0000000000..a0617ee3fb --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -0,0 +1,61 @@ +package org.bukkit.command; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.bukkit.entity.Player; + +public final class SimpleCommandMap implements CommandMap { + private final Map knownCommands = new HashMap(); + + /** + * Registers multiple commands. Returns name of first command for which fallback had to be used if any. + * @param plugin + * @return + */ + public void registerAll(String fallbackPrefix, List commands) { + if (commands != null) { + for(Command c : commands) { + List names = new ArrayList(); + names.add(c.getName()); + names.addAll(c.getAliases()); + + for(String name : names) { + register(name, fallbackPrefix, c); + } + } + } + } + + /** + * {@inheritDoc} + */ + public boolean register(String name, String fallbackPrefix, Command command) { + boolean nameInUse = (knownCommands.get(command.getName()) != null); + if (nameInUse) + name = fallbackPrefix + ":" + name; + + knownCommands.put(name, command); + System.out.println("Adding cmd: " + name + " for plugin " + fallbackPrefix); + + return !nameInUse; + } + + /** + * {@inheritDoc} + */ + public boolean dispatch(Player sender, String commandLine) { + String[] args = commandLine.split(" "); + String sentCommandLabel = args[0].substring(1); + + Command target = knownCommands.get(sentCommandLabel); + boolean isRegisteredCommand = (target != null); + if (isRegisteredCommand) { + target.execute(sender, sentCommandLabel, args); + } + return isRegisteredCommand; + } + +} diff --git a/paper-api/src/main/java/org/bukkit/plugin/Command.java b/paper-api/src/main/java/org/bukkit/plugin/Command.java deleted file mode 100644 index d48e503ef9..0000000000 --- a/paper-api/src/main/java/org/bukkit/plugin/Command.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.bukkit.plugin; - -public final class Command { - private final String name; - private final String tooltip; - private final String usage; - private final Plugin owner; - - public Plugin getPlugin() { - return owner; - } - - public String getName() { - return name; - } - - public String getTooltip() { - return tooltip; - } - - public String getHelpMessage() { - return usage; - } - - public Command(String name, String tooltip, String helpMessage, Plugin owner) { - this.name = name; - this.tooltip = tooltip; - this.usage = helpMessage; - this.owner = owner; - } -} \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/plugin/CommandManager.java b/paper-api/src/main/java/org/bukkit/plugin/CommandManager.java deleted file mode 100644 index 98747d2392..0000000000 --- a/paper-api/src/main/java/org/bukkit/plugin/CommandManager.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.bukkit.plugin; - -import org.bukkit.entity.Player; - -public interface CommandManager { - /** - * Registers all the commands belonging to a certain plugin. - * @param plugin - * @return - */ - public boolean registerCommands(Plugin plugin); - - /** - * Adds a command to the registeredCommands map. Returns true on success; false if name is already taken. - * - * @param command Name of command, without '/'-prefix. - * @return Returns true if command string was not already registered; false otherwise. - */ - public boolean registerCommand(String command, String tooltip, String helpMessage, Plugin plugin); - - /** Looks up given string in registeredCommands map and calls the onCommand method on the - * appropriate plugin if found. - * - * @param cmdLine command + arguments. Example: "/test abc 123" - * @return targetFound returns false if no target is found. - */ - public boolean dispatchCommand(Player sender, String cmdLine); -} diff --git a/paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java b/paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java deleted file mode 100644 index 50784ba1fa..0000000000 --- a/paper-api/src/main/java/org/bukkit/plugin/CommandParserYaml.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.bukkit.plugin; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -public class CommandParserYaml { - - public static List parse(Plugin plugin) { - List cmds = new ArrayList(); - Object object = plugin.getDescription().getCommands(); - - @SuppressWarnings("unchecked") - Map> map = (Map>)object; - - if (map != null) { - for(Entry> entry : map.entrySet()) { - Object d = entry.getValue().get("description"); - Object u = entry.getValue().get("usage"); - String description = ""; - String usageText = ""; - - if (d != null) - description = d.toString(); - - if (u != null) - usageText = u.toString(); - - cmds.add(new Command(entry.getKey(), description, usageText, plugin)); - } - } - - return cmds; - } - -} diff --git a/paper-api/src/main/java/org/bukkit/plugin/Plugin.java b/paper-api/src/main/java/org/bukkit/plugin/Plugin.java index 74d662ff52..73656c8fe0 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/Plugin.java +++ b/paper-api/src/main/java/org/bukkit/plugin/Plugin.java @@ -3,6 +3,7 @@ package org.bukkit.plugin; import java.io.File; import org.bukkit.Server; +import org.bukkit.command.Command; import org.bukkit.entity.Player; import org.bukkit.util.config.Configuration; @@ -65,6 +66,8 @@ public interface Plugin { /** * Called when a command registered by this plugin is received. + * @param commandLabel + * @return TODO */ - public void onCommand(Player player, String command, String[] args); + public boolean onCommand(Player player, Command command, String commandLabel, String[] args); } diff --git a/paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java b/paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java deleted file mode 100644 index 6c4c8c6043..0000000000 --- a/paper-api/src/main/java/org/bukkit/plugin/SimpleCommandManager.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.bukkit.plugin; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.bukkit.entity.Player; - -public final class SimpleCommandManager implements CommandManager { - private final Map registeredCommands = new HashMap(); - - /** - * Registers all the commands specified in the description file of a plugin. - * @param plugin - * @return - */ - public boolean registerCommands(Plugin plugin) { - List commands = CommandParserYaml.parse(plugin); - boolean existsCommands = (commands != null); - - if (existsCommands) { - for(Command c : commands) { - if (!registerCommand(c)) - return false; // Command name conflict :( - } - } - return existsCommands; - } - - public boolean registerCommand(Command command) { - return registerCommand(command.getName(), command.getTooltip(), command.getHelpMessage(), command.getPlugin()); - } - - /** - * {@inheritDoc} - */ - public boolean registerCommand(String command, String tooltip, String helpMessage, Plugin plugin) { - boolean nameAvailable = (registeredCommands.get(command) == null); - - if (nameAvailable) { - Command newCmd = new Command(command, tooltip, helpMessage, plugin); - registeredCommands.put(command, newCmd); - } - return nameAvailable; - } - - /** - * {@inheritDoc} - */ - public boolean dispatchCommand(Player sender, String cmdLine) { - String[] args = cmdLine.split(" "); - - // Remove '/'-prefix and check if command is registered. - Command target = registeredCommands.get(args[0].substring(1)); - boolean targetFound = (target != null); - - if (targetFound) { - target.getPlugin().onCommand(sender, args[0], args); - } - return targetFound; - } -} diff --git a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index cbf82bc5c5..69e50861a3 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -3,6 +3,7 @@ package org.bukkit.plugin.java; import java.io.File; import org.bukkit.Server; +import org.bukkit.command.Command; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -141,7 +142,7 @@ public abstract class JavaPlugin implements Plugin { /** * Called when a command registered by this plugin is received. */ - public void onCommand(Player player, String command, String[] args) { - // default implementation: do nothing! + public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) { + return false; // default implementation: do nothing! } }