1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-24 02:08:48 +00:00

feat: 添加插件工具集

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-26 20:13:17 +08:00
parent a6f9faffa4
commit 757ad7065e

View File

@ -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<ClassLoader, Plugin> 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());
}
}
}