1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-21 01:38:51 +00:00

fix: 修复开发版本无法自动更新的问题 添加单元测试

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-08-22 19:24:32 +08:00
parent e578f33608
commit d767dba2e6
8 changed files with 189 additions and 160 deletions

15
pom.xml
View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>
<version>1.8.3</version> <version>1.8.5</version>
<build> <build>
<finalName>${project.artifactId}</finalName> <finalName>${project.artifactId}</finalName>
<plugins> <plugins>
@ -66,7 +66,7 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.11.2-R0.1-SNAPSHOT</version> <version>[1.12.1-R0.1-SNAPSHOT,)</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
@ -81,7 +81,7 @@
<dependency> <dependency>
<groupId>net.md-5</groupId> <groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId> <artifactId>bungeecord-api</artifactId>
<version>1.11-SNAPSHOT</version> <version>[1.12-SNAPSHOT,)</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>snakeyaml</artifactId> <artifactId>snakeyaml</artifactId>
@ -92,7 +92,7 @@
<dependency> <dependency>
<groupId>me.clip</groupId> <groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId> <artifactId>placeholderapi</artifactId>
<version>2.5.1</version> <version>[2.5.1,)</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
@ -108,7 +108,7 @@
<dependency> <dependency>
<groupId>net.milkbowl.vault</groupId> <groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId> <artifactId>Vault</artifactId>
<version>1.5.6</version> <version>[1.5.6,)</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.mail</groupId> <groupId>javax.mail</groupId>
@ -130,6 +130,11 @@
<artifactId>worldedit-bukkit</artifactId> <artifactId>worldedit-bukkit</artifactId>
<version>6.1.5</version> <version>6.1.5</version>
</dependency> </dependency>
<dependency>
<groupId>io.puharesource.mc</groupId>
<artifactId>TitleManager</artifactId>
<version>2.0.0</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>

View File

@ -2,7 +2,7 @@ package pw.yumc.YumCore.bukkit;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.Optional;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -27,6 +27,7 @@ public class Log {
private static Logger logger; private static Logger logger;
private static CommandSender console; private static CommandSender console;
private static String prefix; private static String prefix;
static { static {
try { try {
debug = globalDebug || P.getDescription().getVersion().contains("DEV"); debug = globalDebug || P.getDescription().getVersion().contains("DEV");
@ -36,7 +37,6 @@ public class Log {
} catch (Throwable ex) { } catch (Throwable ex) {
logger = Logger.getLogger("YumCore"); logger = Logger.getLogger("YumCore");
debug = true; debug = true;
d(ex);
} }
} }
@ -51,10 +51,10 @@ public class Log {
* that essentially act as default handlers for all loggers. * that essentially act as default handlers for all loggers.
* *
* @param handler * @param handler
* a logging Handler * a logging Handler
* @throws SecurityException * @throws SecurityException
* if a security manager exists and if * if a security manager exists and if
* the caller does not have LoggingPermission("control"). * the caller does not have LoggingPermission("control").
*/ */
public static void addHandler(Handler handler) throws SecurityException { public static void addHandler(Handler handler) throws SecurityException {
logger.addHandler(handler); logger.addHandler(handler);
@ -64,7 +64,7 @@ public class Log {
* Sends console a message * Sends console a message
* *
* @param message * @param message
* Message to be displayed * Message to be displayed
*/ */
public static void console(String message) { public static void console(String message) {
console.sendMessage(prefix + message); console.sendMessage(prefix + message);
@ -74,9 +74,9 @@ public class Log {
* Sends console a message * Sends console a message
* *
* @param message * @param message
* 消息 * 消息
* @param object * @param object
* 格式化参数 * 格式化参数
*/ */
public static void console(String message, Object... object) { public static void console(String message, Object... object) {
console.sendMessage(prefix + String.format(message, object)); console.sendMessage(prefix + String.format(message, object));
@ -86,7 +86,7 @@ public class Log {
* Sends console a message * Sends console a message
* *
* @param msg * @param msg
* Message to be displayed * Message to be displayed
*/ */
public static void console(String[] msg) { public static void console(String[] msg) {
for (String str : msg) { for (String str : msg) {
@ -98,7 +98,7 @@ public class Log {
* 调试消息 * 调试消息
* *
* @param msg * @param msg
* 消息 * 消息
*/ */
public static void d(String msg) { public static void d(String msg) {
if (debug) { if (debug) {
@ -110,9 +110,9 @@ public class Log {
* 调试消息 * 调试消息
* *
* @param msg * @param msg
* 消息 * 消息
* @param object * @param object
* 参数 * 参数
*/ */
public static void d(String msg, Object... object) { public static void d(String msg, Object... object) {
if (debug) { if (debug) {
@ -124,7 +124,7 @@ public class Log {
* 调试消息 * 调试消息
* *
* @param e * @param e
* 异常 * 异常
*/ */
public static void d(Throwable e) { public static void d(Throwable e) {
if (debug) { if (debug) {
@ -136,9 +136,9 @@ public class Log {
* 调试消息 * 调试消息
* *
* @param msg * @param msg
* 消息 * 消息
* @param e * @param e
* 异常 * 异常
*/ */
public static void d(String msg, Throwable e) { public static void d(String msg, Throwable e) {
if (debug) { if (debug) {
@ -151,7 +151,7 @@ public class Log {
* 完全调试消息 * 完全调试消息
* *
* @param msg * @param msg
* 消息 * 消息
*/ */
public static void fd(String msg) { public static void fd(String msg) {
if (fullDebug) { if (fullDebug) {
@ -163,9 +163,9 @@ public class Log {
* 完全调试消息 * 完全调试消息
* *
* @param msg * @param msg
* 消息 * 消息
* @param object * @param object
* 参数 * 参数
*/ */
public static void fd(String msg, Object... object) { public static void fd(String msg, Object... object) {
if (fullDebug) { if (fullDebug) {
@ -177,7 +177,7 @@ public class Log {
* 完全调试消息 * 完全调试消息
* *
* @param e * @param e
* 异常 * 异常
*/ */
public static void fd(Throwable e) { public static void fd(Throwable e) {
if (fullDebug) { if (fullDebug) {
@ -189,9 +189,9 @@ public class Log {
* 完全调试消息 * 完全调试消息
* *
* @param msg * @param msg
* 消息 * 消息
* @param e * @param e
* 异常 * 异常
*/ */
public static void fd(String msg, Throwable e) { public static void fd(String msg, Throwable e) {
if (fullDebug) { if (fullDebug) {
@ -224,9 +224,9 @@ public class Log {
* <p> * <p>
* *
* @param level * @param level
* One of the message level identifiers, e.g., SEVERE * One of the message level identifiers, e.g., SEVERE
* @param msg * @param msg
* The string message (or a key in the message catalog) * The string message (or a key in the message catalog)
*/ */
public static void log(Level level, String msg) { public static void log(Level level, String msg) {
logger.log(level, msg); logger.log(level, msg);
@ -241,11 +241,11 @@ public class Log {
* <p> * <p>
* *
* @param level * @param level
* One of the message level identifiers, e.g., SEVERE * One of the message level identifiers, e.g., SEVERE
* @param msg * @param msg
* The string message (or a key in the message catalog) * The string message (or a key in the message catalog)
* @param param1 * @param param1
* parameter to the message * parameter to the message
*/ */
public static void log(Level level, String msg, Object param1) { public static void log(Level level, String msg, Object param1) {
logger.log(level, msg, param1); logger.log(level, msg, param1);
@ -260,11 +260,11 @@ public class Log {
* <p> * <p>
* *
* @param level * @param level
* One of the message level identifiers, e.g., SEVERE * One of the message level identifiers, e.g., SEVERE
* @param msg * @param msg
* The string message (or a key in the message catalog) * The string message (or a key in the message catalog)
* @param params * @param params
* array of parameters to the message * array of parameters to the message
*/ */
public static void log(Level level, String msg, Object[] params) { public static void log(Level level, String msg, Object[] params) {
logger.log(level, msg, params); logger.log(level, msg, params);
@ -284,11 +284,11 @@ public class Log {
* <p> * <p>
* *
* @param level * @param level
* One of the message level identifiers, e.g., SEVERE * One of the message level identifiers, e.g., SEVERE
* @param msg * @param msg
* The string message (or a key in the message catalog) * The string message (or a key in the message catalog)
* @param thrown * @param thrown
* Throwable associated with log message. * Throwable associated with log message.
*/ */
public static void log(Level level, String msg, Throwable thrown) { public static void log(Level level, String msg, Throwable thrown) {
logger.log(level, msg, thrown); logger.log(level, msg, thrown);
@ -296,7 +296,7 @@ public class Log {
/** /**
* @param prefix * @param prefix
* 插件前缀 * 插件前缀
*/ */
public static void setPrefix(String prefix) { public static void setPrefix(String prefix) {
Log.prefix = ChatColor.translateAlternateColorCodes('&', prefix); Log.prefix = ChatColor.translateAlternateColorCodes('&', prefix);
@ -309,7 +309,7 @@ public class Log {
* given message is forwarded to all the registered output Handler objects. * given message is forwarded to all the registered output Handler objects.
* *
* @param msg * @param msg
* The string message (or a key in the message catalog) * The string message (or a key in the message catalog)
*/ */
public static void severe(String msg) { public static void severe(String msg) {
logger.severe(msg); logger.severe(msg);
@ -319,9 +319,9 @@ public class Log {
* Sends this sender a message * Sends this sender a message
* *
* @param sender * @param sender
* 命令发送者 * 命令发送者
* @param msg * @param msg
* 消息 * 消息
*/ */
public static void sender(CommandSender sender, String msg) { public static void sender(CommandSender sender, String msg) {
sender.sendMessage(prefix + msg); sender.sendMessage(prefix + msg);
@ -331,11 +331,11 @@ public class Log {
* Sends this sender a message * Sends this sender a message
* *
* @param sender * @param sender
* 命令发送者 * 命令发送者
* @param msg * @param msg
* 消息 * 消息
* @param objs * @param objs
* 参数 * 参数
*/ */
public static void sender(CommandSender sender, String msg, Object... objs) { public static void sender(CommandSender sender, String msg, Object... objs) {
sender.sendMessage(prefix + String.format(msg, objs)); sender.sendMessage(prefix + String.format(msg, objs));
@ -345,9 +345,9 @@ public class Log {
* Sends this sender a message * Sends this sender a message
* *
* @param sender * @param sender
* 命令发送者 * 命令发送者
* @param msg * @param msg
* 消息 * 消息
*/ */
public static void sender(CommandSender sender, String[] msg) { public static void sender(CommandSender sender, String[] msg) {
Arrays.stream(msg).forEach(str -> sender(sender, str)); Arrays.stream(msg).forEach(str -> sender(sender, str));
@ -357,7 +357,7 @@ public class Log {
* 格式化警告消息 * 格式化警告消息
* *
* @param string * @param string
* 消息 * 消息
*/ */
public static void w(String string) { public static void w(String string) {
logger.warning(string); logger.warning(string);
@ -367,14 +367,21 @@ public class Log {
* 格式化警告消息 * 格式化警告消息
* *
* @param string * @param string
* 消息 * 消息
* @param objects * @param objects
* 参数 * 参数
*/ */
public static void w(String string, Object... objects) { public static void w(String string, Object... objects) {
w(String.format(string, objects)); w(String.format(string, objects));
} }
/**
* @return 是否为调试模式
*/
public static boolean isDebug() {
return debug;
}
/** /**
* @return 是否为全局调试模式 * @return 是否为全局调试模式
*/ */
@ -382,23 +389,20 @@ public class Log {
return globalDebug; return globalDebug;
} }
public static String osn(List<?> classes) { /**
* 打印对象简易名称
*
* @param objects
* 对象
* @return
*/
public static String getSimpleNames(Object... objects) {
StringBuilder str = new StringBuilder("["); StringBuilder str = new StringBuilder("[");
classes.forEach(c -> str.append(c == null ? null : c.getClass().getSimpleName()).append(", ")); Arrays.stream(objects)
return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]"; .forEach(o -> str.append(Optional.ofNullable(o)
} .map(obj -> obj instanceof Class ? (Class) obj : obj.getClass())
.map(Class::getSimpleName)
public static String osn(Object... classes) { .orElse(null)).append(", "));
return osn(Arrays.asList(classes)); return objects.length == 0 ? "[]" : str.substring(0, str.length() - 2) + "]";
}
public static String csn(List<Class> classes) {
StringBuilder str = new StringBuilder("[");
classes.forEach(c -> str.append(c == null ? null : c.getSimpleName()).append(", "));
return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]";
}
public static String csn(Class[] classes) {
return csn(Arrays.asList(classes));
} }
} }

View File

@ -7,18 +7,21 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Arrays;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
/** /**
* 插件Instance获取类 * 插件Instance获取类
* *
* @since 2016年7月23日 上午9:09:57
* @author * @author
* @since 2016年7月23日 上午9:09:57
*/ */
public class P { public class P {
/** /**
@ -48,8 +51,7 @@ public class P {
/** /**
* @param name * @param name
* 命令名称 * 命令名称
*
* @return 插件命令 * @return 插件命令
*/ */
public static PluginCommand getCommand(String name) { public static PluginCommand getCommand(String name) {
@ -58,7 +60,7 @@ public class P {
/** /**
* @param <FC> * @param <FC>
* 配置源类型 * 配置源类型
* @return 获得插件配置文件 * @return 获得插件配置文件
*/ */
public static <FC> FC getConfig() { public static <FC> FC getConfig() {
@ -67,7 +69,7 @@ public class P {
/** /**
* @param <FC> * @param <FC>
* 注入配置源类型 * 注入配置源类型
* @return 获得插件注入配置 * @return 获得插件注入配置
*/ */
public static <FC> FC getInjectConfig() { public static <FC> FC getInjectConfig() {
@ -108,7 +110,7 @@ public class P {
/** /**
* @param <PI> * @param <PI>
* 插件源类型 * 插件源类型
* @return 获得插件 * @return 获得插件
*/ */
public static <PI> PI getPlugin() { public static <PI> PI getPlugin() {
@ -122,11 +124,21 @@ public class P {
return instance.isEnabled(); return instance.isEnabled();
} }
/**
* 批量注册监听器
*
* @param listeners
* 监听器
*/
public static void registerEvents(Listener... listeners) {
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, instance));
}
/** /**
* 保存文件 * 保存文件
* *
* @param dirs * @param dirs
* 目录 * 目录
*/ */
public static void saveFile(final String... dirs) { public static void saveFile(final String... dirs) {
saveFile(false, dirs); saveFile(false, dirs);
@ -134,11 +146,11 @@ public class P {
/** /**
* 保存文件 * 保存文件
* *
* @param replace * @param replace
* 是否替换 * 是否替换
* @param dirs * @param dirs
* 目录 * 目录
*/ */
public static void saveFile(boolean replace, final String... dirs) { public static void saveFile(boolean replace, final String... dirs) {
URL url = instance.getClass().getClassLoader().getResource("plugin.yml"); URL url = instance.getClass().getClassLoader().getResource("plugin.yml");

View File

@ -21,7 +21,7 @@ import pw.yumc.YumCore.commands.interfaces.HelpGenerator;
/** /**
* 主类命令管理 * 主类命令管理
* *
* @author * @author
* @since 2016/11/18 0018 * @since 2016/11/18 0018
*/ */
@ -46,9 +46,9 @@ public class CommandMain implements CommandExecutor {
/** /**
* 主类命令管理类 * 主类命令管理类
* *
* @param clazzs * @param clazzs
* 命令类 * 命令类
*/ */
public CommandMain(Executor... clazzs) { public CommandMain(Executor... clazzs) {
register(clazzs); register(clazzs);
@ -56,12 +56,12 @@ public class CommandMain implements CommandExecutor {
/** /**
* 注册命令 * 注册命令
* *
* @param clazzs * @param clazzs
* 命令类 * 命令类
* @return {@link CommandMain} * @return {@link CommandMain}
*/ */
public CommandMain register(Executor... clazzs) { public void register(Executor... clazzs) {
for (Executor clazz : clazzs) { for (Executor clazz : clazzs) {
Method[] methods = clazz.getClass().getDeclaredMethods(); Method[] methods = clazz.getClass().getDeclaredMethods();
for (Method method : methods) { for (Method method : methods) {
@ -70,30 +70,29 @@ public class CommandMain implements CommandExecutor {
} }
help = new CommandHelp(cmds); help = new CommandHelp(cmds);
help.setHelpGenerator(helpGenerator); help.setHelpGenerator(helpGenerator);
return this;
} }
private boolean registerCommand(Method method, Executor clazz) { private void registerCommand(Method method, Executor clazz) {
CommandInfo ci = CommandInfo.parse(method, clazz); CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) { if (ci != null) {
injectPluginCommand(ci); injectPluginCommand(ci);
Class[] params = method.getParameterTypes(); Class[] params = method.getParameterTypes();
Log.d("注册主命令 %s 参数类型: %s", ci.getName(), Log.csn(params)); Log.d("注册主命令 %s 参数类型: %s", ci.getName(), Log.getSimpleNames((Object[]) params));
try { try {
Class<? extends CommandSender> sender = params[0]; Class<? extends CommandSender> sender = params[0];
cmds.add(ci); cmds.add(ci);
return true;
} catch (ArrayIndexOutOfBoundsException | ClassCastException ignored) { } catch (ArrayIndexOutOfBoundsException | ClassCastException ignored) {
Log.w(argumentTypeError, method.getName(), clazz.getClass().getName());
} }
Log.w(argumentTypeError, method.getName(), clazz.getClass().getName());
} }
return false;
} }
private void injectPluginCommand(CommandInfo ci) { private void injectPluginCommand(CommandInfo ci) {
PluginCommand cmd = P.getCommand(ci.getName()); PluginCommand cmd = P.getCommand(ci.getName());
if (cmd == null) { if (cmd == null) {
if ((cmd = CommandKit.create(ci.getName(), ci.getAliases().toArray(new String[] {}))) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + ci.getName() + " 命令!"); } if ((cmd = CommandKit.create(ci.getName(), ci.getAliases().toArray(new String[]{}))) == null) {
throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + ci.getName() + " 命令!");
}
} }
cmd.setExecutor(this); cmd.setExecutor(this);
} }
@ -102,7 +101,7 @@ public class CommandMain implements CommandExecutor {
* 检查缓存并获得命令 * 检查缓存并获得命令
* *
* @param cmd * @param cmd
* 子命令 * 子命令
* @return 命令信息 * @return 命令信息
*/ */
private CommandInfo getByCache(String cmd) { private CommandInfo getByCache(String cmd) {

View File

@ -29,6 +29,7 @@ public class CommandParse {
private static Map<String, Class> primitiveMap = new HashMap<>(); private static Map<String, Class> primitiveMap = new HashMap<>();
private boolean isMain; private boolean isMain;
private List<Parse> parses = new LinkedList<>(); private List<Parse> parses = new LinkedList<>();
static { static {
register(File.class, FileParse.class); register(File.class, FileParse.class);
register(Player.class, PlayerParse.class); register(Player.class, PlayerParse.class);
@ -68,7 +69,7 @@ public class CommandParse {
if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); } if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); }
this.parses.add(parse.parseAnnotation(annotations).handleAttrs()); this.parses.add(parse.parseAnnotation(annotations).handleAttrs());
} }
Log.d("命令解析器 %s", Log.osn(parses)); Log.d("命令解析器 %s", Log.getSimpleNames(parses));
} }
public static CommandParse get(Method method) { public static CommandParse get(Method method) {
@ -79,9 +80,9 @@ public class CommandParse {
* 转化数组为字符串 * 转化数组为字符串
* *
* @param arr * @param arr
* 数组 * 数组
* @param split * @param split
* 分割符 * 分割符
* @return 字符串 * @return 字符串
*/ */
public static String join(Object[] arr, String split) { public static String join(Object[] arr, String split) {
@ -114,7 +115,7 @@ public class CommandParse {
throw new ParseException(String.format("第 %s 个参数 %s", isMain ? 1 : 2 + i, e.getMessage())); throw new ParseException(String.format("第 %s 个参数 %s", isMain ? 1 : 2 + i, e.getMessage()));
} }
} }
Log.d("解析参数: %s => %s", Arrays.toString(args), Log.osn(pobjs)); Log.d("解析参数: %s => %s", Arrays.toString(args), Log.getSimpleNames(pobjs));
return pobjs.toArray(); return pobjs.toArray();
} }

View File

@ -74,7 +74,7 @@ public class CommandSub implements TabExecutor {
* 命令管理器 * 命令管理器
* *
* @param name * @param name
* 注册的命令 * 注册的命令
*/ */
public CommandSub(String name) { public CommandSub(String name) {
cmd = plugin.getCommand(name); cmd = plugin.getCommand(name);
@ -90,9 +90,9 @@ public class CommandSub implements TabExecutor {
* 命令管理器 * 命令管理器
* *
* @param name * @param name
* 注册的命令 * 注册的命令
* @param executor * @param executor
* 命令执行类 * 命令执行类
*/ */
public CommandSub(String name, Executor... executor) { public CommandSub(String name, Executor... executor) {
this(name); this(name);
@ -115,7 +115,7 @@ public class CommandSub implements TabExecutor {
* 检查缓存并获得命令 * 检查缓存并获得命令
* *
* @param subcmd * @param subcmd
* 子命令 * 子命令
* @return 命令信息 * @return 命令信息
*/ */
private CommandInfo getByCache(String subcmd) { private CommandInfo getByCache(String subcmd) {
@ -137,21 +137,25 @@ public class CommandSub implements TabExecutor {
* 获取玩家命令补全 * 获取玩家命令补全
* *
* @param sender * @param sender
* 命令发送者 * 命令发送者
* @param command * @param command
* 命令 * 命令
* @param alias * @param alias
* 别名 * 别名
* @param args * @param args
* 数组 * 数组
* @return 在线玩家数组 * @return 在线玩家数组
*/ */
private List<String> getPlayerTabComplete(CommandSender sender, Command command, String alias, String[] args) { private List<String> getPlayerTabComplete(CommandSender sender, Command command, String alias, String[] args) {
String lastWord = args[args.length - 1]; String lastWord = args[args.length - 1];
Player senderPlayer = sender instanceof Player ? (Player) sender : null; Player senderPlayer = sender instanceof Player ? (Player) sender : null;
List<String> matchedPlayers = new ArrayList<>(); List<String> matchedPlayers = new ArrayList<>();
C.Player.getOnlinePlayers().stream().filter(player -> (senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(player.getName(), lastWord)).forEach( C.Player.getOnlinePlayers()
player -> matchedPlayers.add(player.getName())); .stream()
.filter(player -> (senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(player.getName(),
lastWord))
.forEach(
player -> matchedPlayers.add(player.getName()));
return matchedPlayers; return matchedPlayers;
} }
@ -159,7 +163,7 @@ public class CommandSub implements TabExecutor {
* 转移数组 * 转移数组
* *
* @param args * @param args
* 原数组 * 原数组
* @return 转移后的数组字符串 * @return 转移后的数组字符串
*/ */
private String[] moveStrings(String[] args) { private String[] moveStrings(String[] args) {
@ -203,10 +207,10 @@ public class CommandSub implements TabExecutor {
* 通过注解读取命令并注册 * 通过注解读取命令并注册
* *
* @param clazzs * @param clazzs
* 子命令处理类 * 子命令处理类
* @return {@link CommandSub} * @return {@link CommandSub}
*/ */
public CommandSub register(Executor... clazzs) { public void register(Executor... clazzs) {
for (Executor clazz : clazzs) { for (Executor clazz : clazzs) {
Log.d("解析执行类: %s", clazz.getClass().getName()); Log.d("解析执行类: %s", clazz.getClass().getName());
Method[] methods = clazz.getClass().getDeclaredMethods(); Method[] methods = clazz.getClass().getDeclaredMethods();
@ -219,23 +223,22 @@ public class CommandSub implements TabExecutor {
} }
help = new CommandHelp(defCmd, cmds); help = new CommandHelp(defCmd, cmds);
buildCmdNameCache(); buildCmdNameCache();
return this;
} }
/** /**
* 注册命令 * 注册命令
* *
* @param method * @param method
* 方法 * 方法
* @param clazz * @param clazz
* 调用对象 * 调用对象
* @return 是否成功 * @return 是否成功
*/ */
private boolean registerCommand(Method method, Executor clazz) { private boolean registerCommand(Method method, Executor clazz) {
CommandInfo ci = CommandInfo.parse(method, clazz); CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) { if (ci != null) {
Class[] params = method.getParameterTypes(); Class[] params = method.getParameterTypes();
Log.d("注册子命令: %s 参数类型: %s", ci.getName(), Log.csn(params)); Log.d("注册子命令: %s 参数类型: %s", ci.getName(), Log.getSimpleNames((Object[]) params));
try { try {
Class<? extends CommandSender> sender = params[0]; Class<? extends CommandSender> sender = params[0];
// 用于消除unuse警告 // 用于消除unuse警告
@ -257,29 +260,28 @@ public class CommandSub implements TabExecutor {
* 注册Tab补全 * 注册Tab补全
* *
* @param method * @param method
* 方法 * 方法
* @param clazz * @param clazz
* 调用对象 * 调用对象
* @return 是否成功 * @return 是否成功
*/ */
private boolean registerTab(Method method, Executor clazz) { private void registerTab(Method method, Executor clazz) {
CommandTabInfo ti = CommandTabInfo.parse(method, clazz); CommandTabInfo ti = CommandTabInfo.parse(method, clazz);
if (ti != null) { if (ti != null) {
if (method.getReturnType().equals(List.class)) { if (method.getReturnType().equals(List.class)) {
Log.d("注册子命令补全: %s ", method.getName()); Log.d("注册子命令补全: %s ", method.getName());
tabs.add(ti); tabs.add(ti);
return true; } else {
Log.w(returnTypeError, method.getName(), clazz.getClass().getName());
} }
Log.w(returnTypeError, method.getName(), clazz.getClass().getName());
} }
return false;
} }
/** /**
* 设置命令错误处理器 * 设置命令错误处理器
* *
* @param commandErrorHanlder * @param commandErrorHanlder
* 命令错误处理器 * 命令错误处理器
* @return {@link CommandSub} * @return {@link CommandSub}
*/ */
public CommandSub setCommandErrorHanlder(ErrorHanlder commandErrorHanlder) { public CommandSub setCommandErrorHanlder(ErrorHanlder commandErrorHanlder) {
@ -291,7 +293,7 @@ public class CommandSub implements TabExecutor {
* 设置帮助生成器 * 设置帮助生成器
* *
* @param helpGenerator * @param helpGenerator
* 帮助生成器 * 帮助生成器
* @return {@link CommandSub} * @return {@link CommandSub}
*/ */
public CommandSub setHelpGenerator(HelpGenerator helpGenerator) { public CommandSub setHelpGenerator(HelpGenerator helpGenerator) {
@ -303,7 +305,7 @@ public class CommandSub implements TabExecutor {
* 设置帮助解析器 * 设置帮助解析器
* *
* @param helpParse * @param helpParse
* 帮助解析器 * 帮助解析器
* @return {@link CommandSub} * @return {@link CommandSub}
*/ */
public CommandSub setHelpParse(HelpParse helpParse) { public CommandSub setHelpParse(HelpParse helpParse) {

View File

@ -3,7 +3,6 @@ package pw.yumc.YumCore.update;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
@ -30,31 +29,21 @@ import org.w3c.dom.Document;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.tellraw.Tellraw; import pw.yumc.YumCore.tellraw.Tellraw;
import pw.yumc.YumCore.text.Encrypt; import pw.yumc.YumCore.text.Encrypt;
/** /**
* 自动更新程序 * 自动更新程序
* *
* @since 2016年8月3日 上午11:20:21
* @author * @author
* @since 2016年8月3日 上午11:20:21
*/ */
public class SubscribeTask implements Runnable, Listener { public class SubscribeTask implements Runnable, Listener {
/** /**
* 插件实例 * 插件实例
*/ */
private static JavaPlugin instance; private static JavaPlugin instance = P.instance;
static {
try {
Object pluginClassLoader = SubscribeTask.class.getClassLoader();
Field field = pluginClassLoader.getClass().getDeclaredField("plugin");
field.setAccessible(true);
instance = (JavaPlugin) field.get(pluginClassLoader);
} catch (Exception e) {
Log.d(e);
}
}
/** /**
* 检查间隔 * 检查间隔
@ -82,7 +71,7 @@ public class SubscribeTask implements Runnable, Listener {
* 自动更新 * 自动更新
* *
* @param type * @param type
* 是否为Maven * 是否为Maven
*/ */
public SubscribeTask(UpdateType type) { public SubscribeTask(UpdateType type) {
this(false, type); this(false, type);
@ -92,9 +81,9 @@ public class SubscribeTask implements Runnable, Listener {
* 自动更新 * 自动更新
* *
* @param isSecret * @param isSecret
* 是否为私有 * 是否为私有
* @param type * @param type
* 更新类型 * 更新类型
*/ */
public SubscribeTask(boolean isSecret, UpdateType type) { public SubscribeTask(boolean isSecret, UpdateType type) {
this("master", isSecret, type); this("master", isSecret, type);
@ -104,13 +93,14 @@ public class SubscribeTask implements Runnable, Listener {
* 自动更新 * 自动更新
* *
* @param branch * @param branch
* 更新分支 * 更新分支
* @param isSecret * @param isSecret
* 是否为私有 * 是否为私有
* @param type * @param type
* 更新类型 * 更新类型
*/ */
public SubscribeTask(String branch, boolean isSecret, UpdateType type) { public SubscribeTask(String branch, boolean isSecret, UpdateType type) {
Log.d("订阅更新 分支 %s 是否加密 %s 更新类型 %s", branch, isSecret, type.name());
updateFile = new UpdateFile(instance); updateFile = new UpdateFile(instance);
versionInfo = new VersionInfo(instance, branch, isSecret); versionInfo = new VersionInfo(instance, branch, isSecret);
updateType = type; updateType = type;
@ -180,11 +170,11 @@ public class SubscribeTask implements Runnable, Listener {
public String getDownloadUrl(Plugin instance, String version) { public String getDownloadUrl(Plugin instance, String version) {
switch (this) { switch (this) {
case DIRECT: case DIRECT:
case WS: case WS:
return String.format(url, instance.getName()); return String.format(url, instance.getName());
case MAVEN: case MAVEN:
return String.format(url, instance.getClass().getPackage().getName().replaceAll("\\.", "/"), version, instance.getName()); return String.format(url, instance.getClass().getPackage().getName().replaceAll("\\.", "/"), version, instance.getName());
} }
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -216,7 +206,7 @@ public class SubscribeTask implements Runnable, Listener {
* 获得插件绝对路径 * 获得插件绝对路径
* *
* @param plugin * @param plugin
* - 插件 * - 插件
* @return 插件的绝对路径 * @return 插件的绝对路径
*/ */
public File getPluginFile(Plugin plugin) { public File getPluginFile(Plugin plugin) {
@ -287,11 +277,11 @@ public class SubscribeTask implements Runnable, Listener {
/** /**
* 获得插件信息 * 获得插件信息
* *
* @param tag * @param tag
* 数据标签 * 数据标签
* @param def * @param def
* 默认值 * 默认值
* @return 信息 * @return 信息
*/ */
public String getPluginInfo(String tag, String def) { public String getPluginInfo(String tag, String def) {
@ -313,11 +303,11 @@ public class SubscribeTask implements Runnable, Listener {
*/ */
public String[] getUpdateChanges() { public String[] getUpdateChanges() {
final String des = getPluginInfo("update.changes", null); final String des = getPluginInfo("update.changes", null);
if (des == null) { return new String[] {}; } if (des == null) { return new String[]{}; }
String[] temp = ChatColor.translateAlternateColorCodes('&', des).replaceAll("\n", "").replaceAll("\u0009", "").split(";"); String[] temp = ChatColor.translateAlternateColorCodes('&', des).replaceAll("\n", "").replaceAll("\u0009", "").split(";");
List<String> ltemp = new ArrayList<>(); List<String> ltemp = new ArrayList<>();
Arrays.stream(temp).forEach(s -> ltemp.add(s.trim())); Arrays.stream(temp).forEach(s -> ltemp.add(s.trim()));
return ltemp.toArray(new String[] {}); return ltemp.toArray(new String[]{});
} }
/** /**
@ -341,9 +331,9 @@ public class SubscribeTask implements Runnable, Listener {
/** /**
* 通知更新信息 * 通知更新信息
* *
* @param sender * @param sender
* 命令接受者 * 命令接受者
*/ */
public void notify(CommandSender sender) { public void notify(CommandSender sender) {
Log.sender(sender, "§a插件更新: §b" + name + " §a已更新到最新版本 §bv" + getLastestVersion()); Log.sender(sender, "§a插件更新: §b" + name + " §a已更新到最新版本 §bv" + getLastestVersion());
@ -367,9 +357,9 @@ public class SubscribeTask implements Runnable, Listener {
* 比较版本号 * 比较版本号
* *
* @param v1 * @param v1
* 新版本 * 新版本
* @param v2 * @param v2
* 旧版本 * 旧版本
* @return 是否需要更新 * @return 是否需要更新
*/ */
public boolean needUpdate(String v1, String v2) { public boolean needUpdate(String v1, String v2) {
@ -379,7 +369,7 @@ public class SubscribeTask implements Runnable, Listener {
int minLength = Math.min(va1.length, va2.length);// 取最小长度值 int minLength = Math.min(va1.length, va2.length);// 取最小长度值
int diff = 0; int diff = 0;
while (idx < minLength && (diff = va1[idx].length() - va2[idx].length()) == 0// 先比较长度 while (idx < minLength && (diff = va1[idx].length() - va2[idx].length()) == 0// 先比较长度
&& (diff = va1[idx].compareTo(va2[idx])) == 0) {// 再比较字符 && (diff = va1[idx].compareTo(va2[idx])) == 0) {// 再比较字符
++idx; ++idx;
} }
// 如果已经分出大小 则直接返回 如果未分出大小 则再比较位数 有子版本的为大 // 如果已经分出大小 则直接返回 如果未分出大小 则再比较位数 有子版本的为大
@ -390,12 +380,11 @@ public class SubscribeTask implements Runnable, Listener {
public String getNewVersion() { public String getNewVersion() {
try { try {
String result = getLastestVersion(); String result = getLastestVersion();
if (version.contains("DEV") && !Log.isGlobalDebug()) { if (Log.isDebug() && !Log.isGlobalDebug()) {
Log.console("§4注意: §c当前版本为开发版本 且未开启全局调试 已自动下载最新稳定版!"); Log.console("§4注意: §c当前版本为开发版本 且未开启全局调试 已自动下载最新稳定版!");
return result; return result;
} }
String current = version.split("-")[0]; if (needUpdate(result, version)) { return result; }
if (needUpdate(result, current)) { return result; }
} catch (Exception e) { } catch (Exception e) {
Log.d(e); Log.d(e);
} }

View File

@ -0,0 +1,17 @@
package pw.yumc.YumCore.bukkit;
import org.junit.Assert;
import org.junit.Test;
/**
* Created with IntelliJ IDEA
*
* @author
* Created on 2017/8/22 15:16.
*/
public class LogTest {
@Test
public void testSimpleNames() {
Assert.assertTrue("[LogTest, LogTest, null]".equals(Log.getSimpleNames(this, this.getClass(), null)));
}
}