From 70f6de05e9521db8e73d929245262c9ac318b148 Mon Sep 17 00:00:00 2001 From: GeekFrog Date: Mon, 10 Jul 2017 20:55:45 +0800 Subject: [PATCH] hook api --- pom.xml | 1 + .../frog/mc/permissionstime/PluginMain.java | 50 ++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index ad9b606..fdc5768 100644 --- a/pom.xml +++ b/pom.xml @@ -112,6 +112,7 @@ maven-shade-plugin 3.0.0 + false org.mcstats.*:* diff --git a/src/main/gg/frog/mc/permissionstime/PluginMain.java b/src/main/gg/frog/mc/permissionstime/PluginMain.java index c60c79b..63fa94e 100644 --- a/src/main/gg/frog/mc/permissionstime/PluginMain.java +++ b/src/main/gg/frog/mc/permissionstime/PluginMain.java @@ -4,6 +4,7 @@ import java.util.Locale; import java.util.logging.Logger; import org.bukkit.Bukkit; +import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.mcstats.Metrics; @@ -12,16 +13,20 @@ import gg.frog.mc.permissionstime.config.ConfigManager; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.listener.TheListener; import gg.frog.mc.permissionstime.utils.StrUtil; +import lib.PatPeter.SQLibrary.SQLite; +import net.milkbowl.vault.permission.Permission; public class PluginMain extends JavaPlugin { public static String PLUGIN_NAME; public static String PLUGIN_VERSION; public static String PLUGIN_NAME_LOWER_CASE; + public static final String DEPEND_PLUGIN = "SQLibrary,Vault"; public static Logger LOG = Logger.getLogger("Minecraft"); - private static PluginMain pm = null; - private ConfigManager cm = null; + public static PluginMain pm = null; + public static ConfigManager cm = null; + public static Permission permission = null; public PluginMain() { PLUGIN_NAME = getDescription().getName(); @@ -40,8 +45,6 @@ public class PluginMain extends JavaPlugin { getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " author:GeekFrog QQ:324747460")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " https://github.com/geekfrog/PermissionsTime/ ")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX)); - registerListeners(); - registerCommands(); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "===============================")); if (PluginCfg.IS_METRICS) { try { @@ -55,6 +58,9 @@ public class PluginMain extends JavaPlugin { public void run() { if (!checkPluginDepends()) { getServer().getPluginManager().disablePlugin(pm); + } else { + registerListeners(); + registerCommands(); } } }, 0L, 432000L); @@ -86,13 +92,21 @@ public class PluginMain extends JavaPlugin { private boolean checkPluginDepends() { boolean needDepend = false; - for(String name : getDescription().getSoftDepend()){ - if(getServer().getPluginManager().getPlugin(name) == null){ + for (String name : DEPEND_PLUGIN.split(",")) { + if (getServer().getPluginManager().getPlugin(name) == null) { getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Need depend plugins : " + name)); needDepend = true; } } - if(needDepend){ + if (!setupPermissions()) { + getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann't hook vault permission")); + needDepend = true; + } + if (!setupDatabase()) { + getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann't setup database")); + needDepend = true; + } + if (needDepend) { return false; } return true; @@ -104,4 +118,26 @@ public class PluginMain extends JavaPlugin { getServer().getServicesManager().unregisterAll(this); Bukkit.getScheduler().cancelTasks(this); } + + private boolean setupPermissions() { + RegisteredServiceProvider permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class); + if (permissionProvider != null) { + permission = permissionProvider.getProvider(); + } + return (permission != null); + } + + private boolean setupDatabase() { + try { + SQLite sql = new SQLite(Logger.getLogger("Minecraft"), "[" + PLUGIN_NAME + "] ", this.getDataFolder().getAbsolutePath(), PLUGIN_NAME, ".sqlite"); + if(!sql.isOpen()){ + sql.open(); + } + sql.close(); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } }