diff options
author | Linnea Gräf <nea@nea.moe> | 2024-05-29 21:09:07 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-06-01 01:34:34 +0200 |
commit | e630426656b713ec67bfe7ede2f2081751e349db (patch) | |
tree | 63eff82e16d400a42b5a48bfcb6c89ec81c6763e /src/main/java | |
parent | fac6103658b2c8d6bab3598606d57041cfe16e0c (diff) | |
download | Firmament-e630426656b713ec67bfe7ede2f2081751e349db.tar.gz Firmament-e630426656b713ec67bfe7ede2f2081751e349db.tar.bz2 Firmament-e630426656b713ec67bfe7ede2f2081751e349db.zip |
[WIP] Add mod api
Diffstat (limited to 'src/main/java')
4 files changed, 139 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/firmament/mixins/CustomPayloadEventDispatcher.java b/src/main/java/moe/nea/firmament/mixins/CustomPayloadEventDispatcher.java new file mode 100644 index 0000000..66710eb --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/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; + +import moe.nea.firmament.apis.ingame.FirmamentCustomPayload; +import moe.nea.firmament.events.FirmamentCustomPayloadEvent; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.packet.CustomPayload; +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(ClientPlayNetworkHandler.class) +public class CustomPayloadEventDispatcher { + @Inject(method = "onCustomPayload", at = @At("HEAD"), cancellable = true) + private void handleFirmamentParsedPayload(CustomPayload payload, CallbackInfo ci) { + if (payload instanceof FirmamentCustomPayload customPayload) { + FirmamentCustomPayloadEvent.Companion.publish(new FirmamentCustomPayloadEvent(customPayload)); + ci.cancel(); + } + } +} diff --git a/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadC2SPacketCodec.java b/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadC2SPacketCodec.java new file mode 100644 index 0000000..150611e --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadC2SPacketCodec.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; + +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 WrapCustomPayloadC2SPacketCodec { + + @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/WrapCustomPayloadS2CPacketCodec.java b/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadS2CPacketCodec.java new file mode 100644 index 0000000..7cb8f47 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadS2CPacketCodec.java @@ -0,0 +1,55 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import moe.nea.firmament.apis.ingame.InGameCodecWrapper; +import moe.nea.firmament.apis.ingame.JoinedCustomPayload; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +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; + +import java.util.List; + +@Mixin(priority = 1001, value = CustomPayloadS2CPacket.class) +public abstract class WrapCustomPayloadS2CPacketCodec { + + @Shadow + public abstract CustomPayload payload(); + + @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); + } + + + // TODO: move to own class + @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(); + } + } + + +} diff --git a/src/main/java/moe/nea/firmament/mixins/devenv/WarnForUnknownCustomPayloadSends.java b/src/main/java/moe/nea/firmament/mixins/devenv/WarnForUnknownCustomPayloadSends.java new file mode 100644 index 0000000..1e99285 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/devenv/WarnForUnknownCustomPayloadSends.java @@ -0,0 +1,23 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.mixins.devenv; + +import moe.nea.firmament.Firmament; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.UnknownCustomPayload; +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(UnknownCustomPayload.class) +public class WarnForUnknownCustomPayloadSends { + @Inject(method = "method_56493", at = @At("HEAD")) + private static void warn(UnknownCustomPayload value, PacketByteBuf buf, CallbackInfo ci) { + Firmament.INSTANCE.getLogger().warn("Unknown custom payload is being sent: {}", value); + } +} |