@@ -29,7 +29,7 @@ class ConnectionGetterImpl : ConnectionGetter() {
29
29
val major = MinecraftVersion .major
30
30
val addressUsed = ConcurrentHashMap <InetSocketAddress , Any >()
31
31
32
- override fun getConnection (address : InetAddress , first : Boolean ): Any {
32
+ override fun getConnection (address : InetAddress , init : Boolean ): Any {
33
33
// 获取服务器中的所有连接
34
34
val serverConnections = when (major) {
35
35
// 1.8, 1.9, 1.10, 1.11, 1.12 -> List<NetworkManager> h
@@ -63,29 +63,45 @@ class ConnectionGetterImpl : ConnectionGetter() {
63
63
else -> error(" Unsupported Minecraft version: $major " )
64
64
} ? : error(" Unable to get connections from ${Bukkit .getServer()} " )
65
65
// 获取相同 IP 的连接
66
- val connections = serverConnections.filter { getAddress(it ).address == address }
66
+ val connections = serverConnections.filter { conn -> conn.address( ).address == address }
67
67
// 没有相同 IP 的连接
68
68
if (connections.isEmpty()) {
69
- warning(" Unable to get player connection (${address} )" )
69
+ warning(" No connection found with the same address (${address} )" )
70
70
warning(" Server connections:" )
71
- serverConnections.forEach { conn -> warning(" - ${getAddress( conn)} " ) }
71
+ serverConnections.forEach { conn -> warning(" - ${conn.address( )} " ) }
72
72
throw IllegalStateException ()
73
73
}
74
74
// 打印信息
75
75
if (isDevelopmentMode) {
76
76
info(" Player connection ($address )" )
77
77
info(" Server connections:" )
78
- serverConnections.forEach { conn -> info(" - ${getAddress( conn)} " ) }
78
+ serverConnections.forEach { conn -> info(" - ${conn.address( )} " ) }
79
79
}
80
- // 首次进入服务器
81
- val connection = if (first ) {
80
+ // 是否进行初始化
81
+ val connection = if (init ) {
82
82
// 获取未被使用的连接
83
- connections.first { ! addressUsed.containsKey(getAddress(it)) }.also { addressUsed[getAddress(it)] = it }
83
+ val unused = connections.find { conn -> ! addressUsed.containsKey(conn.address()) }
84
+ if (unused == null ) {
85
+ warning(" Connections with the same address are already occupied (${address} )" )
86
+ warning(" Server connections:" )
87
+ serverConnections.forEach { conn -> warning(" - ${conn.address()} " ) }
88
+ throw IllegalStateException ()
89
+ }
90
+ addressUsed[unused.address()] = unused
91
+ unused
84
92
} else {
85
93
// 获取已使用的连接
86
- connections.first { conn -> addressUsed[getAddress(conn)] == conn }
94
+ val used = connections.find { conn -> addressUsed[conn.address()] == conn }
95
+ // 没有找到玩家之前存入插件的连接
96
+ if (used == null ) {
97
+ warning(" Get the connection before initialisation (${address} )" )
98
+ warning(" Server connections:" )
99
+ serverConnections.forEach { conn -> warning(" - ${conn.address()} " ) }
100
+ throw IllegalStateException ()
101
+ }
102
+ used
87
103
}
88
- dev(" Player connection ($address ) -> ${getAddress( connection)} (first= $first )" )
104
+ dev(" Player connection ($address ) -> ${connection.address( )} (init= $init )" )
89
105
return connection
90
106
}
91
107
@@ -102,20 +118,20 @@ class ConnectionGetterImpl : ConnectionGetter() {
102
118
return ClientboundBundlePacket (iterator.asIterable() as Iterable <Packet <PacketListenerPlayOut >>)
103
119
}
104
120
105
- private fun getAddress ( connection : Any ): InetSocketAddress {
121
+ fun Any. address ( ): InetSocketAddress {
106
122
// 这种方式无法在 BungeeCord 中获取到正确的地址:
107
123
// return (getChannel(connection).remoteAddress() as? InetSocketAddress)?.address
108
124
// 因此要根据不同的版本获取不同的 SocketAddress 字段:
109
125
return when (major) {
110
126
// 1.8, 1.9, 1.10, 1.11, 1.12
111
127
// public SocketAddress l;
112
- 0 , 1 , 2 , 3 , 4 -> ((connection as NMS8NetworkManager ).l as InetSocketAddress )
128
+ 0 , 1 , 2 , 3 , 4 -> ((this as NMS8NetworkManager ).l as InetSocketAddress )
113
129
// 1.13, 1.14, 1.15, 1.16
114
130
// public SocketAddress socketAddress;
115
- 5 , 6 , 7 , 8 -> ((connection as NMS13NetworkManager ).socketAddress as InetSocketAddress )
131
+ 5 , 6 , 7 , 8 -> ((this as NMS13NetworkManager ).socketAddress as InetSocketAddress )
116
132
// 1.17, 1.18, 1.19, 1.20
117
133
// public SocketAddress address;
118
- 9 , 10 , 11 , 12 -> ((connection as NetworkManager ).address as InetSocketAddress )
134
+ 9 , 10 , 11 , 12 -> ((this as NetworkManager ).address as InetSocketAddress )
119
135
// 不支持
120
136
else -> error(" Unsupported Minecraft version: $major " )
121
137
}
0 commit comments