package ru.dbotthepony.kstarbound.network.packets.clientbound

import com.google.gson.JsonElement
import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.defs.world.WorldStructure
import ru.dbotthepony.kstarbound.fromJson
import ru.dbotthepony.kstarbound.json.readJsonElement
import ru.dbotthepony.kstarbound.json.writeJsonElement
import ru.dbotthepony.kstarbound.network.IClientPacket
import java.io.DataInputStream
import java.io.DataOutputStream

class CentralStructureUpdatePacket(val data: JsonElement) : IClientPacket {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(stream.readJsonElement())

	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeJsonElement(data)
	}

	override fun play(connection: ClientConnection) {
		val read = Starbound.gson.fromJson(data, WorldStructure::class.java)

		connection.enqueue {
			world?.centralStructure = read
		}
	}
}
