From b767df3fc30018f31e6a56e6df137ab7936d8ea5 Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Tue, 16 Jun 2020 18:28:09 +0200 Subject: [PATCH] fix data sync and tank render --- .../contraptions/fluids/FluidTankBlock.java | 4 +- .../fluids/FluidTankRenderer.java | 42 ++++++++--- .../fluids/FluidTankTileEntity.java | 72 ++++++++++++++++--- 3 files changed, 96 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankBlock.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankBlock.java index 86a856f88..6ef74d921 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidTankBlock.java @@ -88,10 +88,10 @@ public class FluidTankBlock extends Block { public AxisAlignedBB getTankBodyShape(IBlockReader world, BlockPos pos) { return new AxisAlignedBB((isTankToDirection(world, pos, Direction.WEST) ? 0 : 2) / 16f, - (isTankToDirection(world, pos, Direction.DOWN) ? 0 : 3) / 16f, + (isTankToDirection(world, pos, Direction.DOWN) ? 0 : 4) / 16f, (isTankToDirection(world, pos, Direction.NORTH) ? 0 : 2) / 16f, (isTankToDirection(world, pos, Direction.EAST) ? 16 : 14) / 16f, - (isTankToDirection(world, pos, Direction.UP) ? 16 : 13) / 16f, + (isTankToDirection(world, pos, Direction.UP) ? 16 : 12) / 16f, (isTankToDirection(world, pos, Direction.SOUTH) ? 16 : 14) / 16f); } 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 406557de6..61ae12e2f 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 @@ -165,13 +165,11 @@ public class FluidTankRenderer extends SafeTileEntityRenderer getTanksToRender(FluidTankTileEntity te) { 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()))); + FluidTankTileEntity eastTE = te.getOtherFluidTankTileEntity(Direction.EAST); + FluidTankTileEntity westTE = te.getOtherFluidTankTileEntity(Direction.WEST); + FluidTankTileEntity northTE = te.getOtherFluidTankTileEntity(Direction.NORTH); + FluidTankTileEntity southTE = te.getOtherFluidTankTileEntity(Direction.SOUTH); + + boolean up = upTE != null && ( upTE.getTank().getFluidAmount() == 0 || te.getTank().getFluid().getRawFluid() != upTE.getTank().getFluid().getRawFluid()); + boolean down = downTE != null && (downTE.getTank().getFluidAmount() < downTE.getTank().getCapacity() || te.getTank().getFluid().getRawFluid() != downTE.getTank().getFluid().getRawFluid()); + boolean east = eastTE == null || te.getTank().getFluid().getRawFluid() != eastTE.getTank().getFluid().getRawFluid(); + boolean west = westTE == null || te.getTank().getFluid().getRawFluid() != westTE.getTank().getFluid().getRawFluid(); + boolean north = northTE == null || te.getTank().getFluid().getRawFluid() != northTE.getTank().getFluid().getRawFluid(); + boolean south = southTE == null || te.getTank().getFluid().getRawFluid() != southTE.getTank().getFluid().getRawFluid(); + + return Collections.singletonList(new FluidTankRenderInfo(te.getTank(), up, down, east, west, north, south, ((FluidTankBlock) te.getBlockState().getBlock()).getTankBodyShape(te.getWorld(), te.getPos()))); } private static class FluidTankRenderInfo extends TankRenderInfo { private final boolean up; private final boolean down; + private final boolean east; + private final boolean west; + private final boolean north; + private final boolean south; - FluidTankRenderInfo(IFluidTank tank, boolean up, boolean down, AxisAlignedBB bounds) { + FluidTankRenderInfo(IFluidTank tank, boolean up, boolean down, boolean east, boolean west, boolean north, boolean south, AxisAlignedBB bounds) { super(tank, bounds); this.up = up; this.down = down; + this.east = east; + this.west = west; + this.north = north; + this.south = south; } @Override @@ -207,6 +223,14 @@ public class FluidTankRenderer extends SafeTileEntityRenderer fluid = LazyOptional.of(this::createFluidHandler); + private int priority = 1000; public FluidTankTileEntity(TileEntityType tileEntityTypeIn) { super(tileEntityTypeIn); } + private int calculateDrainAmount(FluidTankTileEntity other, int delta) { + boolean roundDirection = other.getPriority() > this.getPriority(); + return (int) Math.abs(roundDirection ? Math.floor(delta / 2f) : Math.ceil(delta / 2f)); + } + @Override public void tick() { super.tick(); - + updatePriority(); 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); + this.getTank().fill(other.getTank().drain(calculateDrainAmount(other, delta), FluidAction.EXECUTE), FluidAction.EXECUTE); + other.markDirty(); + this.markDirty(); + other.sendData(); + sendData(); } else if (delta < 0) { - other.getTank().fill(this.getTank().drain(-delta / 2, FluidAction.EXECUTE), FluidAction.EXECUTE); + other.getTank().fill(this.getTank().drain(calculateDrainAmount(other, delta), FluidAction.EXECUTE), FluidAction.EXECUTE); + other.markDirty(); + this.markDirty(); + other.sendData(); + sendData(); } } @@ -49,9 +62,17 @@ public class FluidTankTileEntity extends SmartTileEntity { 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); + this.getTank().fill(other.getTank().drain(calculateDrainAmount(other, delta), FluidAction.EXECUTE), FluidAction.EXECUTE); + other.markDirty(); + this.markDirty(); + other.sendData(); + sendData(); } else if (delta < 0) { - other.getTank().fill(this.getTank().drain(-delta / 2, FluidAction.EXECUTE), FluidAction.EXECUTE); + other.getTank().fill(this.getTank().drain(calculateDrainAmount(other, delta), FluidAction.EXECUTE), FluidAction.EXECUTE); + other.markDirty(); + this.markDirty(); + other.sendData(); + sendData(); } } @@ -60,6 +81,10 @@ public class FluidTankTileEntity extends SmartTileEntity { 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); + other.markDirty(); + this.markDirty(); + other.sendData(); + sendData(); } } } @@ -105,4 +130,29 @@ public class FluidTankTileEntity extends SmartTileEntity { public IFluidTank getTank() { return fluid.orElseGet(this::createFluidHandler); } + + private void updatePriority() { + FluidTankTileEntity other = getOtherFluidTankTileEntity(Direction.DOWN); + priority = 1000; + if (other != null && other.getTank().getFluidAmount() < other.getTank().getCapacity()) { + priority = 0; + return; + } + + updatePriorityFrom(Direction.SOUTH); + updatePriorityFrom(Direction.NORTH); + updatePriorityFrom(Direction.WEST); + updatePriorityFrom(Direction.EAST); + } + + private void updatePriorityFrom(Direction direction) { + FluidTankTileEntity other = getOtherFluidTankTileEntity(direction); + if (other != null && other.getPriority() + 1 < priority) { + priority = other.getPriority() + 1; + } + } + + public int getPriority() { + return priority; + } }