From 4ba1964e2c728152b029d6017ef1be63837e161a Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Thu, 31 Oct 2024 18:53:50 +0100 Subject: [PATCH] Add Immersive Portals compat for blocks --- build.gradle.kts | 8 ++++++ cardinal-components-block/build.gradle | 1 + .../cca/internal/BlockEntityAddress.java | 7 ++++- .../cca/internal/CcaBlockClient.java | 28 ++++++++++++++++--- .../mixin/block/common/MixinBlockEntity.java | 7 ++++- gradle.properties | 2 ++ 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6ee6e38f..118494ea 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,6 +49,14 @@ allprojects { name = "JitPack" url = URI("https://jitpack.io") } + maven { + name = "Modrinth" + url = URI("https://api.modrinth.com/maven") + } + maven { + name = "Shedaniel" + url = URI("https://maven.shedaniel.me") + } } dependencies { diff --git a/cardinal-components-block/build.gradle b/cardinal-components-block/build.gradle index 5e8a3ae3..9c4196fd 100644 --- a/cardinal-components-block/build.gradle +++ b/cardinal-components-block/build.gradle @@ -4,4 +4,5 @@ dependencies { annotationProcessor api(project(path: ":cardinal-components-base", configuration: "namedElements")) modApi fabricApi.module("fabric-api-lookup-api-v1", rootProject.fabric_api_version) testmodImplementation project(":cardinal-components-base").sourceSets.testmod.output + modCompileOnly "com.github.iPortalTeam:ImmersivePortalsMod:${rootProject.immersive_portals_version}" } diff --git a/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/BlockEntityAddress.java b/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/BlockEntityAddress.java index 670fbf9c..d3c10ca3 100644 --- a/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/BlockEntityAddress.java +++ b/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/BlockEntityAddress.java @@ -27,16 +27,21 @@ import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; public record BlockEntityAddress( BlockEntityType beType, - BlockPos bePos + BlockPos bePos, + RegistryKey worldKey ) { public static final PacketCodec CODEC = PacketCodec.tuple( PacketCodecs.entryOf(Registries.BLOCK_ENTITY_TYPE), BlockEntityAddress::beType, BlockPos.PACKET_CODEC, BlockEntityAddress::bePos, + RegistryKey.createPacketCodec(RegistryKeys.WORLD), BlockEntityAddress::worldKey, BlockEntityAddress::new ); } diff --git a/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/CcaBlockClient.java b/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/CcaBlockClient.java index 8b4ef6b0..9a8345a5 100644 --- a/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/CcaBlockClient.java +++ b/cardinal-components-block/src/main/java/org/ladysnake/cca/internal/CcaBlockClient.java @@ -24,17 +24,37 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientBlockEntityEvents; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.world.World; import org.ladysnake.cca.api.v3.component.ComponentProvider; import org.ladysnake.cca.internal.base.CcaClientInternals; +import qouteall.imm_ptl.core.ClientWorldLoader; + +import java.util.Optional; public class CcaBlockClient { + private static boolean hasImmersivePortals = false; + public static void initClient() { + hasImmersivePortals = FabricLoader.getInstance().isModLoaded("immersive_portals"); + if (FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1")) { CcaClientInternals.registerComponentSync(CardinalComponentsBlock.PACKET_ID, - (payload, ctx) -> payload.componentKey().flatMap(key -> key.maybeGet(payload.targetData().beType().get( - ctx.client().world, - payload.targetData().bePos() - )) + (payload, ctx) -> payload.componentKey().flatMap(key -> { + World world; + if (hasImmersivePortals) { + world = ClientWorldLoader.getOptionalWorld(payload.targetData().worldKey()); + if (world == null) { + return Optional.empty(); + } + } else { + world = ctx.client().world; + } + + return key.maybeGet(payload.targetData().beType().get( + world, + payload.targetData().bePos() + )); + } )); } if (FabricLoader.getInstance().isModLoaded("fabric-lifecycle-events-v1")) { diff --git a/cardinal-components-block/src/main/java/org/ladysnake/cca/mixin/block/common/MixinBlockEntity.java b/cardinal-components-block/src/main/java/org/ladysnake/cca/mixin/block/common/MixinBlockEntity.java index e6341ceb..fa362bc9 100644 --- a/cardinal-components-block/src/main/java/org/ladysnake/cca/mixin/block/common/MixinBlockEntity.java +++ b/cardinal-components-block/src/main/java/org/ladysnake/cca/mixin/block/common/MixinBlockEntity.java @@ -102,9 +102,14 @@ public Iterable getRecipientsForComponentSync() { @Override public ComponentUpdatePayload toComponentPacket(ComponentKey key, boolean required, RegistryByteBuf data) { + World world = this.getWorld(); + if (world == null) { + return null; + } + return new ComponentUpdatePayload<>( CardinalComponentsBlock.PACKET_ID, - new BlockEntityAddress(this.getType(), this.getPos()), + new BlockEntityAddress(this.getType(), this.getPos(), world.getRegistryKey()), required, key.getId(), data diff --git a/gradle.properties b/gradle.properties index 4c8d9c17..247667fe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,6 +12,8 @@ fabric_api_version=0.106.1+1.21.3 elmendorf_version=0.13.0 +immersive_portals_version=v6.0.3-mc1.21.1 + #Publishing mod_version = 6.2.0 curseforge_id = 318449