diff --git a/src/main/java/tk/smileyik/luainminecraftbukkit/LuaInMinecraftBukkit.java b/src/main/java/tk/smileyik/luainminecraftbukkit/LuaInMinecraftBukkit.java index 34824d7..cca6655 100644 --- a/src/main/java/tk/smileyik/luainminecraftbukkit/LuaInMinecraftBukkit.java +++ b/src/main/java/tk/smileyik/luainminecraftbukkit/LuaInMinecraftBukkit.java @@ -110,7 +110,7 @@ private void setupMetrics() { try { Metrics metrics = new Metrics(this, 14952); } catch (Exception e) { - + // 注册bstats失败, 但不影响插件正常功能, 忽略报错结果 } } @@ -153,9 +153,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } else if (args.length == 3) { if (args[0].equalsIgnoreCase("testLoop")) { - getServer().getScheduler().runTaskAsynchronously(this, () -> { - LoopTest.doLoop(sender, args[2], args[1]); - }); + getServer().getScheduler().runTaskAsynchronously(this, () -> + LoopTest.doLoop(sender, args[2], args[1])); return true; } } @@ -174,6 +173,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return super.onCommand(sender, command, label, args); } + /** + * 此方法不会在正常运行中启用. + * @param str 要写入的内容 + * @param fileName 要写入的文件 + */ + @Deprecated public static void writeToFile(String str, String fileName) { File file = new File(instance.getDataFolder(), fileName); file.getParentFile().mkdirs(); diff --git a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/event/EventRegister.java b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/event/EventRegister.java index 7d0696b..760cbfe 100644 --- a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/event/EventRegister.java +++ b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/event/EventRegister.java @@ -10,7 +10,8 @@ import java.util.*; public class EventRegister { - private static final Map>> events = new HashMap<>(); + private static final Map>> events = new HashMap<>(); private static final Properties EVENT_MAPPER = new Properties(); public static final EventPriority[] PRIORITIES = { EventPriority.LOWEST, @@ -22,8 +23,11 @@ public class EventRegister { }; static { + // 加载事件名与全类名映射 try { - EVENT_MAPPER.load(EventRegister.class.getResourceAsStream("/events.properties")); + EVENT_MAPPER.load( + EventRegister.class + .getResourceAsStream("/events.properties")); } catch (IOException e) { throw new RuntimeException(e); } @@ -54,11 +58,19 @@ private void registerEvent(LuaEvent listener, int priority) { } if (handler != null) { - InvocationHandler invocationHandler = Proxy.getInvocationHandler(handler); - Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues"); + InvocationHandler invocationHandler = + Proxy.getInvocationHandler(handler); + Field memberValues = + invocationHandler.getClass() + .getDeclaredField("memberValues"); memberValues.setAccessible(true); - ((Map) memberValues.get(invocationHandler)).put("priority", PRIORITIES[priority]); - LuaInMinecraftBukkit.debug("注册事件优先级: %s; 闭包: %s", handler.priority(), listener.getId()); + ((Map) memberValues.get(invocationHandler)) + .put("priority", PRIORITIES[priority]); + LuaInMinecraftBukkit.debug( + "注册事件优先级: %s; 闭包: %s", + handler.priority(), + listener.getId() + ); } } catch (Exception e) { // 恢复默认优先级 @@ -68,7 +80,8 @@ private void registerEvent(LuaEvent listener, int priority) { events.put(listener.getPluginId(), new ArrayList<>()); } events.get(listener.getPluginId()).add(listener); - Bukkit.getServer().getPluginManager().registerEvents(listener, LuaInMinecraftBukkit.getInstance()); + Bukkit.getServer().getPluginManager().registerEvents( + listener, LuaInMinecraftBukkit.getInstance()); } /** @@ -78,26 +91,27 @@ private void registerEvent(LuaEvent listener, int priority) { * @param priority 优先级, 范围为[0, 5], 不再此区间内则为正常默认等级. */ @Deprecated - public void registerListener(String event, String id, int priority) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + public void registerListener(String event, String id, int priority) { register(event, id, priority); - LuaInMinecraftBukkit.log("eventRegister:registerListener 即将被弃用, 请使用event:register"); + LuaInMinecraftBukkit.log( + "eventRegister:registerListener 即将被弃用, " + + "请使用event:register"); } - private Class getClass(String event) throws ClassNotFoundException, NoSuchMethodException { + private Class getClass(String event) throws ClassNotFoundException { String classPath; if (EVENT_MAPPER.containsKey(event)) { classPath = EVENT_MAPPER.getProperty(event); } else { classPath = event.startsWith(".") ? - ("tk.smileyik.luainminecraftbukkit.bridge.event" + event) : - event; + ("tk.smileyik.luainminecraftbukkit.bridge.event" + event) : event; } return Class.forName(classPath); } /** * 注册监听事件. - * @param event 要注册的事件. + * @param event 要注册的事件名. * @param id 闭包id * @param priority 优先级, 范围为[0, 5], 不再此区间内则为正常默认等级. */ @@ -123,21 +137,21 @@ public void register(String event, String id, int priority) { * @param priority 优先级, 范围为[0, 5], 不再此区间内则为正常默认等级. */ public String register(LuaPlugin plugin, - Object closure, - String event, - int priority) { + Object closure, String event, int priority) { LuaEvent listener = null; try { listener = (LuaEvent) getClass(event) .getDeclaredConstructor(String.class) .newInstance(plugin.getId()); listener.setClosure(closure); - String id = String.format("%s.%s.%s", plugin.getId(), event, listener.getId()); + String id = String.format( + "%s.%s.%s", plugin.getId(), event, listener.getId()); listener.setId(id); registerEvent(listener, priority); return id; } catch (Exception e) { - LuaInMinecraftBukkit.log("注册%s事件失败! id: %s", event, plugin.getId()); + LuaInMinecraftBukkit.log( + "注册%s事件失败! id: %s", event, plugin.getId()); e.printStackTrace(); } return null; diff --git a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/hybrid/LuaPluginHybrid.java b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/hybrid/LuaPluginHybrid.java index f3572cd..708b8da 100644 --- a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/hybrid/LuaPluginHybrid.java +++ b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/hybrid/LuaPluginHybrid.java @@ -25,7 +25,6 @@ protected LuaPluginHybrid(AbstractLuaPlugin luaPlugin, RunType runType) { * * @param module 模块名称 * @return 返回脚本插件require所认可的模块路径. - * @throws IOException */ @Override public String getRequirePath(String module) throws IOException { diff --git a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/inside/LuaPluginManagerInside.java b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/inside/LuaPluginManagerInside.java index 1b133fd..24831f3 100644 --- a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/inside/LuaPluginManagerInside.java +++ b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/inside/LuaPluginManagerInside.java @@ -91,7 +91,7 @@ public boolean loadPlugin(LuaPlugin plugin) { } pluginGlobals.set("self", LuaValueHelper.valueOf(plugin)); // 环境生成完毕 - LuaValue func = null; + LuaValue func; try { func = pluginGlobals.loadfile( new File(plugin.getPluginPath(), PLUGIN_MAIN).getCanonicalPath() diff --git a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/outside/LuaPluginManagerOutside.java b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/outside/LuaPluginManagerOutside.java index bd238f4..701d9d0 100644 --- a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/outside/LuaPluginManagerOutside.java +++ b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/mode/outside/LuaPluginManagerOutside.java @@ -163,7 +163,7 @@ public LuaObject getClosure(String[] vars) { if (g == null) { throw new LuaPluginNotFountException(vars[0]); } - LuaObject obj = null; + LuaObject obj; try { obj = g.getLuaObject(vars[1]); for (int i = 2; i < vars.length; ++i) { diff --git a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/task/TaskRegister.java b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/task/TaskRegister.java index 69914e5..d1a5914 100644 --- a/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/task/TaskRegister.java +++ b/src/main/java/tk/smileyik/luainminecraftbukkit/plugin/task/TaskRegister.java @@ -18,6 +18,12 @@ public TaskRegister() { scheduler = plugin.getServer().getScheduler(); } + /** + * 在服务器下一刻运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTask(LuaPlugin luaPlugin, String functionId) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -26,6 +32,13 @@ public BukkitTask runTask(LuaPlugin luaPlugin, String functionId) { }); } + /** + * 在服务器下一刻运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTask(LuaPlugin luaPlugin, String functionId, Object obj) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -34,6 +47,12 @@ public BukkitTask runTask(LuaPlugin luaPlugin, String functionId, Object obj) { }); } + /** + * 在服务器下一刻运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTask(LuaPlugin luaPlugin, Object function) { if (function instanceof String) { return runTask(luaPlugin, (String) function); @@ -44,6 +63,13 @@ public BukkitTask runTask(LuaPlugin luaPlugin, Object function) { }); } + /** + * 在服务器下一刻运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTask(LuaPlugin luaPlugin, Object function, Object obj) { if (function instanceof String) { return runTask(luaPlugin, (String) function, obj); @@ -54,6 +80,12 @@ public BukkitTask runTask(LuaPlugin luaPlugin, Object function, Object obj) { }); } + /** + * 运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, String functionId) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -62,6 +94,13 @@ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, String functionId) { }); } + /** + * 运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, String functionId, Object obj) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -70,6 +109,12 @@ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, String functionId, Object ob }); } + /** + * 运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, Object function) { if (function instanceof String) { return runTaskAsync(luaPlugin, (String) function); @@ -80,6 +125,13 @@ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, Object function) { }); } + /** + * 运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, Object function, Object obj) { if (function instanceof String) { return runTaskAsync(luaPlugin, (String) function, obj); @@ -90,6 +142,13 @@ public BukkitTask runTaskAsync(LuaPlugin luaPlugin, Object function, Object obj) }); } + /** + * 延迟运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLater(LuaPlugin luaPlugin, String functionId, int delay) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -98,6 +157,14 @@ public BukkitTask runTaskLater(LuaPlugin luaPlugin, String functionId, int delay }, delay); } + /** + * 延迟运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLater(LuaPlugin luaPlugin, String functionId, int delay, Object obj) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -106,6 +173,13 @@ public BukkitTask runTaskLater(LuaPlugin luaPlugin, String functionId, int delay }, delay); } + /** + * 延迟运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLater(LuaPlugin luaPlugin, Object function, int delay) { if (function instanceof String) { return runTaskLater(luaPlugin, (String) function, delay); @@ -116,6 +190,14 @@ public BukkitTask runTaskLater(LuaPlugin luaPlugin, Object function, int delay) }, delay); } + /** + * 延迟运行一个任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLater(LuaPlugin luaPlugin, Object function, int delay, Object obj) { if (function instanceof String) { return runTaskLater(luaPlugin, (String) function, delay, obj); @@ -126,6 +208,13 @@ public BukkitTask runTaskLater(LuaPlugin luaPlugin, Object function, int delay, }, delay); } + /** + * 延迟运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, String functionId, int delay) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -134,6 +223,14 @@ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, String functionId, int }, delay); } + /** + * 延迟运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, String functionId, int delay, Object obj) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -142,6 +239,13 @@ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, String functionId, int }, delay); } + /** + * 延迟运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, Object function, int delay) { if (function instanceof String) { return runTaskLaterAsync(luaPlugin, (String) function, delay); @@ -152,6 +256,14 @@ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, Object function, int de }, delay); } + /** + * 延迟运行一个异步任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, Object function, int delay, Object obj) { if (function instanceof String) { return runTaskLaterAsync(luaPlugin, (String) function, delay, obj); @@ -162,6 +274,14 @@ public BukkitTask runTaskLaterAsync(LuaPlugin luaPlugin, Object function, int de }, delay); } + /** + * 运行一个重复运行的任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimer(LuaPlugin luaPlugin, String functionId, int delay, int period) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -172,6 +292,15 @@ public BukkitTask runTimer(LuaPlugin luaPlugin, String functionId, int delay, in return task; } + /** + * 运行一个重复运行的任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimer(LuaPlugin luaPlugin, String functionId, int delay, int period, Object obj) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -182,6 +311,14 @@ public BukkitTask runTimer(LuaPlugin luaPlugin, String functionId, int delay, in return task; } + /** + * 运行一个重复运行的任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimer(LuaPlugin luaPlugin, Object function, int delay, int period) { if (function instanceof String) { return runTimer(luaPlugin, (String) function, delay, period); @@ -194,6 +331,15 @@ public BukkitTask runTimer(LuaPlugin luaPlugin, Object function, int delay, int return task; } + /** + * 运行一个重复运行的任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimer(LuaPlugin luaPlugin, Object function, int delay, int period, Object obj) { if (function instanceof String) { return runTimer(luaPlugin, (String) function, delay, period, obj); @@ -206,6 +352,14 @@ public BukkitTask runTimer(LuaPlugin luaPlugin, Object function, int delay, int return task; } + /** + * 运行一个重复运行的任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, String functionId, int delay, int period) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -216,6 +370,15 @@ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, String functionId, int dela return task; } + /** + * 运行一个重复运行的异步任务. + * @param luaPlugin 脚本插件实例. + * @param functionId 函数id. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, String functionId, int delay, int period, Object obj) { functionId = String.format("%s.%s", luaPlugin.getId(), functionId); String[] vars = functionId.split("\\."); @@ -226,6 +389,14 @@ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, String functionId, int dela return task; } + /** + * 运行一个重复运行的异步任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, Object function, int delay, int period) { if (function instanceof String) { return runTimerAsync(luaPlugin, (String) function, delay, period); @@ -238,6 +409,15 @@ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, Object function, int delay, return task; } + /** + * 运行一个重复运行的异步任务. + * @param luaPlugin 脚本插件实例. + * @param function 脚本函数. + * @param delay 延迟刻数. + * @param period 重复间隔刻数. + * @param obj 运行时要给函数传入的参数. + * @return 一个包含任务id的BukkitTask. + */ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, Object function, int delay, int period, Object obj) { if (function instanceof String) { return runTimerAsync(luaPlugin, (String) function, delay, period, obj); @@ -250,11 +430,19 @@ public BukkitTask runTimerAsync(LuaPlugin luaPlugin, Object function, int delay, return task; } + /** + * 通过BukkitTask的id去取消一个任务. + * @param id BukkitTask中的id. + */ public void cancel(int id) { scheduler.cancelTask(id); timers.values().forEach(it -> it.remove(id)); } + /** + * 取消一个脚本插件中的所有的任务. + * @param plugin 脚本插件实例. + */ public void cancelAll(LuaPlugin plugin) { cancelAll(plugin.getId()); }