mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-31 21:37:39 +01:00
Updated commands to have optional aliases, and to fallback to /pluginName:cmdName on name conflict.
By: VictorD <victor.danell@gmail.com>
This commit is contained in:
parent
b46210453c
commit
ab6f5d4bc2
52
paper-api/src/main/java/org/bukkit/command/Command.java
Normal file
52
paper-api/src/main/java/org/bukkit/command/Command.java
Normal file
@ -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<String> aliases;
|
||||
protected String tooltip = "";
|
||||
protected String usageMessage;
|
||||
|
||||
public Command(String name) {
|
||||
this.name = name;
|
||||
this.aliases = new ArrayList<String>();
|
||||
this.usageMessage = "/" + name;
|
||||
}
|
||||
|
||||
public abstract boolean execute(Player player, String currentAlias, String[] args);
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<String> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public String getTooltip() {
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
public String getUsage() {
|
||||
return usageMessage;
|
||||
}
|
||||
|
||||
public Command setAliases(List<String> 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;
|
||||
}
|
||||
}
|
29
paper-api/src/main/java/org/bukkit/command/CommandMap.java
Normal file
29
paper-api/src/main/java/org/bukkit/command/CommandMap.java
Normal file
@ -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<Command> 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);
|
||||
|
||||
}
|
@ -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("<command>", 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;
|
||||
}
|
||||
}
|
@ -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<Command> parse(Plugin plugin) {
|
||||
List<Command> pluginCmds = new ArrayList<Command>();
|
||||
Object object = plugin.getDescription().getCommands();
|
||||
if (object == null)
|
||||
return pluginCmds;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)object;
|
||||
|
||||
if (map != null) {
|
||||
for(Entry<String, Map<String, Object>> 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<String> aliasList = new ArrayList<String>();
|
||||
|
||||
for(String a : aliases.toString().split(",")) {
|
||||
aliasList.add(a);
|
||||
}
|
||||
|
||||
newCmd.setAliases(aliasList);
|
||||
}
|
||||
|
||||
pluginCmds.add(newCmd);
|
||||
}
|
||||
}
|
||||
return pluginCmds;
|
||||
}
|
||||
|
||||
}
|
@ -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<String, Command> knownCommands = new HashMap<String, Command>();
|
||||
|
||||
/**
|
||||
* 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<Command> commands) {
|
||||
if (commands != null) {
|
||||
for(Command c : commands) {
|
||||
List<String> names = new ArrayList<String>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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<Command> parse(Plugin plugin) {
|
||||
List<Command> cmds = new ArrayList<Command>();
|
||||
Object object = plugin.getDescription().getCommands();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)object;
|
||||
|
||||
if (map != null) {
|
||||
for(Entry<String, Map<String, Object>> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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<String, Command> registeredCommands = new HashMap<String, Command>();
|
||||
|
||||
/**
|
||||
* Registers all the commands specified in the description file of a plugin.
|
||||
* @param plugin
|
||||
* @return
|
||||
*/
|
||||
public boolean registerCommands(Plugin plugin) {
|
||||
List<Command> 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;
|
||||
}
|
||||
}
|
@ -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!
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user