97 lines
2.7 KiB
Java
97 lines
2.7 KiB
Java
package cn.citycraft.CTZServer;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.util.HashMap;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import cn.citycraft.CTZServer.commands.HandlerCommand;
|
|
import cn.citycraft.CTZServer.socket.CTZLoginServerSocket;
|
|
import cn.citycraft.sql.MySQLHelper;
|
|
import cn.citycraft.sql.SQLHelper;
|
|
import cn.citycraft.utils.FileUtil;
|
|
|
|
public class ServerThread implements Runnable {
|
|
protected CTZLoginServerSocket server;
|
|
protected SQLHelper sql;
|
|
|
|
HandlerCommand handlercmd;
|
|
|
|
public CTZLoginServerSocket getServer() {
|
|
return server;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
CTZServer.getLogger().info(ChatColor.GREEN + "服务器开始启动...");
|
|
|
|
initCommand();
|
|
initDatabase();
|
|
initServerInfo();
|
|
|
|
server = new CTZLoginServerSocket();
|
|
server.start();
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
while (true) {
|
|
String cmdline;
|
|
try {
|
|
cmdline = br.readLine();
|
|
if (cmdline == null || cmdline.isEmpty())
|
|
continue;
|
|
String[] cmdargs = cmdline.split(" ");
|
|
String cmd = cmdargs[0];
|
|
handlercmd.execute(cmd, cmdargs);
|
|
} catch (IOException e) {
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 初始化命令
|
|
*/
|
|
void initCommand() {
|
|
CTZServer.getLogger().info(ChatColor.GREEN + "初始化基础命令...");
|
|
handlercmd = new HandlerCommand(this);
|
|
}
|
|
|
|
/**
|
|
* 初始化数据库
|
|
*/
|
|
void initDatabase() {
|
|
String dbtable = "ctzserver";
|
|
CTZServer.getLogger().info(ChatColor.GREEN + "初始化数据库连接...");
|
|
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "");
|
|
if (!sql.dbConnection()) {
|
|
CTZServer.getLogger().warning(ChatColor.RED + "数据库连接失败...");
|
|
return;
|
|
}
|
|
CTZServer.getLogger().info("数据库连接成功,检查数据表是否存在...");
|
|
if (!sql.isTableExists(dbtable)) {
|
|
CTZServer.getLogger().info("数据表不存在,新建表" + dbtable + "...");
|
|
HashMap<String, String> fields = new HashMap<String, String>();
|
|
fields.put("player", "VARCHAR(16) NOT NULL");
|
|
fields.put("prefix", "VARCHAR(600) NOT NULL");
|
|
String Conditions = "UNIQUE (`player`)";
|
|
if (!sql.createTables(dbtable, fields, Conditions))
|
|
CTZServer.getLogger().warning("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器...");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 初始化服务器信息
|
|
*/
|
|
void initServerInfo() {
|
|
CTZServer.getLogger().info(ChatColor.GREEN + "加载服务器信息...");
|
|
FileUtil.saveResource(this, "/servers.json", false);
|
|
String json = FileUtil.readFile(new File("servers.json"));
|
|
if (CTZServer.Init(json))
|
|
CTZServer.getLogger().info(ChatColor.GREEN + "服务器信息加载成功...");
|
|
else
|
|
CTZServer.getLogger().warning(ChatColor.RED + "服务器信息加载失败...");
|
|
}
|
|
}
|