player/console definition and on-the-fly commands
This commit is contained in:
@@ -28,6 +28,7 @@ import com.nemez.cmdmgr.util.BranchStack;
|
||||
import com.nemez.cmdmgr.util.Executable;
|
||||
import com.nemez.cmdmgr.util.HelpPageCommand;
|
||||
import com.nemez.cmdmgr.util.Property;
|
||||
import com.nemez.cmdmgr.util.Type;
|
||||
|
||||
/**
|
||||
* Example command.cmd
|
||||
@@ -92,6 +93,7 @@ public class CommandManager {
|
||||
public static String helpPageHeaderFormatting = "<EFBFBD>a";
|
||||
public static String helpInvalidPageFormatting = "<EFBFBD>c";
|
||||
public static String noPermissionFormatting = "<EFBFBD>c";
|
||||
public static String notAllowedFormatting = "<EFBFBD>c";
|
||||
|
||||
public static boolean registerCommand(String cmdSourceCode, Object commandHandler, JavaPlugin plugin) {
|
||||
if (cmdSourceCode == null || commandHandler == null || plugin == null) {
|
||||
@@ -198,6 +200,13 @@ public class CommandManager {
|
||||
stack.get().execute = buffer.toString();
|
||||
}else if (currentProp == Property.PERMISSION) {
|
||||
stack.get().permission = buffer.toString().trim();
|
||||
}else if (currentProp == Property.TYPE) {
|
||||
stack.get().type = resolveExecutionType(buffer.toString().trim());
|
||||
if (stack.get().type == null) {
|
||||
plugin.getLogger().log(Level.WARNING, "Attribute error at line " + line + ": Invalid attribute value. (" + buffer.toString().trim() + ").");
|
||||
errors = true;
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
plugin.getLogger().log(Level.WARNING, "Attribute error at line " + line + ": Invalid attribute type.");
|
||||
errors = true;
|
||||
@@ -261,6 +270,8 @@ public class CommandManager {
|
||||
currentProp = Property.EXECUTE;
|
||||
}else if (buffer.toString().equals("perm")) {
|
||||
currentProp = Property.PERMISSION;
|
||||
}else if (buffer.toString().equals("type")) {
|
||||
currentProp = Property.TYPE;
|
||||
}else{
|
||||
if (gettingName && cmdName == null) {
|
||||
cmdName = buffer.toString().trim();
|
||||
@@ -361,6 +372,24 @@ public class CommandManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Type resolveExecutionType(String type) {
|
||||
switch (type) {
|
||||
case "player":
|
||||
return Type.PLAYER;
|
||||
case "both":
|
||||
case "any":
|
||||
case "all":
|
||||
return Type.BOTH;
|
||||
case "server":
|
||||
case "console":
|
||||
return Type.CONSOLE;
|
||||
case "none":
|
||||
case "nobody":
|
||||
return Type.NOBODY;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void postProcess(String cmdName, ChainComponent components, ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer) {
|
||||
Executable cmd = new Executable(cmdName, constructHelpPages(cmdName, components));
|
||||
cmd.register(methods, plugin, methodContainer);
|
||||
@@ -373,7 +402,7 @@ public class CommandManager {
|
||||
HelpPageCommand[] page = new HelpPageCommand[5];
|
||||
|
||||
for (int i = 0; i < rawLines.length; i++) {
|
||||
if (rawLines[i].length() > 0 && !rawLines[i].equals("\0null\0null\0null\0")) {
|
||||
if (rawLines[i].length() > 0 && !rawLines[i].equals("\0null\0null\0null\0null\0")) {
|
||||
lines.add(rawLines[i]);
|
||||
}
|
||||
}
|
||||
@@ -386,14 +415,14 @@ public class CommandManager {
|
||||
page = new HelpPageCommand[5];
|
||||
}
|
||||
String[] cmd = lines.get(i).split("\0");
|
||||
page[i % 5] = new HelpPageCommand(cmd[1], "/" + cmdName + " " + cmd[0], cmd[3], cmd[2]);
|
||||
page[i % 5] = new HelpPageCommand(cmd[1], "/" + cmdName + " " + cmd[0], cmd[3], cmd[2], Type.parse(cmd[4]));
|
||||
firstPass = false;
|
||||
}
|
||||
if (i % 5 == 0) {
|
||||
pages.add(page);
|
||||
page = new HelpPageCommand[5];
|
||||
}
|
||||
page[i % 5] = new HelpPageCommand(cmdName + ".help", "/" + cmdName + " help <page:i32>", "Shows help.", null);
|
||||
page[i % 5] = new HelpPageCommand(cmdName + ".help", "/" + cmdName + " help <page:i32>", "Shows help.", null, Type.BOTH);
|
||||
pages.add(page);
|
||||
|
||||
return pages;
|
||||
@@ -416,7 +445,7 @@ public class CommandManager {
|
||||
chain += temp;
|
||||
}
|
||||
}
|
||||
data += chain + "\0" + comp.permission + "\0" + comp.execute + "\0" + comp.help + "\0";
|
||||
data += chain + "\0" + comp.permission + "\0" + comp.execute + "\0" + comp.help + "\0" + Type.get(comp.type) + "\0";
|
||||
for (String s : leaves) {
|
||||
data += s;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package com.nemez.cmdmgr.component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.nemez.cmdmgr.util.Type;
|
||||
|
||||
public class ChainComponent implements ICommandComponent {
|
||||
|
||||
private ArrayList<ICommandComponent> components;
|
||||
public String permission;
|
||||
public String help;
|
||||
public String execute;
|
||||
public Type type;
|
||||
|
||||
public ChainComponent() {
|
||||
components = new ArrayList<ICommandComponent>();
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package com.nemez.cmdmgr.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.nemez.cmdmgr.Command;
|
||||
@@ -22,7 +25,7 @@ import com.nemez.cmdmgr.component.LongComponent;
|
||||
import com.nemez.cmdmgr.component.ShortComponent;
|
||||
import com.nemez.cmdmgr.component.StringComponent;
|
||||
|
||||
public class Executable implements CommandExecutor {
|
||||
public class Executable extends org.bukkit.command.Command {
|
||||
|
||||
private ArrayList<ExecutableDefinition> commands;
|
||||
private ArrayList<HelpPageCommand[]> help;
|
||||
@@ -30,6 +33,7 @@ public class Executable implements CommandExecutor {
|
||||
private JavaPlugin plugin;
|
||||
|
||||
public Executable(String name, ArrayList<HelpPageCommand[]> help) {
|
||||
super(name);
|
||||
this.help = help;
|
||||
this.name = name;
|
||||
this.commands = new ArrayList<ExecutableDefinition>();
|
||||
@@ -39,13 +43,26 @@ public class Executable implements CommandExecutor {
|
||||
for (HelpPageCommand[] page : help) {
|
||||
for (HelpPageCommand cmd : page) {
|
||||
if (cmd != null) {
|
||||
processLine(cmd.usage.split("\\ "), cmd.permission, cmd.method, methods, methodContainer, plugin);
|
||||
processLine(cmd.usage.split("\\ "), cmd.permission, cmd.method, methods, methodContainer, plugin, cmd.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.plugin = plugin;
|
||||
plugin.getCommand(name).setExecutor(this);
|
||||
try {
|
||||
final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||
cmdMap.setAccessible(true);
|
||||
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
|
||||
map.register(name, this);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (CommandManager.errors) {
|
||||
plugin.getLogger().log(Level.WARNING, "There were parser errors, some commands may not function properly!");
|
||||
@@ -53,14 +70,14 @@ public class Executable implements CommandExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private void processLine(String[] line, String permission, String method, ArrayList<Method> methods, Object methodContainer, JavaPlugin plugin) {
|
||||
private void processLine(String[] line, String permission, String method, ArrayList<Method> methods, Object methodContainer, JavaPlugin plugin, Type etype) {
|
||||
ArrayList<ICommandComponent> command = new ArrayList<ICommandComponent>();
|
||||
if (method == null && line[1].equals("help")) {
|
||||
command.add(new ConstantComponent("help"));
|
||||
IntegerComponent pageID = new IntegerComponent();
|
||||
pageID.argName = "page";
|
||||
command.add(pageID);
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, permission, null, methodContainer);
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, permission, null, methodContainer, Type.BOTH);
|
||||
commands.add(def);
|
||||
return;
|
||||
}
|
||||
@@ -198,12 +215,15 @@ public class Executable implements CommandExecutor {
|
||||
CommandManager.errors = true;
|
||||
return;
|
||||
}
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, permission, target, methodContainer);
|
||||
if (etype == null) {
|
||||
etype = Type.BOTH;
|
||||
}
|
||||
ExecutableDefinition def = new ExecutableDefinition(command, permission, target, methodContainer, etype);
|
||||
commands.add(def);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String name, String[] args) {
|
||||
public boolean execute(CommandSender sender, String name, String[] args) {
|
||||
ArrayList<ExecutableDefinition> defs = new ArrayList<ExecutableDefinition>();
|
||||
defs.addAll(commands);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
@@ -218,10 +238,35 @@ public class Executable implements CommandExecutor {
|
||||
printPage(sender, 1);
|
||||
}else{
|
||||
ExecutableDefinition def = defs.get(0);
|
||||
if (!sender.hasPermission(def.getPermission())) {
|
||||
for (ExecutableDefinition d : defs) {
|
||||
if (d.isHelp() && args[0].equals("help")) {
|
||||
try {
|
||||
int page = Integer.parseInt(args[1]);
|
||||
printPage(sender, page);
|
||||
} catch (Exception e) {
|
||||
printPage(sender, 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (def.getPermission() != null && !sender.hasPermission(def.getPermission())) {
|
||||
sender.sendMessage(CommandManager.noPermissionFormatting + "You do not have permission to execute this command.");
|
||||
return true;
|
||||
}
|
||||
if (def.getExecType() == Type.PLAYER) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(CommandManager.notAllowedFormatting + "Only players are allowed to run this command.");
|
||||
return true;
|
||||
}
|
||||
}else if (def.getExecType() == Type.CONSOLE) {
|
||||
if (sender instanceof Player) {
|
||||
sender.sendMessage(CommandManager.notAllowedFormatting + "Only console is allowed to run this command.");
|
||||
return true;
|
||||
}
|
||||
}else if (def.getExecType() == Type.NOBODY) {
|
||||
sender.sendMessage(CommandManager.notAllowedFormatting + "Nobody can run this command.");
|
||||
return true;
|
||||
}
|
||||
if (def.getLength() != args.length) {
|
||||
printPage(sender, 1);
|
||||
return true;
|
||||
@@ -232,14 +277,7 @@ public class Executable implements CommandExecutor {
|
||||
arguments.add(def.get(i, args[i]));
|
||||
}
|
||||
}
|
||||
if (def.isHelp() || args[0].equals("help")) {
|
||||
try {
|
||||
int page = Integer.parseInt(args[1]);
|
||||
printPage(sender, page);
|
||||
} catch (Exception e) {
|
||||
printPage(sender, 1);
|
||||
}
|
||||
}else if (!def.invoke(arguments, sender, plugin)) {
|
||||
if (!def.invoke(arguments, sender, plugin)) {
|
||||
printPage(sender, 1);
|
||||
}
|
||||
}
|
||||
@@ -254,7 +292,7 @@ public class Executable implements CommandExecutor {
|
||||
HelpPageCommand[] pageData = help.get(page);
|
||||
sender.sendMessage(CommandManager.helpPageHeaderFormatting + "### Help Page " + (page + 1) + "/" + (help.size()) + " ###");
|
||||
for (HelpPageCommand c : pageData) {
|
||||
if (c != null) {
|
||||
if (c != null && (c.permission == null || sender.hasPermission(c.permission))) {
|
||||
sender.sendMessage(CommandManager.helpUsageFormatting + c.usage);
|
||||
sender.sendMessage(CommandManager.helpDescriptionFormatting + c.description);
|
||||
}
|
||||
|
||||
@@ -16,12 +16,14 @@ public class ExecutableDefinition {
|
||||
private String permission;
|
||||
private Method target;
|
||||
private Object methodContainer;
|
||||
private Type type;
|
||||
|
||||
public ExecutableDefinition(ArrayList<ICommandComponent> cmd, String perm, Method method, Object methodContainer) {
|
||||
public ExecutableDefinition(ArrayList<ICommandComponent> cmd, String perm, Method method, Object methodContainer, Type type) {
|
||||
this.components = cmd;
|
||||
this.permission = perm;
|
||||
this.target = method;
|
||||
this.methodContainer = methodContainer;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public boolean valid(int index, String arg) {
|
||||
@@ -53,6 +55,10 @@ public class ExecutableDefinition {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public Type getExecType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return components.size();
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ public class HelpPageCommand {
|
||||
public String usage;
|
||||
public String description;
|
||||
public String method;
|
||||
public Type type;
|
||||
|
||||
public HelpPageCommand(String perm, String usage, String description, String method) {
|
||||
public HelpPageCommand(String perm, String usage, String description, String method, Type type) {
|
||||
this.permission = perm;
|
||||
this.usage = usage;
|
||||
this.description = description;
|
||||
this.method = method;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ package com.nemez.cmdmgr.util;
|
||||
|
||||
public enum Property {
|
||||
|
||||
NONE, PERMISSION, HELP, EXECUTE;
|
||||
NONE, PERMISSION, HELP, EXECUTE, TYPE;
|
||||
|
||||
}
|
||||
|
||||
37
com/nemez/cmdmgr/util/Type.java
Normal file
37
com/nemez/cmdmgr/util/Type.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.nemez.cmdmgr.util;
|
||||
|
||||
public enum Type {
|
||||
|
||||
BOTH, PLAYER, CONSOLE, NOBODY;
|
||||
|
||||
public static Type parse(String string) {
|
||||
if (string.equals("both")) {
|
||||
return BOTH;
|
||||
}else if (string.equals("player")) {
|
||||
return PLAYER;
|
||||
}else if (string.equals("console")) {
|
||||
return CONSOLE;
|
||||
}else if (string.equals("nobody")) {
|
||||
return NOBODY;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String get(Type t) {
|
||||
if (t == null) {
|
||||
return "null";
|
||||
}
|
||||
switch (t) {
|
||||
case BOTH:
|
||||
return "both";
|
||||
case PLAYER:
|
||||
return "player";
|
||||
case CONSOLE:
|
||||
return "console";
|
||||
case NOBODY:
|
||||
return "nobody";
|
||||
}
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,4 @@
|
||||
name: CommandManagerTest
|
||||
main: CmdMgrTest
|
||||
version: 13.37
|
||||
author: Nemes
|
||||
commands:
|
||||
home:
|
||||
description: I dont get displayed, but the one below me is important
|
||||
usage: /home
|
||||
permission: home.blabla.idk.might.be.important
|
||||
permission-message: yeah if this ever got displayed xD
|
||||
|
||||
author: Nemes
|
||||
Reference in New Issue
Block a user