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

import ru.dbotthepony.kommons.io.readVector2d
import ru.dbotthepony.kommons.io.readVector2f
import ru.dbotthepony.kommons.io.writeStruct2d
import ru.dbotthepony.kommons.io.writeStruct2f
import ru.dbotthepony.kommons.vector.Vector2d
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.network.IClientPacket
import java.io.DataInputStream
import java.io.DataOutputStream

class SetPlayerStartPacket(val position: Vector2d, val respawnInWorld: Boolean) : IClientPacket {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(if (isLegacy) stream.readVector2f().toDoubleVector() else stream.readVector2d(), stream.readBoolean())

	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		if (isLegacy)
			stream.writeStruct2f(position.toFloatVector())
		else
			stream.writeStruct2d(position)

		stream.writeBoolean(respawnInWorld)
	}

	override fun play(connection: ClientConnection) {
		connection.enqueue {
			world?.setPlayerSpawn(position, respawnInWorld)
		}
	}
}
