mirror of
https://e.coding.net/circlecloud/CTZLoginServer.git
synced 2025-01-07 12:39:01 +00:00
add Login and register...
Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
parent
49198264f3
commit
1aede8f0e3
@ -3,8 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
package cn.citycraft.CTZLoginServer;
|
package cn.citycraft.CTZLoginServer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 蒋天蓓
|
* @author 蒋天蓓
|
||||||
@ -12,14 +11,36 @@ import java.util.List;
|
|||||||
* TODO
|
* TODO
|
||||||
*/
|
*/
|
||||||
public class CTZLoginQueue {
|
public class CTZLoginQueue {
|
||||||
protected static List<String> list = new ArrayList<String>();
|
protected static boolean checkIP = true;;
|
||||||
|
|
||||||
|
public static boolean isCheckIP() {
|
||||||
|
return checkIP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setCheckIP(boolean checkIP) {
|
||||||
|
CTZLoginQueue.checkIP = checkIP;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static HashMap<String, String> list = new HashMap<String, String>();
|
||||||
|
|
||||||
public static boolean Login(String player) {
|
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) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package cn.citycraft.CTZLoginServer;
|
package cn.citycraft.CTZLoginServer;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import cn.citycraft.CTZLoginServer.Socket.CTZLoginServerSocket;
|
import cn.citycraft.CTZLoginServer.Socket.CTZLoginServerSocket;
|
||||||
import cn.citycraft.CTZLoginServer.config.Config;
|
import cn.citycraft.CTZLoginServer.config.Config;
|
||||||
|
import cn.citycraft.CTZLoginServer.listen.PlayerListen;
|
||||||
|
|
||||||
public class CTZLoginServer extends JavaPlugin {
|
public class CTZLoginServer extends JavaPlugin {
|
||||||
public String version;
|
public String version;
|
||||||
@ -15,18 +18,21 @@ public class CTZLoginServer extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
// PluginManager pm = Bukkit.getPluginManager();
|
PluginManager pm = Bukkit.getPluginManager();
|
||||||
version = this.getServer().getBukkitVersion().substring(0, 5);
|
version = this.getServer().getBukkitVersion().substring(0, 5);
|
||||||
this.getLogger().info("Bukkit 版本: " + version);
|
this.getLogger().info("Bukkit 版本: " + version);
|
||||||
// if (pm.isPluginEnabled("AuthMe")) {
|
if (pm.isPluginEnabled("AuthMe")) {
|
||||||
// this.getLogger().info("发现前置插件 AuthMe 载入API...");
|
this.getLogger().info("发现前置插件 AuthMe 载入API...");
|
||||||
// } else {
|
} else {
|
||||||
// this.getLogger().info("未发现 前置插件 AuthMe 关闭插件...");
|
this.getLogger().info("未发现 前置插件 AuthMe 关闭插件...");
|
||||||
// pm.disablePlugin(this);
|
pm.disablePlugin(this);
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
this.getServer().getScheduler()
|
this.getServer()
|
||||||
.runTaskAsynchronously(this, new CTZLoginServerSocket(this, 2000));
|
.getScheduler()
|
||||||
|
.runTaskAsynchronously(this,
|
||||||
|
new CTZLoginServerSocket(this, Config.getInstance().getInt("server.port")));
|
||||||
|
pm.registerEvents(new PlayerListen(), this);
|
||||||
getLogger().info("已加载!");
|
getLogger().info("已加载!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,13 +10,14 @@ import java.net.Socket;
|
|||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
import cn.citycraft.CTZLoginServer.CTZLoginQueue;
|
||||||
import cn.citycraft.CTZLoginServer.CTZLoginServer;
|
import cn.citycraft.CTZLoginServer.CTZLoginServer;
|
||||||
import cn.citycraft.CTZLoginServer.Socket.Response.HttpStates;
|
import cn.citycraft.CTZLoginServer.Socket.Response.HttpStates;
|
||||||
import fr.xephi.authme.api.API;
|
import fr.xephi.authme.api.API;
|
||||||
|
|
||||||
public class CTZLoginServerSocket implements Runnable {
|
public class CTZLoginServerSocket implements Runnable {
|
||||||
|
|
||||||
public int PORT = 8080;
|
public int port = 8080;
|
||||||
public CTZLoginServer plugin;
|
public CTZLoginServer plugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,7 +25,7 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
*/
|
*/
|
||||||
public CTZLoginServerSocket(CTZLoginServer plugin, int port) {
|
public CTZLoginServerSocket(CTZLoginServer plugin, int port) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
PORT = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -34,7 +35,7 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 设定服务端的端口号
|
// 设定服务端的端口号
|
||||||
s = new ServerSocket(2000);
|
s = new ServerSocket(port);
|
||||||
System.out.println("CTZL服务器开始监听 端口:" + s.getLocalPort());
|
System.out.println("CTZL服务器开始监听 端口:" + s.getLocalPort());
|
||||||
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
|
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -57,6 +58,7 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
Socket client = null;
|
Socket client = null;
|
||||||
BufferedReader br = null;
|
BufferedReader br = null;
|
||||||
PrintWriter pw = null;
|
PrintWriter pw = null;
|
||||||
|
String ip = null;
|
||||||
|
|
||||||
public ClientThread(Socket client) {
|
public ClientThread(Socket client) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
@ -70,6 +72,7 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
// 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
|
// 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
|
||||||
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
|
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
|
||||||
client.getOutputStream())), true);
|
client.getOutputStream())), true);
|
||||||
|
ip = client.getInetAddress().getHostAddress();
|
||||||
while (true) {
|
while (true) {
|
||||||
String str = br.readLine();
|
String str = br.readLine();
|
||||||
if (str != null) {
|
if (str != null) {
|
||||||
@ -85,11 +88,11 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
res.setState(HttpStates.Bad_Request);
|
res.setState(HttpStates.Bad_Request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Bukkit.getConsoleSender().sendMessage(
|
if (API.isRegistered(username)) {
|
||||||
"§6玩家: §a" + username + " §6请求注册检测!");
|
|
||||||
if (API.isRegistered(username))
|
|
||||||
res.setHtml("true");
|
res.setHtml("true");
|
||||||
else
|
Bukkit.getConsoleSender().sendMessage(
|
||||||
|
"§6玩家: §a" + username + " §d注册成功 IP: " + ip);
|
||||||
|
} else
|
||||||
res.setHtml("false");
|
res.setHtml("false");
|
||||||
break;
|
break;
|
||||||
case "register":
|
case "register":
|
||||||
@ -99,8 +102,6 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
res.setState(HttpStates.Bad_Request);
|
res.setState(HttpStates.Bad_Request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Bukkit.getConsoleSender().sendMessage(
|
|
||||||
"§6玩家: §a" + username + " §6请求注册!");
|
|
||||||
if (API.registerPlayer(username, password))
|
if (API.registerPlayer(username, password))
|
||||||
res.setHtml("true");
|
res.setHtml("true");
|
||||||
else
|
else
|
||||||
@ -113,12 +114,15 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
res.setState(HttpStates.Bad_Request);
|
res.setState(HttpStates.Bad_Request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Bukkit.getConsoleSender().sendMessage(
|
if (API.checkPassword(username, password)) {
|
||||||
"§6玩家: §a" + username + " §6请求登录!");
|
|
||||||
if (API.checkPassword(username, password))
|
|
||||||
res.setHtml("true");
|
res.setHtml("true");
|
||||||
// TODO 处理玩家后续登录事件
|
if (CTZLoginQueue.isCheckIP())
|
||||||
else
|
CTZLoginQueue.add(username, ip);
|
||||||
|
else
|
||||||
|
CTZLoginQueue.add(username);
|
||||||
|
Bukkit.getConsoleSender().sendMessage(
|
||||||
|
"§6玩家: §a" + username + " §3登录成功 IP: " + ip);
|
||||||
|
} else
|
||||||
res.setHtml("false");
|
res.setHtml("false");
|
||||||
break;
|
break;
|
||||||
case "islogin":
|
case "islogin":
|
||||||
@ -127,8 +131,6 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
res.setState(HttpStates.Bad_Request);
|
res.setState(HttpStates.Bad_Request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Bukkit.getConsoleSender().sendMessage(
|
|
||||||
"§6玩家: §a" + username + " §6请求登录检测!");
|
|
||||||
if (Bukkit.getPlayer(username) != null)
|
if (Bukkit.getPlayer(username) != null)
|
||||||
res.setHtml("true");
|
res.setHtml("true");
|
||||||
else
|
else
|
||||||
@ -137,7 +139,7 @@ public class CTZLoginServerSocket implements Runnable {
|
|||||||
case "changepassword":
|
case "changepassword":
|
||||||
username = req.Query("username");
|
username = req.Query("username");
|
||||||
password = req.Query("password");
|
password = req.Query("password");
|
||||||
if (username == null) {
|
if (username == null || password == null) {
|
||||||
res.setState(HttpStates.Bad_Request);
|
res.setState(HttpStates.Bad_Request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,16 @@
|
|||||||
*/
|
*/
|
||||||
package cn.citycraft.CTZLoginServer.listen;
|
package cn.citycraft.CTZLoginServer.listen;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
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.CTZLoginQueue;
|
||||||
|
import cn.citycraft.CTZLoginServer.config.Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 蒋天蓓
|
* @author 蒋天蓓
|
||||||
@ -15,11 +20,34 @@ import cn.citycraft.CTZLoginServer.CTZLoginQueue;
|
|||||||
* TODO
|
* TODO
|
||||||
*/
|
*/
|
||||||
public class PlayerListen implements Listener {
|
public class PlayerListen implements Listener {
|
||||||
|
protected HashMap<String, Long> lastquittime = new HashMap<String, Long>();
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void PlayerLogin(PlayerJoinEvent e) {
|
public void PlayerLogin(PlayerLoginEvent e) {
|
||||||
if (!CTZLoginQueue.Login(e.getPlayer().getName())) {
|
String name = e.getPlayer().getName().toLowerCase();
|
||||||
e.getPlayer().kickPlayer("&c请从服务器专用启动器进入游戏!");
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,5 +8,7 @@ pluginname: '&6[&bCTZL&6]&r'
|
|||||||
tipplayer: true
|
tipplayer: true
|
||||||
#服务器配置
|
#服务器配置
|
||||||
server:
|
server:
|
||||||
port: 20000
|
#服务器监听端口
|
||||||
timeout: 120
|
port: 25580
|
||||||
|
#断线重连超时
|
||||||
|
timeout: 10
|
Loading…
Reference in New Issue
Block a user