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

View file

@ -29,7 +29,6 @@ import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
@ -46,7 +45,6 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
SmartFluidTankBehaviour internalTank; SmartFluidTankBehaviour internalTank;
TransportedItemStack heldItem; TransportedItemStack heldItem;
protected int processingTicks; protected int processingTicks;
protected int lastRedstoneLevel;
Map<Direction, LazyOptional<ItemDrainItemHandler>> itemHandlers; Map<Direction, LazyOptional<ItemDrainItemHandler>> itemHandlers;
public ItemDrainTileEntity(TileEntityType<?> tileEntityTypeIn) { public ItemDrainTileEntity(TileEntityType<?> tileEntityTypeIn) {
@ -73,12 +71,12 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
if (!getHeldItemStack().isEmpty()) if (!getHeldItemStack().isEmpty())
return inserted; return inserted;
if (inserted.getCount() > 1 && EmptyingByBasin.canItemBeEmptied(world, inserted)) { if (inserted.getCount() > 1 && EmptyingByBasin.canItemBeEmptied(world, inserted)) {
returned = ItemHandlerHelper.copyStackWithSize(inserted, inserted.getCount() - 1); returned = ItemHandlerHelper.copyStackWithSize(inserted, inserted.getCount() - 1);
inserted = ItemHandlerHelper.copyStackWithSize(inserted, 1); inserted = ItemHandlerHelper.copyStackWithSize(inserted, 1);
} }
if (simulate) if (simulate)
return returned; return returned;
@ -102,12 +100,6 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
public void tick() { public void tick() {
super.tick(); super.tick();
if (lastRedstoneLevel != getComparatorOutput()) {
lastRedstoneLevel = getComparatorOutput();
if (world != null)
world.updateComparatorOutputLevel(getPos(), getBlockState().getBlock());
}
if (heldItem == null) { if (heldItem == null) {
processingTicks = 0; processingTicks = 0;
return; return;
@ -296,12 +288,6 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI
return super.getCapability(cap, side); 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 @Override
public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) { public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) {
ItemDrainTileEntity te = this; 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.AllShapes;
import com.simibubi.create.AllTileEntities; import com.simibubi.create.AllTileEntities;
import com.simibubi.create.content.contraptions.wrench.IWrenchable; import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.foundation.tileEntity.ComparatorUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
@ -42,12 +43,7 @@ public class SpoutBlock extends Block implements IWrenchable {
@Override @Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) { public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
TileEntity te = worldIn.getTileEntity(pos); return ComparatorUtil.levelOfSmartFluidTank(worldIn, pos);
if (te == null)
return 0;
if (te instanceof SpoutTileEntity)
return ((SpoutTileEntity) te).getComparatorOutput();
return 0;
} }
} }

View file

@ -8,8 +8,8 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.simibubi.create.AllFluids; 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.FluidFX;
import com.simibubi.create.content.contraptions.fluids.FluidFullnessOverlay;
import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler; import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler;
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation; import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack; 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.tileentity.TileEntityType;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
@ -50,7 +49,6 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
protected BeltProcessingBehaviour beltProcessing; protected BeltProcessingBehaviour beltProcessing;
protected int processingTicks; protected int processingTicks;
protected boolean sendSplash; protected boolean sendSplash;
protected int lastRedstoneLevel;
SmartFluidTankBehaviour tank; SmartFluidTankBehaviour tank;
@ -60,12 +58,12 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
} }
protected AxisAlignedBB cachedBoundingBox; protected AxisAlignedBB cachedBoundingBox;
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public AxisAlignedBB getRenderBoundingBox() { public AxisAlignedBB getRenderBoundingBox() {
if (cachedBoundingBox == null) { if (cachedBoundingBox == null)
cachedBoundingBox = super.getRenderBoundingBox().expand(0, -2, 0); cachedBoundingBox = super.getRenderBoundingBox().expand(0, -2, 0);
}
return cachedBoundingBox; return cachedBoundingBox;
} }
@ -126,9 +124,10 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
} }
AllTriggers.triggerForNearbyPlayers(AllTriggers.SPOUT, world, pos, 5); 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); AllTriggers.triggerForNearbyPlayers(AllTriggers.SPOUT_POTION, world, pos, 5);
tank.getPrimaryHandler() tank.getPrimaryHandler()
.setFluid(fluid); .setFluid(fluid);
sendSplash = true; sendSplash = true;
@ -178,12 +177,6 @@ public class SpoutTileEntity extends SmartTileEntity implements IHaveGoggleInfor
if (processingTicks >= 8 && world.isRemote) if (processingTicks >= 8 && world.isRemote)
spawnProcessingParticles(tank.getPrimaryTank() spawnProcessingParticles(tank.getPrimaryTank()
.getRenderedFluid()); .getRenderedFluid());
if (lastRedstoneLevel != getComparatorOutput()) {
lastRedstoneLevel = getComparatorOutput();
if (world != null)
world.updateComparatorOutputLevel(getPos(), getBlockState().getBlock());
}
} }
protected void spawnProcessingParticles(FluidStack fluid) { 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 @Override
public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) { public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) {
SpoutTileEntity te = this; 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.block.ITE;
import com.simibubi.create.foundation.fluid.FluidHelper; import com.simibubi.create.foundation.fluid.FluidHelper;
import com.simibubi.create.foundation.fluid.FluidHelper.FluidExchange; import com.simibubi.create.foundation.fluid.FluidHelper.FluidExchange;
import com.simibubi.create.foundation.tileEntity.ComparatorUtil;
import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.Lang;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -318,14 +319,9 @@ public class FluidTankBlock extends Block implements IWrenchable, ITE<FluidTankT
@Override @Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) { public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
TileEntity te = worldIn.getTileEntity(pos); return getTileEntityOptional(worldIn, pos).map(FluidTankTileEntity::getControllerTE)
if (te instanceof FluidTankTileEntity) { .map(te -> ComparatorUtil.fractionToRedstoneLevel(te.getFillState()))
FluidTankTileEntity fluidTankTileEntity = ((FluidTankTileEntity) te).getControllerTE(); .orElse(0);
if (fluidTankTileEntity == null)
return 0;
return fluidTankTileEntity.getComparatorOutput();
}
return 0;
} }
} }

View file

@ -29,7 +29,6 @@ import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
@ -58,7 +57,6 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
protected int luminosity; protected int luminosity;
protected int width; protected int width;
protected int height; protected int height;
protected int lastRedstoneLevel;
private static final int SYNC_RATE = 8; private static final int SYNC_RATE = 8;
protected int syncCooldown; protected int syncCooldown;
@ -100,24 +98,18 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
if (syncCooldown == 0 && queuedSync) if (syncCooldown == 0 && queuedSync)
sendData(); sendData();
} }
if (lastKnownPos == null) if (lastKnownPos == null)
lastKnownPos = getPos(); lastKnownPos = getPos();
else if (!lastKnownPos.equals(pos) && pos != null) { else if (!lastKnownPos.equals(pos) && pos != null) {
onPositionChanged(); onPositionChanged();
return; return;
} }
if (updateConnectivity) if (updateConnectivity)
updateConnectivity(); updateConnectivity();
if (fluidLevel != null) if (fluidLevel != null)
fluidLevel.tick(); fluidLevel.tick();
if (lastRedstoneLevel != getComparatorOutput()) {
lastRedstoneLevel = getComparatorOutput();
if (world != null)
world.updateComparatorOutputLevel(getPos(), getBlockState().getBlock());
}
} }
public boolean isController() { public boolean isController() {
@ -302,6 +294,7 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
} }
private AxisAlignedBB cachedBoundingBox; private AxisAlignedBB cachedBoundingBox;
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public AxisAlignedBB getRenderBoundingBox() { public AxisAlignedBB getRenderBoundingBox() {
@ -329,13 +322,6 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI
return null; 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 @Override
public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) { public boolean addToGoggleTooltip(List<String> tooltip, boolean isPlayerSneaking) {
FluidTankTileEntity controllerTE = getControllerTE(); 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() { protected void updateFluids() {
fluidUpdateCallback.run(); fluidUpdateCallback.run();
tileEntity.sendData(); tileEntity.sendData();
tileEntity.markDirty();
} }
@Override @Override