fix data sync and tank render

This commit is contained in:
LordGrimmauld 2020-06-16 18:28:09 +02:00
parent aa9c227bee
commit b767df3fc3
3 changed files with 96 additions and 22 deletions

View file

@ -88,10 +88,10 @@ public class FluidTankBlock extends Block {
public AxisAlignedBB getTankBodyShape(IBlockReader world, BlockPos pos) { public AxisAlignedBB getTankBodyShape(IBlockReader world, BlockPos pos) {
return new AxisAlignedBB((isTankToDirection(world, pos, Direction.WEST) ? 0 : 2) / 16f, 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.NORTH) ? 0 : 2) / 16f,
(isTankToDirection(world, pos, Direction.EAST) ? 16 : 14) / 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); (isTankToDirection(world, pos, Direction.SOUTH) ? 16 : 14) / 16f);
} }

View file

@ -165,13 +165,11 @@ public class FluidTankRenderer extends SafeTileEntityRenderer<FluidTankTileEntit
} }
private AxisAlignedBB getRenderBounds(IFluidTank tank, AxisAlignedBB tankBounds) { private AxisAlignedBB getRenderBounds(IFluidTank tank, AxisAlignedBB tankBounds) {
float percent = (float) tank.getFluidAmount() / (float) tank.getCapacity(); double percent = (double) tank.getFluidAmount() / (double) tank.getCapacity();
double tankHeight = tankBounds.maxY - tankBounds.minY;
double y1 = tankBounds.minY; double y1 = tankBounds.minY;
double y2 = tank.getFluidAmount() < tank.getCapacity() ? (3 / 16f + (10 / 16f * percent)) : tankBounds.maxY; double y2 = tank.getFluidAmount() < tank.getCapacity() ? (4 + 8 * percent) / 16f : 1f;
if (tank.getFluid().getFluid().getAttributes().isLighterThanAir()) { if (tank.getFluid().getFluid().getAttributes().isLighterThanAir()) {
double yOff = tankBounds.maxY - y2; // lighter than air fluids move to the top of the tank double yOff = tankBounds.maxY - y2; // FIXME: lighter than air fluids move to the top of the tank, add behavior in TE
y1 += yOff; y1 += yOff;
y2 += yOff; y2 += yOff;
} }
@ -181,19 +179,37 @@ public class FluidTankRenderer extends SafeTileEntityRenderer<FluidTankTileEntit
private Collection<TankRenderInfo> getTanksToRender(FluidTankTileEntity te) { private Collection<TankRenderInfo> getTanksToRender(FluidTankTileEntity te) {
FluidTankTileEntity upTE = te.getOtherFluidTankTileEntity(Direction.UP); FluidTankTileEntity upTE = te.getOtherFluidTankTileEntity(Direction.UP);
FluidTankTileEntity downTE = te.getOtherFluidTankTileEntity(Direction.DOWN); FluidTankTileEntity downTE = te.getOtherFluidTankTileEntity(Direction.DOWN);
boolean up = upTE == null || upTE.getTank().getFluidAmount() == 0 || te.getTank().getFluid() != upTE.getTank().getFluid(); FluidTankTileEntity eastTE = te.getOtherFluidTankTileEntity(Direction.EAST);
boolean down = downTE == null || downTE.getTank().getFluidAmount() == downTE.getTank().getCapacity() || te.getTank().getFluid() != downTE.getTank().getFluid(); FluidTankTileEntity westTE = te.getOtherFluidTankTileEntity(Direction.WEST);
return Collections.singletonList(new FluidTankRenderInfo(te.getTank(), up, down, ((FluidTankBlock) te.getBlockState().getBlock()).getTankBodyShape(te.getWorld(), te.getPos()))); 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 static class FluidTankRenderInfo extends TankRenderInfo {
private final boolean up; private final boolean up;
private final boolean down; 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); super(tank, bounds);
this.up = up; this.up = up;
this.down = down; this.down = down;
this.east = east;
this.west = west;
this.north = north;
this.south = south;
} }
@Override @Override
@ -207,6 +223,14 @@ public class FluidTankRenderer extends SafeTileEntityRenderer<FluidTankTileEntit
return down return down
|| getTank().getFluid().getAmount() < getTank().getCapacity() || getTank().getFluid().getAmount() < getTank().getCapacity()
&& getTank().getFluid().getFluid().getAttributes().isLighterThanAir(); && getTank().getFluid().getFluid().getAttributes().isLighterThanAir();
case EAST:
return east;
case WEST:
return west;
case NORTH:
return north;
case SOUTH:
return south;
default: default:
return true; return true;
} }

View file

@ -1,13 +1,7 @@
package com.simibubi.create.content.contraptions.fluids; 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.SmartTileEntity;
import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.tileentity.TileEntityType;
@ -19,28 +13,47 @@ import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction; import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.fluids.capability.templates.FluidTank;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class FluidTankTileEntity extends SmartTileEntity { public class FluidTankTileEntity extends SmartTileEntity {
LazyOptional<FluidTank> fluid = LazyOptional.of(this::createFluidHandler); LazyOptional<FluidTank> fluid = LazyOptional.of(this::createFluidHandler);
private int priority = 1000;
public FluidTankTileEntity(TileEntityType<?> tileEntityTypeIn) { public FluidTankTileEntity(TileEntityType<?> tileEntityTypeIn) {
super(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 @Override
public void tick() { public void tick() {
super.tick(); super.tick();
updatePriority();
FluidTankTileEntity other; FluidTankTileEntity other;
other = getOtherFluidTankTileEntity(Direction.NORTH); other = getOtherFluidTankTileEntity(Direction.NORTH);
if (other != null && other.getTank().isFluidValid(this.getTank().getFluid())) { if (other != null && other.getTank().isFluidValid(this.getTank().getFluid())) {
int delta = other.getTank().getFluidAmount() - this.getTank().getFluidAmount(); int delta = other.getTank().getFluidAmount() - this.getTank().getFluidAmount();
if (delta > 0) { 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) { } 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())) { if (other != null && other.getTank().isFluidValid(this.getTank().getFluid())) {
int delta = other.getTank().getFluidAmount() - this.getTank().getFluidAmount(); int delta = other.getTank().getFluidAmount() - this.getTank().getFluidAmount();
if (delta > 0) { 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) { } 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(); int space = this.getTank().getCapacity() - this.getTank().getFluidAmount();
if (space > 0 && other.getTank().getFluidAmount() > 0) { if (space > 0 && other.getTank().getFluidAmount() > 0) {
this.getTank().fill(other.getTank().drain(space, FluidAction.EXECUTE), FluidAction.EXECUTE); 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() { public IFluidTank getTank() {
return fluid.orElseGet(this::createFluidHandler); 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;
}
} }