Refactor comparator overrides

- Removed unnecessary level tracking
- Remove code duplicates
- Fixed smart fluid tank behaviours not triggering markDirty on deferred fluid update
This commit is contained in:
simibubi 2021-03-03 17:59:27 +01:00
parent 5567e57265
commit 31cb704894
8 changed files with 46 additions and 72 deletions

View file

@ -6,6 +6,7 @@ import com.simibubi.create.content.contraptions.processing.EmptyingByBasin;
import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.fluid.FluidHelper;
import com.simibubi.create.foundation.tileEntity.ComparatorUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
@ -106,12 +107,7 @@ public class ItemDrainBlock extends Block implements IWrenchable, ITE<ItemDrainT
@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
TileEntity te = worldIn.getTileEntity(pos);
if (te == null)
return 0;
if (te instanceof ItemDrainTileEntity)
return ((ItemDrainTileEntity) te).getComparatorOutput();
return 0;
return ComparatorUtil.levelOfSmartFluidTank(worldIn, pos);
}
}

View file

@ -29,7 +29,6 @@ import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
@ -46,7 +45,6 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
SmartFluidTankBehaviour internalTank;
TransportedItemStack heldItem;
protected int processingTicks;
protected int lastRedstoneLevel;
Map<Direction, LazyOptional<ItemDrainItemHandler>> itemHandlers;
public ItemDrainTileEntity(TileEntityType<?> tileEntityTypeIn) {
@ -102,12 +100,6 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
public void tick() {
super.tick();
if (lastRedstoneLevel != getComparatorOutput()) {
lastRedstoneLevel = getComparatorOutput();
if (world != null)
world.updateComparatorOutputLevel(getPos(), getBlockState().getBlock());
}
if (heldItem == null) {
processingTicks = 0;
return;
@ -296,12 +288,6 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
return super.getCapability(cap, side);
}
public int getComparatorOutput() {
ItemDrainTileEntity te = this;
double fillFraction = (double) te.internalTank.getPrimaryHandler().getFluidAmount() / te.internalTank.getPrimaryHandler().getCapacity();
return MathHelper.floor(MathHelper.clamp(fillFraction * 14 + (fillFraction > 0 ? 1 : 0), 0, 15));
}
@Override
public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) {
ItemDrainTileEntity te = this;

View file

@ -3,6 +3,7 @@ package com.simibubi.create.content.contraptions.fluids.actors;
import com.simibubi.create.AllShapes;
import com.simibubi.create.AllTileEntities;
import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.foundation.tileEntity.ComparatorUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
@ -42,12 +43,7 @@ public class SpoutBlock extends Block implements IWrenchable {
@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
TileEntity te = worldIn.getTileEntity(pos);
if (te == null)
return 0;
if (te instanceof SpoutTileEntity)
return ((SpoutTileEntity) te).getComparatorOutput();
return 0;
return ComparatorUtil.levelOfSmartFluidTank(worldIn, pos);
}
}

View file

@ -8,8 +8,8 @@ import java.util.List;
import java.util.stream.Collectors;
import com.simibubi.create.AllFluids;
import com.simibubi.create.content.contraptions.fluids.FluidFullnessOverlay;
import com.simibubi.create.content.contraptions.fluids.FluidFX;
import com.simibubi.create.content.contraptions.fluids.FluidFullnessOverlay;
import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler;
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack;
@ -32,7 +32,6 @@ import net.minecraft.potion.PotionUtils;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
@ -50,7 +49,6 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
protected BeltProcessingBehaviour beltProcessing;
protected int processingTicks;
protected boolean sendSplash;
protected int lastRedstoneLevel;
SmartFluidTankBehaviour tank;
@ -60,12 +58,12 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
}
protected AxisAlignedBB cachedBoundingBox;
@Override
@OnlyIn(Dist.CLIENT)
public AxisAlignedBB getRenderBoundingBox() {
if (cachedBoundingBox == null) {
if (cachedBoundingBox == null)
cachedBoundingBox = super.getRenderBoundingBox().expand(0, -2, 0);
}
return cachedBoundingBox;
}
@ -126,7 +124,8 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
}
AllTriggers.triggerForNearbyPlayers(AllTriggers.SPOUT, world, pos, 5);
if (out.getItem() instanceof PotionItem && !PotionUtils.getEffectsFromStack(out).isEmpty())
if (out.getItem() instanceof PotionItem && !PotionUtils.getEffectsFromStack(out)
.isEmpty())
AllTriggers.triggerForNearbyPlayers(AllTriggers.SPOUT_POTION, world, pos, 5);
tank.getPrimaryHandler()
@ -178,12 +177,6 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
if (processingTicks >= 8 && world.isRemote)
spawnProcessingParticles(tank.getPrimaryTank()
.getRenderedFluid());
if (lastRedstoneLevel != getComparatorOutput()) {
lastRedstoneLevel = getComparatorOutput();
if (world != null)
world.updateComparatorOutputLevel(getPos(), getBlockState().getBlock());
}
}
protected void spawnProcessingParticles(FluidStack fluid) {
@ -206,12 +199,6 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
}
}
public int getComparatorOutput() {
SpoutTileEntity te = this;
double fillFraction = (double) te.getCurrentFluidInTank().getAmount() / te.tank.getPrimaryHandler().getCapacity();
return MathHelper.floor(MathHelper.clamp(fillFraction * 14 + (fillFraction > 0 ? 1 : 0), 0, 15));
}
@Override
public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) {
SpoutTileEntity te = this;

View file

@ -8,6 +8,7 @@ import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.fluid.FluidHelper;
import com.simibubi.create.foundation.fluid.FluidHelper.FluidExchange;
import com.simibubi.create.foundation.tileEntity.ComparatorUtil;
import com.simibubi.create.foundation.utility.Lang;
import net.minecraft.block.Block;
@ -318,14 +319,9 @@ public class FluidTankBlock extends Block implements IWrenchable, ITE<FluidTankT
@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
TileEntity te = worldIn.getTileEntity(pos);
if (te instanceof FluidTankTileEntity) {
FluidTankTileEntity fluidTankTileEntity = ((FluidTankTileEntity) te).getControllerTE();
if (fluidTankTileEntity == null)
return 0;
return fluidTankTileEntity.getComparatorOutput();
}
return 0;
return getTileEntityOptional(worldIn, pos).map(FluidTankTileEntity::getControllerTE)
.map(te -> ComparatorUtil.fractionToRedstoneLevel(te.getFillState()))
.orElse(0);
}
}

View file

@ -29,7 +29,6 @@ import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist;
@ -58,7 +57,6 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
protected int luminosity;
protected int width;
protected int height;
protected int lastRedstoneLevel;
private static final int SYNC_RATE = 8;
protected int syncCooldown;
@ -112,12 +110,6 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
updateConnectivity();
if (fluidLevel != null)
fluidLevel.tick();
if (lastRedstoneLevel != getComparatorOutput()) {
lastRedstoneLevel = getComparatorOutput();
if (world != null)
world.updateComparatorOutputLevel(getPos(), getBlockState().getBlock());
}
}
public boolean isController() {
@ -302,6 +294,7 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
}
private AxisAlignedBB cachedBoundingBox;
@Override
@OnlyIn(Dist.CLIENT)
public AxisAlignedBB getRenderBoundingBox() {
@ -329,13 +322,6 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
return null;
}
public int getComparatorOutput() {
FluidTankTileEntity te = getControllerTE();
if (te == null)
return 0;
return MathHelper.floor(MathHelper.clamp(te.getFillState() * 14 + (te.getFillState() > 0 ? 1 : 0), 0, 15));
}
@Override
public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) {
FluidTankTileEntity controllerTE = getControllerTE();

View file

@ -0,0 +1,26 @@
package com.simibubi.create.foundation.tileEntity;
import com.simibubi.create.foundation.fluid.SmartFluidTank;
import com.simibubi.create.foundation.tileEntity.behaviour.fluid.SmartFluidTankBehaviour;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.IBlockReader;
public class ComparatorUtil {
public static int fractionToRedstoneLevel(double frac) {
return MathHelper.floor(MathHelper.clamp(frac * 14 + (frac > 0 ? 1 : 0), 0, 15));
}
public static int levelOfSmartFluidTank(IBlockReader world, BlockPos pos) {
SmartFluidTankBehaviour fluidBehaviour = TileEntityBehaviour.get(world, pos, SmartFluidTankBehaviour.TYPE);
if (fluidBehaviour == null)
return 0;
SmartFluidTank primaryHandler = fluidBehaviour.getPrimaryHandler();
double fillFraction = (double) primaryHandler.getFluid()
.getAmount() / primaryHandler.getCapacity();
return fractionToRedstoneLevel(fillFraction);
}
}

View file

@ -132,6 +132,7 @@ public class SmartFluidTankBehaviour extends TileEntityBehaviour {
protected void updateFluids() {
fluidUpdateCallback.run();
tileEntity.sendData();
tileEntity.markDirty();
}
@Override