103 lines
4.1 KiB
Java
103 lines
4.1 KiB
Java
package cn.citycraft.CTZServerCommon;
|
|
|
|
import cn.citycraft.PluginHelper.sql.KeyValue;
|
|
import cn.citycraft.PluginHelper.sql.MySQLHelper;
|
|
import cn.citycraft.PluginHelper.sql.SQLHelper;
|
|
import cn.citycraft.PluginHelper.utils.StringUtil;
|
|
import net.md_5.bungee.api.ChatColor;
|
|
|
|
public class CTZAuth {
|
|
static final String TableName = "ctzserver";
|
|
static final String UserField = "player";
|
|
static final String PWDField = "password";
|
|
static final String ISLoginField = "islogin";
|
|
static final String ALLOWLoginField = "allowlogin";
|
|
static final String LASTLOGOUT = "lastlogout";
|
|
static final String IP = "ip";
|
|
static SQLHelper sql;
|
|
|
|
public static boolean allowLogin(String username) {
|
|
return allowLogin(username, null);
|
|
}
|
|
|
|
public static boolean allowLogin(String username, String ip) {
|
|
KeyValue cdt = new KeyValue(UserField, username);
|
|
if (ip != null)
|
|
cdt.add(IP, ip);
|
|
String result = sql.dbSelectFirst(TableName, ALLOWLoginField, cdt);
|
|
return (result != null && result.equalsIgnoreCase("1"));
|
|
}
|
|
|
|
public static void changePassword(String username, String password) {
|
|
// TODO 处理玩家密码修改事件
|
|
}
|
|
|
|
public static boolean checkLastLogout(String username, long timeout) {
|
|
try {
|
|
String lastlogout = sql.dbSelectFirst(TableName, LASTLOGOUT, new KeyValue(UserField, username));
|
|
return System.currentTimeMillis() - Integer.parseInt(lastlogout) < timeout * 1000;
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static boolean checkPassword(String username, String password) {
|
|
return sql.isFieldExists(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
|
|
}
|
|
|
|
public static void init(SQLHelper sql, String address, int port, String datebase, String username, String password) {
|
|
CTZAuth.sql = sql;
|
|
CTZServer.print(ChatColor.GREEN + "初始化数据库连接...");
|
|
sql = new MySQLHelper(address, port, datebase, username, password);
|
|
if (!sql.dbConnection()) {
|
|
CTZServer.warn(ChatColor.RED + "数据库连接失败...");
|
|
return;
|
|
}
|
|
CTZServer.print(ChatColor.GREEN + "数据库连接成功,检查数据表是否存在...");
|
|
if (!sql.isTableExists(TableName)) {
|
|
CTZServer.print(ChatColor.RED + "数据表不存在,新建表" + TableName + "...");
|
|
KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL")
|
|
.add("password", "VARCHAR(50) NOT NULL")
|
|
.add("ip", "VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'")
|
|
.add("lastlogout", "BIGINT(30) NOT NULL DEFAULT 0")
|
|
.add("x", "DOUBLE NOT NULL DEFAULT 0")
|
|
.add("y", "DOUBLE NOT NULL DEFAULT 0")
|
|
.add("z", "DOUBLE NOT NULL DEFAULT 0")
|
|
.add("email", "VARCHAR(50) NOT NULL DEFAULT 'mc@mc.com'")
|
|
.add("world", "VARCHAR(20) DEFAULT 'world'")
|
|
.add("islogged", "SMALLINT(6) NOT NULL DEFAULT 0")
|
|
.add("allowlogin", "SMALLINT(6) NOT NULL DEFAULT 0");
|
|
String Conditions = "UNIQUE (`player`)";
|
|
if (!sql.createTables(TableName, kv, Conditions))
|
|
CTZServer.warn(ChatColor.RED + "数据表 " + TableName + " 创建失败,请尝试手动创建并重启服务器...");
|
|
else
|
|
CTZServer.print(ChatColor.GREEN + "数据表 " + TableName + " 创建成功...");
|
|
}
|
|
}
|
|
|
|
public static boolean isLogin(String username) {
|
|
String result = sql.dbSelectFirst(TableName, ISLoginField, new KeyValue(UserField, username));
|
|
return (result != null && result.equalsIgnoreCase("1"));
|
|
}
|
|
|
|
public static boolean isRegistered(String username) {
|
|
return sql.isFieldExists(TableName, new KeyValue(UserField, username));
|
|
}
|
|
|
|
public static boolean login(String username, String ip) {
|
|
return sql.dbUpdate(TableName, new KeyValue(ALLOWLoginField, "1").add(IP, ip == null ? "127.0.0.1" : ip), new KeyValue(UserField, username));
|
|
}
|
|
|
|
public static boolean registerPlayer(String username, String password) {
|
|
if (isRegistered(username))
|
|
return false;
|
|
else
|
|
return sql.dbInsert(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
|
|
}
|
|
|
|
public static boolean setLastLogout(String username, long logouttime) {
|
|
return sql.dbUpdate(TableName, new KeyValue(ALLOWLoginField, "1"), new KeyValue(UserField, username));
|
|
}
|
|
|
|
}
|