package ru.dbotthepony.kstarbound.client.network.packets

import ru.dbotthepony.kommons.io.writeStruct2i
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.network.IClientPacket
import ru.dbotthepony.kstarbound.io.readChunkPos
import ru.dbotthepony.kstarbound.world.ChunkPos
import java.io.DataInputStream
import java.io.DataOutputStream

class ForgetChunkPacket(val pos: ChunkPos) : IClientPacket {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(stream.readChunkPos())

	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeStruct2i(pos)
	}

	override fun play(connection: ClientConnection) {
		connection.client.mailbox.execute {
			val world = connection.client.world ?: return@execute
			world.chunkMap.remove(pos)

			world.forEachRenderRegion(pos) {
				it.notifyChunkForget()
			}
		}
	}
}
