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

import ru.dbotthepony.kommons.io.readUUID
import ru.dbotthepony.kommons.io.writeUUID
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.client.world.ClientWorld
import ru.dbotthepony.kstarbound.defs.world.WorldTemplate
import ru.dbotthepony.kstarbound.network.IClientPacket
import ru.dbotthepony.kstarbound.world.World
import ru.dbotthepony.kstarbound.world.WorldGeometry
import java.io.DataInputStream
import java.io.DataOutputStream
import java.util.*

data class JoinWorldPacket(val uuid: UUID, val geometry: WorldGeometry) : IClientPacket {
	constructor(buff: DataInputStream, isLegacy: Boolean) : this(buff.readUUID(), WorldGeometry(buff))
	constructor(world: World<*, *>) : this(UUID(0L, 0L), world.geometry)

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

	override fun play(connection: ClientConnection) {
		connection.client.mailbox.execute {
			connection.client.world = ClientWorld(connection.client, WorldTemplate(geometry))
		}
	}
}
