Skip to content

Commit 82e5d49

Browse files
committed
Clear new or unused maps
1 parent ed54086 commit 82e5d49

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

Diff for: silk-compose/src/main/kotlin/net/silkmc/silk/compose/MinecraftComposeGui.kt

+35-14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import net.silkmc.silk.core.annotations.ExperimentalSilkApi
3232
import net.silkmc.silk.core.event.Events
3333
import net.silkmc.silk.core.event.Server
3434
import net.silkmc.silk.core.logging.logError
35+
import net.silkmc.silk.core.task.mcSyncLaunch
3536
import net.silkmc.silk.core.task.silkCoroutineScope
3637
import org.jetbrains.kotlinx.multik.api.linalg.dot
3738
import org.jetbrains.kotlinx.multik.api.mk
@@ -204,6 +205,11 @@ class MinecraftComposeGui(
204205
val updateData = MapItemSavedData.MapPatch(startX, startY, width, height, packetColors)
205206
return ClientboundMapItemDataPacket(mapId, 0, false, null, updateData)
206207
}
208+
209+
fun createClearPacket(): ClientboundMapItemDataPacket {
210+
val updateData = MapItemSavedData.MapPatch(0, 0, 128, 128, ByteArray(128 * 128))
211+
return ClientboundMapItemDataPacket(mapId, 0, false, null, updateData)
212+
}
207213
}
208214

209215
private val coroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
@@ -292,14 +298,13 @@ class MinecraftComposeGui(
292298

293299
scene.render(canvas, System.nanoTime())
294300

295-
val networkHandler = player.connection
301+
val connection = player.connection
296302

297303
coroutineScope {
298304
for (xFrame in 0 until blockWidth) {
299305
for (yFrame in 0 until blockHeight) {
300306
launch(Dispatchers.Default) {
301307
val guiChunk = getGuiChunk(xFrame, yFrame)
302-
val mapId = guiChunk.mapId
303308

304309
for (x in 0 until 128) {
305310
for (y in 0 until 128) {
@@ -308,29 +313,31 @@ class MinecraftComposeGui(
308313
}
309314
}
310315

311-
// send the map data
312-
val updatePacket = guiChunk.createPacket()
313-
if (updatePacket != null) {
314-
networkHandler.send(updatePacket)
315-
}
316-
317316
if (!placedItemFrames) {
318317
val framePos = position.below(yFrame).relative(placementDirection, xFrame)
319318

320319
// spawn the fake item frame
321320
val itemFrame = GlowItemFrame(player.level, framePos, guiDirection)
322321
itemFrame.isInvisible = true
323-
networkHandler.send(itemFrame.addEntityPacket)
322+
connection.send(itemFrame.addEntityPacket)
324323
withContext(limitedDispatcher) {
325324
itemFrameEntityIds += itemFrame.id
326325
}
327326

328327
// put the map in the item frame
329328
val composeStack = Items.FILLED_MAP.defaultInstance.apply {
330-
orCreateTag.putInt("map", mapId)
329+
orCreateTag.putInt("map", guiChunk.mapId)
331330
}
332331
itemFrame.setItem(composeStack, false)
333-
networkHandler.send(ClientboundSetEntityDataPacket(itemFrame.id, itemFrame.entityData, true))
332+
connection.send(ClientboundSetEntityDataPacket(itemFrame.id, itemFrame.entityData, true))
333+
334+
connection.send(guiChunk.createClearPacket())
335+
}
336+
337+
// send the map data
338+
val updatePacket = guiChunk.createPacket()
339+
if (updatePacket != null) {
340+
connection.send(updatePacket)
334341
}
335342
}
336343
}
@@ -410,11 +417,25 @@ class MinecraftComposeGui(
410417
silkCoroutineScope.launch {
411418
withContext(guiContext) {
412419
player.connection.send(ClientboundRemoveEntitiesPacket(*itemFrameEntityIds.toIntArray()))
413-
MapIdGenerator.makeOldIdAvailable(guiChunks.map { it.mapId })
414420

415-
frameDispatcher.cancel()
416-
scene.close()
421+
try {
422+
frameDispatcher.cancel()
423+
scene.close()
424+
} finally {
425+
coroutineScope {
426+
guiChunks.forEach {
427+
mcSyncLaunch {
428+
if (!player.hasDisconnected()) {
429+
player.connection.send(it.createClearPacket())
430+
}
431+
}
432+
}
433+
}
434+
435+
MapIdGenerator.makeOldIdsAvailable(guiChunks.map { it.mapId })
436+
}
417437
}
438+
418439
guiContext.cancel()
419440
guiContext.close()
420441
}

Diff for: silk-compose/src/main/kotlin/net/silkmc/silk/compose/internal/MapIdGenerator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal object MapIdGenerator {
1919
}
2020
}
2121

22-
fun makeOldIdAvailable(ids: Collection<Int>) {
22+
fun makeOldIdsAvailable(ids: Collection<Int>) {
2323
silkCoroutineScope.launch {
2424
mutex.withLock {
2525
availableOldIds.addAll(ids)

0 commit comments

Comments
 (0)