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

add gc command...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-09-13 21:47:55 +08:00
parent 9374fa8dd0
commit 3301d2b7bf
8 changed files with 109 additions and 151 deletions

View File

@ -42,9 +42,9 @@ public class CTZServer {
return chinaToUnicode(gson.toJson(sl));
}
public static Logger getLogger() {
return log;
}
// public static Logger getLogger() {
// return log;
// }
/**
* 初始化服务器序列化类
@ -87,6 +87,14 @@ public class CTZServer {
}
}
public static void print(String message) {
log.info(message);
}
public static void warn(String message) {
log.warning(message);
}
/**
* 判断是否为中文字符
*

View File

@ -3,9 +3,25 @@ package cn.citycraft.CTZServer;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.bukkit.ChatColor;
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);
}
@ -22,20 +38,4 @@ public class Logger {
public void warning(String string) {
log(LEVEL.WARN, string);
}
enum LEVEL {
INFO("信息"),
WARN("警告"),
ERROR("错误");
String prefix;
LEVEL(String prefix) {
this.prefix = prefix;
}
@Override
public String toString() {
return prefix;
}
}
}

View File

@ -5,14 +5,13 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.bukkit.ChatColor;
import cn.citycraft.CTZServer.commands.HandlerCommand;
import cn.citycraft.CTZServer.socket.CTZLoginServerSocket;
import cn.citycraft.sql.KeyValue;
import cn.citycraft.sql.MySQLHelper;
import cn.citycraft.sql.SQLHelper;
import cn.citycraft.utils.FileUtil;
import net.md_5.bungee.api.ChatColor;
public class ServerThread implements Runnable {
protected CTZLoginServerSocket server;
@ -26,7 +25,7 @@ public class ServerThread implements Runnable {
@Override
public void run() {
CTZServer.getLogger().info(ChatColor.GREEN + "服务器开始启动...");
CTZServer.print(ChatColor.GREEN + "服务器开始启动...");
initCommand();
initDatabase();
@ -54,7 +53,7 @@ public class ServerThread implements Runnable {
* 初始化命令
*/
void initCommand() {
CTZServer.getLogger().info(ChatColor.GREEN + "初始化基础命令...");
CTZServer.print(ChatColor.GREEN + "初始化基础命令...");
handlercmd = new HandlerCommand(this);
}
@ -63,18 +62,18 @@ public class ServerThread implements Runnable {
*/
void initDatabase() {
String dbtable = "ctzserver";
CTZServer.getLogger().info(ChatColor.GREEN + "初始化数据库连接...");
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "");
CTZServer.print(ChatColor.GREEN + "初始化数据库连接...");
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "325325");
if (!sql.dbConnection()) {
CTZServer.getLogger().warning(ChatColor.RED + "数据库连接失败...");
CTZServer.warn(ChatColor.RED + "数据库连接失败...");
return;
}
CTZServer.getLogger().info("数据库连接成功,检查数据表是否存在...");
CTZServer.print("数据库连接成功,检查数据表是否存在...");
if (!sql.isTableExists(dbtable)) {
CTZServer.getLogger().info("数据表不存在,新建表" + dbtable + "...");
CTZServer.print("数据表不存在,新建表" + dbtable + "...");
KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL")
.add("password", "VARCHAR(50) NOT NULL")
.add("ip", "varchar(40) NOT NULL")
.add("ip", "VARCHAR(40) NOT NULL")
.add("lastloginout", "BIGINT(20) NOT NULL")
.add("x", "DOUBLE NOT NULL")
.add("y", "DOUBLE NOT NULL")
@ -84,7 +83,9 @@ public class ServerThread implements Runnable {
.add("islogged", "SMALLINT(6) NOT NULL");
String Conditions = "UNIQUE (`player`)";
if (!sql.createTables(dbtable, kv, Conditions))
CTZServer.getLogger().warning("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器...");
CTZServer.warn("数据表 " + dbtable + " 创建失败,请尝试手动创建并重启服务器...");
else
CTZServer.print("数据表 " + dbtable + " 创建成功...");
}
}
@ -92,12 +93,12 @@ public class ServerThread implements Runnable {
* 初始化服务器信息
*/
void initServerInfo() {
CTZServer.getLogger().info(ChatColor.GREEN + "加载服务器信息...");
CTZServer.print(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 + "服务器信息加载成功...");
CTZServer.print(ChatColor.GREEN + "服务器信息加载成功...");
else
CTZServer.getLogger().warning(ChatColor.RED + "服务器信息加载失败...");
CTZServer.warn(ChatColor.RED + "服务器信息加载失败...");
}
}

View File

@ -1,9 +1,8 @@
package cn.citycraft.CTZServer.commands;
import org.bukkit.ChatColor;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.ServerThread;
import net.md_5.bungee.api.ChatColor;
public class CommandStop extends BaseCommand {
ServerThread serverThread;
@ -15,7 +14,7 @@ public class CommandStop extends BaseCommand {
@Override
public void execute(String label, String[] args) {
CTZServer.getLogger().info(ChatColor.RED + "开始关闭服务器...");
CTZServer.print(ChatColor.RED + "开始关闭服务器...");
serverThread.getServer().ShutDown();
}
@ -31,7 +30,7 @@ public class CommandStop extends BaseCommand {
@Override
public String getPossibleArguments() {
return null;
return "";
}
}

View File

@ -3,11 +3,10 @@ package cn.citycraft.CTZServer.commands;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.ServerThread;
import cn.citycraft.CTZServer.utils.StringUtil;
import cn.citycraft.utils.StringUtil;
import net.md_5.bungee.api.ChatColor;
public class HandlerCommand {
ServerThread serverThread;
@ -24,10 +23,17 @@ public class HandlerCommand {
public HandlerCommand(ServerThread serverThread) {
this.serverThread = serverThread;
registerCommand(new CommandStop(serverThread));
registerCommand(new CommandGc(serverThread));
}
public boolean execute(String cmd, String[] args) {
String subcmd = args[0];
if (subcmd.equalsIgnoreCase("help")) {
CTZServer.print("§6=========CTZ服务器帮助列表=========");
for (BaseCommand command : commandlist)
CTZServer.print(String.format("§6/yum §a%1$s %2$s §6- §b%3$s", command.getName(), command.getPossibleArguments(), command.getDescription()));
return true;
}
String[] subargs = StringUtil.moveStrings(args, 1);
for (BaseCommand command : commandlist)
if (command.isValidTrigger(subcmd))
@ -35,8 +41,8 @@ public class HandlerCommand {
command.execute(subcmd, subargs);
return true;
} else
CTZServer.getLogger().info(ChatColor.RED + "错误的参数 " + ChatColor.YELLOW + "使用方法 /yum " + command.getName() + command.getPossibleArguments());
CTZServer.getLogger().info("未知命令 请使用help查看帮助...");
CTZServer.print(ChatColor.RED + "错误的参数 " + ChatColor.YELLOW + "使用方法 /yum " + command.getName() + command.getPossibleArguments());
CTZServer.print("未知命令 请使用help查看帮助...");
return false;
}

View File

@ -8,61 +8,12 @@ import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import org.bukkit.ChatColor;
import cn.citycraft.CTZServer.CTZAuth;
import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.socket.Response.HttpStates;
import net.md_5.bungee.api.ChatColor;
public class CTZLoginServerSocket extends Thread {
ServerSocket s = null;
Socket socket = null;
int port = 25580;
/**
* 初始化服务端口
*/
public CTZLoginServerSocket() {
}
/**
* 初始化服务端口
*/
public CTZLoginServerSocket(int port) {
this.port = port;
}
@Override
public void run() {
try {
// 设定服务端的端口号
s = new ServerSocket(port);
CTZServer.getLogger().info(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
while (true) {
socket = s.accept();
new ClientThread(socket).start();
}
} catch (Exception e) {
CTZServer.getLogger().warning(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
} finally {
ShutDown();
}
}
/**
* 关闭服务器
*/
public void ShutDown() {
try {
socket.close();
s.close();
} catch (Exception e) {
}
CTZServer.getLogger().info(ChatColor.RED + "CTZ服务器已关闭...");
System.exit(0);
}
class ClientThread extends Thread {
Socket client = null;
BufferedReader br = null;
@ -110,7 +61,7 @@ public class CTZLoginServerSocket extends Thread {
}
if (CTZAuth.registerPlayer(username, password)) {
res.setHtml("true");
CTZServer.getLogger().info("§6玩家: §a" + username + " §d注册成功 IP: " + ip);
CTZServer.print("§6玩家: §a" + username + " §d注册成功 IP: " + ip);
} else
res.setHtml("false");
break;
@ -124,7 +75,7 @@ public class CTZLoginServerSocket extends Thread {
if (CTZAuth.checkPassword(username, password)) {
res.setHtml("true");
// TODO 是否检查登录IP
CTZServer.getLogger().info("§6玩家: §a" + username + " §3登录成功 IP: " + ip);
CTZServer.print("§6玩家: §a" + username + " §3登录成功 IP: " + ip);
} else
res.setHtml("false");
break;
@ -172,4 +123,53 @@ public class CTZLoginServerSocket extends Thread {
}
}
}
ServerSocket s = null;
Socket socket = null;
int port = 25580;
/**
* 初始化服务端口
*/
public CTZLoginServerSocket() {
}
/**
* 初始化服务端口
*/
public CTZLoginServerSocket(int port) {
this.port = port;
}
@Override
public void run() {
try {
// 设定服务端的端口号
s = new ServerSocket(port);
CTZServer.print(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
while (true) {
socket = s.accept();
new ClientThread(socket).start();
}
} catch (Exception e) {
CTZServer.print(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
} finally {
ShutDown();
}
}
/**
* 关闭服务器
*/
public void ShutDown() {
try {
socket.close();
s.close();
} catch (Exception e) {
}
CTZServer.print(ChatColor.RED + "CTZ服务器已关闭...");
System.exit(0);
}
}

View File

@ -1,56 +0,0 @@
package cn.citycraft.CTZServer.utils;
import java.util.Collection;
/**
* 字符串工具
*
* @author 蒋天蓓
* 2015年8月22日下午12:41:59
*/
public class StringUtil {
/**
* 转移数组后获取字符串
*
* @param args
* - 原数组
* @param start
* - 数组开始位置
* @return 转移后的数组字符串
*/
public static String consolidateStrings(String[] args, int start) {
String ret = args[start];
if (args.length > start + 1)
for (int i = start + 1; i < args.length; i++)
ret = ret + " " + args[i];
return ret;
}
/**
* 转移数组
*
* @param args
* - 原数组
* @param start
* - 数组开始位置
* @return 转移后的数组字符串
*/
public static String[] moveStrings(String[] args, int start) {
String[] ret = new String[args.length - start];
System.arraycopy(args, start, ret, 0, ret.length);
return ret;
}
/**
* 给玩家或控制台发送消息组
*
* @param sender
* - 接收消息的玩家
* @param msg
* - 消息组
*/
public static void sendStringArray(Collection<String> msg) {
for (String string : msg)
System.out.println(string);
}
}