mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-21 01:38:51 +00:00
init: 项目初始化...
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
commit
73eeac2484
8
.classpath
Normal file
8
.classpath
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="src" path="src/main/resources"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
# Eclipse stuff
|
||||
/.settings
|
||||
|
||||
# netbeans
|
||||
/nbproject
|
||||
|
||||
# we use maven!
|
||||
/build.xml
|
||||
|
||||
# maven
|
||||
/target
|
||||
/repo
|
||||
|
||||
# vim
|
||||
.*.sw[a-p]
|
||||
|
||||
# various other potential build files
|
||||
/build
|
||||
/bin
|
||||
/dist
|
||||
/manifest.mf
|
||||
|
||||
/world
|
||||
|
||||
# Mac filesystem dust
|
||||
*.DS_Store
|
||||
|
||||
# intellij
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# Project Stuff
|
||||
/src/main/resources/Soulbound
|
||||
|
||||
# Atlassian Stuff
|
||||
/atlassian-ide-plugin.xml
|
23
.project
Normal file
23
.project
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>YumCore</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
BIN
lib/PlaceholderAPI.jar
Normal file
BIN
lib/PlaceholderAPI.jar
Normal file
Binary file not shown.
BIN
lib/ProtocolLib-3.6.5-SNAPSHOT.jar
Normal file
BIN
lib/ProtocolLib-3.6.5-SNAPSHOT.jar
Normal file
Binary file not shown.
BIN
lib/craftbukkit-1.7.10.jar
Normal file
BIN
lib/craftbukkit-1.7.10.jar
Normal file
Binary file not shown.
117
pom.xml
Normal file
117
pom.xml
Normal file
@ -0,0 +1,117 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>pw.yumc</groupId>
|
||||
<artifactId>YumCore</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>YumCore</name>
|
||||
<build>
|
||||
<finalName>${project.name}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>source</id><!--指定一个名字 -->
|
||||
<phase>compile</phase><!--在编译阶段生成source包 -->
|
||||
<goals>
|
||||
<goal>jar</goal><!--指定生成的文件为jar包 -->
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>javadocs</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<failOnError>false</failOnError>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<ciManagement>
|
||||
<system>Jenkins</system>
|
||||
<url>http://ci.yumc.pw</url>
|
||||
</ciManagement>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<env.GIT_COMMIT>开发版本</env.GIT_COMMIT>
|
||||
</properties>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>yumc-repo</id>
|
||||
<url>http://repo.yumc.pw/content/groups/public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>jtb</id>
|
||||
<name>YUMC</name>
|
||||
<url>http://repo.yumc.pw/content/repositories/yumcenter/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.10.2-R0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.milkbowl.vault</groupId>
|
||||
<artifactId>Vault</artifactId>
|
||||
<version>1.5.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>1.7.10</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/craftbukkit-1.7.10.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.comphenix.protocol</groupId>
|
||||
<artifactId>ProtocolLib</artifactId>
|
||||
<version>3.6.5-SNAPSHOT</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/ProtocolLib-3.6.5-SNAPSHOT.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>PlaceholderAPI</artifactId>
|
||||
<version>1.8.6</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/PlaceholderAPI.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
11
src/main/java/pw/yumc/YumCore/YumCore.java
Normal file
11
src/main/java/pw/yumc/YumCore/YumCore.java
Normal file
@ -0,0 +1,11 @@
|
||||
package pw.yumc.YumCore;
|
||||
|
||||
/**
|
||||
* YumCore核心类库
|
||||
*
|
||||
* @since 2016年7月23日 下午1:03:41
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class YumCore {
|
||||
|
||||
}
|
48
src/main/java/pw/yumc/YumCore/bukkit/Log.java
Normal file
48
src/main/java/pw/yumc/YumCore/bukkit/Log.java
Normal file
@ -0,0 +1,48 @@
|
||||
package pw.yumc.YumCore.bukkit;
|
||||
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* 插件日志输出类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:11:01
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class Log {
|
||||
private static Logger logger = P.instance.getLogger();
|
||||
|
||||
public static void addHandler(final Handler handler) throws SecurityException {
|
||||
logger.addHandler(handler);
|
||||
}
|
||||
|
||||
public static void info(final String msg) {
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
public static void log(final Level level, final String msg) {
|
||||
logger.log(level, msg);
|
||||
}
|
||||
|
||||
public static void log(final Level level, final String msg, final Object param1) {
|
||||
logger.log(level, msg, param1);
|
||||
}
|
||||
|
||||
public static void log(final Level level, final String msg, final Object[] params) {
|
||||
logger.log(level, msg, params);
|
||||
}
|
||||
|
||||
public static void log(final Level level, final String msg, final Throwable thrown) {
|
||||
logger.log(level, msg, thrown);
|
||||
}
|
||||
|
||||
public static void severe(final String msg) {
|
||||
logger.severe(msg);
|
||||
}
|
||||
|
||||
public static void warning(final String msg) {
|
||||
logger.warning(msg);
|
||||
}
|
||||
|
||||
}
|
29
src/main/java/pw/yumc/YumCore/bukkit/P.java
Normal file
29
src/main/java/pw/yumc/YumCore/bukkit/P.java
Normal file
@ -0,0 +1,29 @@
|
||||
package pw.yumc.YumCore.bukkit;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
/**
|
||||
* 插件Instance获取类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:09:57
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class P {
|
||||
/**
|
||||
* 插件实例
|
||||
*/
|
||||
public static JavaPlugin instance;
|
||||
|
||||
static {
|
||||
final Object pluginClassLoader = P.class.getClassLoader();
|
||||
try {
|
||||
final Field field = pluginClassLoader.getClass().getDeclaredField("plugin");
|
||||
field.setAccessible(true);
|
||||
instance = (JavaPlugin) field.get(pluginClassLoader);
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
96
src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java
Normal file
96
src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java
Normal file
@ -0,0 +1,96 @@
|
||||
package pw.yumc.YumCore.bukkit.compatible;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import pw.yumc.YumCore.bukkit.Log;
|
||||
|
||||
/**
|
||||
* Bukkit兼容类
|
||||
*
|
||||
* @since 2016年7月23日 下午1:04:56
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class C {
|
||||
/**
|
||||
* Bukkit Player兼容类
|
||||
*
|
||||
* @since 2016年7月23日 下午4:33:40
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public static class Player {
|
||||
private static Class<?> gameProfileClass;
|
||||
private static Constructor<?> gameProfileConstructor;
|
||||
private static Constructor<?> craftOfflinePlayerConstructor;
|
||||
|
||||
private static Method getOnlinePlayers;
|
||||
|
||||
static {
|
||||
try {
|
||||
// getOnlinePlayers start
|
||||
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
if (getOnlinePlayers.getReturnType() != Player[].class) {
|
||||
getOnlinePlayers = Bukkit.class.getDeclaredMethod("_INVALID_getOnlinePlayers");
|
||||
}
|
||||
// getOnlinePlayers end
|
||||
// getOfflinePlayer start
|
||||
try {
|
||||
gameProfileClass = Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile");
|
||||
} catch (final ClassNotFoundException e) {
|
||||
try {
|
||||
gameProfileClass = Class.forName("com.mojang.authlib.GameProfile");
|
||||
} catch (final ClassNotFoundException e1) {
|
||||
}
|
||||
}
|
||||
gameProfileConstructor = gameProfileClass.getDeclaredConstructor(new Class[] { UUID.class, String.class });
|
||||
gameProfileConstructor.setAccessible(true);
|
||||
final Class<? extends Server> craftServer = Bukkit.getServer().getClass();
|
||||
final Class<?> craftOfflinePlayer = Class.forName(craftServer.getName().replace("CraftServer", "CraftOfflinePlayer"));
|
||||
craftOfflinePlayerConstructor = craftOfflinePlayer.getDeclaredConstructor(new Class[] { craftServer, gameProfileClass });
|
||||
craftOfflinePlayerConstructor.setAccessible(true);
|
||||
// getOfflinePlayer end
|
||||
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
|
||||
Log.warning(Player.class.getSimpleName() + "兼容性工具初始化失败 可能造成部分功能不可用!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取离线玩家(跳过网络获取)
|
||||
*
|
||||
* @param playerName
|
||||
* 玩家名称
|
||||
* @return {@link OfflinePlayer}
|
||||
*/
|
||||
public static OfflinePlayer getOfflinePlayer(final String playerName) {
|
||||
try {
|
||||
final Object gameProfile = gameProfileConstructor.newInstance(new Object[] { UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(Charsets.UTF_8)), playerName });
|
||||
final Object offlinePlayer = craftOfflinePlayerConstructor.newInstance(new Object[] { Bukkit.getServer(), gameProfile });
|
||||
return (OfflinePlayer) offlinePlayer;
|
||||
} catch (final Throwable var5) {
|
||||
return Bukkit.getOfflinePlayer(playerName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线玩家
|
||||
*
|
||||
* @return 在线玩家
|
||||
*/
|
||||
public static Collection<? extends org.bukkit.entity.Player> getOnlinePlayers() {
|
||||
try {
|
||||
return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null));
|
||||
} catch (final Throwable e) {
|
||||
return Bukkit.getOnlinePlayers();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
80
src/main/java/pw/yumc/YumCore/commands/CommandArgument.java
Normal file
80
src/main/java/pw/yumc/YumCore/commands/CommandArgument.java
Normal file
@ -0,0 +1,80 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import pw.yumc.YumCore.commands.exception.IllegalPermissionException;
|
||||
import pw.yumc.YumCore.commands.exception.IllegalSenderException;
|
||||
|
||||
/**
|
||||
* 子命令调用事件类
|
||||
*
|
||||
* @since 2015年8月22日上午8:29:44
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandArgument {
|
||||
private final CommandSender sender;
|
||||
private final Command command;
|
||||
private final String alias;
|
||||
private final String[] args;
|
||||
|
||||
public CommandArgument(final CommandSender sender, final Command command, final String alias, final String[] args) {
|
||||
this.sender = sender;
|
||||
this.command = command;
|
||||
this.alias = alias;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public boolean check(final CommandInfo info) {
|
||||
if (sender instanceof Player) {
|
||||
if (info.getCommand().onlyConsoleExecutable()) {
|
||||
throw new IllegalSenderException("§c玩家无法使用此命令(§4请使用控制台执行§c)!");
|
||||
}
|
||||
} else if (info.getCommand().onlyPlayerExecutable()) {
|
||||
throw new IllegalSenderException("§c玩家无法使用此命令(§4请使用控制台执行§c)!");
|
||||
}
|
||||
final String perm = info.getCommand().permission();
|
||||
if (perm != null && !sender.hasPermission(perm)) {
|
||||
throw new IllegalPermissionException("§c你需要有 " + perm + " 的权限才能执行此命令!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令别名
|
||||
*
|
||||
* @return alias
|
||||
*/
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令参数
|
||||
*
|
||||
* @return args
|
||||
*/
|
||||
public String[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令实体
|
||||
*
|
||||
* @return command
|
||||
*/
|
||||
public Command getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令发送者
|
||||
*
|
||||
* @return sender
|
||||
*/
|
||||
public CommandSender getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
}
|
121
src/main/java/pw/yumc/YumCore/commands/CommandHelp.java
Normal file
121
src/main/java/pw/yumc/YumCore/commands/CommandHelp.java
Normal file
@ -0,0 +1,121 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import pw.yumc.YumCore.bukkit.P;
|
||||
import pw.yumc.YumCore.commands.annotation.Help;
|
||||
|
||||
/**
|
||||
* 命令帮助生成类
|
||||
*
|
||||
* @since 2016年7月23日 上午10:12:32
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandHelp {
|
||||
/**
|
||||
* 插件实例
|
||||
*/
|
||||
Plugin plugin = P.instance;
|
||||
/**
|
||||
* 消息配置
|
||||
*/
|
||||
String prefix = String.format("§6[§b%s§6] ", P.instance.getName());
|
||||
String commandNotFound = prefix + "§c当前插件未注册任何子命令!";
|
||||
String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s";
|
||||
String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix);
|
||||
String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s";
|
||||
String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s";
|
||||
/**
|
||||
* 已排序的命令列表
|
||||
*/
|
||||
List<CommandInfo> cmdlist;
|
||||
/**
|
||||
* 帮助页面数量
|
||||
*/
|
||||
private final int HELPPAGECOUNT;
|
||||
/**
|
||||
* 帮助页面每页行数
|
||||
*/
|
||||
private final int LINES_PER_PAGE = 7;
|
||||
/**
|
||||
* 帮助列表缓存
|
||||
*/
|
||||
private final Map<String, String[]> cacheHelp = new HashMap<>();
|
||||
|
||||
public CommandHelp(final Collection<? extends CommandInfo> list) {
|
||||
cmdlist = new LinkedList<>(list);
|
||||
Collections.sort(cmdlist, new CommandComparator());
|
||||
this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送帮助
|
||||
*
|
||||
* @param ca
|
||||
* 命令参数
|
||||
*/
|
||||
public void send(final CommandSender sender, final Command command, final String label, final String[] args) {
|
||||
if (this.HELPPAGECOUNT == 0) {
|
||||
sender.sendMessage(commandNotFound);
|
||||
return;
|
||||
}
|
||||
int page = 1;
|
||||
try {
|
||||
page = Integer.parseInt(args[1]);
|
||||
page = page == 0 ? 1 : page;
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
final String helpkey = label + page;
|
||||
if (!cacheHelp.containsKey(helpkey)) {
|
||||
final List<String> helpList = new ArrayList<>();
|
||||
if (page > this.HELPPAGECOUNT || page < 1) {
|
||||
// 帮助页面不存在
|
||||
helpList.add(String.format(commandNotFound, HELPPAGECOUNT));
|
||||
} else {
|
||||
// 帮助标题
|
||||
helpList.add(helpTitle);
|
||||
final int start = this.LINES_PER_PAGE * (page - 1);
|
||||
final int end = start + this.LINES_PER_PAGE;
|
||||
for (int i = start; i < end; i++) {
|
||||
if (this.cmdlist.size() > i) {
|
||||
final CommandInfo ci = cmdlist.get(i);
|
||||
final String aliases = Arrays.toString(ci.getCommand().aliases());
|
||||
final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases);
|
||||
final Help help = ci.getHelp();
|
||||
// 帮助列表
|
||||
helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.description()));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 帮助结尾
|
||||
helpList.add(String.format(helpFooter, label, HELPPAGECOUNT));
|
||||
cacheHelp.put(helpkey, helpList.toArray(new String[0]));
|
||||
}
|
||||
sender.sendMessage(cacheHelp.get(helpkey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令排序比较器
|
||||
*
|
||||
* @since 2016年7月23日 下午4:17:18
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
static class CommandComparator implements Comparator<CommandInfo> {
|
||||
@Override
|
||||
public int compare(final CommandInfo o1, final CommandInfo o2) {
|
||||
return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort()));
|
||||
}
|
||||
}
|
||||
}
|
130
src/main/java/pw/yumc/YumCore/commands/CommandInfo.java
Normal file
130
src/main/java/pw/yumc/YumCore/commands/CommandInfo.java
Normal file
@ -0,0 +1,130 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import pw.yumc.YumCore.bukkit.P;
|
||||
import pw.yumc.YumCore.commands.annotation.Async;
|
||||
import pw.yumc.YumCore.commands.annotation.Cmd;
|
||||
import pw.yumc.YumCore.commands.annotation.Help;
|
||||
import pw.yumc.YumCore.commands.annotation.Sort;
|
||||
import pw.yumc.YumCore.commands.exception.CommandException;
|
||||
|
||||
/**
|
||||
* 命令信息存储类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:56:42
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandInfo {
|
||||
public static CommandInfo Unknow = new CommandInfo();
|
||||
private final Object origin;
|
||||
private final Method method;
|
||||
private final String name;
|
||||
private final List<String> aliases;
|
||||
private final boolean async;
|
||||
private final Cmd command;
|
||||
private final Help help;
|
||||
private final int sort;
|
||||
|
||||
public CommandInfo(final Method method, final Object origin, final Cmd command, final Help help, final boolean async, final int sort) {
|
||||
this.method = method;
|
||||
this.origin = origin;
|
||||
this.name = "".equals(command.name()) ? method.getName().toLowerCase() : command.name();
|
||||
this.aliases = Arrays.asList(command.aliases());
|
||||
this.command = command;
|
||||
this.help = help;
|
||||
this.async = async;
|
||||
this.sort = sort;
|
||||
}
|
||||
|
||||
private CommandInfo() {
|
||||
this.method = null;
|
||||
this.origin = null;
|
||||
this.name = "unknow";
|
||||
this.aliases = null;
|
||||
this.command = null;
|
||||
this.help = null;
|
||||
this.async = false;
|
||||
this.sort = 0;
|
||||
}
|
||||
|
||||
public static CommandInfo parse(final Method method, final Object origin) {
|
||||
final Class<?> clazz = method.getClass();
|
||||
final Cmd command = clazz.getAnnotation(Cmd.class);
|
||||
if (command != null) {
|
||||
final Help help = clazz.getAnnotation(Help.class);
|
||||
final Async async = clazz.getAnnotation(Async.class);
|
||||
final Sort sort = clazz.getAnnotation(Sort.class);
|
||||
return new CommandInfo(method, origin, command, help != null ? help : Help.DEFAULT, async != null, sort != null ? sort.sort() : 50);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (obj instanceof CommandInfo) {
|
||||
return name.equalsIgnoreCase(((CommandInfo) obj).getName());
|
||||
}
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
public boolean execute(final CommandSender sender, final Command command, final String label, final String[] args) {
|
||||
if (method == null) {
|
||||
return false;
|
||||
}
|
||||
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, args);
|
||||
final Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
try {
|
||||
cmdArgs.check(CommandInfo.this);
|
||||
} catch (final CommandException e) {
|
||||
sender.sendMessage(e.getMessage());
|
||||
return;
|
||||
}
|
||||
method.invoke(origin, cmdArgs);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
throw new CommandException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (async) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(P.instance, runnable);
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Cmd getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
public Help getHelp() {
|
||||
return help;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
public boolean isAsync() {
|
||||
return async;
|
||||
}
|
||||
|
||||
public boolean isValid(final String cmd) {
|
||||
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
|
||||
}
|
||||
}
|
198
src/main/java/pw/yumc/YumCore/commands/CommandManager.java
Normal file
198
src/main/java/pw/yumc/YumCore/commands/CommandManager.java
Normal file
@ -0,0 +1,198 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import pw.yumc.YumCore.bukkit.Log;
|
||||
import pw.yumc.YumCore.bukkit.P;
|
||||
import pw.yumc.YumCore.bukkit.compatible.C;
|
||||
import pw.yumc.YumCore.commands.api.CommandExecutor;
|
||||
|
||||
/**
|
||||
* 命令管理类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:06:03
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandManager implements TabExecutor {
|
||||
private final String argumentTypeError = "注解命令方法 %s 位于 %s 的参数错误 应只有 CommandArgument 参数!";
|
||||
private final String returnTypeError = "注解命令补全 %s 位于 %s 的返回值错误 应实现 List 接口!";
|
||||
/**
|
||||
* 插件实例类
|
||||
*/
|
||||
JavaPlugin plugin = P.instance;
|
||||
/**
|
||||
* 命令列表
|
||||
*/
|
||||
Set<CommandInfo> cmdlist = new HashSet<>();
|
||||
/**
|
||||
* Tab列表
|
||||
*/
|
||||
Set<TabInfo> tablist = new HashSet<>();
|
||||
/**
|
||||
* 命令缓存列表
|
||||
*/
|
||||
Map<String, CommandInfo> cmdcache = new HashMap<>();
|
||||
/**
|
||||
* 命令帮助
|
||||
*/
|
||||
CommandHelp help = new CommandHelp(cmdlist);
|
||||
|
||||
/**
|
||||
* 命令管理器
|
||||
*
|
||||
* @param name
|
||||
* 注册的命令
|
||||
*/
|
||||
public CommandManager(final String name) {
|
||||
final PluginCommand cmd = plugin.getCommand(name);
|
||||
if (cmd == null) {
|
||||
throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + name + " 命令!");
|
||||
}
|
||||
cmd.setExecutor(this);
|
||||
cmd.setTabCompleter(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令管理器
|
||||
*
|
||||
* @param name
|
||||
* 注册的命令
|
||||
* @param executor
|
||||
* 命令执行类
|
||||
*/
|
||||
public CommandManager(final String name, final CommandExecutor executor) {
|
||||
this(name);
|
||||
registerCommands(executor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
|
||||
if (args.length == 0) {
|
||||
help.send(sender, command, label, args);
|
||||
return true;
|
||||
}
|
||||
final String subcmd = args[0].toLowerCase();
|
||||
if (subcmd.equalsIgnoreCase("help")) {
|
||||
help.send(sender, command, label, args);
|
||||
return true;
|
||||
}
|
||||
final String[] subargs = moveStrings(args, 1);
|
||||
if (!cmdcache.containsKey(label)) {
|
||||
for (final CommandInfo cmdinfo : cmdlist) {
|
||||
if (cmdinfo.isValid(label)) {
|
||||
cmdcache.put(label, cmdinfo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
cmdcache.put(label, CommandInfo.Unknow);
|
||||
}
|
||||
return cmdcache.get(label).execute(sender, command, label, subargs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(final CommandSender sender, final Command command, final String alias, final String[] args) {
|
||||
final List<String> completions = new ArrayList<>();
|
||||
final String token = args[args.length - 1];
|
||||
if (args.length == 1) {
|
||||
final Set<String> commands = this.cmdcache.keySet();
|
||||
StringUtil.copyPartialMatches(args[0], commands, completions);
|
||||
} else if (args.length >= 2) {
|
||||
for (final TabInfo tab : tablist) {
|
||||
StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions);
|
||||
}
|
||||
StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions);
|
||||
}
|
||||
Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
|
||||
return completions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过注解读取命令并注册
|
||||
*
|
||||
* @param clazz
|
||||
* 子命令处理类
|
||||
*/
|
||||
public void registerCommands(final CommandExecutor clazz) {
|
||||
final Method[] methods = clazz.getClass().getDeclaredMethods();
|
||||
for (final Method method : methods) {
|
||||
final CommandInfo ci = CommandInfo.parse(method, clazz);
|
||||
if (ci != null) {
|
||||
final Class<?>[] params = method.getParameterTypes();
|
||||
if (params.length == 1 && params[0].equals(CommandArgument.class)) {
|
||||
if (cmdlist.add(ci)) {
|
||||
cmdcache.put(ci.getName(), ci);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName()));
|
||||
}
|
||||
}
|
||||
final TabInfo ti = TabInfo.parse(method, clazz);
|
||||
if (ti != null) {
|
||||
if (!method.getReturnType().equals(List.class)) {
|
||||
Log.warning(String.format(returnTypeError, method.getName(), clazz.getClass().getName()));
|
||||
continue;
|
||||
}
|
||||
tablist.add(ti);
|
||||
}
|
||||
}
|
||||
help = new CommandHelp(cmdlist);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家命令补全
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param command
|
||||
* 命令
|
||||
* @param alias
|
||||
* 别名
|
||||
* @param args
|
||||
* 数组
|
||||
* @return 在线玩家数组
|
||||
*/
|
||||
private List<String> getPlayerTabComplete(final CommandSender sender, final Command command, final String alias, final String[] args) {
|
||||
final String lastWord = args[args.length - 1];
|
||||
final Player senderPlayer = sender instanceof Player ? (Player) sender : null;
|
||||
final ArrayList<String> matchedPlayers = new ArrayList<>();
|
||||
for (final Player player : C.Player.getOnlinePlayers()) {
|
||||
final String name = player.getName();
|
||||
if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, lastWord)) {
|
||||
matchedPlayers.add(name);
|
||||
}
|
||||
}
|
||||
return matchedPlayers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转移数组
|
||||
*
|
||||
* @param args
|
||||
* 原数组
|
||||
* @param start
|
||||
* 数组开始位置
|
||||
* @return 转移后的数组字符串
|
||||
*/
|
||||
private String[] moveStrings(final String[] args, final int start) {
|
||||
final String[] ret = new String[args.length - start];
|
||||
System.arraycopy(args, start, ret, 0, ret.length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
80
src/main/java/pw/yumc/YumCore/commands/TabInfo.java
Normal file
80
src/main/java/pw/yumc/YumCore/commands/TabInfo.java
Normal file
@ -0,0 +1,80 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import pw.yumc.YumCore.bukkit.P;
|
||||
import pw.yumc.YumCore.commands.annotation.Tab;
|
||||
|
||||
/**
|
||||
* Tab补全
|
||||
*
|
||||
* @since 2016年7月23日 上午9:56:42
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class TabInfo {
|
||||
private final Object origin;
|
||||
private final Method method;
|
||||
|
||||
public TabInfo(final Method method, final Object origin) {
|
||||
this.method = method;
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析TabInfo
|
||||
*
|
||||
* @param method
|
||||
* 方法
|
||||
* @param origin
|
||||
* 对象
|
||||
* @return {@link TabInfo}
|
||||
*/
|
||||
public static TabInfo parse(final Method method, final Object origin) {
|
||||
final Class<?> clazz = method.getClass();
|
||||
final Tab tab = clazz.getAnnotation(Tab.class);
|
||||
if (tab != null) {
|
||||
return new TabInfo(method, origin);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (obj instanceof TabInfo) {
|
||||
return method.equals(((TabInfo) obj).getMethod());
|
||||
}
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得补全List
|
||||
*
|
||||
* @param sender
|
||||
* 发送者
|
||||
* @param command
|
||||
* 命令
|
||||
* @param label
|
||||
* 命令
|
||||
* @param args
|
||||
* 参数
|
||||
* @return Tab补全信息
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> execute(final CommandSender sender, final org.bukkit.command.Command command, final String label, final String[] args) {
|
||||
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, args);
|
||||
try {
|
||||
return (List<String>) method.invoke(origin, cmdArgs);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
throw new RuntimeException("调用Tab自动补全发生错误 请反馈给开发者 " + Arrays.toString(P.instance.getDescription().getAuthors().toArray()) + " !", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
}
|
18
src/main/java/pw/yumc/YumCore/commands/annotation/Async.java
Normal file
18
src/main/java/pw/yumc/YumCore/commands/annotation/Async.java
Normal file
@ -0,0 +1,18 @@
|
||||
package pw.yumc.YumCore.commands.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 异步命令注解
|
||||
*
|
||||
* @since 2016年7月23日 上午9:00:27
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Async {
|
||||
|
||||
}
|
46
src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java
Normal file
46
src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java
Normal file
@ -0,0 +1,46 @@
|
||||
package pw.yumc.YumCore.commands.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 命令注解
|
||||
*
|
||||
* @since 2016年7月23日 上午8:59:05
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Cmd {
|
||||
/**
|
||||
* @return 命令别名
|
||||
*/
|
||||
String[] aliases() default "";
|
||||
|
||||
/**
|
||||
* @return 命令最小参数
|
||||
*/
|
||||
int minimumArguments() default 0;
|
||||
|
||||
/**
|
||||
* @return 命令名称
|
||||
*/
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* @return 只允许控制台执行
|
||||
*/
|
||||
boolean onlyConsoleExecutable() default false;
|
||||
|
||||
/**
|
||||
* @return 只允许玩家执行
|
||||
*/
|
||||
boolean onlyPlayerExecutable() default false;
|
||||
|
||||
/**
|
||||
* @return 当前命令权限
|
||||
*/
|
||||
String permission() default "";
|
||||
}
|
44
src/main/java/pw/yumc/YumCore/commands/annotation/Help.java
Normal file
44
src/main/java/pw/yumc/YumCore/commands/annotation/Help.java
Normal file
@ -0,0 +1,44 @@
|
||||
package pw.yumc.YumCore.commands.annotation;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 命令帮助注解
|
||||
*
|
||||
* @since 2016年7月23日 上午9:00:07
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Help {
|
||||
public static Help DEFAULT = new Help() {
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return getClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "这家伙很懒";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String possibleArguments() {
|
||||
return "没有帮助信息";
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @return 命令描述
|
||||
*/
|
||||
String description() default "无";
|
||||
|
||||
/**
|
||||
* @return 当前命令可能需要的参数
|
||||
*/
|
||||
String possibleArguments() default "";
|
||||
}
|
13
src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java
Normal file
13
src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java
Normal file
@ -0,0 +1,13 @@
|
||||
package pw.yumc.YumCore.commands.annotation;
|
||||
|
||||
/**
|
||||
*
|
||||
* @since 2016年7月23日 上午9:04:56
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public @interface Sort {
|
||||
/**
|
||||
* @return 命令排序
|
||||
*/
|
||||
int sort() default 50;
|
||||
}
|
10
src/main/java/pw/yumc/YumCore/commands/annotation/Tab.java
Normal file
10
src/main/java/pw/yumc/YumCore/commands/annotation/Tab.java
Normal file
@ -0,0 +1,10 @@
|
||||
package pw.yumc.YumCore.commands.annotation;
|
||||
|
||||
/**
|
||||
* 自动补全注解
|
||||
*
|
||||
* @since 2016年7月23日 上午9:43:40
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public @interface Tab {
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package pw.yumc.YumCore.commands.api;
|
||||
|
||||
/**
|
||||
* 命令执行类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:55:51
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public interface CommandExecutor {
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package pw.yumc.YumCore.commands.exception;
|
||||
|
||||
/**
|
||||
* 命令执行异常类
|
||||
*
|
||||
* @since 2016年7月23日 上午10:50:23
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandException extends RuntimeException {
|
||||
|
||||
public CommandException(final Exception e) {
|
||||
super(e);
|
||||
}
|
||||
|
||||
public CommandException(final String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package pw.yumc.YumCore.commands.exception;
|
||||
|
||||
/**
|
||||
*
|
||||
* @since 2016年7月23日 上午11:00:34
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class IllegalArgumentException extends CommandException {
|
||||
|
||||
public IllegalArgumentException(final String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package pw.yumc.YumCore.commands.exception;
|
||||
|
||||
/**
|
||||
*
|
||||
* @since 2016年7月23日 上午10:50:23
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class IllegalPermissionException extends CommandException {
|
||||
public IllegalPermissionException(final String string) {
|
||||
super(string);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package pw.yumc.YumCore.commands.exception;
|
||||
|
||||
/**
|
||||
*
|
||||
* @since 2016年7月23日 上午11:00:34
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class IllegalSenderException extends CommandException {
|
||||
|
||||
public IllegalSenderException(final String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user