package eu.univento.core.api; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; /** * @author PostCrafter * @see href http://postcrafter.de/viewtopic.php?f=15&t=143 * @param

main class */ public abstract class AutoCommand

extends Command { private static String VERSION; static { String path = Bukkit.getServer().getClass().getPackage().getName(); AutoCommand.VERSION = path.substring(path.lastIndexOf(".") + 1, path.length()); System.out.println("[PostLib] AutoCommand hook for Bukkit " + AutoCommand.VERSION); } protected final P plugin; protected final String command; public AutoCommand(P plugin, String command, String description, String... aliases) { super(command); this.plugin = plugin; this.command = command; super.setDescription(description); List aliasList = new ArrayList(); for (String alias : aliases) { aliasList.add(alias); } super.setAliases(aliasList); this.register(); } public void register() { try { Field f = Class.forName("org.bukkit.craftbukkit." + AutoCommand.VERSION + ".CraftServer").getDeclaredField("commandMap"); f.setAccessible(true); CommandMap map = (CommandMap) f.get(Bukkit.getServer()); map.register(this.plugin.getName(), this); } catch (Exception exc) { exc.printStackTrace(); } } public abstract boolean execute(CommandSender sender, String label, String[] args); public abstract List tabComplete(CommandSender sender, String label, String[] args); public String buildString(String[] args, int start) { String str = ""; if (args.length > start) { str += args[start]; for (int i = start + 1; i < args.length; i++) { str += " " + args[i]; } } return str; } public P getPlugin() { return this.plugin; } }