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

import com.google.gson.JsonObject
import ru.dbotthepony.kommons.io.readUUID
import ru.dbotthepony.kommons.io.writeUUID
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.json.readJsonObject
import ru.dbotthepony.kstarbound.json.writeJsonObject
import ru.dbotthepony.kstarbound.network.IClientPacket
import ru.dbotthepony.kstarbound.world.entities.tile.WorldObject
import java.io.DataInputStream
import java.io.DataOutputStream
import java.util.UUID

class SpawnWorldObjectPacket(val uuid: UUID, val data: JsonObject) : IClientPacket {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(stream.readUUID(), stream.readJsonObject())

	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeUUID(uuid)
		stream.writeJsonObject(data)
	}

	override fun play(connection: ClientConnection) {
		connection.client.mailbox.execute {
			val world = connection.client.world ?: return@execute
			val obj = WorldObject.fromJson(data)
			//obj.uuid = uuid
			obj.joinWorld(world)
		}
	}
}
