Skip to content

Commit fa5ec30

Browse files
committed
重构RenderEntity / (300$ token GPT 5.5 感觉依旧依托狗屎,先凑合着用 哪天我会再学一下完整的着色之后自己写)
1 parent f299a38 commit fa5ec30

5 files changed

Lines changed: 667 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.coostack.cooparticlesapi.network.packet.client.listener
2+
3+
import cn.coostack.cooparticlesapi.CooParticlesConstants
4+
import cn.coostack.cooparticlesapi.network.packet.server.PacketRendererPostEffectS2C
5+
import cn.coostack.cooparticlesapi.platform.network.ClientContext
6+
import cn.coostack.cooparticlesapi.renderer.post.CooPostEffectTypes
7+
import cn.coostack.cooparticlesapi.renderer.post.CooPostEffects
8+
9+
object ClientRendererPostEffectHandler {
10+
fun receive(packet: PacketRendererPostEffectS2C, context: ClientContext) {
11+
context.client().execute {
12+
apply(packet)
13+
}
14+
}
15+
16+
internal fun apply(packet: PacketRendererPostEffectS2C) {
17+
when (packet.operation) {
18+
PacketRendererPostEffectS2C.Operation.CREATE,
19+
PacketRendererPostEffectS2C.Operation.UPDATE -> applyState(packet)
20+
PacketRendererPostEffectS2C.Operation.REMOVE -> CooPostEffects.client.remove(packet.instanceId)
21+
}
22+
}
23+
24+
private fun applyState(packet: PacketRendererPostEffectS2C) {
25+
val state = packet.state ?: return
26+
if (!CooPostEffectTypes.contains(state.effectType)) {
27+
CooParticlesConstants.logger.warn(
28+
"Skipping synced post effect id={} because type={} is not registered on the client",
29+
state.instanceId,
30+
state.effectType
31+
)
32+
return
33+
}
34+
val instance = state.instantiate(serverSynced = true) ?: return
35+
when (packet.operation) {
36+
PacketRendererPostEffectS2C.Operation.CREATE -> CooPostEffects.client.add(instance)
37+
PacketRendererPostEffectS2C.Operation.UPDATE -> CooPostEffects.client.update(instance)
38+
PacketRendererPostEffectS2C.Operation.REMOVE -> Unit
39+
}
40+
}
41+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)