Java 1.5 compat.

By: Erik Broes <erikbroes@ripe.net>
This commit is contained in:
Bukkit/Spigot 2011-03-02 15:23:15 +01:00
parent 2d93fec7d2
commit 6410e49ab9
3 changed files with 24 additions and 3 deletions

View File

@ -33,7 +33,7 @@ public final class PluginCommand extends Command {
throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
}
if (!success && !usageMessage.isEmpty()) {
if (!success && usageMessage.length() > 0) {
sender.sendMessage(usageMessage.replace("<command>", commandLabel));
}

View File

@ -10,6 +10,7 @@ import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import static org.bukkit.util.Java15Compat.Arrays_copyOfRange;
public final class SimpleCommandMap implements CommandMap {
private final Map<String, Command> knownCommands = new HashMap<String, Command>();
@ -62,7 +63,7 @@ public final class SimpleCommandMap implements CommandMap {
knownCommands.put(name.toLowerCase(), command);
return !nameInUse;
}
/**
* {@inheritDoc}
*/
@ -70,7 +71,7 @@ public final class SimpleCommandMap implements CommandMap {
String[] args = commandLine.split(" ");
String sentCommandLabel = args[0].toLowerCase();
args = Arrays.copyOfRange(args, 1, args.length);
args = Arrays_copyOfRange(args, 1, args.length);
Command target = getCommand(sentCommandLabel);
boolean isRegisteredCommand = (target != null);

View File

@ -0,0 +1,20 @@
package org.bukkit.util;
import java.lang.reflect.Array;
public class Java15Compat {
@SuppressWarnings("unchecked")
public static <T> T[] Arrays_copyOfRange(T[] original, int start, int end) {
if (original.length >= start && 0 <= start) {
if (start <= end) {
int length = end - start;
int copyLength = Math.min( length, original.length - start);
T[] copy = (T[]) Array.newInstance(original.getClass().getComponentType(), length);
System.arraycopy(original, start, copy, 0, copyLength);
return copy;
}
throw new IllegalArgumentException();
}
throw new ArrayIndexOutOfBoundsException();
}
}