From 757ad7065e32adfe335628fb5926233c2ae1a557 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 26 Sep 2016 20:13:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/kit/PKit.java | 186 ++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 src/main/java/pw/yumc/YumCore/kit/PKit.java diff --git a/src/main/java/pw/yumc/YumCore/kit/PKit.java b/src/main/java/pw/yumc/YumCore/kit/PKit.java new file mode 100644 index 0000000..b9b25fc --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/kit/PKit.java @@ -0,0 +1,186 @@ +package pw.yumc.YumCore.kit; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; + +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.bukkit.P; + +/** + * 插件工具集 + * + * @since 2016年3月31日 下午7:13:25 + * @author 喵♂呜 + */ +public class PKit { + private static final Map pluginMap = new HashMap<>(); + + /** + * 关闭插件 + */ + public static void disable() { + Bukkit.getPluginManager().disablePlugin(P.instance); + } + + /** + * 关闭插件 + * + * @param msg + * 关闭提示 + */ + public static void disable(final String msg) { + Log.warning(msg); + disable(); + } + + /** + * 通过堆栈获取操作插件 + * + * @return 操作插件 + */ + public static Plugin getOperatePlugin() { + return getOperatePlugin(new Exception().getStackTrace()); + } + + /** + * 通过堆栈获取操作插件 + * + * @param stacktrace + * 堆栈 + * @return 操作插件 + */ + public static Plugin getOperatePlugin(final StackTraceElement[] stacktrace) { + collectPlugin(); + for (final StackTraceElement element : stacktrace) { + try { + final ClassLoader loader = Class.forName(element.getClassName(), false, PKit.class.getClassLoader()).getClassLoader(); + if (pluginMap.containsKey(loader)) { + return pluginMap.get(loader); + } + } catch (final ClassNotFoundException ex) { + } + } + return null; + } + + /** + * 运行主线程任务 + * + * @param run + * 任务 + * @return Bukkit 任务 {@link BukkitTask} + */ + public static BukkitTask runTask(final Runnable run) { + return Bukkit.getScheduler().runTask(P.instance, run); + } + + /** + * 运行异步任务 + * + * @param run + * 任务 + * @return Bukkit 任务 {@link BukkitTask} + */ + public static BukkitTask runTaskAsync(final Runnable run) { + return Bukkit.getScheduler().runTaskAsynchronously(P.instance, run); + } + + /** + * 运行主线程延时任务 + * + * @param run + * 任务 + * @param delay + * 延时多久 + * @return Bukkit 任务 {@link BukkitTask} + */ + public static BukkitTask runTaskLater(final Runnable run, final long delay) { + return Bukkit.getScheduler().runTaskLater(P.instance, run, delay); + } + + /** + * 运行异步延时任务 + * + * @param run + * 任务 + * @param delay + * 延时多久 + * @return Bukkit 任务 {@link BukkitTask} + */ + public static BukkitTask runTaskLaterAsync(final Runnable run, final long delay) { + return Bukkit.getScheduler().runTaskLaterAsynchronously(P.instance, run, delay); + } + + /** + * 运行主线程循环任务 + * + * @param run + * 任务 + * @param delay + * 开启延时 + * @param timer + * 任务间隔 + * @return Bukkit 任务 {@link BukkitTask} + */ + public static BukkitTask runTaskTimer(final Runnable run, final long delay, final long timer) { + return Bukkit.getScheduler().runTaskTimer(P.instance, run, delay, timer); + } + + /** + * 运行异步循环任务 + * + * @param run + * 任务 + * @param delay + * 开启延时 + * @param timer + * 任务间隔 + * @return Bukkit 任务 {@link BukkitTask} + */ + public static BukkitTask runTaskTimerAsync(final Runnable run, final long delay, final long timer) { + return Bukkit.getScheduler().runTaskTimerAsynchronously(P.instance, run, delay, timer); + } + + /** + * 安排一个主线程任务 + * + * @param run + * 任务 + * @return 任务ID + */ + public static int scheduleTask(final Runnable run) { + return Bukkit.getScheduler().scheduleSyncDelayedTask(P.instance, run); + } + + /** + * 安排一个主线程循环任务 + * + * @param run + * 任务 + * @param delay + * 开启延时 + * @param timer + * 执行间隔 + * @return 任务ID + */ + public static int scheduleTask(final Runnable run, final long delay, final long timer) { + return Bukkit.getScheduler().scheduleSyncRepeatingTask(P.instance, run, delay, timer); + } + + /** + * 收集插件信息 + */ + private static void collectPlugin() { + if (Bukkit.getPluginManager().getPlugins().length != pluginMap.keySet().size() - 1) { + pluginMap.clear(); + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { + pluginMap.put(plugin.getClass().getClassLoader(), plugin); + } + pluginMap.remove(PKit.class.getClassLoader()); + } + } +}