YumCore/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java

81 lines
2.4 KiB
Java
Raw Normal View History

package pw.yumc.YumCore.commands.info;
import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.annotation.Tab;
import pw.yumc.YumCore.commands.exception.CommandException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* Tab
*
* @since 2016723 9:56:42
* @author
*/
public class CommandTabInfo {
private Object origin;
private Method method;
public CommandTabInfo(Method method, Object origin) {
this.method = method;
this.origin = origin;
}
/**
* TabInfo
*
* @param method
*
* @param origin
*
* @return {@link CommandTabInfo}
*/
public static CommandTabInfo parse(Method method, Object origin) {
Tab tab = method.getAnnotation(Tab.class);
if (tab != null) { return new CommandTabInfo(method, origin); }
return null;
}
/**
* List
*
* @param sender
*
* @param command
*
* @param label
*
* @param args
*
* @return Tab
*/
@SuppressWarnings("unchecked")
public List<String> execute(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) {
CommandArgument cmdArgs = new CommandArgument(sender, command, label, args);
try {
return (List<String>) method.invoke(origin, cmdArgs);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new CommandException("调用Tab自动补全发生错误 请反馈给开发者 " + Arrays.toString(P.getDescription().getAuthors().toArray()) + " !", e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandTabInfo that = (CommandTabInfo) o;
return Objects.equals(origin, that.origin) && Objects.equals(method, that.method);
}
@Override
public int hashCode() {
return Objects.hash(origin, method);
}
}