From 4fc551baeb567f8b81ae97918309707d67515a8b Mon Sep 17 00:00:00 2001 From: j502647092 Date: Mon, 27 Jul 2015 21:14:51 +0800 Subject: [PATCH] change depend fix listen... --- .../CTZLoginServer/CTZLoginServer.java | 32 ++++---- .../Socket/CTZLoginServerSocket.java | 75 +++++++++++-------- src/plugin.yml | 2 +- 3 files changed, 59 insertions(+), 50 deletions(-) diff --git a/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java b/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java index 44db25b..56b654e 100644 --- a/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java +++ b/src/cn/citycraft/CTZLoginServer/CTZLoginServer.java @@ -1,15 +1,13 @@ 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; - -public class CTZLoginServer extends JavaPlugin{ +public class CTZLoginServer extends JavaPlugin { public String version; - + @Override public void onDisable() { getLogger().info("已卸载!"); @@ -17,17 +15,18 @@ 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; - } - + // 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)); getLogger().info("已加载!"); } @@ -36,8 +35,3 @@ public class CTZLoginServer extends JavaPlugin{ Config.load(this, "1.0"); } } - - - - - diff --git a/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java b/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java index 1c03adf..507aadb 100644 --- a/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java +++ b/src/cn/citycraft/CTZLoginServer/Socket/CTZLoginServerSocket.java @@ -8,53 +8,68 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; -public class CTZLoginServerSocket { +import cn.citycraft.CTZLoginServer.CTZLoginServer; +import fr.xephi.authme.api.API; - public static int PORT = 8080; - public static void main(String[] agrs) { +public class CTZLoginServerSocket implements Runnable { + + public int PORT = 8080; + public CTZLoginServer plugin; + + /** + * 初始化服务端口 + */ + public CTZLoginServerSocket(CTZLoginServer plugin, int port) { + this.plugin = plugin; + PORT = port; + } + + @Override + public void run() { ServerSocket s = null; Socket socket = null; BufferedReader br = null; PrintWriter pw = null; try { - //设定服务端的端口号 + // 设定服务端的端口号 s = new ServerSocket(PORT); - System.out.println("ServerSocket Start:"+s); - //等待请求,此方法会一直阻塞,直到获得请求才往下走 - socket = s.accept(); - System.out.println("Connection accept socket:"+socket); - //用于接收客户端发来的请求 - br = new BufferedReader(new InputStreamReader(socket.getInputStream())); - //用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法 - pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); - while(true){ - String str = br.readLine(); - if(str.equals("END")){ - break; + System.out.println("CTZL服务器开始监听:" + s); + // 等待请求,此方法会一直阻塞,直到获得请求才往下走 + while (true) { + socket = s.accept(); + // 用于接收客户端发来的请求 + br = new BufferedReader(new InputStreamReader(socket.getInputStream())); + // 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法 + pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( + socket.getOutputStream())), true); + while (true) { + if (socket.isClosed()) { + break; + } + String str = br.readLine(); + if (str.equals("END")) { + break; + } + Thread.sleep(1000); + API.isRegistered(str); + pw.println("Message Received"); + pw.flush(); } - System.out.println("Client Socket Message:"+str); - Thread.sleep(1000); - pw.println("Message Received"); - pw.flush(); } - + } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - }finally{ - System.out.println("Close....."); + System.out.println("CTZL服务器崩溃..."); + } finally { + System.out.println("关闭连接"); try { br.close(); pw.close(); socket.close(); s.close(); } catch (Exception e2) { - + } } } - + } - - - diff --git a/src/plugin.yml b/src/plugin.yml index 907ec5c..e0fb0d7 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,7 +1,7 @@ name: CTZLoginServer main: cn.citycraft.CTZLoginServer.CTZLoginServer version: 0.0.1 -depended: [Authme] +depend: [AuthMe] commands: ctzl: description: CTZLoginServer Reload.