Skip to content

Commit

Permalink
[6.0.11][publish] Experimental > update connection error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Jun 12, 2023
1 parent aa89b55 commit 77fb001
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ object ChannelExecutor {
return PacketSendEvent::class.java.isListened() || PacketReceiveEvent::class.java.isListened()
}

fun getPlayerChannel(address: InetAddress, first: Boolean): Channel {
val connection = ConnectionGetter.instance.getConnection(address, first)
return ConnectionGetter.instance.getChannel(connection)
fun getPlayerChannel(address: InetAddress, init: Boolean): Channel {
return ConnectionGetter.instance.getChannel(ConnectionGetter.instance.getConnection(address, init))
}

fun addPlayerChannel(player: Player, address: InetAddress) {
Expand All @@ -70,7 +69,7 @@ object ChannelExecutor {
}
}

fun removePlayerChannel(player: Player) {
fun removePlayerChannel(player: Player, async: Boolean = true) {
if (isDisabled || !isPacketEventListened()) {
return
}
Expand All @@ -79,7 +78,7 @@ object ChannelExecutor {
return
}
val address = player.address?.address ?: return
pool.submit {
fun process() {
try {
val pipeline = getPlayerChannel(address, false).pipeline()
if (pipeline[id] != null) {
Expand All @@ -89,6 +88,11 @@ object ChannelExecutor {
ex.printStackTrace()
}
}
if (async) {
pool.submit(::process)
} else {
process()
}
}

@SubscribeEvent
Expand All @@ -114,6 +118,6 @@ object ChannelExecutor {
if (TabooLibCommon.isStopped()) {
return
}
onlinePlayers.forEach { removePlayerChannel(it) }
onlinePlayers.forEach { removePlayerChannel(it, async = false) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.net.InetSocketAddress
*/
abstract class ConnectionGetter {

abstract fun getConnection(address: InetAddress, first: Boolean): Any
abstract fun getConnection(address: InetAddress, init: Boolean): Any

abstract fun getChannel(connection: Any): Channel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConnectionGetterImpl : ConnectionGetter() {
val major = MinecraftVersion.major
val addressUsed = ConcurrentHashMap<InetSocketAddress, Any>()

override fun getConnection(address: InetAddress, first: Boolean): Any {
override fun getConnection(address: InetAddress, init: Boolean): Any {
// 获取服务器中的所有连接
val serverConnections = when (major) {
// 1.8, 1.9, 1.10, 1.11, 1.12 -> List<NetworkManager> h
Expand Down Expand Up @@ -63,29 +63,45 @@ class ConnectionGetterImpl : ConnectionGetter() {
else -> error("Unsupported Minecraft version: $major")
} ?: error("Unable to get connections from ${Bukkit.getServer()}")
// 获取相同 IP 的连接
val connections = serverConnections.filter { getAddress(it).address == address }
val connections = serverConnections.filter { conn -> conn.address().address == address }
// 没有相同 IP 的连接
if (connections.isEmpty()) {
warning("Unable to get player connection (${address})")
warning("No connection found with the same address (${address})")
warning("Server connections:")
serverConnections.forEach { conn -> warning("- ${getAddress(conn)}") }
serverConnections.forEach { conn -> warning("- ${conn.address()}") }
throw IllegalStateException()
}
// 打印信息
if (isDevelopmentMode) {
info("Player connection ($address)")
info("Server connections:")
serverConnections.forEach { conn -> info("- ${getAddress(conn)}") }
serverConnections.forEach { conn -> info("- ${conn.address()}") }
}
// 首次进入服务器
val connection = if (first) {
// 是否进行初始化
val connection = if (init) {
// 获取未被使用的连接
connections.first { !addressUsed.containsKey(getAddress(it)) }.also { addressUsed[getAddress(it)] = it }
val unused = connections.find { conn -> !addressUsed.containsKey(conn.address()) }
if (unused == null) {
warning("Connections with the same address are already occupied (${address})")
warning("Server connections:")
serverConnections.forEach { conn -> warning("- ${conn.address()}") }
throw IllegalStateException()
}
addressUsed[unused.address()] = unused
unused
} else {
// 获取已使用的连接
connections.first { conn -> addressUsed[getAddress(conn)] == conn }
val used = connections.find { conn -> addressUsed[conn.address()] == conn }
// 没有找到玩家之前存入插件的连接
if (used == null) {
warning("Get the connection before initialisation (${address})")
warning("Server connections:")
serverConnections.forEach { conn -> warning("- ${conn.address()}") }
throw IllegalStateException()
}
used
}
dev("Player connection ($address) -> ${getAddress(connection)} (first=$first)")
dev("Player connection ($address) -> ${connection.address()} (init=$init)")
return connection
}

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

private fun getAddress(connection: Any): InetSocketAddress {
fun Any.address(): InetSocketAddress {
// 这种方式无法在 BungeeCord 中获取到正确的地址:
// return (getChannel(connection).remoteAddress() as? InetSocketAddress)?.address
// 因此要根据不同的版本获取不同的 SocketAddress 字段:
return when (major) {
// 1.8, 1.9, 1.10, 1.11, 1.12
// public SocketAddress l;
0, 1, 2, 3, 4 -> ((connection as NMS8NetworkManager).l as InetSocketAddress)
0, 1, 2, 3, 4 -> ((this as NMS8NetworkManager).l as InetSocketAddress)
// 1.13, 1.14, 1.15, 1.16
// public SocketAddress socketAddress;
5, 6, 7, 8 -> ((connection as NMS13NetworkManager).socketAddress as InetSocketAddress)
5, 6, 7, 8 -> ((this as NMS13NetworkManager).socketAddress as InetSocketAddress)
// 1.17, 1.18, 1.19, 1.20
// public SocketAddress address;
9, 10, 11, 12 -> ((connection as NetworkManager).address as InetSocketAddress)
9, 10, 11, 12 -> ((this as NetworkManager).address as InetSocketAddress)
// 不支持
else -> error("Unsupported Minecraft version: $major")
}
Expand Down

0 comments on commit 77fb001

Please sign in to comment.