1
0
mirror of https://e.coding.net/circlecloud/CTZServer.git synced 2024-11-21 11:28:47 +00:00

use CTZServerCommon...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-16 14:15:42 +08:00
parent e2a2fedf53
commit ac513ed551
10 changed files with 92 additions and 468 deletions

View File

@ -61,6 +61,12 @@
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>CTZServerCommon</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

View File

@ -1,47 +0,0 @@
package cn.citycraft.CTZServer;
import cn.citycraft.PluginHelper.sql.KeyValue;
import cn.citycraft.PluginHelper.sql.SQLHelper;
import cn.citycraft.PluginHelper.utils.StringUtil;
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 SQLHelper sql;
public static void changePassword(String username, String password) {
// TODO 处理玩家密码修改事件
}
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) {
CTZAuth.sql = sql;
}
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) {
return sql.dbUpdate(TableName, new KeyValue(ALLOWLoginField, "1"), 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)));
}
}

View File

@ -1,260 +0,0 @@
package cn.citycraft.CTZServer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.bukkit.configuration.ConfigurationSection;
import com.google.gson.Gson;
public class CTZServer {
protected static Logger log = new Logger();
static ServerInfo sl = new ServerInfo();
static Gson gson = new Gson();
/**
* 把中文转成Unicode码
*
* @param str
* - 待转换的字符串
* @return 转换后的字符串
*/
public static String chinaToUnicode(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int chr1 = str.charAt(i);
if (chr1 >= 19968 && chr1 <= 171941)
result += "\\u" + Integer.toHexString(chr1);
else
result += str.charAt(i);
}
return result;
}
/**
* 获得json字符串
*
* @return
*/
public static String getJson() {
return chinaToUnicode(gson.toJson(sl));
}
// public static Logger getLogger() {
// return log;
// }
/**
* 初始化服务器序列化类
*
* @param 配置类
*/
public static void Init(ConfigurationSection cs) {
Set<String> arealist = cs.getKeys(false);
for (String a : arealist) {
Area area = new Area();
area.setName(cs.getString(a + ".name"));
Set<String> serverlist = cs.getConfigurationSection(a + ".servers").getKeys(false);
for (String s : serverlist) {
Server server = new Server();
server.name = cs.getString(a + ".servers." + s + ".name");
server.address = cs.getString(a + ".servers." + s + ".address");
server.port = cs.getInt(a + ".servers." + s + ".port");
server.version = cs.getString(a + ".servers." + s + ".version");
server.info = cs.getString(a + ".servers." + s + ".info");
server.url = cs.getString(a + ".servers." + s + ".url");
server.depend = cs.getString(a + ".servers." + s + ".depend");
area.servers.add(server);
}
sl.areas.add(area);
}
}
/**
* 初始化服务器序列化类
*
* @param json
* - json字符串
*/
public static boolean Init(String json) {
try {
sl = gson.fromJson(json, ServerInfo.class);
return true;
} catch (Exception e) {
return false;
}
}
public static void print(String message) {
log.info(message);
}
public static void warn(String message) {
log.warning(message);
}
/**
* 判断是否为中文字符
*
* @param c
* @return
*/
public boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
return true;
return false;
}
}
class Area {
/**
* 大区名称
*/
String name;
/**
* 服务器信息
*/
List<Server> servers = new ArrayList<Server>();
public String getName() {
return name;
}
public List<Server> getServers() {
return servers;
}
public void setName(String name) {
this.name = name;
}
public void setServers(List<Server> servers) {
this.servers = servers;
}
}
/**
* 服务器数据序列化类
*
* @author 蒋天蓓
* 2015年8月14日下午4:36:12
*
*/
class Server {
/**
* 服务器名称
*/
public String name;
/**
* 服务器地址
*/
public String address;
/**
* 服务器端口
*/
public int port;
/**
* 服务器描述
*/
public String info;
/**
* 服务器版本
*/
public String version;
/**
* 版本下载地址
*/
public String url;
/**
* 依赖版本
*/
public String depend;
public String getAddress() {
return address;
}
public String getDepend() {
return depend;
}
public String getInfo() {
return info;
}
public String getName() {
return name;
}
public int getPort() {
return port;
}
public String getUrl() {
return url;
}
public String getVersion() {
return version;
}
public void setAddress(String address) {
this.address = address;
}
public void setDepend(String depend) {
this.depend = depend;
}
public void setInfo(String info) {
this.info = info;
}
public void setName(String name) {
this.name = name;
}
public void setPort(int port) {
this.port = port;
}
public void setUrl(String url) {
this.url = url;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return String.format("{0}:{1}", address, port);
}
}
class ServerInfo {
/**
* 服务器分区信息
*/
List<Area> areas = new ArrayList<Area>();
public List<Area> getAreas() {
return areas;
}
public void setAreas(List<Area> aareas) {
areas = aareas;
}
}

View File

@ -1,41 +0,0 @@
package cn.citycraft.CTZServer;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.md_5.bungee.api.ChatColor;
public class Logger {
enum LEVEL {
INFO("信息"),
WARN("警告"),
ERROR("错误");
String prefix;
LEVEL(String prefix) {
this.prefix = prefix;
}
@Override
public String toString() {
return prefix;
}
}
public void error(String string) {
log(LEVEL.ERROR, string);
}
public void info(String string) {
log(LEVEL.INFO, string);
}
public void log(LEVEL lvl, String string) {
String time = new SimpleDateFormat("HH:mm:ss").format(new Date(System.currentTimeMillis()));
System.out.println(String.format("[%1$s %2$s] %3$s", time, lvl, ChatColor.stripColor(string)));
}
public void warning(String string) {
log(LEVEL.WARN, string);
}
}

View File

@ -7,8 +7,8 @@ import java.io.InputStreamReader;
import cn.citycraft.CTZServer.commands.HandlerCommand;
import cn.citycraft.CTZServer.socket.CTZLoginServerSocket;
import cn.citycraft.PluginHelper.sql.KeyValue;
import cn.citycraft.PluginHelper.sql.MySQLHelper;
import cn.citycraft.CTZServerCommon.CTZAuth;
import cn.citycraft.CTZServerCommon.CTZServer;
import cn.citycraft.PluginHelper.sql.SQLHelper;
import cn.citycraft.PluginHelper.utils.FileUtil;
import net.md_5.bungee.api.ChatColor;
@ -28,9 +28,8 @@ public class ServerThread implements Runnable {
CTZServer.print(ChatColor.GREEN + "服务器开始启动...");
initCommand();
initDatabase();
initServerInfo();
CTZAuth.init(sql);
CTZAuth.init(sql, "127.0.0.1", 3306, "ctzserver", "root", "325325");
server = new CTZLoginServerSocket();
server.start();
@ -58,39 +57,6 @@ public class ServerThread implements Runnable {
handlercmd = new HandlerCommand(this);
}
/**
* 初始化数据库
*/
void initDatabase() {
String dbtable = "ctzserver";
CTZServer.print(ChatColor.GREEN + "初始化数据库连接...");
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "325325");
if (!sql.dbConnection()) {
CTZServer.warn(ChatColor.RED + "数据库连接失败...");
return;
}
CTZServer.print(ChatColor.GREEN + "数据库连接成功,检查数据表是否存在...");
if (!sql.isTableExists(dbtable)) {
CTZServer.print(ChatColor.RED + "数据表不存在,新建表" + dbtable + "...");
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("lastloginout", "BIGINT(20) 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(dbtable, kv, Conditions))
CTZServer.warn(ChatColor.RED + "数据表 " + dbtable + " 创建失败,请尝试手动创建并重启服务器...");
else
CTZServer.print(ChatColor.GREEN + "数据表 " + dbtable + " 创建成功...");
}
}
/**
* 初始化服务器信息
*/

View File

@ -1,7 +1,7 @@
package cn.citycraft.CTZServer.commands;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.ServerThread;
import cn.citycraft.CTZServerCommon.CTZServer;
import cn.citycraft.PluginHelper.utils.StringUtil;
import cn.citycraft.PluginHelper.utils.SystemUtil;
import net.md_5.bungee.api.ChatColor;

View File

@ -1,8 +1,8 @@
package cn.citycraft.CTZServer.commands;
import cn.citycraft.CTZServer.CTZAuth;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.ServerThread;
import cn.citycraft.CTZServerCommon.CTZAuth;
import cn.citycraft.CTZServerCommon.CTZServer;
import net.md_5.bungee.api.ChatColor;
public class CommandRegister extends BaseCommand {

View File

@ -1,7 +1,7 @@
package cn.citycraft.CTZServer.commands;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.ServerThread;
import cn.citycraft.CTZServerCommon.CTZServer;
import net.md_5.bungee.api.ChatColor;
public class CommandStop extends BaseCommand {

View File

@ -3,8 +3,8 @@ package cn.citycraft.CTZServer.commands;
import java.util.ArrayList;
import java.util.List;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.ServerThread;
import cn.citycraft.CTZServerCommon.CTZServer;
import cn.citycraft.PluginHelper.utils.StringUtil;
import net.md_5.bungee.api.ChatColor;

View File

@ -8,9 +8,9 @@ import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import cn.citycraft.CTZServer.CTZAuth;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.socket.Response.HttpStates;
import cn.citycraft.CTZServerCommon.CTZAuth;
import cn.citycraft.CTZServerCommon.CTZServer;
import net.md_5.bungee.api.ChatColor;
public class CTZLoginServerSocket extends Thread {
@ -124,7 +124,7 @@ public class CTZLoginServerSocket extends Thread {
}
if (CTZAuth.checkPassword(username, password)) {
res.setHtml("true");
// TODO 是否检查登录IP
CTZAuth.login(username, ip);
CTZServer.print("§6玩家: §a" + username + " §3登录成功 IP: " + ip);
} else
res.setHtml("false");