mirror of
https://e.coding.net/circlecloud/CTZServerCommon.git
synced 2024-11-22 13:28:47 +00:00
init project ...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
commit
cc0f1e71d9
26
.classpath
Normal file
26
.classpath
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
# Eclipse stuff
|
||||
/.settings
|
||||
|
||||
# netbeans
|
||||
/nbproject
|
||||
|
||||
# we use maven!
|
||||
/build.xml
|
||||
|
||||
# maven
|
||||
/target
|
||||
/repo
|
||||
|
||||
# vim
|
||||
.*.sw[a-p]
|
||||
|
||||
# various other potential build files
|
||||
/build
|
||||
/bin
|
||||
/dist
|
||||
/manifest.mf
|
||||
|
||||
/world
|
||||
|
||||
# Mac filesystem dust
|
||||
*.DS_Store
|
||||
|
||||
# intellij
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# Project Stuff
|
||||
/src/main/resources/Soulbound
|
||||
|
||||
# Atlassian Stuff
|
||||
/atlassian-ide-plugin.xml
|
23
.project
Normal file
23
.project
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>CTZServerCommon</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
64
pom.xml
Normal file
64
pom.xml
Normal file
@ -0,0 +1,64 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>cn.citycraft</groupId>
|
||||
<artifactId>CTZServerCommon</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>CTZServerCommon</name>
|
||||
<build>
|
||||
<finalName>${project.name}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<artifactSet>
|
||||
<includes>
|
||||
<include>cn.citycraft:PluginHelper</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<relocation>
|
||||
<pattern>cn.citycraft.PluginHelper</pattern>
|
||||
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
|
||||
</relocation>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>citycraft-repo</id>
|
||||
<url>http://ci.citycraft.cn:8800/jenkins/plugin/repository/everything/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.citycraft</groupId>
|
||||
<artifactId>PluginHelper</artifactId>
|
||||
<type>jar</type>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
102
src/main/java/cn/citycraft/CTZServerCommon/CTZAuth.java
Normal file
102
src/main/java/cn/citycraft/CTZServerCommon/CTZAuth.java
Normal file
@ -0,0 +1,102 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
260
src/main/java/cn/citycraft/CTZServerCommon/CTZServer.java
Normal file
260
src/main/java/cn/citycraft/CTZServerCommon/CTZServer.java
Normal file
@ -0,0 +1,260 @@
|
||||
package cn.citycraft.CTZServerCommon;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
41
src/main/java/cn/citycraft/CTZServerCommon/Logger.java
Normal file
41
src/main/java/cn/citycraft/CTZServerCommon/Logger.java
Normal file
@ -0,0 +1,41 @@
|
||||
package cn.citycraft.CTZServerCommon;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user