Skip to content

Commit

Permalink
优化日志输出,重构代码结构,提高代码可读性和性能。
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed May 29, 2024
1 parent 6fb4bff commit 38ddf7f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 78 deletions.
3 changes: 2 additions & 1 deletion src/main/java/cc/baka9/catseedlogin/bungee/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Commands extends net.md_5.bungee.api.plugin.Command {

public Commands(String name, String permission, String... aliases) {
super(name, permission, aliases);
Config.load(); // 加载配置
}

@Override
Expand All @@ -14,4 +15,4 @@ public void execute(CommandSender commandSender, String[] args) {
Config.load();
}
}
}
}
25 changes: 11 additions & 14 deletions src/main/java/cc/baka9/catseedlogin/bungee/Communication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,45 @@
* bc 与 bukkit 的通讯交流
*/
public class Communication {
private static final String HOST = Config.Host;
private static final int PORT = Config.Port;

public static int sendConnectRequest(String playerName) {
try (Socket socket = getSocket(); BufferedWriter bufferedWriter = getSocketBufferedWriter(socket)) {
// 请求类型
bufferedWriter.write("Connect");
bufferedWriter.newLine();
// 玩家名
bufferedWriter.write(playerName);
bufferedWriter.newLine();

bufferedWriter.flush();
return socket.getInputStream().read();
} catch (IOException e) {
e.printStackTrace();
handleIOException(e);
}
return 0;
}

public static void sendKeepLoggedInRequest(String playerName) {
try (Socket socket = getSocket(); BufferedWriter bufferedWriter = getSocketBufferedWriter(socket)) {
// 请求类型
bufferedWriter.write("KeepLoggedIn");
bufferedWriter.newLine();
// 玩家名
bufferedWriter.write(playerName);
bufferedWriter.newLine();
// 时间戳
String time = String.valueOf(System.currentTimeMillis());
long currentTime = System.currentTimeMillis();
String time = String.valueOf(currentTime);
bufferedWriter.write(time);
bufferedWriter.newLine();
// 根据玩家名,时间戳,和authKey加密的结果(加密是因为如果登录服不在内网环境下,则可能会被人使用这个功能给发包来绕过登录)
String sign = CommunicationAuth.encryption(playerName, time, Config.AuthKey);
bufferedWriter.write(sign);
bufferedWriter.newLine();

bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
handleIOException(e);
}
}

private static Socket getSocket() throws IOException {
try {
return new Socket(Config.Host, Config.Port);
return new Socket(HOST, PORT);
} catch (IOException e) {
ProxyServer.getInstance().getLogger().warning("§c请检查装载登录插件的子服是否在 bungeecord.yml 中开启了bungeecord功能,以及Host和Port是否与bc端的配置相同");
throw new IOException(e);
Expand All @@ -66,5 +61,7 @@ private static BufferedWriter getSocketBufferedWriter(Socket socket) throws IOEx
return new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}


}
private static void handleIOException(IOException e) {
ProxyServer.getInstance().getLogger().severe("发生 I/O 异常: " + e.getMessage());
}
}
20 changes: 10 additions & 10 deletions src/main/java/cc/baka9/catseedlogin/bungee/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.logging.Logger;

public class Config {

Expand All @@ -16,11 +17,10 @@ public class Config {
public static int Port;
public static String LoginServerName;
public static String AuthKey;
private static Logger logger = PluginMain.instance.getLogger();

public static void load() {

File dataFolder = PluginMain.instance.getDataFolder();

if (!dataFolder.exists()) {
dataFolder.mkdir();
}
Expand All @@ -43,15 +43,15 @@ public static void load() {
Port = config.getInt("Port");
LoginServerName = config.getString("LoginServerName");
AuthKey = config.getString("AuthKey");
PluginMain.instance.getLogger().info("Host:" + Host);
PluginMain.instance.getLogger().info("Port:" + Port);
PluginMain.instance.getLogger().info("LoginServerName:" + LoginServerName);

// 合并日志输出
StringBuilder logBuilder = new StringBuilder();
logBuilder.append("Host: ").append(Host).append("\n");
logBuilder.append("Port: ").append(Port).append("\n");
logBuilder.append("LoginServerName: ").append(LoginServerName);
logger.info(logBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}


}


}
}
72 changes: 19 additions & 53 deletions src/main/java/cc/baka9/catseedlogin/bungee/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.List;

/**
Expand All @@ -19,7 +19,7 @@ public class Listeners implements Listener {

private final ProxyServer proxyServer = ProxyServer.getInstance();

private final List<String> loggedInPlayerList = new ArrayList<>();
private final List<String> loggedInPlayerList = new CopyOnWriteArrayList<>();

/**
* 登录之前不能输入bc指令
Expand All @@ -30,62 +30,45 @@ public void onChat(ChatEvent event) {
if (!event.isProxyCommand() || !(sender instanceof ProxiedPlayer)) return;
ProxiedPlayer proxiedPlayer = (ProxiedPlayer) sender;
String message = event.getMessage();
boolean loggedIn;
String playerName = proxiedPlayer.getName();
synchronized (loggedInPlayerList) {
loggedIn = loggedInPlayerList.contains(playerName);
}
if (!loggedIn) {

if (!loggedInPlayerList.contains(playerName)) {
event.setCancelled(true);

PluginMain.runAsync(() -> {
if (Communication.sendConnectRequest(playerName) == 1) {
synchronized (loggedInPlayerList) {
loggedInPlayerList.add(playerName);
}
PluginMain.instance.getProxy().getPluginManager().dispatchCommand(proxiedPlayer, message.substring(1));
loggedInPlayerList.add(playerName);
proxyServer.getPluginManager().dispatchCommand(proxiedPlayer, message.substring(1));
}
});
}


}

/**
* 玩家切换子服时,检查bc端该玩家的登录状态,
* 如果没有登录,就请求登录服获取登录状态然后更新bc端该玩家的登录状态
* 如果登录服获取结果还是没有登录,就强制把切换目标服务器改为登录服
* 如果没有登录,在登录服获取登录状态后更新bc端该玩家的登录状态
* 如果登录服依然未登录,强制切换目标服务器为登录服
*/
@EventHandler
public void onServerConnect(ServerConnectEvent event) {
ServerInfo target = event.getTarget();
if (event.isCancelled() || target.getName().equals(Config.LoginServerName)) return;
ProxiedPlayer player = event.getPlayer();
boolean loggedIn;
synchronized (loggedInPlayerList) {
loggedIn = loggedInPlayerList.contains(player.getName());
}
if (!loggedIn) {

if (!loggedInPlayerList.contains(player.getName())) {
PluginMain.runAsync(() -> {
if (Communication.sendConnectRequest(player.getName()) == 1) {
synchronized (loggedInPlayerList) {
loggedInPlayerList.add(player.getName());
player.connect(target);
}
loggedInPlayerList.add(player.getName());
player.connect(target);
}
});

event.setTarget(proxyServer.getServerInfo(Config.LoginServerName));


}

}


/**
* 玩家切换到登录服务之后,如果bc端是已登录的状态,就使用bc端的登录状态去更新子服的登录状态,
* 避免使玩家每次切换到登录服时需要重新进行登录
* 玩家切换到登录服务之后,如果bc端已登录模式,使用登录状态更新子服务器登录状态,避免玩家需要重新登录
*/
@EventHandler
public void onServerConnected(ServerConnectedEvent event) {
Expand All @@ -94,49 +77,32 @@ public void onServerConnected(ServerConnectedEvent event) {
String playerName = player.getName();

PluginMain.runAsync(() -> {
boolean loggedIn;
synchronized (loggedInPlayerList) {
loggedIn = loggedInPlayerList.contains(playerName);
}
if (loggedIn) {
if (loggedInPlayerList.contains(playerName)) {
Communication.sendKeepLoggedInRequest(playerName);
}

});
}

}

/**
* 玩家离线时,删除玩家在bc端的登录状态
* 玩家离线时,从bc端删除玩家的登录状态
*/
@EventHandler
public void onPlayerDisconnect(PlayerDisconnectEvent event) {
ProxiedPlayer player = event.getPlayer();
String playerName = player.getName();
PluginMain.runAsync(() -> {
synchronized (loggedInPlayerList) {
loggedInPlayerList.remove(playerName);
}
});
loggedInPlayerList.remove(playerName);
}

/**
* 玩家在登录之前,检查bc端和子服的登录状态,如果是其中一项是已登录,则禁止连接
* 玩家在登录前,检查bc端和子服务器的登录状态,如果任一已登录,阻止连接
*/
@EventHandler
public void onPreLogin(PreLoginEvent event) {
String playerName = event.getConnection().getName();
boolean loggedIn;
synchronized (loggedInPlayerList) {
loggedIn = loggedInPlayerList.contains(playerName);
}
if (loggedIn || Communication.sendConnectRequest(playerName) == 1) {
if (loggedInPlayerList.contains(playerName) || Communication.sendConnectRequest(playerName) == 1) {
event.setCancelReason(new TextComponent(""));
event.setCancelled(true);
}

}


}
}

0 comments on commit 38ddf7f

Please sign in to comment.