Skip to content

Commit 87d472b

Browse files
[1.6]重构连接管理并添加中间件
引入了 PlayerLoginMiddleware 以及多个新的连接相关类,以改进玩家会话和路由管理。移除了传统的路由管理类,更新了插件初始化和事件处理以使用中间件,并增强了命令逻辑和配置。在 pom.xml 中将版本提高到 1.6。
1 parent b91f002 commit 87d472b

17 files changed

Lines changed: 3713 additions & 2736 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>ict.minesunshineone</groupId>
77
<artifactId>simpletransfer</artifactId>
8-
<version>1.5</version>
8+
<version>1.6</version>
99
<packaging>jar</packaging>
1010

1111
<name>SimpleTransfer</name>

src/main/java/ict/minesunshineone/simpleTransfer/SimpleTransfer.java

Lines changed: 171 additions & 121 deletions
Large diffs are not rendered by default.

src/main/java/ict/minesunshineone/simpleTransfer/command/MainCommand.java

Lines changed: 1038 additions & 844 deletions
Large diffs are not rendered by default.

src/main/java/ict/minesunshineone/simpleTransfer/command/TransferCommand.java

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import java.net.InetSocketAddress;
1313

1414
public class TransferCommand implements SimpleCommand {
15-
15+
1616
private final String routeName;
1717
private final ServerInfo serverInfo;
1818
private final ConfigManager configManager;
1919
private final Logger logger;
2020
private final PlayerConnectionManager connectionManager;
2121

22-
public TransferCommand(String routeName, ServerInfo serverInfo, ConfigManager configManager,
23-
Logger logger, PlayerConnectionManager connectionManager) {
22+
public TransferCommand(String routeName, ServerInfo serverInfo, ConfigManager configManager,
23+
Logger logger, PlayerConnectionManager connectionManager) {
2424
this.routeName = routeName;
2525
this.serverInfo = serverInfo;
2626
this.configManager = configManager;
@@ -37,19 +37,36 @@ public void execute(Invocation invocation) {
3737
.build());
3838
return;
3939
}
40-
40+
41+
// 检查客户端版本是否支持转移
42+
if (!connectionManager.isPlayerVersionSupported(player)) {
43+
player.sendMessage(Component.text(" "));
44+
player.sendMessage(Component.text()
45+
.append(Component.text(" ✘ ", NamedTextColor.RED))
46+
.append(Component.text("您的客户端版本过低,不支持线路切换功能",
47+
net.kyori.adventure.text.format.TextColor.color(0xFFAAAA)))
48+
.build());
49+
player.sendMessage(Component.text()
50+
.append(Component.text(" ➤ ", net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
51+
.append(Component.text("请升级到 Minecraft 1.20.5 或更高版本",
52+
net.kyori.adventure.text.format.TextColor.color(0xFFFFAA)))
53+
.build());
54+
player.sendMessage(Component.text(" "));
55+
return;
56+
}
57+
4158
try {
4259
// 获取玩家地理位置信息
4360
String location = connectionManager.getPlayerLocation(player.getUniqueId());
44-
61+
4562
// 显示传送信息(不显示IP和端口,保护服务器信息)
4663
player.sendMessage(Component.text(" "));
4764
player.sendMessage(Component.text()
4865
.append(Component.text("⚡ ", net.kyori.adventure.text.format.TextColor.color(0xFFDD00)))
4966
.append(Component.text("线路切换", net.kyori.adventure.text.format.TextColor.color(0x00FFFF)))
5067
.build());
5168
player.sendMessage(Component.text(" "));
52-
69+
5370
// 显示玩家位置
5471
if (location != null) {
5572
player.sendMessage(Component.text()
@@ -58,11 +75,12 @@ public void execute(Invocation invocation) {
5875
.append(Component.text(location, net.kyori.adventure.text.format.TextColor.color(0xAAFFFF)))
5976
.build());
6077
}
61-
78+
6279
player.sendMessage(Component.text()
6380
.append(Component.text(" ➤ ", net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
6481
.append(Component.text("目标线路: ", NamedTextColor.WHITE))
65-
.append(Component.text(serverInfo.getDescription(), net.kyori.adventure.text.format.TextColor.color(0x00FF88)))
82+
.append(Component.text(serverInfo.getDescription(),
83+
net.kyori.adventure.text.format.TextColor.color(0x00FF88)))
6684
.build());
6785
player.sendMessage(Component.text()
6886
.append(Component.text(" ➤ ", net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
@@ -79,63 +97,67 @@ public void execute(Invocation invocation) {
7997
.append(Component.text("您将在数秒后自动重新连接", net.kyori.adventure.text.format.TextColor.color(0xFFFFAA)))
8098
.build());
8199
player.sendMessage(Component.text(" "));
82-
100+
83101
// 获取当前线路
84102
String currentRoute = connectionManager.getCurrentRoute(player.getUniqueId());
85-
String currentRouteDesc = currentRoute != null ?
86-
configManager.getTransferServers().get(currentRoute).getDescription() : "默认服务器";
87-
103+
String currentRouteDesc = currentRoute != null
104+
? configManager.getTransferServers().get(currentRoute).getDescription()
105+
: "默认服务器";
106+
88107
// 简洁日志(带地理位置)
89108
if (location != null) {
90-
logger.info("{} [{}] 从 {} 转到 {}",
91-
player.getUsername(),
92-
location,
93-
currentRouteDesc,
94-
serverInfo.getDescription());
109+
logger.info("{} [{}] 从 {} 转到 {}",
110+
player.getUsername(),
111+
location,
112+
currentRouteDesc,
113+
serverInfo.getDescription());
95114
} else {
96-
logger.info("{} 从 {} 转到 {}",
97-
player.getUsername(),
98-
currentRouteDesc,
99-
serverInfo.getDescription());
115+
logger.info("{} 从 {} 转到 {}",
116+
player.getUsername(),
117+
currentRouteDesc,
118+
serverInfo.getDescription());
100119
}
101-
120+
102121
// 使用 Transfer 协议传送玩家
103122
InetSocketAddress targetAddress = new InetSocketAddress(serverInfo.getHost(), serverInfo.getPort());
104-
123+
105124
// 检查地址是否解析成功
106125
if (targetAddress.isUnresolved()) {
107126
throw new Exception("DNS 解析失败");
108127
}
109-
110-
logDebug("Transfer → {}:{} ({}) (解析为: {})",
111-
serverInfo.getHost(),
112-
serverInfo.getPort(),
113-
serverInfo.getDescription(),
114-
targetAddress.getAddress().getHostAddress());
115-
128+
129+
logDebug("Transfer → {}:{} ({}) (解析为: {})",
130+
serverInfo.getHost(),
131+
serverInfo.getPort(),
132+
serverInfo.getDescription(),
133+
targetAddress.getAddress().getHostAddress());
134+
116135
player.transferToHost(targetAddress);
117-
136+
118137
// 记录玩家使用的线路
119138
connectionManager.recordRoute(player, routeName);
120-
139+
121140
// 添加待发送消息(转移成功后显示)
122141
connectionManager.addPendingMessage(player.getUniqueId(), Component.text(" "));
123142
connectionManager.addPendingMessage(player.getUniqueId(), Component.text()
124143
.append(Component.text(" ➤ ", net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
125144
.append(Component.text("已切换到线路: ", NamedTextColor.WHITE))
126-
.append(Component.text(serverInfo.getDescription(), net.kyori.adventure.text.format.TextColor.color(0x00FF88)))
145+
.append(Component.text(serverInfo.getDescription(),
146+
net.kyori.adventure.text.format.TextColor.color(0x00FF88)))
127147
.build());
128148
connectionManager.addPendingMessage(player.getUniqueId(), Component.text()
129149
.append(Component.text(" ➤ ", net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
130150
.append(Component.text("您当前在: ", net.kyori.adventure.text.format.TextColor.color(0xAAAAAA)))
131-
.append(Component.text("[" + routeName.toUpperCase() + "] ", net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
132-
.append(Component.text(serverInfo.getDescription(), net.kyori.adventure.text.format.TextColor.color(0xAAFFFF)))
151+
.append(Component.text("[" + routeName.toUpperCase() + "] ",
152+
net.kyori.adventure.text.format.TextColor.color(0x00DDFF)))
153+
.append(Component.text(serverInfo.getDescription(),
154+
net.kyori.adventure.text.format.TextColor.color(0xAAFFFF)))
133155
.build());
134156
connectionManager.addPendingMessage(player.getUniqueId(), Component.text(" "));
135-
157+
136158
// 标记本次登录已手动转发,避免自动转移重复提示
137159
connectionManager.markManualTransferThisSession(player.getUniqueId());
138-
160+
139161
} catch (IllegalArgumentException e) {
140162
// 版本不支持 Transfer 协议(理论上不会到这里,因为前面已检查)
141163
player.sendMessage(Component.text()

0 commit comments

Comments
 (0)