0

Reformatted code.

This commit is contained in:
Logan Fick
2019-02-08 16:53:55 -05:00
parent 50f53a63d1
commit 619c603a4f
24 changed files with 554 additions and 581 deletions

View File

@@ -5,16 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Command
{
enum AsyncType
{
NEVER, ALWAYS;
}
@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
public @interface Command {
String hook();
AsyncType async() default AsyncType.NEVER;
enum AsyncType {
NEVER, ALWAYS;
}
}

View File

@@ -1,11 +1,13 @@
//@noformat
package com.nemez.cmdmgr;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.nemez.cmdmgr.component.*;
import com.nemez.cmdmgr.util.*;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -15,33 +17,9 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.component.ArgumentComponent;
import com.nemez.cmdmgr.component.BooleanComponent;
import com.nemez.cmdmgr.component.ByteComponent;
import com.nemez.cmdmgr.component.ChainComponent;
import com.nemez.cmdmgr.component.ConstantComponent;
import com.nemez.cmdmgr.component.DoubleComponent;
import com.nemez.cmdmgr.component.EmptyComponent;
import com.nemez.cmdmgr.component.FloatComponent;
import com.nemez.cmdmgr.component.ICommandComponent;
import com.nemez.cmdmgr.component.IntegerComponent;
import com.nemez.cmdmgr.component.LongComponent;
import com.nemez.cmdmgr.component.OptionalComponent;
import com.nemez.cmdmgr.component.ShortComponent;
import com.nemez.cmdmgr.component.StringComponent;
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
*
* <p>
* command home:
* set [string:name]:
* run home_set name
@@ -59,35 +37,28 @@ import com.nemez.cmdmgr.util.Type;
* run home_tp name
* help Teleport to specified home
* perm home.tp
*
* <p>
* Generated in-game command structure:
* (will only show commands the user has permission to execute)
*
* <p>
* /home set <name>
* /home del <name>
* /home list
* /home <name>
* /home help
*
* <p>
* Java code:
*
* @Command(hook="home_set")
* public void executeHomeSet(String name) {
* @Command(hook="home_set") public void executeHomeSet(String name) {
* ...
* }
*
* @Command(hook="home_del")
* public void executeHomeDelete(String name) {
* @Command(hook="home_del") public void executeHomeDelete(String name) {
* ...
* }
*
* @Command(hook="home_list")
* public void executeHomeList() {
* @Command(hook="home_list") public void executeHomeList() {
* ...
* }
*
* @Command(hook="home_tp")
* public void executeHomeTeleport(String name) {
* @Command(hook="home_tp") public void executeHomeTeleport(String name) {
* ...
* }
*/
@@ -118,6 +89,7 @@ public class CommandManager {
* @param cmdSourceCode source code
* @param commandHandler instance of a class where your java functions are located
* @param plugin your plugin class
*
* @return success - if command was processed and registered successfully
*/
public static boolean registerCommand(String cmdSourceCode, Object commandHandler, JavaPlugin plugin) {
@@ -143,6 +115,7 @@ public class CommandManager {
* @param sourceFile file containing source code
* @param commandHandler instance of a class where your java functions are located
* @param plugin your plugin class
*
* @return success - if command was processed and registered successfully
*/
public static boolean registerCommand(File sourceFile, Object commandHandler, JavaPlugin plugin) {
@@ -170,6 +143,7 @@ public class CommandManager {
* @param sourceStream input stream containing source code
* @param commandHandler instance of a class where your java functions are located
* @param plugin your plugin class
*
* @return success - if command was processed and registered successfully
*/
public static boolean registerCommand(InputStream sourceStream, Object commandHandler, JavaPlugin plugin) {
@@ -213,29 +187,6 @@ public class CommandManager {
unregisterCommands(src.toString());
}
/**
* Unregisters all commands from an InputStream
*
* @param sourceStream input stream containing source code
* @param plugin your plugin class
*/
public static void unregisterCommands(InputStream sourceStream, JavaPlugin plugin) {
StringBuilder src = new StringBuilder();
String buf = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(sourceStream));
while ((buf = reader.readLine()) != null) {
src.append(buf + '\n');
}
reader.close();
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Error while loading command file. (" + sourceStream.toString() + ")");
plugin.getLogger().log(Level.WARNING, e.getCause().toString());
return;
}
unregisterCommands(src.toString());
}
/**
* Unregisters all commands from a String of source code
*
@@ -264,7 +215,7 @@ public class CommandManager {
}
}
buffer = new StringBuilder();
}else if (c == '{') {
} else if (c == '{') {
if (nesting == 0) {
String[] command = buffer.toString().trim().split("\\ ");
if (command.length == 2 && command[0].equals("command")) {
@@ -275,10 +226,10 @@ public class CommandManager {
nesting++;
}
buffer = new StringBuilder();
}else if (c == '}') {
} else if (c == '}') {
nesting--;
buffer = new StringBuilder();
}else {
} else {
buffer.append(c);
}
}
@@ -297,7 +248,7 @@ public class CommandManager {
CommandMap map = (CommandMap) mapField.get(Bukkit.getServer());
final Field knownCommandsField = mapField.getClass().getSuperclass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
@SuppressWarnings("unchecked")
@SuppressWarnings ("unchecked")
Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(map);
knownCommands.remove(name);
map.register(name, empty);
@@ -306,10 +257,31 @@ public class CommandManager {
}
}
public static void unregisterAll(String[] commands)
{
for (String name : commands)
{
/**
* Unregisters all commands from an InputStream
*
* @param sourceStream input stream containing source code
* @param plugin your plugin class
*/
public static void unregisterCommands(InputStream sourceStream, JavaPlugin plugin) {
StringBuilder src = new StringBuilder();
String buf = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(sourceStream));
while ((buf = reader.readLine()) != null) {
src.append(buf + '\n');
}
reader.close();
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Error while loading command file. (" + sourceStream.toString() + ")");
plugin.getLogger().log(Level.WARNING, e.getCause().toString());
return;
}
unregisterCommands(src.toString());
}
public static void unregisterAll(String[] commands) {
for (String name : commands) {
EmptyCommand emptyCommand = new EmptyCommand(name);
try {
final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
@@ -317,7 +289,7 @@ public class CommandManager {
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
final Field knownCommandsField = map.getClass().getSuperclass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
@SuppressWarnings("unchecked")
@SuppressWarnings ("unchecked")
Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(map);
knownCommands.remove(name);
map.register(name, emptyCommand);
@@ -334,16 +306,16 @@ public class CommandManager {
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
final Field knownCommandsField = map.getClass().getSuperclass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
@SuppressWarnings("unchecked")
@SuppressWarnings ("unchecked")
Map<String, org.bukkit.command.Command> knownCommands = (Map<String, org.bukkit.command.Command>) knownCommandsField.get(map);
fallback = fallback.toLowerCase();
List<String> toRemove = new ArrayList<>();
for (String key: knownCommands.keySet()) {
for (String key : knownCommands.keySet()) {
org.bukkit.command.Command value = knownCommands.get(key);
if ((value instanceof Executable) && ((Executable)value).getMethodContainerName().equals(fallback)) {
if ((value instanceof Executable) && ((Executable) value).getMethodContainerName().equals(fallback)) {
toRemove.add(key);
value.unregister(map);
}
@@ -367,6 +339,7 @@ public class CommandManager {
* @param methods ArrayList of methods gathered from the plugin's handler class
* @param plugin plugin to register commands as
* @param methodContainer class containing method handles
*
* @return success - if command parsing and registration was successful
*/
private static boolean parse(String source, ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer) {
@@ -421,7 +394,7 @@ public class CommandManager {
plugin.getLogger().log(Level.WARNING, "Syntax error at line " + line + ": Already defining a type.");
errors = true;
return false;
}else{
} else {
/* okay, resolve what type this is */
currentArgComp = resolveComponentType(buffer.toString());
buffer = new StringBuilder();
@@ -432,18 +405,18 @@ public class CommandManager {
return false;
}
}
}else{
} else {
/* not inside a type, probably just a string in the help property */
buffer.append(':');
}
/* current is a semicolon, we just finished a property line */
/* help this is an example; */
/* ^ */
}else if (current == ';') {
} else if (current == ';') {
/* semicolon is backslash escaped, treat it as a normal character */
if (previous == '\\') {
buffer.append(';');
}else{
} else {
/* there is nothing on the stack, we are defining properties of 'nothing', throw an error */
if (stack.get() == null) {
plugin.getLogger().log(Level.WARNING, "Syntax error at line " + line + ": Not in code section.");
@@ -454,25 +427,27 @@ public class CommandManager {
if (gettingAlias) {
stack.get().aliases.add(buffer.toString());
gettingAlias = false;
}else if (currentProp == Property.HELP) {
} else if (currentProp == Property.HELP) {
stack.get().help = buffer.toString();
/* same as above, except its the function to run */
}else if (currentProp == Property.EXECUTE) {
} else if (currentProp == Property.EXECUTE) {
stack.get().execute = buffer.toString();
/* same again, but with the permission, and as that should not contain spaces, trim it */
}else if (currentProp == Property.PERMISSION) {
} else if (currentProp == Property.PERMISSION) {
stack.get().permission = buffer.toString().trim();
/* execution type, check if its a valid one and set it */
}else if (currentProp == Property.TYPE) {
} else if (currentProp == Property.TYPE) {
stack.get().type = resolveExecutionType(buffer.toString().trim());
/* not a valid type, throw an error */
if (stack.get().type == null) {
plugin.getLogger().log(Level.WARNING, "Attribute error at line " + line + ": Invalid attribute value. (" + buffer.toString().trim() + ").");
plugin
.getLogger()
.log(Level.WARNING, "Attribute error at line " + line + ": Invalid attribute value. (" + buffer.toString().trim() + ").");
errors = true;
return false;
}
/* currently not defining anything, throw an error */
}else{
} else {
plugin.getLogger().log(Level.WARNING, "Attribute error at line " + line + ": Invalid attribute type.");
errors = true;
return false;
@@ -482,7 +457,7 @@ public class CommandManager {
buffer = new StringBuilder();
}
/* current is an opening curly bracket, we just entered a sub-command property definition */
}else if (current == '{') {
} else if (current == '{') {
/* increment bracket counter */
bracketCounter++;
/* are we getting the name of the command? */
@@ -490,14 +465,14 @@ public class CommandManager {
/* set the command name to what we just gathered (trimmed) */
cmdName = buffer.toString().trim();
gettingName = false;
}else{
} else {
/* are we currently in an argument? */
if (currentArgComp == null) {
/* no, but if there is something that looks like text, put it into the current subcommand as a constant */
if (buffer.toString().trim().length() > 0) {
currentChain.append(new ConstantComponent(buffer.toString().trim()));
}
}else{
} else {
/* yes, put it into the current subcommand */
/* could happen when there are no 'spaces' */
/* [str:example]{ */
@@ -519,7 +494,7 @@ public class CommandManager {
/* reset our current sub-command */
currentChain = new ChainComponent();
/* current is a closing curly bracket, we just finished a property section */
}else if (current == '}') {
} else if (current == '}') {
/* decrement the bracket counter */
bracketCounter--;
/* pop whatever was on the stack */
@@ -559,41 +534,41 @@ public class CommandManager {
}
currentChain = new ChainComponent();
/* current is a space, we just finished defining which property we are about to set */
}else if (current == ' ') {
} else if (current == ' ') {
/* we are already defining a property, append it as text */
if (currentProp != Property.NONE) {
buffer.append(' ');
}else{
} else {
/* we got the 'command' definition, the name of the command will follow */
if (buffer.toString().equals("command") && !gettingName && !gettingAlias && cmdName == null) {
gettingName = true;
/* we got other properties, their values will follow */
}else if (buffer.toString().equals("alias") && !gettingAlias && !gettingName) {
} else if (buffer.toString().equals("alias") && !gettingAlias && !gettingName) {
gettingAlias = true;
}else if (buffer.toString().equals("help")) {
} else if (buffer.toString().equals("help")) {
currentProp = Property.HELP;
}else if (buffer.toString().equals("run")) {
} else if (buffer.toString().equals("run")) {
currentProp = Property.EXECUTE;
}else if (buffer.toString().equals("perm")) {
} else if (buffer.toString().equals("perm")) {
currentProp = Property.PERMISSION;
}else if (buffer.toString().equals("type")) {
} else if (buffer.toString().equals("type")) {
currentProp = Property.TYPE;
/* we didn't get any of those, we are probably in the middle of a sub-command definition */
/* example [int:value] { */
/* ^ ^ */
}else{
} else {
/* we are getting the name and we didn't set it yet, set it */
if (gettingName && cmdName == null) {
cmdName = buffer.toString().trim();
gettingName = false;
}else{
} else {
/* we aren't defining a type, put the current text into the sub-command as a constant */
if (currentArgComp == null) {
if (buffer.toString().trim().length() > 0) {
currentChain.append(new ConstantComponent(buffer.toString().trim()));
}
/* we are defining a command, put it into the sub-command */
}else{
} else {
currentChain.append(currentArgComp);
currentArgComp = null;
}
@@ -603,26 +578,26 @@ public class CommandManager {
buffer = new StringBuilder();
}
/* current is an opening square bracket, we just started a type definition */
}else if (current == '[') {
} else if (current == '[') {
/* we are defining a property, treat it as text */
if (currentProp != Property.NONE) {
buffer.append('[');
/* we are already inside of a type definition, throw an error */
}else if (insideType) {
} else if (insideType) {
plugin.getLogger().log(Level.WARNING, "Syntax error at line " + line + ": Invalid type declaration.");
errors = true;
return false;
}else{
} else {
/* we just entered a type definition */
insideType = true;
}
/* current is a closing square bracket, we just finished a type definition */
}else if (current == ']') {
} else if (current == ']') {
/* we are defining a property, treat it as text */
if (currentProp != Property.NONE) {
buffer.append(']');
/* we are inside of a type */
}else if (insideType) {
} else if (insideType) {
insideType = false;
/* current argument type is null, throw an error */
if (currentArgComp == null) {
@@ -630,13 +605,13 @@ public class CommandManager {
buffer = new StringBuilder();
if (currentArgComp instanceof EmptyComponent) {
}else{
} else {
/* should never happen */
plugin.getLogger().log(Level.WARNING, "Type error at line " + line + ": Type has no type?");
errors = true;
return false;
}
}else{
} else {
/* set the value of the current type and reset the buffer */
currentArgComp.argName = buffer.toString();
buffer = new StringBuilder();
@@ -646,39 +621,39 @@ public class CommandManager {
}
sideBuffer = new StringBuilder();
}
}else{
} else {
/* we are not defining a type, throw an error */
plugin.getLogger().log(Level.WARNING, "Syntax error at line " + line + ": Not in type declaration.");
errors = true;
return false;
}
/* typical escape sequences and such */
}else if (current == 'n' && currentProp == Property.HELP) {
} else if (current == 'n' && currentProp == Property.HELP) {
if (previous == '\\') {
buffer.append('\n');
}else{
} else {
buffer.append('n');
}
}else if (current == 't' && currentProp == Property.HELP) {
} else if (current == 't' && currentProp == Property.HELP) {
if (previous == '\\') {
buffer.append('\t');
}else{
} else {
buffer.append('t');
}
}else if (current == '\\' && currentProp == Property.HELP) {
} else if (current == '\\' && currentProp == Property.HELP) {
if (previous == '\\') {
buffer.append('\\');
}
}else if (current != '\r' && current != '\n' && current != '\t') {
} else if (current != '\r' && current != '\n' && current != '\t') {
if (currentArgComp != null && current == '.') {
if (currentArgComp instanceof StringComponent) {
sideBuffer.append(current);
}else{
} else {
plugin.getLogger().log(Level.WARNING, "Syntax error at line " + line + ": '...' is invalid for non-string types.");
errors = true;
return false;
}
}else{
} else {
buffer.append(current);
}
}
@@ -692,6 +667,7 @@ public class CommandManager {
* Resolves the string into a type, or null if invalid
*
* @param type string you want to evaluate
*
* @return the type class or null if invalid
*/
private static ArgumentComponent resolveComponentType(String type) {
@@ -729,6 +705,7 @@ public class CommandManager {
* Resolves the string into a property, or null if invalid
*
* @param type string you want to evaluate
*
* @return the property enum or null if invalid
*/
private static Type resolveExecutionType(String type) {
@@ -804,15 +781,17 @@ public class CommandManager {
if (c instanceof ChainComponent) {
temp = temp.replaceAll("\r", "\r" + chain);
leaves.add(temp);
}else{
} else {
chain += temp;
}
}
data += chain + "\0" + ((comp.permission == null || comp.permission.equals("-none-")) ? null : comp.permission) + "\0" + comp.execute + "\0" + comp.help + "\0" + Type.get(comp.type) + "\0";
data += chain + "\0" + ((comp.permission == null || comp.permission.equals("-none-"))
? null
: comp.permission) + "\0" + comp.execute + "\0" + comp.help + "\0" + Type.get(comp.type) + "\0";
for (String s : leaves) {
data += s;
}
}else{
} else {
data += component.getComponentInfo() + " ";
}

View File

@@ -1,19 +1,15 @@
package com.nemez.cmdmgr;
import com.nemez.cmdmgr.util.Executable;
import org.bukkit.command.CommandSender;
import com.nemez.cmdmgr.util.Executable;
public class EmptyCommand extends Executable
{
public EmptyCommand(String name)
{
public class EmptyCommand extends Executable {
public EmptyCommand(String name) {
super(name, null, "");
}
@Override
public boolean execute(CommandSender sender, String name, String[] args_)
{
public boolean execute(CommandSender sender, String name, String[] args_) {
sender.sendMessage("§cCommand no longer Exists. Use §e/help§c, §e/plugins§c or ask a mod.");
return true;
}

View File

@@ -12,7 +12,9 @@ public class BooleanComponent extends ArgumentComponent {
@Override
public boolean valid(String input) {
if (input.toLowerCase().equals("true") || input.toLowerCase().equals("false") || input.toLowerCase().equals("yes") || input.toLowerCase().equals("no")) {
if (input.toLowerCase().equals("true") || input.toLowerCase().equals("false") || input.toLowerCase().equals("yes") || input
.toLowerCase()
.equals("no")) {
return true;
}
return false;

View File

@@ -1,17 +1,17 @@
package com.nemez.cmdmgr.component;
import java.util.ArrayList;
import com.nemez.cmdmgr.util.Type;
import java.util.ArrayList;
public class ChainComponent implements ICommandComponent {
private ArrayList<ICommandComponent> components;
public String permission;
public String help;
public String execute;
public Type type;
public ArrayList<String> aliases = new ArrayList<String>();
private ArrayList<ICommandComponent> components;
public ChainComponent() {
components = new ArrayList<ICommandComponent>();

View File

@@ -3,7 +3,10 @@ package com.nemez.cmdmgr.component;
public interface ICommandComponent {
public Object get(String input);
public boolean valid(String input);
public String argName();
public String getComponentInfo();
}

View File

@@ -1,29 +1,23 @@
package com.nemez.cmdmgr.util;
import com.nemez.cmdmgr.component.ICommandComponent;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.component.ICommandComponent;
public class AsyncExecutableDefinition extends ExecutableDefinition
{
public class AsyncExecutableDefinition extends ExecutableDefinition {
public AsyncExecutableDefinition(ArrayList<ICommandComponent> cmd, ArrayList<Integer> paramLinks, String perm,
Method method, Object methodContainer, Type type)
{
Method method, Object methodContainer, Type type) {
super(cmd, paramLinks, perm, method, methodContainer, type);
}
@Override
public boolean invoke(Object[] args, CommandSender sender, JavaPlugin plugin)
{
Thread t = new Thread(new Runnable()
{
public boolean invoke(Object[] args, CommandSender sender, JavaPlugin plugin) {
Thread t = new Thread(new Runnable() {
@Override
public void run()
{
public void run() {
invokeFromThread(args, sender, plugin);
}
});
@@ -32,8 +26,7 @@ public class AsyncExecutableDefinition extends ExecutableDefinition
return true;
}
private final void invokeFromThread(Object[] args, CommandSender sender, JavaPlugin plugin)
{
private final void invokeFromThread(Object[] args, CommandSender sender, JavaPlugin plugin) {
super.invoke(args, sender, plugin);
}
}

View File

@@ -1,9 +1,9 @@
package com.nemez.cmdmgr.util;
import java.util.ArrayList;
import com.nemez.cmdmgr.component.ChainComponent;
import java.util.ArrayList;
public class BranchStack {
private ArrayList<ChainComponent> components;

View File

@@ -1,13 +1,10 @@
//@noformat
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.Map;
import java.util.logging.Level;
import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager;
import com.nemez.cmdmgr.component.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandMap;
@@ -15,20 +12,12 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager;
import com.nemez.cmdmgr.component.BooleanComponent;
import com.nemez.cmdmgr.component.ByteComponent;
import com.nemez.cmdmgr.component.ConstantComponent;
import com.nemez.cmdmgr.component.DoubleComponent;
import com.nemez.cmdmgr.component.FloatComponent;
import com.nemez.cmdmgr.component.ICommandComponent;
import com.nemez.cmdmgr.component.IntegerComponent;
import com.nemez.cmdmgr.component.LongComponent;
import com.nemez.cmdmgr.component.OptionalComponent;
import com.nemez.cmdmgr.component.ShortComponent;
import com.nemez.cmdmgr.component.StringComponent;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
public class Executable extends org.bukkit.command.Command {
@@ -64,24 +53,24 @@ public class Executable extends org.bukkit.command.Command {
for (char c : cmd.usage.toCharArray()) {
if (c == '<') {
toBuffer = true;
}else if (c == ':') {
} else if (c == ':') {
toBuffer = false;
ignore = true;
}else if (c == '>') {
} else if (c == '>') {
ignore = false;
if (typeBuffer.equals("flag")) {
newUsage += '[' + buffer + (CommandManager.debugHelpMenu ? ':' + typeBuffer : "") + ']';
}else{
} else {
newUsage += '<' + buffer + (CommandManager.debugHelpMenu ? ':' + typeBuffer : "") + '>';
}
buffer = "";
typeBuffer = "";
}else{
} else {
if (toBuffer) {
buffer += c;
}else if (ignore) {
} else if (ignore) {
typeBuffer += c;
}else{
} else {
newUsage += c;
}
}
@@ -98,7 +87,7 @@ public class Executable extends org.bukkit.command.Command {
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
final Field knownCommandsField = map.getClass().getSuperclass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
@SuppressWarnings("unchecked")
@SuppressWarnings ("unchecked")
Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(map);
knownCommands.remove(name);
map.register(methodContainerName, this);
@@ -120,7 +109,8 @@ public class Executable extends org.bukkit.command.Command {
}
}
private void processLine(String[] line, String permission, String method, ArrayList<Method> methods, Object methodContainer, JavaPlugin plugin, Type etype) {
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"));
@@ -214,10 +204,10 @@ public class Executable extends org.bukkit.command.Command {
index++;
}
}
}else{
} else {
if (s.equals("<empty>")) {
}else{
} else {
command.add(new ConstantComponent(s));
}
}
@@ -229,14 +219,14 @@ public class Executable extends org.bukkit.command.Command {
plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + ")");
CommandManager.errors = true;
return;
}else{
} else {
if (annotations[0].hook().equals(methodArray[0])) {
Class<?>[] params = m.getParameterTypes();
if (params.length -1 != methodParams.size()) {
if (params.length - 1 != methodParams.size()) {
plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): Arguments don't match");
CommandManager.errors = true;
return;
}else{
} else {
for (int i = 0; i < params.length; i++) {
if (i == 0) {
if (params[0] != CommandSender.class) {
@@ -244,27 +234,27 @@ public class Executable extends org.bukkit.command.Command {
CommandManager.errors = true;
return;
}
}else{
} else {
ICommandComponent comp = methodParams.get(i - 1);
if (comp instanceof ByteComponent && params[i] == byte.class) {
}else if (comp instanceof ShortComponent && params[i] == short.class) {
} else if (comp instanceof ShortComponent && params[i] == short.class) {
}else if (comp instanceof IntegerComponent && params[i] == int.class) {
} else if (comp instanceof IntegerComponent && params[i] == int.class) {
}else if (comp instanceof LongComponent && params[i] == long.class) {
} else if (comp instanceof LongComponent && params[i] == long.class) {
}else if (comp instanceof FloatComponent && params[i] == float.class) {
} else if (comp instanceof FloatComponent && params[i] == float.class) {
}else if (comp instanceof DoubleComponent && params[i] == double.class) {
} else if (comp instanceof DoubleComponent && params[i] == double.class) {
}else if (comp instanceof StringComponent && params[i] == String.class) {
} else if (comp instanceof StringComponent && params[i] == String.class) {
}else if (comp instanceof BooleanComponent && params[i] == boolean.class) {
} else if (comp instanceof BooleanComponent && params[i] == boolean.class) {
}else if (comp instanceof OptionalComponent && params[i] == boolean.class) {
} else if (comp instanceof OptionalComponent && params[i] == boolean.class) {
}else{
} else {
plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): Invalid method arguments");
CommandManager.errors = true;
return;
@@ -301,7 +291,7 @@ public class Executable extends org.bukkit.command.Command {
String[] args;
if (args_.length == 0) {
args = new String[0];
}else{
} else {
ArrayList<String> tempArgs = new ArrayList<String>();
String temp = "";
int counter = 0;
@@ -311,11 +301,11 @@ public class Executable extends org.bukkit.command.Command {
// escaped \
tempArgs.add(temp + s.replace("\\\\", "\\"));
temp = "";
}else{
} else {
// unescaped \
temp += s.substring(0, s.length() - 1).replace("\\\\", "\\") + " ";
}
}else{
} else {
tempArgs.add(temp + s);
temp = "";
}
@@ -335,9 +325,10 @@ public class Executable extends org.bukkit.command.Command {
break;
}
}
}else{
} else {
defs.addAll(commands);
defLoop: for (int j = 0; j < defs.size(); j++) {
defLoop:
for (int j = 0; j < defs.size(); j++) {
if (defs.get(j).getLength(args.length) == 0) {
defs.remove(j);
j--;
@@ -350,7 +341,7 @@ public class Executable extends org.bukkit.command.Command {
defs.remove(j);
j--;
continue defLoop;
}else{
} else {
i--;
continue;
}
@@ -364,7 +355,7 @@ public class Executable extends org.bukkit.command.Command {
}
if (defs.size() == 0) {
printPage(sender, 1);
}else{
} else {
ExecutableDefinition def = defs.get(0);
for (ExecutableDefinition d : defs) {
if (d.isHelp() && args[0].equals("help")) {
@@ -378,20 +369,24 @@ public class Executable extends org.bukkit.command.Command {
}
}
if (def.getPermission() != null && !def.getPermission().equals("null") && !sender.hasPermission(def.getPermission())) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.noPermissionFormatting + "You do not have permission to execute this command."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
CommandManager.noPermissionFormatting + "You do not have permission to execute this command."
));
return true;
}
if (def.getExecType() == Type.PLAYER) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.notAllowedFormatting + "Only players are allowed to run this command."));
sender.sendMessage(
ChatColor.translateAlternateColorCodes('&', CommandManager.notAllowedFormatting + "Only players are allowed to run this command."));
return true;
}
}else if (def.getExecType() == Type.CONSOLE) {
} else if (def.getExecType() == Type.CONSOLE) {
if (sender instanceof Player) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.notAllowedFormatting + "Only console is allowed to run this command."));
sender.sendMessage(
ChatColor.translateAlternateColorCodes('&', CommandManager.notAllowedFormatting + "Only console is allowed to run this command."));
return true;
}
}else if (def.getExecType() == Type.NOBODY) {
} else if (def.getExecType() == Type.NOBODY) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.notAllowedFormatting + "Nobody can run this command."));
return true;
}
@@ -400,7 +395,7 @@ public class Executable extends org.bukkit.command.Command {
if (def.isArgument(j)) {
if (def.valid(j, args[i])) {
arguments.add(def.get(j, args[i]));
}else if (def.isOptional(j)) {
} else if (def.isOptional(j)) {
arguments.add(false);
i--;
}
@@ -411,7 +406,7 @@ public class Executable extends org.bukkit.command.Command {
int link = def.getLink(i) + 1;
if (linkedArgs[link] != null) {
linkedArgs[link] = linkedArgs[link].toString() + " " + arguments.get(i).toString();
}else{
} else {
linkedArgs[link] = arguments.get(i);
}
}
@@ -425,10 +420,15 @@ public class Executable extends org.bukkit.command.Command {
private void printPage(CommandSender sender, int page) {
page--;
if (page < 0 || page >= help.size()) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.helpInvalidPageFormatting + "Non-existant page (" + (page + 1) + ").\nThere are " + help.size() + " pages."));
}else{
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
CommandManager.helpInvalidPageFormatting + "Non-existant page (" + (page + 1) + ").\nThere are " + help
.size() + " pages."
));
} else {
HelpPageCommand[] pageData = help.get(page);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.helpPageHeaderFormatting + "### Help Page " + (page + 1) + "/" + (help.size()) + " ###"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
CommandManager.helpPageHeaderFormatting + "### Help Page " + (page + 1) + "/" + (help.size()) + " ###"
));
for (HelpPageCommand c : pageData) {
if (c != null) {
if (c.type == null || c.type == Type.BOTH || (c.type == Type.CONSOLE && !(sender instanceof Player)) || (c.type == Type.PLAYER && sender instanceof Player)) {

View File

@@ -1,18 +1,17 @@
package com.nemez.cmdmgr.util;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import com.nemez.cmdmgr.CommandManager;
import com.nemez.cmdmgr.component.ArgumentComponent;
import com.nemez.cmdmgr.component.ICommandComponent;
import com.nemez.cmdmgr.component.OptionalComponent;
import com.nemez.cmdmgr.component.StringComponent;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.logging.Level;
public class ExecutableDefinition {
@@ -23,7 +22,8 @@ public class ExecutableDefinition {
private Type type;
private ArrayList<Integer> paramLinks;
public ExecutableDefinition(ArrayList<ICommandComponent> cmd, ArrayList<Integer> paramLinks, String perm, Method method, Object methodContainer, Type type) {
public ExecutableDefinition(ArrayList<ICommandComponent> cmd, ArrayList<Integer> paramLinks, String perm, Method method, Object methodContainer,
Type type) {
this.components = cmd;
this.permission = perm;
this.target = method;
@@ -41,10 +41,10 @@ public class ExecutableDefinition {
StringComponent strComp = (StringComponent) components.get(components.size() - 1);
if (strComp.infinite) {
return strComp.valid(arg);
}else{
} else {
return false;
}
}else{
} else {
return false;
}
}
@@ -60,10 +60,10 @@ public class ExecutableDefinition {
StringComponent strComp = (StringComponent) components.get(components.size() - 1);
if (strComp.infinite) {
return strComp.get(arg);
}else{
} else {
return null;
}
}else{
} else {
return null;
}
}
@@ -79,10 +79,10 @@ public class ExecutableDefinition {
StringComponent strComp = (StringComponent) components.get(components.size() - 1);
if (strComp.infinite) {
return true;
}else{
} else {
return false;
}
}else{
} else {
return false;
}
}
@@ -142,10 +142,10 @@ public class ExecutableDefinition {
StringComponent strComp = (StringComponent) components.get(components.size() - 1);
if (strComp.infinite) {
return paramLinks.get(paramLinks.size() - 1);
}else{
} else {
return i;
}
}else{
} else {
return i;
}
}
@@ -161,11 +161,13 @@ public class ExecutableDefinition {
if (target.getReturnType() == void.class) {
target.invoke(methodContainer, args);
return true;
}else if (target.getReturnType() == boolean.class) {
} else if (target.getReturnType() == boolean.class) {
return (boolean) target.invoke(methodContainer, args);
}
} catch (Exception e) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandManager.helpInvalidPageFormatting + "An internal error occured, please contact the server administrator and/or report a bug."));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
CommandManager.helpInvalidPageFormatting + "An internal error occured, please contact the server administrator and/or report a bug."
));
plugin.getLogger().log(Level.WARNING, "Runtime Error: invalid method");
e.printStackTrace();
return true;

View File

@@ -7,13 +7,13 @@ public enum Type {
public static Type parse(String string) {
if (string.equals("both")) {
return BOTH;
}else if (string.equals("player")) {
} else if (string.equals("player")) {
return PLAYER;
}else if (string.equals("console")) {
} else if (string.equals("console")) {
return CONSOLE;
}else if (string.equals("nobody")) {
} else if (string.equals("nobody")) {
return NOBODY;
}else{
} else {
return null;
}
}