0

added 'empty' commands (e.g /stop)

This commit is contained in:
NEMESIS13cz
2016-11-19 16:56:48 +01:00
parent 5358030f8d
commit cbe402fc73
6 changed files with 118 additions and 57 deletions

View File

@@ -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")
public boolean executeSetHome(CommandSender sender, String name) {
sender.sendMessage("You executed:");

View File

@@ -18,6 +18,7 @@ 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;
@@ -433,10 +434,16 @@ public class CommandManager {
insideType = false;
/* current argument type is null, throw an error */
if (currentArgComp == null) {
/* should never happen */
plugin.getLogger().log(Level.WARNING, "Type error at line " + line + ": Type has no type?");
errors = true;
return false;
currentArgComp = resolveComponentType(buffer.toString());
buffer = new StringBuilder();
if (currentArgComp instanceof EmptyComponent) {
}else{
/* should never happen */
plugin.getLogger().log(Level.WARNING, "Type error at line " + line + ": Type has no type?");
errors = true;
return false;
}
}else{
/* set the value of the current type and reset the buffer */
currentArgComp.argName = buffer.toString();
@@ -519,6 +526,9 @@ public class CommandManager {
case "opt":
case "flag":
return new OptionalComponent();
case "empty":
case "null":
return new EmptyComponent();
}
return null;
}

View 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>";
}
}

View File

@@ -193,7 +193,11 @@ public class Executable extends org.bukkit.command.Command {
}
}
}else{
command.add(new ConstantComponent(s));
if (s.equals("<empty>")) {
}else{
command.add(new ConstantComponent(s));
}
}
}
@@ -265,65 +269,84 @@ public class Executable extends org.bukkit.command.Command {
@Override
public boolean execute(CommandSender sender, String name, String[] args_) {
char[] rawArgs = (String.join(" ", args_) + ' ').toCharArray();
int argSize = 0;
char last = '\0';
boolean inString = false;
for (char c : rawArgs) {
if (c == '"') {
if (last != '\\') {
inString = !inString;
String[] args;
if (args_.length == 0) {
args = new String[0];
}else{
char[] rawArgs = (String.join(" ", args_) + ' ').toCharArray();
int argSize = 0;
char last = '\0';
boolean inString = false;
for (char c : rawArgs) {
if (c == '"') {
if (last != '\\') {
inString = !inString;
}
}else if (c == ' ' && !inString) {
argSize++;
}
}else if (c == ' ' && !inString) {
argSize++;
last = c;
}
last = c;
}
last = '\0';
String[] args = new String[argSize];
String buffer = "";
int index = 0;
last = '\0';
args = new String[argSize];
String buffer = "";
int index = 0;
for (char c : rawArgs) {
if (c == '"') {
if (last != '\\') {
inString = !inString;
}else{
buffer = buffer.substring(0, buffer.length() - 1) + '"';
}
}else if (c == ' ' && !inString) {
args[index] = buffer;
buffer = "";
index++;
}else{
buffer += c;
}
last = c;
}
ArrayList<ExecutableDefinition> defs = new ArrayList<ExecutableDefinition>();
defs.addAll(commands);
defLoop: for (int j = 0; j < defs.size(); j++) {
int i = 0, k = 0;
for (; i < args.length; i++, k++) {
if (!defs.get(j).valid(k, args[i])) {
if (!defs.get(j).isOptional(k)) {
defs.remove(j);
j--;
continue defLoop;
for (char c : rawArgs) {
if (c == '"') {
if (last != '\\') {
inString = !inString;
}else{
i--;
continue;
buffer = buffer.substring(0, buffer.length() - 1) + '"';
}
}else if (c == ' ' && !inString) {
args[index] = buffer;
buffer = "";
index++;
}else{
buffer += c;
}
last = c;
}
}
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);
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;
for (; i < args.length; i++, k++) {
if (!defs.get(j).valid(k, args[i])) {
if (!defs.get(j).isOptional(k)) {
defs.remove(j);
j--;
continue defLoop;
}else{
i--;
continue;
}
}
}
}
if (k != defs.get(j).getLength(k)) {
defs.remove(j);
j--;
if (k != defs.get(j).getLength(k)) {
defs.remove(j);
j--;
}
}
}
if (args.length == 0 || defs.size() == 0) {
if (defs.size() == 0) {
printPage(sender, 1);
}else{
ExecutableDefinition def = defs.get(0);

View File

@@ -109,6 +109,9 @@ public class ExecutableDefinition {
}
public int getLength(int argSize) {
if (components.size() == 0) {
return 0;
}
if (argSize >= components.size()) {
if (components.get(components.size() - 1) instanceof StringComponent) {
StringComponent strComp = (StringComponent) components.get(components.size() - 1);

View File

@@ -10,7 +10,7 @@ public class HelpPageCommand {
public HelpPageCommand(String perm, String usage, String description, String method, Type type) {
this.permission = perm;
this.usage = usage;
this.usage = usage.replaceAll("<empty>", "");
this.description = description;
this.method = method;
this.type = type;