From 31cb70489489051f508a67ca4f501924e226544d Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:59:27 +0100 Subject: [PATCH] Refactor comparator overrides - Removed unnecessary level tracking - Remove code duplicates - Fixed smart fluid tank behaviours not triggering markDirty on deferred fluid update --- .../fluids/actors/ItemDrainBlock.java | 8 ++---- .../fluids/actors/ItemDrainTileEntity.java | 18 ++----------- .../fluids/actors/SpoutBlock.java | 8 ++---- .../fluids/actors/SpoutTileEntity.java | 25 +++++------------- .../fluids/tank/FluidTankBlock.java | 12 +++------ .../fluids/tank/FluidTankTileEntity.java | 20 +++----------- .../foundation/tileEntity/ComparatorUtil.java | 26 +++++++++++++++++++ .../fluid/SmartFluidTankBehaviour.java | 1 + 8 files changed, 46 insertions(+), 72 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/tileEntity/ComparatorUtil.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/ItemDrainBlock.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/ItemDrainBlock.java index 2d12c3fc9..389d49fca 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/ItemDrainBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/ItemDrainBlock.java @@ -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> itemHandlers; public ItemDrainTileEntity(TileEntityType tileEntityTypeIn) { @@ -73,12 +71,12 @@ public class ItemDrainTileEntity extends SmartTileEntity implements IHaveGoggleI if (!getHeldItemStack().isEmpty()) return inserted; - + if (inserted.getCount() > 1 && EmptyingByBasin.canItemBeEmptied(world, inserted)) { returned = ItemHandlerHelper.copyStackWithSize(inserted, inserted.getCount() - 1); inserted = ItemHandlerHelper.copyStackWithSize(inserted, 1); } - + if (simulate) return returned; @@ -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 tooltip, boolean isPlayerSneaking) { ItemDrainTileEntity te = this; diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutBlock.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutBlock.java index bb97ddac8..f53e8331b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutBlock.java @@ -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); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutTileEntity.java index e7ede91af..45a900ec2 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/actors/SpoutTileEntity.java @@ -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,9 +124,10 @@ 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() .setFluid(fluid); sendSplash = true; @@ -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 tooltip, boolean isPlayerSneaking) { SpoutTileEntity te = this; diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankBlock.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankBlock.java index 959272e27..3ae0f9a4e 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankBlock.java @@ -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 ComparatorUtil.fractionToRedstoneLevel(te.getFillState())) + .orElse(0); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankTileEntity.java index cb89cad19..4631f7a23 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/tank/FluidTankTileEntity.java @@ -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; @@ -100,24 +98,18 @@ public class FluidTankTileEntity extends SmartTileEntity implements IHaveGoggleI if (syncCooldown == 0 && queuedSync) sendData(); } - + if (lastKnownPos == null) lastKnownPos = getPos(); else if (!lastKnownPos.equals(pos) && pos != null) { onPositionChanged(); return; } - + if (updateConnectivity) 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 tooltip, boolean isPlayerSneaking) { FluidTankTileEntity controllerTE = getControllerTE(); diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/ComparatorUtil.java b/src/main/java/com/simibubi/create/foundation/tileEntity/ComparatorUtil.java new file mode 100644 index 000000000..e35593b63 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/ComparatorUtil.java @@ -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); + } + +} diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/fluid/SmartFluidTankBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/fluid/SmartFluidTankBehaviour.java index c69c1aeb2..cabc60556 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/fluid/SmartFluidTankBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/fluid/SmartFluidTankBehaviour.java @@ -132,6 +132,7 @@ public class SmartFluidTankBehaviour extends TileEntityBehaviour { protected void updateFluids() { fluidUpdateCallback.run(); tileEntity.sendData(); + tileEntity.markDirty(); } @Override