1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2024-11-22 07:28:47 +00:00
This commit is contained in:
GeekFrog 2017-07-12 07:17:11 +08:00
parent f00284314f
commit e39fad59ef
14 changed files with 157 additions and 128 deletions

View File

@ -1,9 +1,12 @@
package gg.frog.mc.permissionstime; package gg.frog.mc.permissionstime;
import java.util.Locale; import java.util.Locale;
import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics; import org.mcstats.Metrics;
@ -18,16 +21,16 @@ import net.milkbowl.vault.permission.Permission;
public class PluginMain extends JavaPlugin { public class PluginMain extends JavaPlugin {
public static String PLUGIN_NAME; public String PLUGIN_NAME;
public static String PLUGIN_VERSION; public String PLUGIN_VERSION;
public static String PLUGIN_NAME_LOWER_CASE; public String PLUGIN_NAME_LOWER_CASE;
public static final String DEPEND_PLUGIN = "SQLibrary,Vault"; public static final String DEPEND_PLUGIN = "SQLibrary,Vault";
public static Logger LOG = Logger.getLogger("Minecraft"); public static Logger LOG = Logger.getLogger("Minecraft");
public static PluginMain pm = null; private ConfigManager cm = null;
public static ConfigManager cm = null; private PluginMain pm = null;
public static SqlManager sm = null; private SqlManager sm = null;
public static Permission permission = null; private Permission permission = null;
public PluginMain() { public PluginMain() {
PLUGIN_NAME = getDescription().getName(); PLUGIN_NAME = getDescription().getName();
@ -39,23 +42,23 @@ public class PluginMain extends JavaPlugin {
public void onEnable() { public void onEnable() {
super.onEnable(); super.onEnable();
pm = this; 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)); 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 + " authorGeekFrog QQ324747460")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " authorGeekFrog QQ324747460"));
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " https://github.com/geekfrog/PermissionsTime/ ")); 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));
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "===============================")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "==============================="));
if (PluginCfg.IS_METRICS) { if (PluginCfg.IS_METRICS) {
try { try {
Metrics metrics = new Metrics(this); Metrics metrics = new Metrics(pm);
metrics.start(); metrics.start();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
getServer().getScheduler().runTask(this, new Runnable() { getServer().getScheduler().runTask(pm, new Runnable() {
public void run() { public void run() {
if (!checkPluginDepends()) { if (!checkPluginDepends()) {
getServer().getPluginManager().disablePlugin(pm); getServer().getPluginManager().disablePlugin(pm);
@ -67,16 +70,12 @@ public class PluginMain extends JavaPlugin {
}); });
} }
public static PluginMain getInstance() {
return pm;
}
/** /**
* 注册监听器 <br/> * 注册监听器 <br/>
* 这里可以注册多个 * 这里可以注册多个
*/ */
private void registerListeners() { 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() { 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() { public ConfigManager getConfigManager() {
return cm; return cm;
} }
public SqlManager getSqlManager() {
return sm;
}
private boolean checkPluginDepends() { private boolean checkPluginDepends() {
boolean needDepend = false; boolean needDepend = false;
for (String name : DEPEND_PLUGIN.split(",")) { for (String name : DEPEND_PLUGIN.split(",")) {
@ -99,12 +102,12 @@ public class PluginMain extends JavaPlugin {
needDepend = true; needDepend = true;
} }
} }
if (!setupPermissions()) { if (!needDepend && !setupPermissions()) {
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann't hook vault permission")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann''t hook vault permission"));
needDepend = true; needDepend = true;
} }
if (!setupDatabase()) { if (!needDepend && !setupDatabase()) {
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann't setup database")); getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann''t setup database"));
needDepend = true; needDepend = true;
} }
if (needDepend) { if (needDepend) {
@ -116,9 +119,12 @@ public class PluginMain extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
super.onDisable(); super.onDisable();
getServer().getServicesManager().unregisterAll(this); getServer().getServicesManager().unregisterAll(pm);
Bukkit.getScheduler().cancelTasks(this); Bukkit.getScheduler().cancelTasks(pm);
SqlManager.getDb().close(); try {
sm.getDb().close();
} catch (Exception e) {
}
} }
private boolean setupPermissions() { private boolean setupPermissions() {
@ -130,7 +136,21 @@ public class PluginMain extends JavaPlugin {
} }
private boolean setupDatabase() { private boolean setupDatabase() {
sm = SqlManager.getInstance(); sm = new SqlManager(pm);
return sm.updateDatabase(); 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;
}
} }

View File

@ -9,14 +9,19 @@ import gg.frog.mc.permissionstime.config.PackagesCfg;
import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.database.SqlManager;
import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean;
import gg.frog.mc.permissionstime.utils.PluginUtil;
import gg.frog.mc.permissionstime.utils.StrUtil; import gg.frog.mc.permissionstime.utils.StrUtil;
public class GiveCmd { 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) { if (args.length == 4) {
String playerName = args[1]; String playerName = args[1];
String packageName = args[2]; String packageName = args[2];
@ -34,21 +39,21 @@ public class GiveCmd {
} }
PermissionPackageBean pack = PackagesCfg.PACKAGES.get(packageName); PermissionPackageBean pack = PackagesCfg.PACKAGES.get(packageName);
if (pack != null) { if (pack != null) {
UUID uuid = PluginUtil.getPlayerUUIDByName(playerName); UUID uuid = pm.getPlayerUUIDByName(playerName);
if (uuid != null) { if (uuid != null) {
if (PluginCfg.IS_DEBUG) { if (PluginCfg.IS_DEBUG) {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + uuid.toString() + "\n" + pack.toString() + "\n" + time + "")); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + uuid.toString() + "\n" + pack.toString() + "\n" + time + ""));
} }
if (sm.giveTime(uuid.toString(), packageName, days)) { 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 { } 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 { } else {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为'{0}'的玩家", playerName)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为''{0}''的玩家", playerName));
} }
} else { } else {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为'{0}'的权限包", packageName)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为''{0}''的权限包", packageName));
} }
} else { } else {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "参数不正确")); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "参数不正确"));

View File

@ -13,12 +13,21 @@ import gg.frog.mc.permissionstime.utils.StrUtil;
public class MainCommand implements CommandExecutor { public class MainCommand implements CommandExecutor {
private PluginMain pm = PluginMain.getInstance(); private PluginMain pm;
private SqlManager sm = PluginMain.sm; 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 @Override
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) { 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; boolean isPlayer = false;
if (sender instanceof Player) { if (sender instanceof Player) {
isPlayer = true; isPlayer = true;
@ -50,23 +59,23 @@ public class MainCommand implements CommandExecutor {
return true; return true;
} else if (args[0].equalsIgnoreCase("me")) { } else if (args[0].equalsIgnoreCase("me")) {
if (hasPermission(sender, isPlayer, "permissionstime.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")) { } else if (args[0].equalsIgnoreCase("give")) {
if (hasPermission(sender, isPlayer, "permissionstime.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")) { } else if (args[0].equalsIgnoreCase("set")) {
if (hasPermission(sender, isPlayer, "permissionstime.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")) { } else if (args[0].equalsIgnoreCase("remove")) {
if (hasPermission(sender, isPlayer, "permissionstime.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")) { } else if (args[0].equalsIgnoreCase("packages")) {
if (hasPermission(sender, isPlayer, "permissionstime.packages")) { if (hasPermission(sender, isPlayer, "permissionstime.packages")) {
return PackagesCmd.onCommand(sender, isPlayer, args); return packagesCmd.onCommand(sender, isPlayer, args);
} }
} else { } else {
getHelp(sender, isPlayer); getHelp(sender, isPlayer);
@ -78,21 +87,21 @@ public class MainCommand implements CommandExecutor {
} }
private void getHelp(CommandSender sender, boolean isPlayer) { private void getHelp(CommandSender sender, boolean isPlayer) {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "&a===== " + PluginMain.PLUGIN_NAME + " Version:" + PluginMain.PLUGIN_VERSION + " =====")); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "&a===== " + pm.PLUGIN_NAME + " Version:" + pm.PLUGIN_VERSION + " ====="));
if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".reload")) { if (!isPlayer || sender.isOp() || sender.hasPermission(pm.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("/" + pm.PLUGIN_NAME_LOWER_CASE + " reload \n -Reloads the config file."));
} }
if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".give")) { if (!isPlayer || sender.isOp() || sender.hasPermission(pm.PLUGIN_NAME_LOWER_CASE + ".give")) {
sender.sendMessage(StrUtil.messageFormat("/" + PluginMain.PLUGIN_NAME_LOWER_CASE + " give <playerName> <packageName> <time> \n\t- Give player package <time>day.")); sender.sendMessage(StrUtil.messageFormat("/" + pm.PLUGIN_NAME_LOWER_CASE + " give <playerName> <packageName> <time> \n - Give player package <time>day."));
} }
if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".set")) { if (!isPlayer || sender.isOp() || sender.hasPermission(pm.PLUGIN_NAME_LOWER_CASE + ".set")) {
sender.sendMessage(StrUtil.messageFormat("/" + PluginMain.PLUGIN_NAME_LOWER_CASE + " set <playerName> <packageName> <time> \n\t- Set player package <time>day.")); sender.sendMessage(StrUtil.messageFormat("/" + pm.PLUGIN_NAME_LOWER_CASE + " set <playerName> <packageName> <time> \n - Set player package <time>day."));
} }
if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".remove")) { if (!isPlayer || sender.isOp() || sender.hasPermission(pm.PLUGIN_NAME_LOWER_CASE + ".remove")) {
sender.sendMessage(StrUtil.messageFormat("/" + PluginMain.PLUGIN_NAME_LOWER_CASE + " remove <playerName> <packageName> \n\t- Remove player package.")); sender.sendMessage(StrUtil.messageFormat("/" + pm.PLUGIN_NAME_LOWER_CASE + " remove <playerName> <packageName> \n - Remove player package."));
} }
if (!isPlayer || sender.isOp() || sender.hasPermission(PluginMain.PLUGIN_NAME_LOWER_CASE + ".packages")) { if (!isPlayer || sender.isOp() || sender.hasPermission(pm.PLUGIN_NAME_LOWER_CASE + ".packages")) {
sender.sendMessage(StrUtil.messageFormat("/" + PluginMain.PLUGIN_NAME_LOWER_CASE + " packages [packageName] \n\t- View packages.")); sender.sendMessage(StrUtil.messageFormat("/" + pm.PLUGIN_NAME_LOWER_CASE + " packages [packageName] \n - View packages."));
} }
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX));
} }

View File

@ -4,6 +4,7 @@ import java.util.Map.Entry;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PackagesCfg;
import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean;
@ -11,7 +12,13 @@ import gg.frog.mc.permissionstime.utils.StrUtil;
public class PackagesCmd { public class PackagesCmd {
public static boolean onCommand(CommandSender sender, boolean isPlayer, String[] args) { private PluginMain pm;
public PackagesCmd(PluginMain pm) {
this.pm = pm;
}
public boolean onCommand(CommandSender sender, boolean isPlayer, String[] args) {
if (args.length == 1) { if (args.length == 1) {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "共有{0}种权限包", PackagesCfg.PACKAGES.size())); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "共有{0}种权限包", PackagesCfg.PACKAGES.size()));
for (Entry<String, PermissionPackageBean> e : PackagesCfg.PACKAGES.entrySet()) { for (Entry<String, PermissionPackageBean> e : PackagesCfg.PACKAGES.entrySet()) {
@ -24,7 +31,7 @@ public class PackagesCmd {
PermissionPackageBean p = PackagesCfg.PACKAGES.get(packageName); PermissionPackageBean p = PackagesCfg.PACKAGES.get(packageName);
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "PackgeName: {0}, DisplayName: {1}\n" + "Permissions: {2}\n" + "Groups: {3}\n" + "Prefixs: {4}\n" + "Suffixs: {5}\n" + "", packageName, p.getDisplayName(), p.getPermissions(), p.getGroups(), p.getPrefixs(), p.getSuffixs())); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "PackgeName: {0}, DisplayName: {1}\n" + "Permissions: {2}\n" + "Groups: {3}\n" + "Prefixs: {4}\n" + "Suffixs: {5}\n" + "", packageName, p.getDisplayName(), p.getPermissions(), p.getGroups(), p.getPrefixs(), p.getSuffixs()));
} else { } else {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为'{0}'的权限包",packageName)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "找不到名为''{0}''的权限包", packageName));
} }
} }
return true; return true;

View File

@ -1,9 +1,7 @@
package gg.frog.mc.permissionstime.config; package gg.frog.mc.permissionstime.config;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -24,25 +22,26 @@ import gg.frog.mc.permissionstime.utils.config.PluginConfig;
* *
*/ */
public class ConfigManager { public class ConfigManager {
private PluginMain pm = PluginMain.getInstance(); private PluginMain pm;
private Map<String, PluginConfig> cfgMap = new LinkedHashMap<>(); private Map<String, PluginConfig> cfgMap = new LinkedHashMap<>();
public ConfigManager() { public ConfigManager(PluginMain pm) {
this.pm = pm;
File langFolder = new File(pm.getDataFolder(), "lang/"); File langFolder = new File(pm.getDataFolder(), "lang/");
if (!langFolder.exists()) { if (!langFolder.exists()) {
copyLangFilesFromJar(); copyLangFilesFromJar();
} }
// 添加到配置列表 // 添加到配置列表
cfgMap.put("plugin", new PluginCfg()); cfgMap.put("plugin", new PluginCfg(pm));
cfgMap.put("lang", new LangCfg("lang/" + PluginCfg.LANG + ".yml")); cfgMap.put("lang", new LangCfg("lang/" + PluginCfg.LANG + ".yml", pm));
cfgMap.put("packages", new PackagesCfg("packages.yml")); cfgMap.put("packages", new PackagesCfg("packages.yml", pm));
} }
public void reloadConfig() { public void reloadConfig() {
for (Entry<String, PluginConfig> entry : cfgMap.entrySet()) { for (Entry<String, PluginConfig> entry : cfgMap.entrySet()) {
if ("lang".equals(entry.getKey())) { if ("lang".equals(entry.getKey())) {
entry.setValue(new LangCfg("lang/" + PluginCfg.LANG + ".yml")); entry.setValue(new LangCfg("lang/" + PluginCfg.LANG + ".yml", pm));
} }
entry.getValue().reloadConfig(); entry.getValue().reloadConfig();
} }
@ -57,7 +56,7 @@ public class ConfigManager {
@Override @Override
public void process(String fileName, InputStream is) { public void process(String fileName, InputStream is) {
File f = new File(pm.getDataFolder(),fileName); File f = new File(pm.getDataFolder(), fileName);
File parentFolder = f.getParentFile(); File parentFolder = f.getParentFile();
if (!parentFolder.exists()) { if (!parentFolder.exists()) {
parentFolder.mkdirs(); parentFolder.mkdirs();

View File

@ -1,5 +1,6 @@
package gg.frog.mc.permissionstime.config; package gg.frog.mc.permissionstime.config;
import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.utils.config.PluginConfig; import gg.frog.mc.permissionstime.utils.config.PluginConfig;
/** /**
@ -13,18 +14,17 @@ public class LangCfg extends PluginConfig {
public static String NO_PERMISSION = null; public static String NO_PERMISSION = null;
public static String CONFIG_RELOADED = null; public static String CONFIG_RELOADED = null;
public LangCfg(String fileName) { public LangCfg(String fileName, PluginMain pm) {
super(fileName); super(fileName, pm);
} }
@Override @Override
protected void init() { protected void init() {}
}
@Override @Override
protected void loadToDo() { protected void loadToDo() {
NO_PERMISSION = setGetDefault("nopermission","&4你没有权限这么做"); NO_PERMISSION = setGetDefault("nopermission", "&4你没有权限这么做");
CONFIG_RELOADED = setGetDefault("configReloaded","&a配置重载完成"); CONFIG_RELOADED = setGetDefault("configReloaded", "&a配置重载完成");
} }
} }

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean;
import gg.frog.mc.permissionstime.utils.config.PluginConfig; import gg.frog.mc.permissionstime.utils.config.PluginConfig;
@ -13,8 +14,8 @@ public class PackagesCfg extends PluginConfig {
public static String DEFAULT_GROUP = null; public static String DEFAULT_GROUP = null;
public static Map<String, PermissionPackageBean> PACKAGES = new HashMap<>(); public static Map<String, PermissionPackageBean> PACKAGES = new HashMap<>();
public PackagesCfg(String fileName) { public PackagesCfg(String fileName, PluginMain pm) {
super(fileName); super(fileName, pm);
} }
@Override @Override

View File

@ -11,7 +11,7 @@ import gg.frog.mc.permissionstime.utils.config.PluginConfig;
*/ */
public class PluginCfg extends PluginConfig { public class PluginCfg extends PluginConfig {
public static String PLUGIN_PREFIX ; public static String PLUGIN_PREFIX;
public static boolean IS_METRICS = true; public static boolean IS_METRICS = true;
public static boolean IS_DEBUG = false; public static boolean IS_DEBUG = false;
public static String LANG; public static String LANG;
@ -23,8 +23,8 @@ public class PluginCfg extends PluginConfig {
public static String SQL_PASSWORD; public static String SQL_PASSWORD;
public static String SQL_TABLE_PREFIX; public static String SQL_TABLE_PREFIX;
public PluginCfg() { public PluginCfg(PluginMain pm) {
super(); super(pm);
} }
@Override @Override
@ -43,7 +43,7 @@ public class PluginCfg extends PluginConfig {
@Override @Override
protected void loadToDo() { protected void loadToDo() {
PLUGIN_PREFIX = setGetDefault("pluginPrefix", "&b[" + PluginMain.PLUGIN_NAME + "]&r"); PLUGIN_PREFIX = setGetDefault("pluginPrefix", "&b[" + pm.PLUGIN_NAME + "]&r");
IS_DEBUG = setGetDefault("debug", false); IS_DEBUG = setGetDefault("debug", false);
IS_METRICS = setGetDefault("metrics", true); IS_METRICS = setGetDefault("metrics", true);
LANG = setGetDefault("lang", "zh-cn"); LANG = setGetDefault("lang", "zh-cn");

View File

@ -10,18 +10,15 @@ import lib.PatPeter.SQLibrary.SQLite;
public class SqlManager { public class SqlManager {
private PluginMain pm = PluginMain.getInstance(); private PluginMain pm;
private static SqlManager sm = new SqlManager(); private Database db = null;
private static Database db = null; private IPlayerDataService pds = null;
private static IPlayerDataService pds = null;
private SqlManager() {} public SqlManager(PluginMain pm) {
this.pm = pm;
public static SqlManager getInstance() {
return sm;
} }
public static Database getDb() { public Database getDb() {
return db; return db;
} }
@ -30,10 +27,10 @@ public class SqlManager {
db.close(); db.close();
} }
if (PluginCfg.USE_MYSQL) { if (PluginCfg.USE_MYSQL) {
db = new MySQL(PluginMain.LOG, "[" + PluginMain.PLUGIN_NAME + "] ", PluginCfg.SQL_HOSTNAME, PluginCfg.SQL_PORT, PluginCfg.SQL_DATABASE, PluginCfg.SQL_USERNAME, PluginCfg.SQL_PASSWORD); db = new MySQL(PluginMain.LOG, "[" + pm.PLUGIN_NAME + "] ", PluginCfg.SQL_HOSTNAME, PluginCfg.SQL_PORT, PluginCfg.SQL_DATABASE, PluginCfg.SQL_USERNAME, PluginCfg.SQL_PASSWORD);
} else { } else {
db = new SQLite(PluginMain.LOG, "[" + PluginMain.PLUGIN_NAME + "] ", PluginMain.pm.getDataFolder().getAbsolutePath(), "playerData", ".db"); db = new SQLite(PluginMain.LOG, "[" + pm.PLUGIN_NAME + "] ", pm.getDataFolder().getAbsolutePath(), "playerData", ".db");
pds = new SqlitePlayerDataService(); pds = new SqlitePlayerDataService(pm, this);
} }
db.open(); db.open();
try { try {

View File

@ -8,13 +8,19 @@ import java.util.List;
import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.database.IPlayerDataService; import gg.frog.mc.permissionstime.database.IPlayerDataService;
import gg.frog.mc.permissionstime.database.SqlManager;
import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.model.db.PlayerDataBean;
import gg.frog.mc.permissionstime.utils.StrUtil; import gg.frog.mc.permissionstime.utils.StrUtil;
import gg.frog.mc.permissionstime.utils.database.DatabaseUtil; import gg.frog.mc.permissionstime.utils.database.DatabaseUtil;
public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerDataService { public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerDataService {
private PluginMain pm = PluginMain.getInstance(); private PluginMain pm;
public SqlitePlayerDataService(PluginMain pm, SqlManager sm) {
super(sm);
this.pm = pm;
}
@Override @Override
public boolean tableExist() throws Exception { public boolean tableExist() throws Exception {
@ -66,8 +72,8 @@ public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerData
public boolean addTime(String uuid, String packageName, int days) throws Exception { public boolean addTime(String uuid, String packageName, int days) throws Exception {
PlayerDataBean pdb = queryPlayerData(uuid, packageName); PlayerDataBean pdb = queryPlayerData(uuid, packageName);
long now = new Date().getTime(); long now = new Date().getTime();
long addTime = days * 24 * 60 * 60 * 1000; long addTime = days * 24 * 60 * 60 * 1000L;
long expire = 0; long expire = 0L;
if (pdb == null) { if (pdb == null) {
expire = now + addTime; expire = now + addTime;
pdb = new PlayerDataBean(null, uuid, packageName, expire); pdb = new PlayerDataBean(null, uuid, packageName, expire);

View File

@ -9,8 +9,12 @@ import org.bukkit.event.player.PlayerQuitEvent;
import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.PluginMain;
public class TheListener implements Listener { public class TheListener implements Listener {
private PluginMain pm = PluginMain.getInstance(); private PluginMain pm;
public TheListener(PluginMain pm) {
this.pm = pm;
}
/** /**
* 一个监听器例子 * 一个监听器例子

View File

@ -1,27 +0,0 @@
package gg.frog.mc.permissionstime.utils;
import java.util.UUID;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import gg.frog.mc.permissionstime.PluginMain;
public class PluginUtil {
private static PluginMain pm = PluginMain.getInstance();
public static UUID getPlayerUUIDByName(String name) {
for (Player p : pm.getServer().getOnlinePlayers()) {
if (p.getName().equals(name)) {
return p.getUniqueId();
}
}
for (OfflinePlayer p : pm.getServer().getOfflinePlayers()) {
if (p.getName().equals(name)) {
return p.getUniqueId();
}
}
return null;
}
}

View File

@ -25,22 +25,24 @@ import gg.frog.mc.permissionstime.PluginMain;
*/ */
public abstract class PluginConfig { public abstract class PluginConfig {
private PluginMain pm = PluginMain.getInstance(); protected PluginMain pm;
private FileConfiguration config = null; private FileConfiguration config = null;
private File folder = null; private File folder = null;
private String fileName = null; private String fileName = null;
private File configFile = null; private File configFile = null;
protected PluginConfig(PluginMain pm) {
protected PluginConfig() { this.pm = pm;
initConfig(pm.getDataFolder(), "config.yml"); initConfig(pm.getDataFolder(), "config.yml");
} }
protected PluginConfig(String fileName) { protected PluginConfig(String fileName, PluginMain pm) {
this.pm = pm;
initConfig(pm.getDataFolder(), fileName); initConfig(pm.getDataFolder(), fileName);
} }
protected PluginConfig(File folder, String fileName) { protected PluginConfig(File folder, String fileName, PluginMain pm) {
this.pm = pm;
initConfig(folder, fileName); initConfig(folder, fileName);
} }
@ -128,7 +130,7 @@ public abstract class PluginConfig {
config = YamlConfiguration.loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
if (useRes) { if (useRes) {
final InputStream defConfigStream = PluginMain.getInstance().getResource(fileName); final InputStream defConfigStream = pm.getResource(fileName);
if (defConfigStream == null) { if (defConfigStream == null) {
return; return;
} }

View File

@ -5,8 +5,14 @@ import lib.PatPeter.SQLibrary.Database;
public abstract class DatabaseUtil { public abstract class DatabaseUtil {
private SqlManager sm;
public DatabaseUtil(SqlManager sm) {
this.sm = sm;
}
protected Database getDB() { protected Database getDB() {
Database db = SqlManager.getDb(); Database db = sm.getDb();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if (!db.isOpen()) { if (!db.isOpen()) {
if (db.open()) { if (db.open()) {