Skip to content

Commit e8b4bed

Browse files
committed
[2.4.5] 优化跨服玩家列表处理
1 parent 491f4c0 commit e8b4bed

11 files changed

Lines changed: 137 additions & 31 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=me.arasple.mc.trchat
2-
version=2.4.4
2+
version=2.4.5
33
kotlin.incremental=true
44
kotlin.incremental.java=true
55
kotlin.caching.enabled=true

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/api/impl/BukkitProxyManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import taboolib.common.platform.PlatformFactory
1717
import taboolib.common.platform.PlatformSide
1818
import taboolib.common.platform.function.console
1919
import taboolib.common.platform.function.getProxyPlayer
20+
import taboolib.common.platform.function.submitAsync
2021
import taboolib.common.util.unsafeLazy
2122
import taboolib.common5.cint
2223
import taboolib.module.chat.ComponentText
@@ -99,6 +100,9 @@ object BukkitProxyManager : ClientMessageManager {
99100
}
100101
ProxyMode.REDIS -> {
101102
RedisManager()
103+
submitAsync(period = 200L) {
104+
updateNames()
105+
}
102106
BukkitProxyProcessor.RedisSide()
103107
}
104108
else -> null

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/display/format/obj/Text.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import me.arasple.mc.trchat.util.papiRegex
1010
import me.arasple.mc.trchat.util.setPlaceholders
1111
import org.bukkit.command.CommandSender
1212
import org.bukkit.entity.Player
13+
import taboolib.common.platform.Platform
14+
import taboolib.common.platform.PlatformSide
1315
import taboolib.common.util.replaceWithOrder
1416
import taboolib.module.chat.ComponentText
1517
import taboolib.module.chat.Components
@@ -19,6 +21,7 @@ import taboolib.module.configuration.ConfigNode
1921
* @author ItsFlicker
2022
* @since 2022/1/21 23:21
2123
*/
24+
@PlatformSide(Platform.BUKKIT)
2225
open class Text(val content: String, val condition: Condition?) {
2326

2427
val dynamic = papiRegex.containsMatchIn(content)

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/internal/listener/ListenerJoin.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.arasple.mc.trchat.module.internal.listener
22

3+
import me.arasple.mc.trchat.api.impl.BukkitProxyManager
34
import me.arasple.mc.trchat.module.display.channel.Channel
45
import me.arasple.mc.trchat.module.internal.service.Updater
56
import me.arasple.mc.trchat.util.data
@@ -39,6 +40,7 @@ object ListenerJoin {
3940
}
4041
}
4142
Updater.notifyPlayer(adaptPlayer(player))
43+
BukkitProxyManager.updateNames()
4244
}
4345
}
4446
}

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/internal/listener/ListenerQuit.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.arasple.mc.trchat.module.internal.listener
22

3+
import me.arasple.mc.trchat.api.impl.BukkitProxyManager
34
import me.arasple.mc.trchat.module.conf.file.Functions
45
import me.arasple.mc.trchat.module.display.ChatSession
56
import me.arasple.mc.trchat.module.display.channel.Channel
@@ -11,6 +12,7 @@ import taboolib.common.platform.Platform
1112
import taboolib.common.platform.PlatformSide
1213
import taboolib.common.platform.event.EventPriority
1314
import taboolib.common.platform.event.SubscribeEvent
15+
import taboolib.common.platform.function.submit
1416

1517
/**
1618
* @author ItsFlicker
@@ -31,6 +33,9 @@ object ListenerQuit {
3133
Channel.channels.values.forEach { it.listeners -= name }
3234
ChatSession.removeSession(player)
3335
PlayerData.removeData(player)
36+
submit(delay = 10) {
37+
BukkitProxyManager.updateNames()
38+
}
3439
}
3540

3641
}

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/internal/proxy/BukkitProxyProcessor.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import org.bukkit.plugin.messaging.PluginMessageListener
2121
import org.bukkit.plugin.messaging.PluginMessageRecipient
2222
import taboolib.common.platform.function.console
2323
import taboolib.common.platform.function.getProxyPlayer
24-
import taboolib.common.platform.function.submitAsync
2524
import taboolib.common.util.subList
2625
import taboolib.common5.util.decodeBase64
2726
import taboolib.module.chat.Components
@@ -34,6 +33,7 @@ import taboolib.platform.util.deserializeToInventory
3433
import taboolib.platform.util.onlinePlayers
3534
import java.io.IOException
3635
import java.util.*
36+
import java.util.concurrent.ConcurrentHashMap
3737
import java.util.concurrent.ExecutorService
3838
import java.util.concurrent.Future
3939

@@ -106,7 +106,6 @@ sealed interface BukkitProxyProcessor : PluginMessageListener {
106106
}
107107
}
108108
"UpdateAllNames" -> {
109-
BukkitProxyManager.updateNames()
110109
val names = data[1].takeIf { it != "" }?.split(",") ?: return
111110
val displayNames = data[2].split(",")
112111
val uuids = data[3].split(",")
@@ -251,13 +250,7 @@ sealed interface BukkitProxyProcessor : PluginMessageListener {
251250

252251
object RedisSide : BukkitProxyProcessor {
253252

254-
val allNames = mutableMapOf<String, List<Triple<String, String?, UUID>>>()
255-
256-
init {
257-
submitAsync(period = 200L) {
258-
BukkitProxyManager.updateNames()
259-
}
260-
}
253+
val allNames = ConcurrentHashMap<String, List<Triple<String, String?, UUID>>>()
261254

262255
override fun execute(data: Array<String>) {
263256
when (data[0]) {
@@ -309,4 +302,4 @@ sealed interface BukkitProxyProcessor : PluginMessageListener {
309302
}
310303

311304
}
312-
}
305+
}

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/util/color/CustomColor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package me.arasple.mc.trchat.util.color
33
import me.arasple.mc.trchat.util.papiRegex
44
import me.arasple.mc.trchat.util.setPlaceholders
55
import org.bukkit.command.CommandSender
6+
import taboolib.common.platform.Platform
7+
import taboolib.common.platform.PlatformSide
68
import taboolib.module.configuration.ConfigNode
79

810
/**
911
* @author ItsFlicker
1012
* @since 2021/12/12 12:30
1113
*/
14+
@PlatformSide(Platform.BUKKIT)
1215
class CustomColor(val type: ColorType, val color: String) {
1316

1417
enum class ColorType {

project/runtime-bungee/src/main/kotlin/me/arasple/mc/trchat/api/impl/BungeeProxyManager.kt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import taboolib.common.platform.Schedule
1414
import taboolib.common.platform.function.server
1515
import taboolib.common.util.unsafeLazy
1616
import java.io.IOException
17-
import java.util.concurrent.CompletableFuture
18-
import java.util.concurrent.ExecutorService
19-
import java.util.concurrent.Executors
20-
import java.util.concurrent.Future
17+
import java.util.concurrent.*
2118

2219
/**
2320
* @author ItsFlicker
@@ -36,7 +33,7 @@ object BungeeProxyManager : ProxyMessageManager {
3633
Executors.newFixedThreadPool(8, factory)
3734
}
3835

39-
override val allNames = mutableMapOf<Int, List<Triple<String, String, String>>>()
36+
override val allNames = ConcurrentHashMap<Int, List<Triple<String, String, String>>>()
4037

4138
override fun sendMessage(recipient: Any, vararg args: String): Future<*> {
4239
if (recipient !is ServerInfo) {
@@ -68,9 +65,11 @@ object BungeeProxyManager : ProxyMessageManager {
6865
}
6966
}
7067

71-
@Schedule(async = true, period = 100L)
68+
@Schedule(period = 1200L)
69+
@Synchronized
7270
override fun updateAllNames() {
73-
val flat = allNames.values.flatten()
71+
refreshNamesFromProxy()
72+
val flat = allNames.values.toList().flatten()
7473
sendMessageToAll(
7574
"UpdateAllNames",
7675
flat.joinToString(",") { it.first },
@@ -79,4 +78,25 @@ object BungeeProxyManager : ProxyMessageManager {
7978
)
8079
}
8180

82-
}
81+
@Synchronized
82+
fun updateNames(port: Int, names: List<Triple<String, String, String>>): Boolean {
83+
if (allNames[port]?.toSet() == names.toSet()) {
84+
return false
85+
}
86+
allNames[port] = names
87+
return true
88+
}
89+
90+
private fun refreshNamesFromProxy() {
91+
val displayNames = allNames.values.toList().flatten().associate { it.third to it.second }
92+
allNames.clear()
93+
server<ProxyServer>().players
94+
.mapNotNull { player ->
95+
val info = player.server?.info ?: return@mapNotNull null
96+
info.address.port to Triple(player.name, displayNames[player.uniqueId.toString()] ?: "#", player.uniqueId.toString())
97+
}
98+
.groupBy({ it.first }, { it.second })
99+
.forEach { (port, names) -> allNames[port] = names }
100+
}
101+
102+
}

project/runtime-bungee/src/main/kotlin/me/arasple/mc/trchat/module/internal/listener/ListenerBungeeTransfer.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import me.arasple.mc.trchat.util.print
77
import me.arasple.mc.trchat.util.proxy.common.MessageReader
88
import net.md_5.bungee.api.ProxyServer
99
import net.md_5.bungee.api.connection.Connection
10+
import net.md_5.bungee.api.event.PlayerDisconnectEvent
1011
import net.md_5.bungee.api.event.PluginMessageEvent
12+
import net.md_5.bungee.api.event.PostLoginEvent
13+
import net.md_5.bungee.api.event.ServerSwitchEvent
1114
import taboolib.common.platform.Platform
1215
import taboolib.common.platform.PlatformSide
1316
import taboolib.common.platform.event.SubscribeEvent
1417
import taboolib.common.platform.function.console
1518
import taboolib.common.platform.function.server
19+
import taboolib.common.platform.function.submit
1620
import taboolib.module.chat.Components
1721
import java.io.IOException
1822

@@ -44,6 +48,27 @@ object ListenerBungeeTransfer {
4448
}
4549
}
4650

51+
@SubscribeEvent
52+
fun onProxyJoin(e: PostLoginEvent) {
53+
updateAllNamesLater()
54+
}
55+
56+
@SubscribeEvent
57+
fun onProxyQuit(e: PlayerDisconnectEvent) {
58+
updateAllNamesLater()
59+
}
60+
61+
@SubscribeEvent
62+
fun onProxySwitch(e: ServerSwitchEvent) {
63+
updateAllNamesLater()
64+
}
65+
66+
private fun updateAllNamesLater() {
67+
submit(delay = 30) {
68+
BungeeProxyManager.updateAllNames()
69+
}
70+
}
71+
4772
@Suppress("Deprecation")
4873
private fun execute(data: Array<String>, connection: Connection) {
4974
when (data[0]) {
@@ -79,10 +104,13 @@ object ListenerBungeeTransfer {
79104
val names = data[2].split(",")
80105
val displayNames = data[3].split(",")
81106
val uuids = data[4].split(",")
82-
BungeeProxyManager.allNames[port] = names.mapIndexed { index, name ->
107+
val updated = names.mapIndexed { index, name ->
83108
Triple(name, displayNames[index], uuids[index])
84109
}
110+
if (BungeeProxyManager.updateNames(port, updated)) {
111+
BungeeProxyManager.updateAllNames()
112+
}
85113
}
86114
}
87115
}
88-
}
116+
}

project/runtime-velocity/src/main/kotlin/me/arasple/mc/trchat/api/impl/VelocityProxyManager.kt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import taboolib.common.platform.Schedule
1616
import taboolib.common.platform.function.warning
1717
import taboolib.common.util.unsafeLazy
1818
import java.io.IOException
19-
import java.util.concurrent.CompletableFuture
20-
import java.util.concurrent.ExecutorService
21-
import java.util.concurrent.Executors
22-
import java.util.concurrent.Future
19+
import java.util.concurrent.*
2320

2421
/**
2522
* @author ItsFlicker
@@ -42,7 +39,7 @@ object VelocityProxyManager : ProxyMessageManager {
4239
Executors.newFixedThreadPool(8, factory)
4340
}
4441

45-
override val allNames = mutableMapOf<Int, List<Triple<String, String, String>>>()
42+
override val allNames = ConcurrentHashMap<Int, List<Triple<String, String, String>>>()
4643

4744
override fun sendMessage(recipient: Any, vararg args: String): Future<*> {
4845
if (recipient !is ChannelMessageSink) {
@@ -87,9 +84,11 @@ object VelocityProxyManager : ProxyMessageManager {
8784
}
8885
}
8986

90-
@Schedule(async = true, period = 100L)
87+
@Schedule(period = 1200L)
88+
@Synchronized
9189
override fun updateAllNames() {
92-
val flat = allNames.values.flatten()
90+
refreshNamesFromProxy()
91+
val flat = allNames.values.toList().flatten()
9392
sendMessageToAll(
9493
"UpdateAllNames",
9594
flat.joinToString(",") { it.first },
@@ -98,4 +97,25 @@ object VelocityProxyManager : ProxyMessageManager {
9897
)
9998
}
10099

101-
}
100+
@Synchronized
101+
fun updateNames(port: Int, names: List<Triple<String, String, String>>): Boolean {
102+
if (allNames[port]?.toSet() == names.toSet()) {
103+
return false
104+
}
105+
allNames[port] = names
106+
return true
107+
}
108+
109+
private fun refreshNamesFromProxy() {
110+
val displayNames = allNames.values.toList().flatten().associate { it.third to it.second }
111+
allNames.clear()
112+
plugin.server.allPlayers
113+
.mapNotNull { player ->
114+
val info = player.currentServer.map { it.serverInfo }.orElse(null) ?: return@mapNotNull null
115+
info.address.port to Triple(player.username, displayNames[player.uniqueId.toString()] ?: "#", player.uniqueId.toString())
116+
}
117+
.groupBy({ it.first }, { it.second })
118+
.forEach { (port, names) -> allNames[port] = names }
119+
}
120+
121+
}

0 commit comments

Comments
 (0)