diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSlice.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSlice.java index 28a0ba9d6..fd47932cb 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSlice.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSlice.java @@ -467,6 +467,18 @@ public class WorldSlice implements IBlockAccessExtended, FLBlockAccess, LiquidLo return fl$getFluid(x, y, z); } + public int liquidlogged$getFluidLevel(int x, int y, int z) { + if (this.context == null || !blockBoxContains(this.volume, x, y, z)) { + return 0; + } + + final int relX = x - this.baseX; + final int relY = y - this.baseY; + final int relZ = z - this.baseZ; + return this.sections[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)] + .getFluidLevel(relX & 15, relY & 15, relZ & 15); + } + public void fl$setFluid(int x, int y, int z, @Nullable Fluid fluid) { throw new UnsupportedOperationException("Cannot set fluids in a world slice. WorldSlice is read-only"); } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSection.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSection.java index 364bfc95b..0376a7f87 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSection.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSection.java @@ -10,6 +10,7 @@ import com.gtnewhorizons.angelica.compat.mojang.ChunkSectionPos; import com.gtnewhorizons.angelica.mixins.interfaces.IChunkTileEntityMapHolder; import com.gtnewhorizons.angelica.utils.ConcurrentTileEntityMap; import com.gtnewhorizons.liquidlogged.api.LiquidLoggedAPI; +import com.gtnewhorizons.liquidlogged.api.LiquidLoggedBlockAccess; import com.gtnewhorizons.liquidlogged.api.SectionFluidsView; import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap; import mega.fluidlogged.internal.mixin.hook.FLSubChunk; @@ -26,6 +27,9 @@ import net.minecraft.world.chunk.NibbleArray; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import net.minecraftforge.fluids.Fluid; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.util.Map; public class ClonedChunkSection { @@ -45,6 +49,7 @@ public class ClonedChunkSection { // Fluids stored in blocks by LiquidLogged, indexed y << 8 | z << 4 | x; null when the section holds none private Fluid[] liquidLoggedFluids; + private byte[] liquidLoggedFluidLevels; private long lastUsedTimestamp = Long.MAX_VALUE; @@ -143,12 +148,57 @@ public class ClonedChunkSection { final SectionFluidsView view = LiquidLoggedAPI.getSection(section); if (view == null || view.isEmpty()) { this.liquidLoggedFluids = null; + this.liquidLoggedFluidLevels = null; return; } if (this.liquidLoggedFluids == null) { this.liquidLoggedFluids = new Fluid[SECTION_BLOCK_LENGTH * SECTION_BLOCK_LENGTH * SECTION_BLOCK_LENGTH]; } view.copyTo(this.liquidLoggedFluids); + + final MethodHandle getter = LiquidLoggedLevels.GETTER; + if (getter == null) { + return; + } + final int minX = this.pos.getMinX(); + final int minY = this.pos.getMinY(); + final int minZ = this.pos.getMinZ(); + try { + final LiquidLoggedBlockAccess access = (LiquidLoggedBlockAccess) this.world; + for (int i = 0; i < this.liquidLoggedFluids.length; i++) { + final int level = this.liquidLoggedFluids[i] == null ? 0 + : (int) getter.invokeExact(access, minX + (i & 15), minY + (i >> 8), minZ + ((i >> 4) & 15)); + if (level != 0 && this.liquidLoggedFluidLevels == null) { + this.liquidLoggedFluidLevels = new byte[this.liquidLoggedFluids.length]; + } + if (this.liquidLoggedFluidLevels != null) { + this.liquidLoggedFluidLevels[i] = (byte) level; + } + } + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new IllegalStateException("Could not read LiquidLogged fluid levels", e); + } + } + + private static final class LiquidLoggedLevels { + private static final MethodHandle GETTER = findGetter(); + + private static MethodHandle findGetter() { + try { + return MethodHandles.publicLookup().findVirtual(LiquidLoggedBlockAccess.class, "liquidlogged$getFluidLevel", + MethodType.methodType(int.class, int.class, int.class, int.class)); + } catch (NoSuchMethodException e) { + return null; + } catch (IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + } + + public int getFluidLevel(int x, int y, int z) { + return this.liquidLoggedFluidLevels == null ? 0 : this.liquidLoggedFluidLevels[y << 8 | z << 4 | x]; } public Fluid getFluid(int x, int y, int z) { diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSliceFluidLevelTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSliceFluidLevelTest.java new file mode 100644 index 000000000..0a92e142e --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/celeritas/world/WorldSliceFluidLevelTest.java @@ -0,0 +1,74 @@ +package com.gtnewhorizons.angelica.rendering.celeritas.world; + +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.world.WorldProvider; +import net.minecraft.world.gen.structure.StructureBoundingBox; +import net.minecraftforge.fluids.Fluid; + +import org.junit.jupiter.api.Test; + +import com.gtnewhorizons.angelica.compat.ModStatus; +import com.gtnewhorizons.angelica.compat.mojang.ChunkSectionPos; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.proxy.ClientProxy; +import com.gtnewhorizons.angelica.rendering.celeritas.world.cloned.ChunkRenderContext; +import com.gtnewhorizons.angelica.rendering.celeritas.world.cloned.ClonedChunkSection; + +import me.jellysquid.mods.sodium.client.gui.SodiumGameOptions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class WorldSliceFluidLevelTest { + + @Test + void readsSourceAndFlowLevelsAcrossSectionBoundaries() { + final SodiumGameOptions previousOptions = Reflect.getStatic(ClientProxy.class, "CONFIG"); + final boolean wasLoaded = ModStatus.isLiquidLoggedLoaded; + Reflect.setStatic(ClientProxy.class, "CONFIG", new SodiumGameOptions()); + ModStatus.isLiquidLoggedLoaded = true; + try { + checkFluidLevels(); + } finally { + ModStatus.isLiquidLoggedLoaded = wasLoaded; + Reflect.setStatic(ClientProxy.class, "CONFIG", previousOptions); + } + } + + private static void checkFluidLevels() { + final WorldClient world = mock(WorldClient.class); + Reflect.set(world, "provider", mock(WorldProvider.class)); + final WorldSlice slice = new WorldSlice(world); + final ClonedChunkSection[] sections = new ClonedChunkSection[64]; + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 3; y++) { + for (int z = 0; z < 3; z++) { + final ClonedChunkSection section = mock(ClonedChunkSection.class); + when(section.getPosition()).thenReturn(ChunkSectionPos.from(x - 2, y, z - 1)); + sections[WorldSlice.getLocalSectionIndex(x, y, z)] = section; + } + } + } + final Fluid water = new Fluid("water"); + final ClonedChunkSection west = sections[WorldSlice.getLocalSectionIndex(0, 1, 1)]; + final ClonedChunkSection center = sections[WorldSlice.getLocalSectionIndex(1, 1, 1)]; + final ClonedChunkSection east = sections[WorldSlice.getLocalSectionIndex(2, 1, 1)]; + when(center.getFluid(0, 4, 7)).thenReturn(water); + when(west.getFluidLevel(15, 4, 7)).thenReturn(3); + when(center.getFluidLevel(15, 4, 7)).thenReturn(5); + when(east.getFluidLevel(0, 4, 7)).thenReturn(8); + when(east.getFluidLevel(2, 4, 7)).thenReturn(12); + + slice.copyData(new ChunkRenderContext(ChunkSectionPos.from(-1, 1, 0), sections, + new StructureBoundingBox(-18, 14, -2, 1, 33, 17))); + + assertSame(water, slice.liquidlogged$getFluid(-16, 20, 7)); + assertEquals(0, slice.liquidlogged$getFluidLevel(-16, 20, 7)); + assertEquals(3, slice.liquidlogged$getFluidLevel(-17, 20, 7)); + assertEquals(5, slice.liquidlogged$getFluidLevel(-1, 20, 7)); + assertEquals(8, slice.liquidlogged$getFluidLevel(0, 20, 7)); + assertEquals(0, slice.liquidlogged$getFluidLevel(2, 20, 7)); + } +}