From aa9c227beef7b57a1d8a898b793a87ad220b5ad5 Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Tue, 16 Jun 2020 15:17:11 +0200 Subject: [PATCH] fluid distribution and render fixes --- .../contraptions/fluids/FluidPipeBlock.java | 14 +++-- .../fluids/FluidTankRenderer.java | 10 ++-- .../fluids/FluidTankTileEntity.java | 59 +++++++++++++++++-- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidPipeBlock.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidPipeBlock.java index b0786f1b9..20c2ff3e2 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidPipeBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidPipeBlock.java @@ -10,8 +10,10 @@ import net.minecraft.util.Direction; import net.minecraft.util.Direction.Axis; import net.minecraft.util.Direction.AxisDirection; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockReader; import net.minecraft.world.ILightReader; import net.minecraft.world.IWorld; +import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import javax.annotation.Nullable; @@ -25,17 +27,17 @@ public class FluidPipeBlock extends SixWayBlock { return state.getBlock() instanceof FluidPipeBlock; } - public static boolean isTank(BlockState state) { - return state.getBlock() instanceof FluidTankBlock; + public static boolean isTank(BlockState state, IBlockReader world, BlockPos pos, Direction blockFace) { + return state.hasTileEntity() && world.getTileEntity(pos).getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, blockFace.getOpposite()) != null; } // TODO: more generic pipe connection handling. Ideally without marker interface public static boolean canConnectTo(ILightReader world, BlockPos pos, BlockState neighbour, Direction blockFace) { - if (isPipe(neighbour) || isTank(neighbour)) + if (isPipe(neighbour) || isTank(neighbour, world, pos, blockFace)) return true; - return neighbour.getBlock() instanceof PumpBlock && blockFace.getAxis() == neighbour.get(PumpBlock.FACING) - .getAxis(); - } + return neighbour.getBlock() instanceof PumpBlock && blockFace.getAxis() == neighbour.get(PumpBlock.FACING) + .getAxis(); + } public static boolean shouldDrawRim(ILightReader world, BlockPos pos, BlockState state, Direction direction) { if (!isPipe(state)) diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankRenderer.java index dd58b3f88..406557de6 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankRenderer.java @@ -168,7 +168,8 @@ public class FluidTankRenderer extends SafeTileEntityRenderer getTanksToRender(FluidTankTileEntity te) { - boolean up = te.getBlockState().get(FluidTankBlock.TOP); - boolean down = te.getBlockState().get(FluidTankBlock.BOTTOM); - // bounds.shrink(16f); + FluidTankTileEntity upTE = te.getOtherFluidTankTileEntity(Direction.UP); + FluidTankTileEntity downTE = te.getOtherFluidTankTileEntity(Direction.DOWN); + boolean up = upTE == null || upTE.getTank().getFluidAmount() == 0 || te.getTank().getFluid() != upTE.getTank().getFluid(); + boolean down = downTE == null || downTE.getTank().getFluidAmount() == downTE.getTank().getCapacity() || te.getTank().getFluid() != downTE.getTank().getFluid(); return Collections.singletonList(new FluidTankRenderInfo(te.getTank(), up, down, ((FluidTankBlock) te.getBlockState().getBlock()).getTankBodyShape(te.getWorld(), te.getPos()))); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankTileEntity.java index 34f1a1f0b..26cd66ef0 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankTileEntity.java @@ -1,20 +1,24 @@ package com.simibubi.create.content.contraptions.fluids; +import java.util.List; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import com.simibubi.create.foundation.tileEntity.SmartTileEntity; import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; + import net.minecraft.nbt.CompoundNBT; +import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; +import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction; import net.minecraftforge.fluids.capability.templates.FluidTank; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.List; - public class FluidTankTileEntity extends SmartTileEntity { LazyOptional fluid = LazyOptional.of(this::createFluidHandler); @@ -23,6 +27,51 @@ public class FluidTankTileEntity extends SmartTileEntity { super(tileEntityTypeIn); } + @Override + public void tick() { + super.tick(); + + + FluidTankTileEntity other; + + other = getOtherFluidTankTileEntity(Direction.NORTH); + if (other != null && other.getTank().isFluidValid(this.getTank().getFluid())) { + int delta = other.getTank().getFluidAmount() - this.getTank().getFluidAmount(); + if (delta > 0) { + this.getTank().fill(other.getTank().drain(delta / 2, FluidAction.EXECUTE), FluidAction.EXECUTE); + } else if (delta < 0) { + other.getTank().fill(this.getTank().drain(-delta / 2, FluidAction.EXECUTE), FluidAction.EXECUTE); + } + } + + + other = getOtherFluidTankTileEntity(Direction.WEST); + if (other != null && other.getTank().isFluidValid(this.getTank().getFluid())) { + int delta = other.getTank().getFluidAmount() - this.getTank().getFluidAmount(); + if (delta > 0) { + this.getTank().fill(other.getTank().drain(delta / 2, FluidAction.EXECUTE), FluidAction.EXECUTE); + } else if (delta < 0) { + other.getTank().fill(this.getTank().drain(-delta / 2, FluidAction.EXECUTE), FluidAction.EXECUTE); + } + } + + other = getOtherFluidTankTileEntity(Direction.UP); + if (other != null && other.getTank().isFluidValid(this.getTank().getFluid())) { + int space = this.getTank().getCapacity() - this.getTank().getFluidAmount(); + if (space > 0 && other.getTank().getFluidAmount() > 0) { + this.getTank().fill(other.getTank().drain(space, FluidAction.EXECUTE), FluidAction.EXECUTE); + } + } + } + + @Nullable + public FluidTankTileEntity getOtherFluidTankTileEntity(Direction direction) { + TileEntity otherTE = world.getTileEntity(pos.offset(direction)); + if (otherTE instanceof FluidTankTileEntity) + return (FluidTankTileEntity) otherTE; + return null; + } + @Nonnull @Override public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { @@ -50,7 +99,7 @@ public class FluidTankTileEntity extends SmartTileEntity { @Nonnull public FluidTank createFluidHandler() { - return new FluidTank(16); + return new FluidTank(16000); } public IFluidTank getTank() {