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

import com.google.gson.JsonObject
import ru.dbotthepony.kommons.gson.set
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.json.mergeJson
import ru.dbotthepony.kstarbound.json.readJsonObject
import ru.dbotthepony.kstarbound.json.writeJsonObject
import ru.dbotthepony.kstarbound.network.IClientPacket
import ru.dbotthepony.kstarbound.network.IServerPacket
import ru.dbotthepony.kstarbound.server.ServerConnection
import java.io.DataInputStream
import java.io.DataOutputStream

class UpdateWorldPropertiesPacket(val update: JsonObject) : IClientPacket, IServerPacket {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(stream.readJsonObject())

	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeJsonObject(update)
	}

	override fun play(connection: ClientConnection) {
		connection.enqueue {
			world?.updateProperties(update)
		}
	}

	override fun play(connection: ServerConnection) {
		connection.enqueue {
			updateProperties(update)
			broadcast(this@UpdateWorldPropertiesPacket)
		}
	}
}
