diff --git a/src/cn/citycraft/CTZLoginServer/CTZLoginQueue.java b/src/cn/citycraft/CTZLoginServer/CTZLoginQueue.java index 550110c..8795523 100644 --- a/src/cn/citycraft/CTZLoginServer/CTZLoginQueue.java +++ b/src/cn/citycraft/CTZLoginServer/CTZLoginQueue.java @@ -3,8 +3,7 @@ */ package cn.citycraft.CTZLoginServer; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; /** * @author 蒋天蓓 @@ -12,14 +11,36 @@ import java.util.List; * TODO */ public class CTZLoginQueue { - protected static List list = new ArrayList(); + protected static boolean checkIP = true;; + + public static boolean isCheckIP() { + return checkIP; + } + + public static void setCheckIP(boolean checkIP) { + CTZLoginQueue.checkIP = checkIP; + } + + protected static HashMap list = new HashMap(); public static boolean Login(String player) { - return list.remove(player); + return list.remove(player.toLowerCase()) != null; + } + + public static boolean Login(String player, String IP) { + if (list.containsKey(player.toLowerCase()) + && list.get(player.toLowerCase()).equalsIgnoreCase(IP)) { + list.remove(player.toLowerCase()); + return true; + } + return false; } public static void add(String player) { - list.add(player); + list.put(player.toLowerCase(), null); } + public static void add(String player, String IP) { + list.put(player.toLowerCase(), IP); + } } diff --git a/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java b/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java index 56b654e..cfc817c 100644 --- a/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java +++ b/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java @@ -1,9 +1,12 @@ package cn.citycraft.CTZLoginServer; +import org.bukkit.Bukkit; +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; public class CTZLoginServer extends JavaPlugin { public String version; @@ -15,18 +18,21 @@ public class CTZLoginServer extends JavaPlugin { @Override public void onEnable() { - // PluginManager pm = Bukkit.getPluginManager(); + 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; - // } - this.getServer().getScheduler() - .runTaskAsynchronously(this, new CTZLoginServerSocket(this, 2000)); + if (pm.isPluginEnabled("AuthMe")) { + this.getLogger().info("发现前置插件 AuthMe 载入API..."); + } else { + this.getLogger().info("未发现 前置插件 AuthMe 关闭插件..."); + pm.disablePlugin(this); + return; + } + this.getServer() + .getScheduler() + .runTaskAsynchronously(this, + new CTZLoginServerSocket(this, Config.getInstance().getInt("server.port"))); + pm.registerEvents(new PlayerListen(), this); getLogger().info("已加载!"); } diff --git a/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java b/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java index 0efc33c..7ff2b3a 100644 --- a/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java +++ b/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java @@ -10,13 +10,14 @@ import java.net.Socket; import org.bukkit.Bukkit; +import cn.citycraft.CTZLoginServer.CTZLoginQueue; import cn.citycraft.CTZLoginServer.CTZLoginServer; import cn.citycraft.CTZLoginServer.Socket.Response.HttpStates; import fr.xephi.authme.api.API; public class CTZLoginServerSocket implements Runnable { - public int PORT = 8080; + public int port = 8080; public CTZLoginServer plugin; /** @@ -24,7 +25,7 @@ public class CTZLoginServerSocket implements Runnable { */ public CTZLoginServerSocket(CTZLoginServer plugin, int port) { this.plugin = plugin; - PORT = port; + this.port = port; } @Override @@ -34,7 +35,7 @@ public class CTZLoginServerSocket implements Runnable { try { // 设定服务端的端口号 - s = new ServerSocket(2000); + s = new ServerSocket(port); System.out.println("CTZL服务器开始监听 端口:" + s.getLocalPort()); // 等待请求,此方法会一直阻塞,直到获得请求才往下走 while (true) { @@ -57,6 +58,7 @@ public class CTZLoginServerSocket implements Runnable { Socket client = null; BufferedReader br = null; PrintWriter pw = null; + String ip = null; public ClientThread(Socket client) { this.client = client; @@ -70,6 +72,7 @@ public class CTZLoginServerSocket implements Runnable { // 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法 pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( client.getOutputStream())), true); + ip = client.getInetAddress().getHostAddress(); while (true) { String str = br.readLine(); if (str != null) { @@ -85,11 +88,11 @@ public class CTZLoginServerSocket implements Runnable { res.setState(HttpStates.Bad_Request); break; } - Bukkit.getConsoleSender().sendMessage( - "§6玩家: §a" + username + " §6请求注册检测!"); - if (API.isRegistered(username)) + if (API.isRegistered(username)) { res.setHtml("true"); - else + Bukkit.getConsoleSender().sendMessage( + "§6玩家: §a" + username + " §d注册成功 IP: " + ip); + } else res.setHtml("false"); break; case "register": @@ -99,8 +102,6 @@ public class CTZLoginServerSocket implements Runnable { res.setState(HttpStates.Bad_Request); break; } - Bukkit.getConsoleSender().sendMessage( - "§6玩家: §a" + username + " §6请求注册!"); if (API.registerPlayer(username, password)) res.setHtml("true"); else @@ -113,12 +114,15 @@ public class CTZLoginServerSocket implements Runnable { res.setState(HttpStates.Bad_Request); break; } - Bukkit.getConsoleSender().sendMessage( - "§6玩家: §a" + username + " §6请求登录!"); - if (API.checkPassword(username, password)) + if (API.checkPassword(username, password)) { res.setHtml("true"); - // TODO 处理玩家后续登录事件 - else + if (CTZLoginQueue.isCheckIP()) + CTZLoginQueue.add(username, ip); + else + CTZLoginQueue.add(username); + Bukkit.getConsoleSender().sendMessage( + "§6玩家: §a" + username + " §3登录成功 IP: " + ip); + } else res.setHtml("false"); break; case "islogin": @@ -127,8 +131,6 @@ public class CTZLoginServerSocket implements Runnable { res.setState(HttpStates.Bad_Request); break; } - Bukkit.getConsoleSender().sendMessage( - "§6玩家: §a" + username + " §6请求登录检测!"); if (Bukkit.getPlayer(username) != null) res.setHtml("true"); else @@ -137,7 +139,7 @@ public class CTZLoginServerSocket implements Runnable { case "changepassword": username = req.Query("username"); password = req.Query("password"); - if (username == null) { + if (username == null || password == null) { res.setState(HttpStates.Bad_Request); break; } diff --git a/src/cn/citycraft/CTZLoginServer/listen/PlayerListen.java b/src/cn/citycraft/CTZLoginServer/listen/PlayerListen.java index 3773516..965d6b0 100644 --- a/src/cn/citycraft/CTZLoginServer/listen/PlayerListen.java +++ b/src/cn/citycraft/CTZLoginServer/listen/PlayerListen.java @@ -3,11 +3,16 @@ */ package cn.citycraft.CTZLoginServer.listen; +import java.util.HashMap; + import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerLoginEvent.Result; +import org.bukkit.event.player.PlayerQuitEvent; import cn.citycraft.CTZLoginServer.CTZLoginQueue; +import cn.citycraft.CTZLoginServer.config.Config; /** * @author 蒋天蓓 @@ -15,11 +20,34 @@ import cn.citycraft.CTZLoginServer.CTZLoginQueue; * TODO */ public class PlayerListen implements Listener { + protected HashMap lastquittime = new HashMap(); @EventHandler - public void PlayerLogin(PlayerJoinEvent e) { - if (!CTZLoginQueue.Login(e.getPlayer().getName())) { - e.getPlayer().kickPlayer("&c请从服务器专用启动器进入游戏!"); + public void PlayerLogin(PlayerLoginEvent e) { + String name = e.getPlayer().getName().toLowerCase(); + long lasttime = lastquittime.get(name) == null ? 0 : lastquittime.get(name); + if (getNowTime() - lasttime < Config.getInstance().getInt("server.timeout")) { + return; } + if (CTZLoginQueue.isCheckIP()) { + if (CTZLoginQueue.Login(name, e.getAddress().getHostAddress())) { + return; + } + } else { + if (CTZLoginQueue.Login(name)) { + return; + } + } + e.setResult(Result.KICK_WHITELIST); + e.setKickMessage("§6[§bCTZLS§6] §c禁止入服 请使用服务器专用启动器进入游戏!"); + } + + @EventHandler + public void PlayerQuit(PlayerQuitEvent e) { + lastquittime.put(e.getPlayer().getName().toLowerCase(), getNowTime()); + } + + private long getNowTime() { + return System.currentTimeMillis() / 1000; } } diff --git a/src/config.yml b/src/config.yml index 5bd142f..6cbd0ab 100644 --- a/src/config.yml +++ b/src/config.yml @@ -8,5 +8,7 @@ pluginname: '&6[&bCTZL&6]&r' tipplayer: true #服务器配置 server: - port: 20000 - timeout: 120 \ No newline at end of file + #服务器监听端口 + port: 25580 + #断线重连超时 + timeout: 10 \ No newline at end of file