Skip to content

Commit

Permalink
Avoid patching IFluidBlock onto vanilla liquids
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Jan 21, 2024
1 parent e2ba856 commit 074d38b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import me.jellysquid.mods.sodium.client.util.task.CancellationSource;
import me.jellysquid.mods.sodium.client.world.WorldSlice;
import me.jellysquid.mods.sodium.client.world.cloned.ChunkRenderContext;
import me.jellysquid.mods.sodium.common.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -124,7 +125,7 @@ public ChunkBuildResult<T> performBuild(ChunkRenderCacheLocal cache, ChunkBuildB

if (CCLCompat.canHandle(renderType)) {
CCLCompat.renderBlock(slice, pos, blockState, buffers.get(layer));
} else if (renderType == EnumBlockRenderType.MODEL && !(block instanceof IFluidBlock)) {
} else if (renderType == EnumBlockRenderType.MODEL && WorldUtil.toFluidBlock(block) == null) {
IBakedModel model = cache.getBlockModels()
.getModelForState(blockState);

Expand All @@ -134,7 +135,7 @@ public ChunkBuildResult<T> performBuild(ChunkRenderCacheLocal cache, ChunkBuildB
bounds.addBlock(relX, relY, relZ);
}

} else if (block instanceof IFluidBlock) {
} else if (WorldUtil.toFluidBlock(block) != null) {
if (cache.getFluidRenderer().render(cache.getLocalSlice(), blockState, pos, buffers.get(layer))) {
bounds.addBlock(relX, relY, relZ);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.embeddedt.embeddium.render.fluid.EmbeddiumFluidSpriteCache;
import repack.joml.Vector3d;

import java.util.Objects;

public class FluidRenderer {

private static final float EPSILON = 0.001f;
Expand Down Expand Up @@ -104,7 +106,7 @@ public boolean render(IBlockAccess world, IBlockState fluidState, BlockPos pos,
int posY = pos.getY();
int posZ = pos.getZ();

Fluid fluid = ((IFluidBlock) fluidState.getBlock()).getFluid();
Fluid fluid = WorldUtil.toFluidBlock(fluidState.getBlock()).getFluid();

boolean sfUp = this.isFluidOccluded(world, posX, posY, posZ, EnumFacing.UP, fluid);
boolean sfDown = this.isFluidOccluded(world, posX, posY, posZ, EnumFacing.DOWN, fluid) ||
Expand Down Expand Up @@ -427,14 +429,14 @@ private float getCornerHeight(IBlockAccess world, int x, int y, int z, Fluid flu
int z2 = z - (i >> 1 & 1);

Block block = world.getBlockState(this.scratchPos.setPos(x2, y + 1, z2)).getBlock();
if (block instanceof IFluidBlock && ((IFluidBlock) block).getFluid() == fluid) {
if (WorldUtil.getFluidOfBlock(block) == fluid) {
return 1.0F;
}

BlockPos pos = this.scratchPos.setPos(x2, y, z2);

IBlockState blockState = world.getBlockState(pos);
Fluid fluid2 = blockState.getBlock() instanceof IFluidBlock ? ((IFluidBlock) blockState.getBlock()).getFluid() : null;
Fluid fluid2 = WorldUtil.getFluidOfBlock(blockState.getBlock());

if (fluid == fluid2) {
float height = WorldUtil.getFluidHeight(fluid2, blockState.getValue(BlockLiquid.LEVEL));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.jellysquid.mods.sodium.client.world;

import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidBlock;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface VanillaFluidBlock {
IFluidBlock getFakeFluidBlock();

class Implementation implements IFluidBlock {
private final Block block;
private Fluid sodium$forgeFluid;

public Implementation(Block block) {
this.block = block;
}

@Override
public Fluid getFluid() {
Fluid fluid = sodium$forgeFluid;
if(fluid == null) {
sodium$forgeFluid = fluid = block.getDefaultState().getMaterial() == Material.WATER ? FluidRegistry.WATER : FluidRegistry.LAVA;
}
return fluid;
}

@Override
public int place(World world, BlockPos pos, @Nonnull FluidStack fluidStack, boolean doPlace) {
throw new UnsupportedOperationException();
}

@Nullable
@Override
public FluidStack drain(World world, BlockPos pos, boolean doDrain) {
throw new UnsupportedOperationException();
}

@Override
public boolean canDrain(World world, BlockPos pos) {
throw new UnsupportedOperationException();
}

@Override
public float getFilledPercentage(World world, BlockPos pos) {
IBlockState blockState = world.getBlockState(pos);
return getFluid() == null ? 0 : 1 - BlockLiquid.getLiquidHeightPercent(blockState.getValue(BlockLiquid.LEVEL));
}
}
}
20 changes: 18 additions & 2 deletions src/main/java/me/jellysquid/mods/sodium/common/util/WorldUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.jellysquid.mods.sodium.common.util;

import me.jellysquid.mods.sodium.client.world.VanillaFluidBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
Expand Down Expand Up @@ -107,8 +108,8 @@ public static boolean shouldDisplayFluidOverlay(IBlockState block) {
}

public static Fluid getFluid(IBlockState b) {
Block block = b.getBlock();
return block instanceof IFluidBlock ? ((IFluidBlock) block).getFluid() : null;
IFluidBlock fluidBlock = toFluidBlock(b.getBlock());
return fluidBlock != null ? fluidBlock.getFluid() : null;
}

/**
Expand All @@ -132,4 +133,19 @@ public static boolean method_15749(IBlockAccess world, Fluid thiz, BlockPos pos,
}
return b.getMaterial() != Material.ICE && b.isSideSolid(world, pos, dir);
}

public static IFluidBlock toFluidBlock(Block block) {
if(block instanceof VanillaFluidBlock) {
return ((VanillaFluidBlock) block).getFakeFluidBlock();
} else if(block instanceof IFluidBlock) {
return (IFluidBlock)block;
} else {
return null;
}
}

public static Fluid getFluidOfBlock(Block block) {
IFluidBlock fluidBlock = toFluidBlock(block);
return fluidBlock != null ? fluidBlock.getFluid() : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.jellysquid.mods.sodium.mixin.core;

import me.jellysquid.mods.sodium.client.world.VanillaFluidBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
Expand All @@ -8,26 +9,19 @@
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidBlock;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(BlockLiquid.class)
public abstract class MixinBlockLiquid implements IFluidBlock {
private Fluid sodium$forgeFluid;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@Override
public Fluid getFluid() {
Fluid fluid = sodium$forgeFluid;
if(fluid == null) {
Block block = ((Block) (Object) this);
sodium$forgeFluid = fluid = block.getDefaultState().getMaterial() == Material.WATER ? FluidRegistry.WATER : FluidRegistry.LAVA;
}
return fluid;
}
@Mixin(BlockLiquid.class)
public abstract class MixinBlockLiquid implements VanillaFluidBlock {
private final IFluidBlock sodium$fluidBlock = new VanillaFluidBlock.Implementation((Block)(Object)this);

@Override
public float getFilledPercentage(World world, BlockPos pos) {
IBlockState blockState = world.getBlockState(pos);
return getFluid() == null ? 0 : 1 - BlockLiquid.getLiquidHeightPercent(blockState.getValue(BlockLiquid.LEVEL));
public IFluidBlock getFakeFluidBlock() {
return sodium$fluidBlock;
}
}

0 comments on commit 074d38b

Please sign in to comment.