added 'empty' commands (e.g /stop)
This commit is contained in:
@@ -17,6 +17,12 @@ public class CmdMgrTest extends JavaPlugin {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Command(hook="home_empty")
|
||||||
|
public boolean executeHomeNull(CommandSender sender) {
|
||||||
|
sender.sendMessage("You executed an empty /home");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Command(hook="home_set")
|
@Command(hook="home_set")
|
||||||
public boolean executeSetHome(CommandSender sender, String name) {
|
public boolean executeSetHome(CommandSender sender, String name) {
|
||||||
sender.sendMessage("You executed:");
|
sender.sendMessage("You executed:");
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import com.nemez.cmdmgr.component.ByteComponent;
|
|||||||
import com.nemez.cmdmgr.component.ChainComponent;
|
import com.nemez.cmdmgr.component.ChainComponent;
|
||||||
import com.nemez.cmdmgr.component.ConstantComponent;
|
import com.nemez.cmdmgr.component.ConstantComponent;
|
||||||
import com.nemez.cmdmgr.component.DoubleComponent;
|
import com.nemez.cmdmgr.component.DoubleComponent;
|
||||||
|
import com.nemez.cmdmgr.component.EmptyComponent;
|
||||||
import com.nemez.cmdmgr.component.FloatComponent;
|
import com.nemez.cmdmgr.component.FloatComponent;
|
||||||
import com.nemez.cmdmgr.component.ICommandComponent;
|
import com.nemez.cmdmgr.component.ICommandComponent;
|
||||||
import com.nemez.cmdmgr.component.IntegerComponent;
|
import com.nemez.cmdmgr.component.IntegerComponent;
|
||||||
@@ -433,10 +434,16 @@ public class CommandManager {
|
|||||||
insideType = false;
|
insideType = false;
|
||||||
/* current argument type is null, throw an error */
|
/* current argument type is null, throw an error */
|
||||||
if (currentArgComp == null) {
|
if (currentArgComp == null) {
|
||||||
|
currentArgComp = resolveComponentType(buffer.toString());
|
||||||
|
buffer = new StringBuilder();
|
||||||
|
if (currentArgComp instanceof EmptyComponent) {
|
||||||
|
|
||||||
|
}else{
|
||||||
/* should never happen */
|
/* should never happen */
|
||||||
plugin.getLogger().log(Level.WARNING, "Type error at line " + line + ": Type has no type?");
|
plugin.getLogger().log(Level.WARNING, "Type error at line " + line + ": Type has no type?");
|
||||||
errors = true;
|
errors = true;
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
/* set the value of the current type and reset the buffer */
|
/* set the value of the current type and reset the buffer */
|
||||||
currentArgComp.argName = buffer.toString();
|
currentArgComp.argName = buffer.toString();
|
||||||
@@ -519,6 +526,9 @@ public class CommandManager {
|
|||||||
case "opt":
|
case "opt":
|
||||||
case "flag":
|
case "flag":
|
||||||
return new OptionalComponent();
|
return new OptionalComponent();
|
||||||
|
case "empty":
|
||||||
|
case "null":
|
||||||
|
return new EmptyComponent();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
19
com/nemez/cmdmgr/component/EmptyComponent.java
Normal file
19
com/nemez/cmdmgr/component/EmptyComponent.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.nemez.cmdmgr.component;
|
||||||
|
|
||||||
|
public class EmptyComponent extends ArgumentComponent {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object get(String input) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean valid(String input) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getComponentInfo() {
|
||||||
|
return "<empty>";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -192,10 +192,14 @@ public class Executable extends org.bukkit.command.Command {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
if (s.equals("<empty>")) {
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
command.add(new ConstantComponent(s));
|
command.add(new ConstantComponent(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (Method m : methods) {
|
for (Method m : methods) {
|
||||||
Command[] annotations = m.getAnnotationsByType(Command.class);
|
Command[] annotations = m.getAnnotationsByType(Command.class);
|
||||||
@@ -265,6 +269,10 @@ public class Executable extends org.bukkit.command.Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(CommandSender sender, String name, String[] args_) {
|
public boolean execute(CommandSender sender, String name, String[] args_) {
|
||||||
|
String[] args;
|
||||||
|
if (args_.length == 0) {
|
||||||
|
args = new String[0];
|
||||||
|
}else{
|
||||||
char[] rawArgs = (String.join(" ", args_) + ' ').toCharArray();
|
char[] rawArgs = (String.join(" ", args_) + ' ').toCharArray();
|
||||||
int argSize = 0;
|
int argSize = 0;
|
||||||
char last = '\0';
|
char last = '\0';
|
||||||
@@ -281,7 +289,7 @@ public class Executable extends org.bukkit.command.Command {
|
|||||||
last = c;
|
last = c;
|
||||||
}
|
}
|
||||||
last = '\0';
|
last = '\0';
|
||||||
String[] args = new String[argSize];
|
args = new String[argSize];
|
||||||
String buffer = "";
|
String buffer = "";
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
@@ -301,10 +309,24 @@ public class Executable extends org.bukkit.command.Command {
|
|||||||
}
|
}
|
||||||
last = c;
|
last = c;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ArrayList<ExecutableDefinition> defs = new ArrayList<ExecutableDefinition>();
|
ArrayList<ExecutableDefinition> defs = new ArrayList<ExecutableDefinition>();
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
for (ExecutableDefinition d : commands) {
|
||||||
|
if (d.getLength(0) == 0) {
|
||||||
|
defs.add(d);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
defs.addAll(commands);
|
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--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
int i = 0, k = 0;
|
int i = 0, k = 0;
|
||||||
for (; i < args.length; i++, k++) {
|
for (; i < args.length; i++, k++) {
|
||||||
if (!defs.get(j).valid(k, args[i])) {
|
if (!defs.get(j).valid(k, args[i])) {
|
||||||
@@ -323,7 +345,8 @@ public class Executable extends org.bukkit.command.Command {
|
|||||||
j--;
|
j--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (args.length == 0 || defs.size() == 0) {
|
}
|
||||||
|
if (defs.size() == 0) {
|
||||||
printPage(sender, 1);
|
printPage(sender, 1);
|
||||||
}else{
|
}else{
|
||||||
ExecutableDefinition def = defs.get(0);
|
ExecutableDefinition def = defs.get(0);
|
||||||
|
|||||||
@@ -109,6 +109,9 @@ public class ExecutableDefinition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getLength(int argSize) {
|
public int getLength(int argSize) {
|
||||||
|
if (components.size() == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if (argSize >= components.size()) {
|
if (argSize >= components.size()) {
|
||||||
if (components.get(components.size() - 1) instanceof StringComponent) {
|
if (components.get(components.size() - 1) instanceof StringComponent) {
|
||||||
StringComponent strComp = (StringComponent) components.get(components.size() - 1);
|
StringComponent strComp = (StringComponent) components.get(components.size() - 1);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public class HelpPageCommand {
|
|||||||
|
|
||||||
public HelpPageCommand(String perm, String usage, String description, String method, Type type) {
|
public HelpPageCommand(String perm, String usage, String description, String method, Type type) {
|
||||||
this.permission = perm;
|
this.permission = perm;
|
||||||
this.usage = usage;
|
this.usage = usage.replaceAll("<empty>", "");
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
|||||||
Reference in New Issue
Block a user