Skip to content

Commit 77fb001

Browse files
committed
[6.0.11][publish] Experimental > update connection error message
1 parent aa89b55 commit 77fb001

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

module/module-nms/src/main/kotlin/taboolib/module/nms/ChannelExecutor.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ object ChannelExecutor {
4141
return PacketSendEvent::class.java.isListened() || PacketReceiveEvent::class.java.isListened()
4242
}
4343

44-
fun getPlayerChannel(address: InetAddress, first: Boolean): Channel {
45-
val connection = ConnectionGetter.instance.getConnection(address, first)
46-
return ConnectionGetter.instance.getChannel(connection)
44+
fun getPlayerChannel(address: InetAddress, init: Boolean): Channel {
45+
return ConnectionGetter.instance.getChannel(ConnectionGetter.instance.getConnection(address, init))
4746
}
4847

4948
fun addPlayerChannel(player: Player, address: InetAddress) {
@@ -70,7 +69,7 @@ object ChannelExecutor {
7069
}
7170
}
7271

73-
fun removePlayerChannel(player: Player) {
72+
fun removePlayerChannel(player: Player, async: Boolean = true) {
7473
if (isDisabled || !isPacketEventListened()) {
7574
return
7675
}
@@ -79,7 +78,7 @@ object ChannelExecutor {
7978
return
8079
}
8180
val address = player.address?.address ?: return
82-
pool.submit {
81+
fun process() {
8382
try {
8483
val pipeline = getPlayerChannel(address, false).pipeline()
8584
if (pipeline[id] != null) {
@@ -89,6 +88,11 @@ object ChannelExecutor {
8988
ex.printStackTrace()
9089
}
9190
}
91+
if (async) {
92+
pool.submit(::process)
93+
} else {
94+
process()
95+
}
9296
}
9397

9498
@SubscribeEvent
@@ -114,6 +118,6 @@ object ChannelExecutor {
114118
if (TabooLibCommon.isStopped()) {
115119
return
116120
}
117-
onlinePlayers.forEach { removePlayerChannel(it) }
121+
onlinePlayers.forEach { removePlayerChannel(it, async = false) }
118122
}
119123
}

module/module-nms/src/main/kotlin/taboolib/module/nms/ConnectionGetter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.net.InetSocketAddress
1414
*/
1515
abstract class ConnectionGetter {
1616

17-
abstract fun getConnection(address: InetAddress, first: Boolean): Any
17+
abstract fun getConnection(address: InetAddress, init: Boolean): Any
1818

1919
abstract fun getChannel(connection: Any): Channel
2020

module/module-nms/src/main/kotlin/taboolib/module/nms/ConnectionGetterImpl.kt

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ConnectionGetterImpl : ConnectionGetter() {
2929
val major = MinecraftVersion.major
3030
val addressUsed = ConcurrentHashMap<InetSocketAddress, Any>()
3131

32-
override fun getConnection(address: InetAddress, first: Boolean): Any {
32+
override fun getConnection(address: InetAddress, init: Boolean): Any {
3333
// 获取服务器中的所有连接
3434
val serverConnections = when (major) {
3535
// 1.8, 1.9, 1.10, 1.11, 1.12 -> List<NetworkManager> h
@@ -63,29 +63,45 @@ class ConnectionGetterImpl : ConnectionGetter() {
6363
else -> error("Unsupported Minecraft version: $major")
6464
} ?: error("Unable to get connections from ${Bukkit.getServer()}")
6565
// 获取相同 IP 的连接
66-
val connections = serverConnections.filter { getAddress(it).address == address }
66+
val connections = serverConnections.filter { conn -> conn.address().address == address }
6767
// 没有相同 IP 的连接
6868
if (connections.isEmpty()) {
69-
warning("Unable to get player connection (${address})")
69+
warning("No connection found with the same address (${address})")
7070
warning("Server connections:")
71-
serverConnections.forEach { conn -> warning("- ${getAddress(conn)}") }
71+
serverConnections.forEach { conn -> warning("- ${conn.address()}") }
7272
throw IllegalStateException()
7373
}
7474
// 打印信息
7575
if (isDevelopmentMode) {
7676
info("Player connection ($address)")
7777
info("Server connections:")
78-
serverConnections.forEach { conn -> info("- ${getAddress(conn)}") }
78+
serverConnections.forEach { conn -> info("- ${conn.address()}") }
7979
}
80-
// 首次进入服务器
81-
val connection = if (first) {
80+
// 是否进行初始化
81+
val connection = if (init) {
8282
// 获取未被使用的连接
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
8492
} else {
8593
// 获取已使用的连接
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
87103
}
88-
dev("Player connection ($address) -> ${getAddress(connection)} (first=$first)")
104+
dev("Player connection ($address) -> ${connection.address()} (init=$init)")
89105
return connection
90106
}
91107

@@ -102,20 +118,20 @@ class ConnectionGetterImpl : ConnectionGetter() {
102118
return ClientboundBundlePacket(iterator.asIterable() as Iterable<Packet<PacketListenerPlayOut>>)
103119
}
104120

105-
private fun getAddress(connection: Any): InetSocketAddress {
121+
fun Any.address(): InetSocketAddress {
106122
// 这种方式无法在 BungeeCord 中获取到正确的地址:
107123
// return (getChannel(connection).remoteAddress() as? InetSocketAddress)?.address
108124
// 因此要根据不同的版本获取不同的 SocketAddress 字段:
109125
return when (major) {
110126
// 1.8, 1.9, 1.10, 1.11, 1.12
111127
// 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)
113129
// 1.13, 1.14, 1.15, 1.16
114130
// 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)
116132
// 1.17, 1.18, 1.19, 1.20
117133
// 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)
119135
// 不支持
120136
else -> error("Unsupported Minecraft version: $major")
121137
}

0 commit comments

Comments
 (0)