1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2024-11-22 07:28:47 +00:00

mysql支持,修改bug

This commit is contained in:
GeekFrog 2017-07-15 06:59:31 +08:00
parent 2959bf5cde
commit c884400781
7 changed files with 217 additions and 18 deletions

View File

@ -21,7 +21,7 @@
- **添加、设置、移除-命令执行失败记录**
- **支持不同世界权限**
- **调用vault API 设置玩家权限 -- 登录时(添加 移除)、游戏中(命令添加/移除 延迟移除)、退出时移除**
- mysql保存数据
- **mysql保存数据**
- 支持跨服
- 手动删除过期的或无效数据
- 语言支持整理

View File

@ -73,6 +73,7 @@
<directory>${basedir}</directory>
<includes>
<include>LICENSE</include>
<include>README.md</include>
</includes>
</resource>
</resources>
@ -118,12 +119,6 @@
<include>org.mcstats.*:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.mcstats</pattern>
<shadedPattern>gg.frog.mc.permissionstime</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>

View File

@ -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");

View File

@ -7,6 +7,8 @@ import gg.frog.mc.permissionstime.model.db.PlayerDataBean;
public interface IPlayerDataService {
static long TIME_UNIT = 24 * 60 * 60 * 1000L;// 一天
/**
* 检查表是否
*

View File

@ -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;
}

View File

@ -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<PlayerDataBean> queryPlayerData(String uuid) throws Exception {
String sql = "SELECT * FROM `" + PluginCfg.SQL_TABLE_PREFIX + "playerData` where (`uuid`='" + uuid + "');";
try {
List<PlayerDataBean> 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<PlayerDataBean> 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<PlayerDataBean> 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;
}
}
}

View File

@ -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;