|
| 1 | +package cn.coostack.cooparticlesapi.network.packet.server |
| 2 | + |
| 3 | +import cn.coostack.cooparticlesapi.CooParticlesConstants |
| 4 | +import cn.coostack.cooparticlesapi.renderer.post.PostEffectBinding |
| 5 | +import cn.coostack.cooparticlesapi.renderer.post.PostEffectLifecycle |
| 6 | +import cn.coostack.cooparticlesapi.renderer.post.PostEffectParams |
| 7 | +import cn.coostack.cooparticlesapi.renderer.post.SyncedPostEffectState |
| 8 | +import net.minecraft.network.FriendlyByteBuf |
| 9 | +import net.minecraft.network.codec.StreamCodec |
| 10 | +import net.minecraft.network.protocol.common.custom.CustomPacketPayload |
| 11 | +import net.minecraft.resources.ResourceLocation |
| 12 | + |
| 13 | +class PacketRendererPostEffectS2C( |
| 14 | + val operation: Operation, |
| 15 | + val state: SyncedPostEffectState?, |
| 16 | + val instanceId: String |
| 17 | +) : CustomPacketPayload { |
| 18 | + enum class Operation(val id: Int) { |
| 19 | + CREATE(0), |
| 20 | + UPDATE(1), |
| 21 | + REMOVE(2); |
| 22 | + |
| 23 | + companion object { |
| 24 | + fun idOf(id: Int): Operation { |
| 25 | + return entries.firstOrNull { it.id == id } ?: CREATE |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + companion object { |
| 31 | + private val identifierID = |
| 32 | + ResourceLocation.fromNamespaceAndPath(CooParticlesConstants.MOD_ID, "renderer_post_effect_packet") |
| 33 | + val payloadID = CustomPacketPayload.Type<PacketRendererPostEffectS2C>(identifierID) |
| 34 | + |
| 35 | + @JvmStatic |
| 36 | + val CODEC: StreamCodec<FriendlyByteBuf, PacketRendererPostEffectS2C> = |
| 37 | + CustomPacketPayload.codec({ packet, buf -> |
| 38 | + buf.writeInt(packet.operation.id) |
| 39 | + buf.writeUtf(packet.instanceId) |
| 40 | + buf.writeBoolean(packet.state != null) |
| 41 | + packet.state?.write(buf) |
| 42 | + }, { buf -> |
| 43 | + val operation = Operation.idOf(buf.readInt()) |
| 44 | + val instanceId = buf.readUtf() |
| 45 | + val state = if (buf.readBoolean()) readState(buf) else null |
| 46 | + PacketRendererPostEffectS2C(operation, state, instanceId) |
| 47 | + }) |
| 48 | + |
| 49 | + fun create(state: SyncedPostEffectState): PacketRendererPostEffectS2C { |
| 50 | + return PacketRendererPostEffectS2C(Operation.CREATE, state, state.instanceId) |
| 51 | + } |
| 52 | + |
| 53 | + fun update(state: SyncedPostEffectState): PacketRendererPostEffectS2C { |
| 54 | + return PacketRendererPostEffectS2C(Operation.UPDATE, state, state.instanceId) |
| 55 | + } |
| 56 | + |
| 57 | + fun remove(instanceId: String): PacketRendererPostEffectS2C { |
| 58 | + return PacketRendererPostEffectS2C(Operation.REMOVE, null, instanceId) |
| 59 | + } |
| 60 | + |
| 61 | + private fun SyncedPostEffectState.write(buf: FriendlyByteBuf) { |
| 62 | + buf.writeResourceLocation(effectType) |
| 63 | + buf.writeUtf(instanceId) |
| 64 | + PostEffectBinding.writeTyped(buf, binding) |
| 65 | + lifecycle.write(buf) |
| 66 | + params.write(buf) |
| 67 | + buf.writeUtf(sourceId) |
| 68 | + buf.writeInt(priority) |
| 69 | + } |
| 70 | + |
| 71 | + private fun readState(buf: FriendlyByteBuf): SyncedPostEffectState { |
| 72 | + return SyncedPostEffectState( |
| 73 | + effectType = buf.readResourceLocation(), |
| 74 | + instanceId = buf.readUtf(), |
| 75 | + binding = PostEffectBinding.readTyped(buf), |
| 76 | + lifecycle = PostEffectLifecycle.read(buf), |
| 77 | + params = PostEffectParams.read(buf), |
| 78 | + sourceId = buf.readUtf(), |
| 79 | + priority = buf.readInt() |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + override fun type(): CustomPacketPayload.Type<out CustomPacketPayload?> { |
| 85 | + return payloadID |
| 86 | + } |
| 87 | +} |
0 commit comments