package ru.dbotthepony.mc.otm.network

import net.minecraft.network.FriendlyByteBuf
import net.minecraft.network.codec.StreamCodec
import net.minecraft.network.protocol.common.custom.CustomPacketPayload
import net.neoforged.neoforge.network.handling.IPayloadContext
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.client.render.ShockwaveRenderer
import ru.dbotthepony.mc.otm.core.ResourceLocation
import ru.dbotthepony.mc.otm.core.math.Vector

class ShockwaveEffectPacket(val pos: Vector) : CustomPacketPayload {
	fun write(buff: FriendlyByteBuf) {
		buff.writeDouble(pos.x)
		buff.writeDouble(pos.y)
		buff.writeDouble(pos.z)
	}

	fun play(context: IPayloadContext) {
		ShockwaveRenderer.handle(this)
	}

	override fun type(): CustomPacketPayload.Type<out CustomPacketPayload> {
		return TYPE
	}

	companion object {
		val TYPE = CustomPacketPayload.Type<ShockwaveEffectPacket>(
			ResourceLocation(
				OverdriveThatMatters.MOD_ID,
				"shockwave_effect"
			)
		)
		val CODEC: StreamCodec<FriendlyByteBuf, ShockwaveEffectPacket> =
			StreamCodec.ofMember(ShockwaveEffectPacket::write, ::read)

		fun read(buff: FriendlyByteBuf): ShockwaveEffectPacket {
			return ShockwaveEffectPacket(Vector(buff.readDouble(), buff.readDouble(), buff.readDouble()))
		}
	}
}
