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-11 20:50:53 +08:00
parent 59a15b2ea1
commit 1486f3e339
9 changed files with 235 additions and 16 deletions

View File

@ -11,9 +11,9 @@ import org.mcstats.Metrics;
import gg.frog.mc.permissionstime.command.TheCommand; import gg.frog.mc.permissionstime.command.TheCommand;
import gg.frog.mc.permissionstime.config.ConfigManager; import gg.frog.mc.permissionstime.config.ConfigManager;
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.listener.TheListener; import gg.frog.mc.permissionstime.listener.TheListener;
import gg.frog.mc.permissionstime.utils.StrUtil; import gg.frog.mc.permissionstime.utils.StrUtil;
import lib.PatPeter.SQLibrary.SQLite;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
public class PluginMain extends JavaPlugin { public class PluginMain extends JavaPlugin {
@ -26,6 +26,7 @@ public class PluginMain extends JavaPlugin {
public static PluginMain pm = null; public static PluginMain pm = null;
public static ConfigManager cm = null; public static ConfigManager cm = null;
public static SqlManager sm = null;
public static Permission permission = null; public static Permission permission = null;
public PluginMain() { public PluginMain() {
@ -54,7 +55,7 @@ public class PluginMain extends JavaPlugin {
e.printStackTrace(); e.printStackTrace();
} }
} }
getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() { getServer().getScheduler().runTask(this, new Runnable() {
public void run() { public void run() {
if (!checkPluginDepends()) { if (!checkPluginDepends()) {
getServer().getPluginManager().disablePlugin(pm); getServer().getPluginManager().disablePlugin(pm);
@ -63,7 +64,7 @@ public class PluginMain extends JavaPlugin {
registerCommands(); registerCommands();
} }
} }
}, 0L, 432000L); });
} }
public static PluginMain getInstance() { public static PluginMain getInstance() {
@ -117,6 +118,7 @@ public class PluginMain extends JavaPlugin {
super.onDisable(); super.onDisable();
getServer().getServicesManager().unregisterAll(this); getServer().getServicesManager().unregisterAll(this);
Bukkit.getScheduler().cancelTasks(this); Bukkit.getScheduler().cancelTasks(this);
SqlManager.getDb().close();
} }
private boolean setupPermissions() { private boolean setupPermissions() {
@ -128,16 +130,7 @@ public class PluginMain extends JavaPlugin {
} }
private boolean setupDatabase() { private boolean setupDatabase() {
try { sm = SqlManager.getInstance();
SQLite sql = new SQLite(Logger.getLogger("Minecraft"), "[" + PLUGIN_NAME + "] ", this.getDataFolder().getAbsolutePath(), PLUGIN_NAME, ".sqlite"); return sm.updateDatabase();
if(!sql.isOpen()){
sql.open();
}
sql.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} }
} }

View File

@ -8,11 +8,13 @@ import org.bukkit.entity.Player;
import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.LangCfg; import gg.frog.mc.permissionstime.config.LangCfg;
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.utils.StrUtil; import gg.frog.mc.permissionstime.utils.StrUtil;
public class TheCommand implements CommandExecutor { public class TheCommand implements CommandExecutor {
private PluginMain pm = PluginMain.getInstance(); private PluginMain pm = PluginMain.getInstance();
private SqlManager sm = PluginMain.sm;
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) { public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
@ -34,13 +36,18 @@ public class TheCommand implements CommandExecutor {
Player player = (Player) sender; Player player = (Player) sender;
if (sender.isOp() || player.hasPermission("quickdevdemo.reload")) { if (sender.isOp() || player.hasPermission("quickdevdemo.reload")) {
pm.getConfigManager().reloadConfig(); pm.getConfigManager().reloadConfig();
if(!sm.updateDatabase()){
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "数据库异常"));
}
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED));
pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED)); pm.getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED));
} else { } else {
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.NO_PERMISSION)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.NO_PERMISSION));
} }
} else { } else {
pm.getConfigManager().reloadConfig(); pm.getConfigManager().reloadConfig();
sm.updateDatabase();
sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED)); sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.CONFIG_RELOADED));
} }
return true; return true;

View File

@ -4,7 +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.model.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;
public class PackagesCfg extends PluginConfig { public class PackagesCfg extends PluginConfig {

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package gg.frog.mc.permissionstime.model; package gg.frog.mc.permissionstime.model.cfg;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

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

View File

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