diff --git a/src/main/gg/frog/mc/permissionstime/PluginMain.java b/src/main/gg/frog/mc/permissionstime/PluginMain.java index 63fa94e..bfbab8f 100644 --- a/src/main/gg/frog/mc/permissionstime/PluginMain.java +++ b/src/main/gg/frog/mc/permissionstime/PluginMain.java @@ -11,9 +11,9 @@ import org.mcstats.Metrics; import gg.frog.mc.permissionstime.command.TheCommand; import gg.frog.mc.permissionstime.config.ConfigManager; import gg.frog.mc.permissionstime.config.PluginCfg; +import gg.frog.mc.permissionstime.database.SqlManager; 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 { @@ -26,6 +26,7 @@ public class PluginMain extends JavaPlugin { public static PluginMain pm = null; public static ConfigManager cm = null; + public static SqlManager sm = null; public static Permission permission = null; public PluginMain() { @@ -54,7 +55,7 @@ public class PluginMain extends JavaPlugin { e.printStackTrace(); } } - getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() { + getServer().getScheduler().runTask(this, new Runnable() { public void run() { if (!checkPluginDepends()) { getServer().getPluginManager().disablePlugin(pm); @@ -63,7 +64,7 @@ public class PluginMain extends JavaPlugin { registerCommands(); } } - }, 0L, 432000L); + }); } public static PluginMain getInstance() { @@ -117,6 +118,7 @@ public class PluginMain extends JavaPlugin { super.onDisable(); getServer().getServicesManager().unregisterAll(this); Bukkit.getScheduler().cancelTasks(this); + SqlManager.getDb().close(); } private boolean setupPermissions() { @@ -128,16 +130,7 @@ public class PluginMain extends JavaPlugin { } 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; + sm = SqlManager.getInstance(); + return sm.updateDatabase(); } } diff --git a/src/main/gg/frog/mc/permissionstime/command/TheCommand.java b/src/main/gg/frog/mc/permissionstime/command/TheCommand.java index 2ce5b61..a5a19cf 100644 --- a/src/main/gg/frog/mc/permissionstime/command/TheCommand.java +++ b/src/main/gg/frog/mc/permissionstime/command/TheCommand.java @@ -8,11 +8,13 @@ import org.bukkit.entity.Player; import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.config.LangCfg; import gg.frog.mc.permissionstime.config.PluginCfg; +import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.utils.StrUtil; public class TheCommand implements CommandExecutor { private PluginMain pm = PluginMain.getInstance(); + private SqlManager sm = PluginMain.sm; @Override public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) { @@ -34,13 +36,18 @@ public class TheCommand implements CommandExecutor { Player player = (Player) sender; if (sender.isOp() || player.hasPermission("quickdevdemo.reload")) { pm.getConfigManager().reloadConfig(); + if(!sm.updateDatabase()){ + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "数据库异常")); + } sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED)); pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED)); } else { + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.NO_PERMISSION)); } } else { pm.getConfigManager().reloadConfig(); + sm.updateDatabase(); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED)); } return true; diff --git a/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java b/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java index 610a1c2..4343061 100644 --- a/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java +++ b/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java @@ -4,7 +4,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import gg.frog.mc.permissionstime.model.PermissionPackageBean; +import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; import gg.frog.mc.permissionstime.utils.config.PluginConfig; public class PackagesCfg extends PluginConfig { diff --git a/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java b/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java new file mode 100644 index 0000000..1d8e8c1 --- /dev/null +++ b/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java @@ -0,0 +1,39 @@ +package gg.frog.mc.permissionstime.database; + +import java.sql.SQLException; + +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; + +public interface IPlayerDataService { + + /** + * 检查表是否 + * @return + * @throws SQLException + */ + boolean tableExist() throws Exception; + + /** + * 创建用户数据表 + * @return + * @throws SQLException + */ + boolean creatTable() throws Exception; + + /** + * 保存更新用户数据 + * @throws Exception + */ + boolean saveOrUpdatePlayerData(PlayerDataBean bean) throws Exception; + + /** + * 删除 + */ + boolean delPlayData(String uuid); + + /** + * 查询 + * @return + */ + PlayerDataBean queryPlayerData(String uuid) throws Exception; +} diff --git a/src/main/gg/frog/mc/permissionstime/database/SqlManager.java b/src/main/gg/frog/mc/permissionstime/database/SqlManager.java new file mode 100644 index 0000000..3c7a4de --- /dev/null +++ b/src/main/gg/frog/mc/permissionstime/database/SqlManager.java @@ -0,0 +1,50 @@ +package gg.frog.mc.permissionstime.database; + +import gg.frog.mc.permissionstime.PluginMain; +import gg.frog.mc.permissionstime.config.PluginCfg; +import gg.frog.mc.permissionstime.database.impl.SqlitePlayerDataService; +import gg.frog.mc.permissionstime.utils.StrUtil; +import lib.PatPeter.SQLibrary.Database; +import lib.PatPeter.SQLibrary.MySQL; +import lib.PatPeter.SQLibrary.SQLite; + +public class SqlManager { + + private PluginMain pm = PluginMain.getInstance(); + private static SqlManager sm = new SqlManager(); + private static Database db = null; + private static IPlayerDataService pds = null; + + private SqlManager() {} + + public static SqlManager getInstance() { + return sm; + } + + public static Database getDb() { + return db; + } + + public boolean updateDatabase() { + if(db != null && db.isOpen()){ + db.close(); + } + 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); + } else { + db = new SQLite(PluginMain.LOG, "[" + PluginMain.PLUGIN_NAME + "] ", PluginMain.pm.getDataFolder().getAbsolutePath(), "playerData", ".db"); + pds = new SqlitePlayerDataService(); + } + db.open(); + try { + if(!pds.tableExist()){ + pds.creatTable(); + } + pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "连接数据库成功"); + return true; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } +} diff --git a/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java b/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java new file mode 100644 index 0000000..69bc5e1 --- /dev/null +++ b/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java @@ -0,0 +1,73 @@ +package gg.frog.mc.permissionstime.database.impl; + +import java.sql.ResultSet; + +import gg.frog.mc.permissionstime.PluginMain; +import gg.frog.mc.permissionstime.config.PluginCfg; +import gg.frog.mc.permissionstime.database.IPlayerDataService; +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; +import gg.frog.mc.permissionstime.utils.StrUtil; +import gg.frog.mc.permissionstime.utils.database.DatabaseUtil; + +public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerDataService { + + private PluginMain pm = PluginMain.getInstance(); + + @Override + public boolean tableExist() throws Exception { + String sql = "SELECT count(*) AS num FROM \"main\".sqlite_master M where tbl_name='playerData';"; + try { + ResultSet rs = getDB().query(sql); + int num = rs.getInt("num"); + if (num == 1) { + return true; + } + return false; + } catch (Exception e) { + pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "无法检查有无数据表"); + throw e; + } + } + + @Override + public boolean creatTable() throws Exception { + String sql = "CREATE TABLE \"main\".\"playerData\" ( \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"uuid\" TEXT NOT NULL, \"packageName\" TEXT NOT NULL, \"expire\" INTEGER NOT NULL )"; + try { + getDB().query(sql); + return true; + } catch (Exception e) { + pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "无法创建数据表"); + throw e; + } + } + + @Override + public boolean saveOrUpdatePlayerData(PlayerDataBean bean) throws Exception { + if(bean.getId()!=null){ + + }else{ + + } + String sql = "CREATE TABLE \"main\".\"playerData\" ( \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"uuid\" TEXT NOT NULL, \"packageName\" TEXT NOT NULL, \"expire\" INTEGER NOT NULL )"; + try { + getDB().query(sql); + return true; + } catch (Exception e) { + pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "无法创建数据表"); + throw e; + } + } + + @Override + public boolean delPlayData(String uuid) { + return false; + // TODO Auto-generated method stub + + } + + @Override + public PlayerDataBean queryPlayerData(String uuid) { + return null; + } + +} diff --git a/src/main/gg/frog/mc/permissionstime/model/PermissionPackageBean.java b/src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java similarity index 97% rename from src/main/gg/frog/mc/permissionstime/model/PermissionPackageBean.java rename to src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java index 492f709..16a1492 100644 --- a/src/main/gg/frog/mc/permissionstime/model/PermissionPackageBean.java +++ b/src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java @@ -1,4 +1,4 @@ -package gg.frog.mc.permissionstime.model; +package gg.frog.mc.permissionstime.model.cfg; import java.util.ArrayList; import java.util.List; diff --git a/src/main/gg/frog/mc/permissionstime/model/db/PlayerDataBean.java b/src/main/gg/frog/mc/permissionstime/model/db/PlayerDataBean.java new file mode 100644 index 0000000..df51966 --- /dev/null +++ b/src/main/gg/frog/mc/permissionstime/model/db/PlayerDataBean.java @@ -0,0 +1,36 @@ +package gg.frog.mc.permissionstime.model.db; + +public class PlayerDataBean { + + private Integer id; + private String uuid; + private String packageName; + private Integer expire; + + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public String getUuid() { + return uuid; + } + public void setUuid(String uuid) { + this.uuid = uuid; + } + public String getPackageName() { + return packageName; + } + public void setPackageName(String packageName) { + this.packageName = packageName; + } + public Integer getExpire() { + return expire; + } + public void setExpire(Integer expire) { + this.expire = expire; + } + + +} diff --git a/src/main/gg/frog/mc/permissionstime/utils/database/DatabaseUtil.java b/src/main/gg/frog/mc/permissionstime/utils/database/DatabaseUtil.java new file mode 100644 index 0000000..91ed9e1 --- /dev/null +++ b/src/main/gg/frog/mc/permissionstime/utils/database/DatabaseUtil.java @@ -0,0 +1,21 @@ +package gg.frog.mc.permissionstime.utils.database; + +import gg.frog.mc.permissionstime.database.SqlManager; +import lib.PatPeter.SQLibrary.Database; + +public abstract class DatabaseUtil { + + protected Database getDB() { + Database db = SqlManager.getDb(); + for (int i = 0; i < 3; i++) { + if (!db.isOpen()) { + if (db.open()) { + return db; + } + } else { + return db; + } + } + return db; + } +}