109 lines
3.8 KiB
Java
109 lines
3.8 KiB
Java
package cn.citycraft.CTZLoginServer;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.plugin.PluginManager;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import cn.citycraft.CTZLoginServer.Socket.CTZLoginServerSocket;
|
|
import cn.citycraft.CTZLoginServer.config.Config;
|
|
import cn.citycraft.CTZLoginServer.listen.PlayerListen;
|
|
import cn.citycraft.CTZLoginServer.utils.MySqlHelper;
|
|
|
|
public class CTZLoginServer extends JavaPlugin {
|
|
public String version;
|
|
public ServerMode servermode;
|
|
public CTZLoginServerSocket serversocket;
|
|
public static MySqlHelper mysql = null;
|
|
|
|
enum ServerMode {
|
|
Main, Obey
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
serversocket.ShutDown();
|
|
getLogger().info("CTZLoginServer已卸载!");
|
|
}
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
PluginManager pm = Bukkit.getPluginManager();
|
|
version = this.getServer().getBukkitVersion().substring(0, 5);
|
|
this.getLogger().info("Bukkit 版本: " + version);
|
|
if (pm.isPluginEnabled("AuthMe")) {
|
|
this.getLogger().info("发现前置插件 AuthMe 载入API...");
|
|
} else {
|
|
this.getLogger().info("未发现 前置插件 AuthMe 关闭插件...");
|
|
pm.disablePlugin(this);
|
|
return;
|
|
}
|
|
if (servermode == ServerMode.Main) {
|
|
getLogger().info("主模式启动服务器...");
|
|
this.onLoadLoginServer();
|
|
} else {
|
|
getLogger().info("子模式启动监听器...");
|
|
}
|
|
if (Config.getInstance().getBoolean("config.mysql.enable")) {
|
|
this.getLogger().info("已启用MySQL保存数据,开始连接数据库...");
|
|
|
|
// 连接数据库用到的一些参数.
|
|
String dbHost = Config.getMessage("config.mysql.ip");
|
|
String dbPort = Config.getMessage("config.mysql.port");
|
|
String dbName = Config.getMessage("config.mysql.database");
|
|
String dbuserName = Config.getMessage("config.mysql.username");
|
|
String dbpwd = Config.getMessage("config.mysql.password");
|
|
String dbtable = Config.getMessage("config.mysql.tablename");
|
|
mysql = new MySqlHelper(dbHost, dbPort, dbName, dbuserName, dbpwd);
|
|
if (mysql.dbConnection()) {
|
|
this.getLogger().info("数据库连接成功,检查数据表是否存在...");
|
|
if (!mysql.isTableExists(dbtable)) {
|
|
this.getLogger().info("数据表不存在,新建表" + dbtable + "...");
|
|
HashMap<String, String> fields = new HashMap<String, String>();
|
|
fields.put("player", "VARCHAR(255) NOT NULL");
|
|
fields.put("ip", "NOT NULL DEFAULT '127.0.0.1'");
|
|
fields.put("islogged", "SMALLINT(6) NOT NULL DEFAULT '0'");
|
|
String Conditions = "UNIQUE (`player`)";
|
|
if (!mysql.createTables(dbtable, fields, Conditions)) {
|
|
this.getLogger().info("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器...");
|
|
}
|
|
}
|
|
} else {
|
|
this.getLogger().warning("数据库连接失败!");
|
|
pm.disablePlugin(this);
|
|
return;
|
|
}
|
|
}
|
|
pm.registerEvents(new PlayerListen(), this);
|
|
getLogger().info("CTZLoginServer已加载!");
|
|
}
|
|
|
|
@Override
|
|
public void onLoad() {
|
|
Config.load(this, "1.0");
|
|
CTZServers.Init(Config.getInstance().getConfigurationSection("areas"));
|
|
if (Config.getMessage("config.mode").equalsIgnoreCase("main"))
|
|
servermode = ServerMode.Main;
|
|
else
|
|
servermode = ServerMode.Obey;
|
|
}
|
|
|
|
public void onLoadLoginServer() {
|
|
if (serversocket != null) {
|
|
serversocket.ShutDown();
|
|
getLogger().info("CTZL登录服务器已关闭!");
|
|
}
|
|
serversocket = new CTZLoginServerSocket(this, Config.getInstance().getInt("config.port", 25580));
|
|
this.getServer().getScheduler().runTaskAsynchronously(this, serversocket);
|
|
getLogger().info("CTZL登录服务器已开启!");
|
|
}
|
|
}
|