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 | |
parent | 3b9fd665af85f08f3bcae711f99c49528fc4b666 (diff) | |
download | firmament-db2b96bd9801d1e9f0dc108520310f9add8df4b7.tar.gz firmament-db2b96bd9801d1e9f0dc108520310f9add8df4b7.tar.bz2 firmament-db2b96bd9801d1e9f0dc108520310f9add8df4b7.zip |
Fix custom payload event
[no changelog]
-rw-r--r-- | build.gradle.kts | 2 | ||||
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/CustomPayloadEventDispatcher.java | 27 | ||||
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/custompayload/CustomPayloadEventDispatcher.java | 27 | ||||
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoC2SCustomPayloadPacket.java (renamed from src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadC2SPacketCodec.java) | 4 | ||||
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoS2CCustomPayloadPacket.java (renamed from src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadS2CPacketCodec.java) | 25 | ||||
-rw-r--r-- | src/main/java/moe/nea/firmament/mixins/custompayload/SplitJoinedCustomPayload.java | 33 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt | 4 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt | 1 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt | 7 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/MC.kt | 35 |
10 files changed, 99 insertions, 66 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index 7d358dd..4fe313d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -74,6 +74,8 @@ repositories { excludeModule("io.github.cottonmc", "LibGui") } } + maven( "https://repo.hypixel.net/repository/Hypixel/") + maven("https://maven.azureaaron.net/snapshots") } kotlin { diff --git a/src/main/java/moe/nea/firmament/mixins/CustomPayloadEventDispatcher.java b/src/main/java/moe/nea/firmament/mixins/CustomPayloadEventDispatcher.java deleted file mode 100644 index 66710eb..0000000 --- a/src/main/java/moe/nea/firmament/mixins/CustomPayloadEventDispatcher.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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/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/WrapCustomPayloadC2SPacketCodec.java b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoC2SCustomPayloadPacket.java index 150611e..6586466 100644 --- a/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadC2SPacketCodec.java +++ b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoC2SCustomPayloadPacket.java @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -package moe.nea.firmament.mixins; +package moe.nea.firmament.mixins.custompayload; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; @@ -19,7 +19,7 @@ import org.spongepowered.asm.mixin.injection.At; import java.util.List; @Mixin(priority = 1001, value = CustomPayloadC2SPacket.class) -public class WrapCustomPayloadC2SPacketCodec { +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( diff --git a/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadS2CPacketCodec.java b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoS2CCustomPayloadPacket.java index 7cb8f47..1bdedad 100644 --- a/src/main/java/moe/nea/firmament/mixins/WrapCustomPayloadS2CPacketCodec.java +++ b/src/main/java/moe/nea/firmament/mixins/custompayload/InjectCustomCodecIntoS2CCustomPayloadPacket.java @@ -4,30 +4,22 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -package moe.nea.firmament.mixins; +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 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(); +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( @@ -39,17 +31,4 @@ public abstract class WrapCustomPayloadS2CPacketCodec { 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/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(); + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt index 460df91..00e6aa9 100644 --- a/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt +++ b/src/main/kotlin/moe/nea/firmament/apis/ingame/HypixelModAPI.kt @@ -6,6 +6,7 @@ package moe.nea.firmament.apis.ingame +import net.hypixel.modapi.fabric.event.HypixelModAPICallback import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket import net.minecraft.text.Text import moe.nea.firmament.annotations.Subscribe @@ -30,6 +31,9 @@ object HypixelModAPI : SubscriptionOwner { InGameCodecWrapper.createStealthyCodec( PartyInfoResponse.intoType() ) + HypixelModAPICallback.EVENT.register(HypixelModAPICallback { + MC.sendChat(Text.literal("Official API: $it")) + }) } @JvmStatic diff --git a/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt b/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt index 0720dbf..447b902 100644 --- a/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt +++ b/src/main/kotlin/moe/nea/firmament/apis/ingame/InGameCodecWrapper.kt @@ -35,6 +35,7 @@ class InGameCodecWrapper( override fun decode(buf: PacketByteBuf): CustomPayload { val duplicateBuffer = PacketByteBuf(buf.slice()) val original = wrapped.decode(buf) + buf.skipBytes(buf.readableBytes()) val duplicate = direction.customCodec.decode(duplicateBuffer) if (duplicate is FirmamentCustomPayload.Unhandled) return original diff --git a/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt b/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt index 8a7b387..f0a0ed3 100644 --- a/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt +++ b/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt @@ -1,5 +1,6 @@ /* * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe> + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -8,6 +9,7 @@ package moe.nea.firmament.events import java.util.concurrent.CopyOnWriteArrayList import moe.nea.firmament.Firmament +import moe.nea.firmament.util.MC /** * A pubsub event bus. @@ -47,4 +49,9 @@ open class FirmamentEventBus<T : FirmamentEvent> { return event } + fun publishSync(event: T) { + MC.onMainThread { + publish(event) + } + } } diff --git a/src/main/kotlin/moe/nea/firmament/util/MC.kt b/src/main/kotlin/moe/nea/firmament/util/MC.kt index 830934f..4a4f7d3 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MC.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MC.kt @@ -56,23 +56,30 @@ object MC { player?.networkHandler?.sendCommand(command) } - inline val resourceManager get() = (MinecraftClient.getInstance().resourceManager as ReloadableResourceManagerImpl) + fun onMainThread(block: () -> Unit) { + if (instance.isOnThread) + block() + else + instance.send(block) + } + + inline val resourceManager get() = (instance.resourceManager as ReloadableResourceManagerImpl) inline val networkHandler get() = player?.networkHandler inline val instance get() = MinecraftClient.getInstance() - inline val keyboard get() = MinecraftClient.getInstance().keyboard - inline val textureManager get() = MinecraftClient.getInstance().textureManager - inline val inGameHud get() = MinecraftClient.getInstance().inGameHud - inline val font get() = MinecraftClient.getInstance().textRenderer - inline val soundManager get() = MinecraftClient.getInstance().soundManager - inline val player get() = MinecraftClient.getInstance().player - inline val camera get() = MinecraftClient.getInstance().cameraEntity - inline val guiAtlasManager get() = MinecraftClient.getInstance().guiAtlasManager - inline val world get() = MinecraftClient.getInstance().world + inline val keyboard get() = instance.keyboard + inline val textureManager get() = instance.textureManager + inline val inGameHud get() = instance.inGameHud + inline val font get() = instance.textRenderer + inline val soundManager get() = instance.soundManager + inline val player get() = instance.player + inline val camera get() = instance.cameraEntity + inline val guiAtlasManager get() = instance.guiAtlasManager + inline val world get() = instance.world inline var screen - get() = MinecraftClient.getInstance().currentScreen - set(value) = MinecraftClient.getInstance().setScreen(value) - inline val handledScreen: HandledScreen<*>? get() = MinecraftClient.getInstance().currentScreen as? HandledScreen<*> - inline val window get() = MinecraftClient.getInstance().window + get() = instance.currentScreen + set(value) = instance.setScreen(value) + inline val handledScreen: HandledScreen<*>? get() = instance.currentScreen as? HandledScreen<*> + inline val window get() = instance.window inline val currentRegistries: RegistryWrapper.WrapperLookup? get() = world?.registryManager val defaultRegistries: RegistryWrapper.WrapperLookup = BuiltinRegistries.createWrapperLookup() val defaultItems = defaultRegistries.getWrapperOrThrow(RegistryKeys.ITEM) |