package se.ranzdo.bukkit.methodcommand;

import java.lang.reflect.Array;

import org.bukkit.command.CommandSender;


public class WildcardArgument extends CommandArgument {
	private boolean join;

	public WildcardArgument(Arg commandArgAnnotation, Class<?> argumentClass, ArgumentHandler<?> argumentHandler, boolean join) {
		super(commandArgAnnotation, argumentClass, argumentHandler);
		this.join = join;
	}
	
	public WildcardArgument(String name, String description, String def, String verifiers, Class<?> argumentClass, ArgumentHandler<?> handler, boolean join) {
		super(name, description, def, verifiers, argumentClass, handler);
		this.join = join;
	}
	
	@Override
	public Object execute(CommandSender sender, Arguments args) throws CommandError {
		if(!args.hasNext()) {
			Object o = getHandler().handle(sender, this, getDefault().equals(" ") ? "" : getDefault());
			if(join)
				return o;
			else {
				Object array = Array.newInstance(getArgumentClass(), 1);
				Array.set(array, 0, o);
				return array;
			}
		}
		
		if(join) {
			StringBuilder sb = new StringBuilder();
			boolean hasRun = false;
			
			while(args.hasNext()) {
				if(hasRun) {
					sb.append(" "+args.nextArgument());
				} else {
					sb.append(args.nextArgument());
					hasRun = true;
				}
			}
			
			return getHandler().handle(sender, this, CommandUtil.escapeArgumentVariable(sb.toString()));
		}
		else {
			Object array = Array.newInstance(getArgumentClass(), args.over());
			
			for(int i = 0; i < args.over();i++)
				Array.set(array, i, getHandler().handle(sender, this, CommandUtil.escapeArgumentVariable(args.nextArgument())));
			
			return array;
		}
	}
	
	public boolean willJoin() {
		return join;
	}
}

@@ -1082,35 +1082,8 @@
 ();%0A
-%09%09%09boolean hasRun = false;%0A
 %09%09%09%0A
@@ -1113,26 +1113,8 @@
 ) %7B%0A
-%09%09%09%09if(hasRun) %7B%0A%09
 %09%09%09%09
@@ -1127,12 +1127,8 @@
 end(
-%22 %22+
 args
@@ -1147,85 +1147,21 @@
 t())
-;%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09sb.append(args.nextArgument());%0A%09%09%09%09%09hasRun = true;%0A%09%09%09%09%7D
+.append(%22 %22);
 %0A%09%09%09
@@ -1258,16 +1258,22 @@
 String()
+.trim(
 ));%0A%09%09%7D%0A

Update WildcardArgument.java