Core/src/main/java/eu/univento/core/api/AutoCommand.java

81 lines
2.1 KiB
Java

/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
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.Collections;
import java.util.List;
/**
* @author PostCrafter
* @see "http://postcrafter.de/viewtopic.php?f=15&t=143"
* @param <P> main class
*/
@Deprecated
public abstract class AutoCommand<P extends JavaPlugin> 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("AutoCommand hook for Bukkit " + AutoCommand.VERSION);
}
private final P plugin;
private 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<String> aliasList = new ArrayList<>();
Collections.addAll(aliasList, aliases);
super.setAliases(aliasList);
this.register();
}
private 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<String> 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;
}
}