diff --git a/README.md b/README.md
index 14aa37d..a08792b 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@
- **添加、设置、移除-命令执行失败记录**
- **支持不同世界权限**
- **调用vault API 设置玩家权限 -- 登录时(添加 移除)、游戏中(命令添加/移除 延迟移除)、退出时移除**
-- mysql保存数据
+- **mysql保存数据**
- 支持跨服
- 手动删除过期的或无效数据
- 语言支持整理
diff --git a/pom.xml b/pom.xml
index b6162b4..74907b5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -73,6 +73,7 @@
${basedir}
LICENSE
+ README.md
@@ -118,12 +119,6 @@
org.mcstats.*:*
-
-
- org.mcstats
- gg.frog.mc.permissionstime
-
-
diff --git a/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java b/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java
index bcde987..f7bbe25 100644
--- a/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java
+++ b/src/main/gg/frog/mc/permissionstime/config/PluginCfg.java
@@ -48,7 +48,7 @@ public class PluginCfg extends PluginConfig {
IS_METRICS = setGetDefault("metrics", true);
LANG = setGetDefault("lang", "zh-cn");
USE_MYSQL = setGetDefault("mysql.enable", false);
- if (!USE_MYSQL) {
+ if (USE_MYSQL) {
SQL_HOSTNAME = setGetDefault("mysql.hostname", "localhost");
SQL_PORT = setGetDefault("mysql.port", 3306);
SQL_DATABASE = setGetDefault("mysql.database", "minecraft");
diff --git a/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java b/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java
index 659d0b4..42c649b 100644
--- a/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java
+++ b/src/main/gg/frog/mc/permissionstime/database/IPlayerDataService.java
@@ -6,6 +6,8 @@ import java.util.List;
import gg.frog.mc.permissionstime.model.db.PlayerDataBean;
public interface IPlayerDataService {
+
+ static long TIME_UNIT = 24 * 60 * 60 * 1000L;// 一天
/**
* 检查表是否
diff --git a/src/main/gg/frog/mc/permissionstime/database/SqlManager.java b/src/main/gg/frog/mc/permissionstime/database/SqlManager.java
index 115a564..e59847b 100644
--- a/src/main/gg/frog/mc/permissionstime/database/SqlManager.java
+++ b/src/main/gg/frog/mc/permissionstime/database/SqlManager.java
@@ -5,6 +5,7 @@ import java.util.List;
import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.PluginCfg;
+import gg.frog.mc.permissionstime.database.impl.MySQLPlayerDataService;
import gg.frog.mc.permissionstime.database.impl.SqlitePlayerDataService;
import gg.frog.mc.permissionstime.model.db.PlayerDataBean;
import gg.frog.mc.permissionstime.utils.StrUtil;
@@ -32,20 +33,23 @@ public class SqlManager {
}
if (PluginCfg.USE_MYSQL) {
db = new MySQL(PluginMain.LOG, "[" + pm.PLUGIN_NAME + "] ", PluginCfg.SQL_HOSTNAME, PluginCfg.SQL_PORT, PluginCfg.SQL_DATABASE, PluginCfg.SQL_USERNAME, PluginCfg.SQL_PASSWORD);
+ pds = new MySQLPlayerDataService(pm, this);
} else {
db = new SQLite(PluginMain.LOG, "[" + pm.PLUGIN_NAME + "] ", pm.getDataFolder().getAbsolutePath(), "playerData", ".db");
pds = new SqlitePlayerDataService(pm, this);
}
- db.open();
- try {
- if (!pds.tableExist()) {
- pds.creatTable();
+ if(db.open()) {
+ try {
+ if (!pds.tableExist()) {
+ pds.creatTable();
+ }
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "连接数据库成功");
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
}
- pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "连接数据库成功");
- return true;
- } catch (Exception e) {
- e.printStackTrace();
}
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX) + "连接数据库失败");
return false;
}
diff --git a/src/main/gg/frog/mc/permissionstime/database/impl/MySQLPlayerDataService.java b/src/main/gg/frog/mc/permissionstime/database/impl/MySQLPlayerDataService.java
new file mode 100644
index 0000000..d76626c
--- /dev/null
+++ b/src/main/gg/frog/mc/permissionstime/database/impl/MySQLPlayerDataService.java
@@ -0,0 +1,200 @@
+package gg.frog.mc.permissionstime.database.impl;
+
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+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.database.SqlManager;
+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 MySQLPlayerDataService extends DatabaseUtil implements IPlayerDataService {
+
+ private PluginMain pm;
+
+ public MySQLPlayerDataService(PluginMain pm, SqlManager sm) {
+ super(sm);
+ this.pm = pm;
+ }
+
+ @Override
+ public boolean tableExist() throws Exception {
+ String sql = "SELECT count(*) AS num FROM information_schema.TABLES WHERE table_name ='" + PluginCfg.SQL_TABLE_PREFIX + "playerData';";
+ try {
+ ResultSet rs = getDB().query(sql);
+ rs.next();
+ 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 `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `uuid` VARCHAR (255) NOT NULL, `packageName` VARCHAR (255) NOT NULL, `expire` BIGINT NOT NULL, PRIMARY KEY (`id`), UNIQUE INDEX `UUID_PACKAGE` (`uuid`, `packageName`));";
+ try {
+ getDB().query(sql);
+ return true;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法创建数据表"));
+ throw e;
+ }
+ }
+
+ @Override
+ public boolean setPlayerData(PlayerDataBean bean) throws Exception {
+ PlayerDataBean pdb = bean;
+ String sql;
+ if (pdb.getId() != null) {
+ sql = "UPDATE `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` SET `uuid`='" + pdb.getUuid() + "', `packageName`='" + pdb.getPackageName() + "', `expire`='" + pdb.getExpire() + "' WHERE (`id`='" + pdb.getId() + "');";
+ } else {
+ sql = "INSERT INTO `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` (`uuid`, `packageName`, `expire`) VALUES ('" + pdb.getUuid() + "', '" + pdb.getPackageName() + "', " + pdb.getExpire() + ");";
+ }
+ try {
+ getDB().query(sql);
+ return true;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法创建数据: {0}", pdb));
+ throw e;
+ }
+ }
+
+ @Override
+ public boolean setTime(String uuid, String packageName, int days) throws Exception {
+ long now = new Date().getTime();
+ long addTime = days * TIME_UNIT;
+ long expire = now + addTime;
+ PlayerDataBean pdb = queryPlayerData(uuid, packageName);
+ if (pdb == null) {
+ pdb = new PlayerDataBean(null, uuid, packageName, expire);
+ return setPlayerData(pdb);
+ } else {
+ pdb.setExpire(expire);
+ return setPlayerData(pdb);
+ }
+ }
+
+ @Override
+ public boolean addTime(String uuid, String packageName, int days) throws Exception {
+ long now = new Date().getTime();
+ long addTime = days * TIME_UNIT;
+ long expire = now + addTime;
+ PlayerDataBean pdb = queryPlayerData(uuid, packageName);
+ if (pdb == null) {
+ pdb = new PlayerDataBean(null, uuid, packageName, expire);
+ return setPlayerData(pdb);
+ } else {
+ if (pdb.getExpire() < now) {
+ pdb.setExpire(expire);
+ return setPlayerData(pdb);
+ } else {
+ String sql = "UPDATE `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` SET `expire`='" + addTime + "' WHERE (`id`='" + pdb.getId() + "');";
+ try {
+ getDB().query(sql);
+ return true;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法更新数据: {0}", pdb));
+ throw e;
+ }
+ }
+ }
+ }
+
+ @Override
+ public List queryPlayerData(String uuid) throws Exception {
+ String sql = "SELECT * FROM `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` where (`uuid`='" + uuid + "');";
+ try {
+ List pdbList = new ArrayList<>();
+ ResultSet rs = getDB().query(sql);
+ while (rs.next()) {
+ long tid = rs.getLong("id");
+ String tuuid = rs.getString("uuid");
+ String tpackageName = rs.getString("packageName");
+ long texpire = rs.getLong("expire");
+ PlayerDataBean tpd = new PlayerDataBean(tid, tuuid, tpackageName, texpire);
+ pdbList.add(tpd);
+ }
+ return pdbList;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法查询UUID: {0} 的数据", uuid));
+ throw e;
+ }
+ }
+
+ @Override
+ public PlayerDataBean queryPlayerData(String uuid, String packageName) throws Exception {
+ String sql = "SELECT * FROM `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` where (`uuid`='" + uuid + "' AND `packageName`='" + packageName + "');";
+ try {
+ ResultSet rs = getDB().query(sql);
+ while (rs.next()) {
+ long tid = rs.getLong("id");
+ String tuuid = rs.getString("uuid");
+ String tpackageName = rs.getString("packageName");
+ long texpire = rs.getLong("expire");
+ PlayerDataBean tpd = new PlayerDataBean(tid, tuuid, tpackageName, texpire);
+ return tpd;
+ }
+ return null;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法查询UUID: {0}, packageName: {1} 的数据", uuid, packageName));
+ throw e;
+ }
+ }
+
+ @Override
+ public List queryNotExpirePlayerData(String uuid) throws Exception {
+ long now = new Date().getTime();
+ String sql = "SELECT * FROM `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` where (`uuid`='" + uuid + "' AND `expire` > " + now + ");";
+ try {
+ List pdbList = new ArrayList<>();
+ ResultSet rs = getDB().query(sql);
+ while (rs.next()) {
+ long tid = rs.getLong("id");
+ String tuuid = rs.getString("uuid");
+ String tpackageName = rs.getString("packageName");
+ long texpire = rs.getLong("expire");
+ PlayerDataBean tpd = new PlayerDataBean(tid, tuuid, tpackageName, texpire);
+ pdbList.add(tpd);
+ }
+ return pdbList;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法查询UUID: {0} 的数据", uuid));
+ throw e;
+ }
+ }
+
+ @Override
+ public boolean delPlayData(String uuid) throws Exception {
+ String sql = "DELETE FROM `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` WHERE (`uuid`='" + uuid + "');";
+ try {
+ getDB().query(sql);
+ return true;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法删除UUID为: {0} 的数据", uuid));
+ throw e;
+ }
+ }
+
+ @Override
+ public boolean delPlayData(String uuid, String packageName) throws Exception {
+ String sql = "DELETE FROM `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` WHERE (`uuid`='" + uuid + "' AND `packageName`='" + packageName + "');";
+ try {
+ getDB().query(sql);
+ return true;
+ } catch (Exception e) {
+ pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "无法删除UUID: {0}, packageName: {1} 的数据", uuid, packageName));
+ throw e;
+ }
+ }
+
+}
diff --git a/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java b/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java
index 59030e4..f5f1d46 100644
--- a/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java
+++ b/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java
@@ -17,8 +17,6 @@ public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerData
private PluginMain pm;
- private static long TIME_UNIT = 24 * 60 * 60 * 1000L;// 一天
-
public SqlitePlayerDataService(PluginMain pm, SqlManager sm) {
super(sm);
this.pm = pm;