diff --git a/src/main/gg/frog/mc/permissionstime/PluginMain.java b/src/main/gg/frog/mc/permissionstime/PluginMain.java index 7bc7d0d..c916888 100644 --- a/src/main/gg/frog/mc/permissionstime/PluginMain.java +++ b/src/main/gg/frog/mc/permissionstime/PluginMain.java @@ -1,9 +1,12 @@ package gg.frog.mc.permissionstime; import java.util.Locale; +import java.util.UUID; import java.util.logging.Logger; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.mcstats.Metrics; @@ -18,16 +21,16 @@ 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 String PLUGIN_NAME; + public String PLUGIN_VERSION; + public String PLUGIN_NAME_LOWER_CASE; public static final String DEPEND_PLUGIN = "SQLibrary,Vault"; public static Logger LOG = Logger.getLogger("Minecraft"); - public static PluginMain pm = null; - public static ConfigManager cm = null; - public static SqlManager sm = null; - public static Permission permission = null; + private ConfigManager cm = null; + private PluginMain pm = null; + private SqlManager sm = null; + private Permission permission = null; public PluginMain() { PLUGIN_NAME = getDescription().getName(); @@ -39,23 +42,23 @@ public class PluginMain extends JavaPlugin { public void onEnable() { super.onEnable(); pm = this; - cm = new ConfigManager(); + cm = new ConfigManager(pm); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "===============================")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX)); - getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " " + PluginMain.PLUGIN_NAME + " v" + PluginMain.PLUGIN_VERSION)); + getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " " + PLUGIN_NAME + " v" + PLUGIN_VERSION)); 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)); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "===============================")); if (PluginCfg.IS_METRICS) { try { - Metrics metrics = new Metrics(this); + Metrics metrics = new Metrics(pm); metrics.start(); } catch (Exception e) { e.printStackTrace(); } } - getServer().getScheduler().runTask(this, new Runnable() { + getServer().getScheduler().runTask(pm, new Runnable() { public void run() { if (!checkPluginDepends()) { getServer().getPluginManager().disablePlugin(pm); @@ -67,16 +70,12 @@ public class PluginMain extends JavaPlugin { }); } - public static PluginMain getInstance() { - return pm; - } - /** * 注册监听器
* 这里可以注册多个 */ private void registerListeners() { - this.getServer().getPluginManager().registerEvents(new TheListener(), this); + pm.getServer().getPluginManager().registerEvents(new TheListener(pm), pm); } /** @@ -84,13 +83,17 @@ public class PluginMain extends JavaPlugin { * 这里可以注册多个,一般注册一个就够用 */ private void registerCommands() { - this.getCommand(PLUGIN_NAME_LOWER_CASE).setExecutor(new MainCommand()); + pm.getCommand(PLUGIN_NAME_LOWER_CASE).setExecutor(new MainCommand(pm)); } public ConfigManager getConfigManager() { return cm; } + public SqlManager getSqlManager() { + return sm; + } + private boolean checkPluginDepends() { boolean needDepend = false; for (String name : DEPEND_PLUGIN.split(",")) { @@ -99,12 +102,12 @@ public class PluginMain extends JavaPlugin { needDepend = true; } } - if (!setupPermissions()) { - getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann't hook vault permission")); + if (!needDepend && !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")); + if (!needDepend && !setupDatabase()) { + getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann''t setup database")); needDepend = true; } if (needDepend) { @@ -116,9 +119,12 @@ public class PluginMain extends JavaPlugin { @Override public void onDisable() { super.onDisable(); - getServer().getServicesManager().unregisterAll(this); - Bukkit.getScheduler().cancelTasks(this); - SqlManager.getDb().close(); + getServer().getServicesManager().unregisterAll(pm); + Bukkit.getScheduler().cancelTasks(pm); + try { + sm.getDb().close(); + } catch (Exception e) { + } } private boolean setupPermissions() { @@ -130,7 +136,21 @@ public class PluginMain extends JavaPlugin { } private boolean setupDatabase() { - sm = SqlManager.getInstance(); + sm = new SqlManager(pm); return sm.updateDatabase(); } + + public UUID getPlayerUUIDByName(String name) { + for (Player p : getServer().getOnlinePlayers()) { + if (p.getName().equals(name)) { + return p.getUniqueId(); + } + } + for (OfflinePlayer p : getServer().getOfflinePlayers()) { + if (p.getName().equals(name)) { + return p.getUniqueId(); + } + } + return null; + } } diff --git a/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java b/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java index a1a220d..473e5ed 100644 --- a/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java +++ b/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java @@ -9,14 +9,19 @@ import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; -import gg.frog.mc.permissionstime.utils.PluginUtil; import gg.frog.mc.permissionstime.utils.StrUtil; public class GiveCmd { - private static SqlManager sm = PluginMain.sm; + private PluginMain pm; + private SqlManager sm; - public static boolean onCommand(CommandSender sender, boolean isPlayer, String[] args) { + public GiveCmd(PluginMain pm) { + this.pm = pm; + this.sm = pm.getSqlManager(); + } + + public boolean onCommand(CommandSender sender, boolean isPlayer, String[] args) { if (args.length == 4) { String playerName = args[1]; String packageName = args[2]; @@ -34,21 +39,21 @@ public class GiveCmd { } PermissionPackageBean pack = PackagesCfg.PACKAGES.get(packageName); if (pack != null) { - UUID uuid = PluginUtil.getPlayerUUIDByName(playerName); + UUID uuid = pm.getPlayerUUIDByName(playerName); if (uuid != null) { if (PluginCfg.IS_DEBUG) { sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + uuid.toString() + "\n" + pack.toString() + "\n" + time + "天")); } if (sm.giveTime(uuid.toString(), packageName, days)) { - sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "给予玩家{0} {1}天的{2}", playerName, time, pack.getDisplayName())); + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "给予玩家 {0} {1}天的 {2}", playerName, time, pack.getDisplayName())); } else { - sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "未给予玩家{0} {1}天的{2}", playerName, time, pack.getDisplayName())); + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "未给予玩家 {0} {1}天的 {2}", playerName, time, pack.getDisplayName())); } } else { - sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为'{0}'的玩家", playerName)); + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为''{0}''的玩家", playerName)); } } else { - sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为'{0}'的权限包", packageName)); + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为''{0}''的权限包", packageName)); } } else { sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "参数不正确")); diff --git a/src/main/gg/frog/mc/permissionstime/command/MainCommand.java b/src/main/gg/frog/mc/permissionstime/command/MainCommand.java index 0728b9e..a5cfa1d 100644 --- a/src/main/gg/frog/mc/permissionstime/command/MainCommand.java +++ b/src/main/gg/frog/mc/permissionstime/command/MainCommand.java @@ -13,12 +13,21 @@ import gg.frog.mc.permissionstime.utils.StrUtil; public class MainCommand implements CommandExecutor { - private PluginMain pm = PluginMain.getInstance(); - private SqlManager sm = PluginMain.sm; + private PluginMain pm; + private SqlManager sm; + private GiveCmd giveCmd; + private PackagesCmd packagesCmd; + + public MainCommand(PluginMain pm) { + this.pm = pm; + this.sm = pm.getSqlManager(); + giveCmd = new GiveCmd(pm); + packagesCmd = new PackagesCmd(pm); + } @Override public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - if (commandLabel.equalsIgnoreCase(PluginMain.PLUGIN_NAME_LOWER_CASE)) { + if (commandLabel.equalsIgnoreCase(pm.PLUGIN_NAME_LOWER_CASE)) { boolean isPlayer = false; if (sender instanceof Player) { isPlayer = true; @@ -50,23 +59,23 @@ public class MainCommand implements CommandExecutor { return true; } else if (args[0].equalsIgnoreCase("me")) { if (hasPermission(sender, isPlayer, "permissionstime.me")) { - return GiveCmd.onCommand(sender, isPlayer, args); + return giveCmd.onCommand(sender, isPlayer, args); } } else if (args[0].equalsIgnoreCase("give")) { if (hasPermission(sender, isPlayer, "permissionstime.give")) { - return GiveCmd.onCommand(sender, isPlayer, args); + return giveCmd.onCommand(sender, isPlayer, args); } } else if (args[0].equalsIgnoreCase("set")) { if (hasPermission(sender, isPlayer, "permissionstime.set")) { - return GiveCmd.onCommand(sender, isPlayer, args); + return giveCmd.onCommand(sender, isPlayer, args); } } else if (args[0].equalsIgnoreCase("remove")) { if (hasPermission(sender, isPlayer, "permissionstime.remove")) { - return GiveCmd.onCommand(sender, isPlayer, args); + return giveCmd.onCommand(sender, isPlayer, args); } } else if (args[0].equalsIgnoreCase("packages")) { if (hasPermission(sender, isPlayer, "permissionstime.packages")) { - return PackagesCmd.onCommand(sender, isPlayer, args); + return packagesCmd.onCommand(sender, isPlayer, args); } } else { getHelp(sender, isPlayer); @@ -78,21 +87,21 @@ public class MainCommand implements CommandExecutor { } private void getHelp(CommandSender sender, boolean isPlayer) { - sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "&a===== " + PluginMain.PLUGIN_NAME + " Version:" + PluginMain.PLUGIN_VERSION + " =====")); - if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".reload")) { - sender.sendMessage(StrUtil.messageFormat("/" + PluginMain.PLUGIN_NAME_LOWER_CASE + " reload \n\t-Reloads the config file.")); + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "&a===== " + pm.PLUGIN_NAME + " Version:" + pm.PLUGIN_VERSION + " =====")); + if (!isPlayer || sender.isOp() || sender.hasPermission(pm.PLUGIN_NAME_LOWER_CASE + ".reload")) { + sender.sendMessage(StrUtil.messageFormat("/" + pm.PLUGIN_NAME_LOWER_CASE + " reload \n -Reloads the config file.")); } - if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".give")) { - sender.sendMessage(StrUtil.messageFormat("/" + PluginMain.PLUGIN_NAME_LOWER_CASE + " give