diff options
author | Linnea Gräf <nea@nea.moe> | 2024-06-05 01:04:55 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-06-05 01:04:55 +0200 |
commit | db2b96bd9801d1e9f0dc108520310f9add8df4b7 (patch) | |
tree | 559fa8ce7db8b3cc6b488c1bf8c0411a1b809bb8 /src/main/java/moe/nea/firmament/mixins/custompayload | |
parent | 3b9fd665af85f08f3bcae711f99c49528fc4b666 (diff) | |
download | firmament-db2b96bd9801d1e9f0dc108520310f9add8df4b7.tar.gz firmament-db2b96bd9801d1e9f0dc108520310f9add8df4b7.tar.bz2 firmament-db2b96bd9801d1e9f0dc108520310f9add8df4b7.zip |
Fix custom payload event
[no changelog]
Diffstat (limited to 'src/main/java/moe/nea/firmament/mixins/custompayload')
4 files changed, 128 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/firmament/mixins/custompayload/CustomPayloadEventDispatcher.java b/src/main/java/moe/nea/firmament/mixins/custompayload/CustomPayloadEventDispatcher.java new file mode 100644 index 0000000..591d8c2 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custompayload/CustomPayloadEventDispatcher.java @@ -0,0 +1,27 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custompayload; + +import moe.nea.firmament.apis.ingame.FirmamentCustomPayload; +import moe.nea.firmament.events.FirmamentCustomPayloadEvent; +import net.minecraft.client.network.ClientCommonNetworkHandler; +import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(value = ClientCommonNetworkHandler.class, priority = 500) +public class CustomPayloadEventDispatcher { + @Inject(method = "onCustomPayload(Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;)V", at = @At("HEAD"), cancellable = true) + private void handleFirmamentParsedPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { + if (packet.payload() instanceof FirmamentCustomPayload customPayload) { + FirmamentCustomPayloadEvent.Companion.publishSync(new FirmamentCustomPayloadEvent(customPayload)); + ci.cancel(); + } + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoC2SCustomPayloadPacket.java b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoC2SCustomPayloadPacket.java new file mode 100644 index 0000000..6586466 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoC2SCustomPayloadPacket.java @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custompayload; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import moe.nea.firmament.apis.ingame.InGameCodecWrapper; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.List; + +@Mixin(priority = 1001, value = CustomPayloadC2SPacket.class) +public class InjectCustomCodecIntoC2SCustomPayloadPacket { + + @WrapOperation(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/CustomPayload;createCodec(Lnet/minecraft/network/packet/CustomPayload$CodecFactory;Ljava/util/List;)Lnet/minecraft/network/codec/PacketCodec;")) + private static PacketCodec<PacketByteBuf, CustomPayload> wrapFactory( + CustomPayload.CodecFactory<PacketByteBuf> unknownCodecFactory, + List<CustomPayload.Type<PacketByteBuf, ?>> types, + Operation<PacketCodec<PacketByteBuf, CustomPayload>> original) { + + var originalCodec = original.call(unknownCodecFactory, types); + + return new InGameCodecWrapper(originalCodec, InGameCodecWrapper.Direction.C2S); + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoS2CCustomPayloadPacket.java b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoS2CCustomPayloadPacket.java new file mode 100644 index 0000000..1bdedad --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoS2CCustomPayloadPacket.java @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custompayload; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import moe.nea.firmament.apis.ingame.InGameCodecWrapper; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.List; + +@Mixin(priority = 1001, value = CustomPayloadS2CPacket.class) +public abstract class InjectCustomCodecIntoS2CCustomPayloadPacket { + + @WrapOperation(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/CustomPayload;createCodec(Lnet/minecraft/network/packet/CustomPayload$CodecFactory;Ljava/util/List;)Lnet/minecraft/network/codec/PacketCodec;")) + private static PacketCodec<PacketByteBuf, CustomPayload> wrapFactory( + CustomPayload.CodecFactory<PacketByteBuf> unknownCodecFactory, + List<CustomPayload.Type<PacketByteBuf, ?>> types, + Operation<PacketCodec<PacketByteBuf, CustomPayload>> original) { + + var originalCodec = original.call(unknownCodecFactory, types); + + return new InGameCodecWrapper(originalCodec, InGameCodecWrapper.Direction.S2C); + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/custompayload/SplitJoinedCustomPayload.java b/src/main/java/moe/nea/firmament/mixins/custompayload/SplitJoinedCustomPayload.java new file mode 100644 index 0000000..3ef4895 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/custompayload/SplitJoinedCustomPayload.java @@ -0,0 +1,33 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.custompayload; + +import moe.nea.firmament.apis.ingame.JoinedCustomPayload; +import net.minecraft.network.listener.ClientCommonPacketListener; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(CustomPayloadS2CPacket.class) +public abstract class SplitJoinedCustomPayload { + + @Shadow + public abstract CustomPayload payload(); + + @Inject(method = "apply(Lnet/minecraft/network/listener/ClientCommonPacketListener;)V", at = @At("HEAD"), cancellable = true) + private void onApply(ClientCommonPacketListener clientCommonPacketListener, CallbackInfo ci) { + if (payload() instanceof JoinedCustomPayload joinedCustomPayload) { + new CustomPayloadS2CPacket(joinedCustomPayload.getOriginal()).apply(clientCommonPacketListener); + new CustomPayloadS2CPacket(joinedCustomPayload.getSmuggled()).apply(clientCommonPacketListener); + ci.cancel(); + } + } +} |