mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
Added command registration to plugin config file. Registered commands are sent to the corresponding plugin.onCommand method when executed.
By: VictorD <victor.danell@gmail.com>
This commit is contained in:
parent
c72a7064d8
commit
a17e7470e8
9 changed files with 184 additions and 0 deletions
|
@ -3,6 +3,7 @@ package org.bukkit;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.bukkit.fillr;
|
||||||
import org.bukkit.*;
|
import org.bukkit.*;
|
||||||
import org.bukkit.plugin.*;
|
import org.bukkit.plugin.*;
|
||||||
import org.bukkit.plugin.java.*;
|
import org.bukkit.plugin.java.*;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.*;
|
import org.bukkit.event.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -30,4 +31,8 @@ public class Fillr extends JavaPlugin {
|
||||||
listener = new FillrListener(getServer());
|
listener = new FillrListener(getServer());
|
||||||
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this);
|
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onCommand(Player player, String command, String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
31
paper-api/src/main/java/org/bukkit/plugin/Command.java
Normal file
31
paper-api/src/main/java/org/bukkit/plugin/Command.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
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);
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
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()) {
|
||||||
|
String description = entry.getValue().get("description").toString();
|
||||||
|
String usageText = entry.getValue().get("usage").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 java.io.File;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.util.config.Configuration;
|
import org.bukkit.util.config.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,4 +62,9 @@ public interface Plugin {
|
||||||
* Called when this plugin is enabled
|
* Called when this plugin is enabled
|
||||||
*/
|
*/
|
||||||
public void onEnable();
|
public void onEnable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a command registered by this plugin is received.
|
||||||
|
*/
|
||||||
|
public void onCommand(Player player, String command, String[] args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ public final class PluginDescriptionFile {
|
||||||
private String name = null;
|
private String name = null;
|
||||||
private String main = null;
|
private String main = null;
|
||||||
private String version = null;
|
private String version = null;
|
||||||
|
private Object commands = null;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException {
|
public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException {
|
||||||
|
@ -80,6 +81,10 @@ public final class PluginDescriptionFile {
|
||||||
return main;
|
return main;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getCommands() {
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
|
||||||
private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
|
private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
|
||||||
try {
|
try {
|
||||||
name = map.get("name").toString();
|
name = map.get("name").toString();
|
||||||
|
@ -104,6 +109,14 @@ public final class PluginDescriptionFile {
|
||||||
} catch (ClassCastException ex) {
|
} catch (ClassCastException ex) {
|
||||||
throw new InvalidDescriptionException(ex, "main is of wrong type");
|
throw new InvalidDescriptionException(ex, "main is of wrong type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
commands = map.get("commands");
|
||||||
|
} catch (NullPointerException ex) {
|
||||||
|
throw new InvalidDescriptionException(ex, "command is not defined");
|
||||||
|
} catch (ClassCastException ex) {
|
||||||
|
throw new InvalidDescriptionException(ex, "command is of wrong type");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> saveMap() {
|
private Map<String, Object> saveMap() {
|
||||||
|
@ -111,6 +124,7 @@ public final class PluginDescriptionFile {
|
||||||
map.put("name", name);
|
map.put("name", name);
|
||||||
map.put("main", main);
|
map.put("main", main);
|
||||||
map.put("version", version);
|
map.put("version", version);
|
||||||
|
map.put("command", commands);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
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 java.io.File;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.PluginDescriptionFile;
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
import org.bukkit.plugin.PluginLoader;
|
import org.bukkit.plugin.PluginLoader;
|
||||||
|
@ -136,4 +137,11 @@ 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!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue