Skip to content

Commit e1c8535

Browse files
committed
extremely basic batching
1 parent 1e15708 commit e1c8535

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/kotlin/org/polyfrost/polyplus/client/cosmetics/ApplyCosmetics.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.polyfrost.polyplus.client.cosmetics
22

3+
import dev.deftu.omnicore.api.client.player
34
import net.minecraft.network.play.server.S38PacketPlayerListItem
45
import net.minecraftforge.event.world.WorldEvent
56
import org.apache.logging.log4j.LogManager
@@ -10,13 +11,18 @@ import org.polyfrost.polyplus.client.network.websocket.ClientboundPacket
1011
import org.polyfrost.polyplus.client.network.websocket.PolyConnection
1112
import org.polyfrost.polyplus.client.network.websocket.ServerboundPacket
1213
import org.polyfrost.polyplus.events.WebSocketMessage
14+
import org.polyfrost.polyplus.utils.Batcher
15+
import java.time.Duration
1316
import java.util.UUID
1417
import kotlin.collections.component1
1518
import kotlin.collections.component2
1619
import kotlin.collections.iterator
1720

1821
object ApplyCosmetics {
1922
private val LOGGER = LogManager.getLogger()
23+
private val BATCHER = Batcher(Duration.ofMillis(200), HashSet<String>()) { players ->
24+
PolyConnection.sendPacket(ServerboundPacket.GetActiveCosmetics(players.toList()))
25+
}
2026

2127
@Subscribe
2228
fun onWorldLoad(event: WorldEvent.Load) {
@@ -29,9 +35,10 @@ object ApplyCosmetics {
2935
val packet = try { event.getPacket<S38PacketPlayerListItem>() ?: return } catch (e: Exception) { return }
3036
when (packet.action) {
3137
S38PacketPlayerListItem.Action.ADD_PLAYER -> {
32-
val players = packet.entries.mapNotNull { it.profile.id.takeUnless { it.version() == 2 }?.toString() } // mojang only uses UUIDv2 so if its not, its a bot and wont have a cape.
33-
PolyConnection.sendPacket(ServerboundPacket.GetActiveCosmetics(players))
34-
LOGGER.info("Requested cosmetics for players: $players")
38+
packet.entries.forEach {
39+
// mojang doesnt use uuidv2 so if it is, its a bot and wont have a cape.
40+
if (it.profile.id.version() != 2) BATCHER.add(it.profile.id.toString())
41+
}
3542
}
3643

3744
S38PacketPlayerListItem.Action.REMOVE_PLAYER -> {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.polyfrost.polyplus.utils
2+
3+
import kotlinx.atomicfu.locks.ReentrantLock
4+
import kotlinx.atomicfu.locks.withLock
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.Job
7+
import kotlinx.coroutines.launch
8+
import org.polyfrost.polyplus.client.PolyPlusClient
9+
import java.time.Duration
10+
11+
class Batcher<T, C: MutableCollection<T>>(val delay: Duration, val set: C, val onBatch: suspend CoroutineScope.(C) -> Unit) {
12+
private val lock = ReentrantLock()
13+
private var job: Job? = null
14+
15+
fun add(item: T) {
16+
if (job == null) {
17+
job = PolyPlusClient.SCOPE.launch {
18+
kotlinx.coroutines.delay(delay.toMillis())
19+
lock.withLock {
20+
onBatch(set)
21+
set.clear()
22+
}
23+
job = null
24+
}
25+
}
26+
27+
lock.withLock {
28+
set.add(item)
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)