From d75793d036909a8da2ffc4f9a71bd620aabf2fbf Mon Sep 17 00:00:00 2001 From: Snownee Date: Thu, 18 Mar 2021 18:43:25 +0800 Subject: [PATCH] Add extra item capability checks Many mods does not invalidate their capabilities, so fix it on our end --- .../crafter/MechanicalCrafterBlock.java | 10 ++++++ .../belts/tunnel/BeltTunnelTileEntity.java | 1 - .../block/chute/AbstractChuteBlock.java | 6 ++-- .../block/chute/ChuteTileEntity.java | 1 + .../block/funnel/AbstractFunnelBlock.java | 3 ++ .../block/funnel/FunnelTileEntity.java | 3 -- .../inventories/AdjustableCrateBlock.java | 2 +- .../block/redstone/ContentObserverBlock.java | 9 ++++++ .../redstone/StockpileSwitchTileEntity.java | 1 + .../schematics/block/SchematicannonBlock.java | 6 ++++ .../block/SchematicannonTileEntity.java | 31 ++++++++++++------- .../tileEntity/TileEntityBehaviour.java | 3 +- .../inventory/InvManipulationBehaviour.java | 9 +++++- 13 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java index a8de2ec50..7f4c9d899 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java @@ -8,6 +8,8 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.components.crafter.ConnectedInputHandler.ConnectedInput; import com.simibubi.create.content.contraptions.components.crafter.MechanicalCrafterTileEntity.Phase; import com.simibubi.create.foundation.block.ITE; +import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; +import com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour; import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Pointing; @@ -234,6 +236,14 @@ public class MechanicalCrafterBlock extends HorizontalKineticBlock implements IT return ActionResultType.PASS; } + @Override + public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, + boolean isMoving) { + InvManipulationBehaviour behaviour = TileEntityBehaviour.get(worldIn, pos, InvManipulationBehaviour.TYPE); + if (behaviour != null) + behaviour.onNeighborChanged(fromPos); + } + @Override public float getParticleTargetRadius() { return .85f; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelTileEntity.java index 943b9eb69..c1a58610f 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelTileEntity.java @@ -2,7 +2,6 @@ package com.simibubi.create.content.logistics.block.belts.tunnel; import java.util.*; -import com.simibubi.create.CreateClient; import com.simibubi.create.content.logistics.packet.TunnelFlapPacket; import com.simibubi.create.foundation.gui.widgets.InterpolatedChasingValue; import com.simibubi.create.foundation.networking.AllPackets; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/chute/AbstractChuteBlock.java b/src/main/java/com/simibubi/create/content/logistics/block/chute/AbstractChuteBlock.java index 513a0c454..33c5c818b 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/chute/AbstractChuteBlock.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/chute/AbstractChuteBlock.java @@ -29,6 +29,7 @@ import net.minecraft.world.IWorld; import net.minecraft.world.World; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.common.util.LazyOptional; public abstract class AbstractChuteBlock extends Block implements IWrenchable, ITE { @@ -156,9 +157,10 @@ public abstract class AbstractChuteBlock extends Block implements IWrenchable, I @Override public void neighborChanged(BlockState p_220069_1_, World world, BlockPos pos, Block p_220069_4_, BlockPos neighbourPos, boolean p_220069_6_) { - if (pos.down() - .equals(neighbourPos)) + if (pos.down().equals(neighbourPos)) withTileEntityDo(world, pos, ChuteTileEntity::blockBelowChanged); + else if (pos.up().equals(neighbourPos)) + withTileEntityDo(world, pos, chute -> chute.capAbove = LazyOptional.empty()); } public abstract BlockState updateChuteState(BlockState state, BlockState above, IBlockReader world, BlockPos pos); diff --git a/src/main/java/com/simibubi/create/content/logistics/block/chute/ChuteTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/chute/ChuteTileEntity.java index fac939e92..605e2e19e 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/chute/ChuteTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/chute/ChuteTileEntity.java @@ -272,6 +272,7 @@ public class ChuteTileEntity extends SmartTileEntity implements IHaveGoggleInfor public void blockBelowChanged() { updateAirFlow = true; + capBelow = LazyOptional.empty(); } private void spawnParticles(float itemMotion) { diff --git a/src/main/java/com/simibubi/create/content/logistics/block/funnel/AbstractFunnelBlock.java b/src/main/java/com/simibubi/create/content/logistics/block/funnel/AbstractFunnelBlock.java index 1b7ec9b39..68c43bc22 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/funnel/AbstractFunnelBlock.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/funnel/AbstractFunnelBlock.java @@ -59,6 +59,9 @@ public abstract class AbstractFunnelBlock extends Block implements ITE te.neighbourCheckCooldown = 0); + } + } diff --git a/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java b/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java index 77b8d69f4..e3317d9aa 100644 --- a/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java +++ b/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java @@ -1,5 +1,6 @@ package com.simibubi.create.content.schematics.block; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -9,7 +10,6 @@ import com.simibubi.create.AllBlocks; import com.simibubi.create.AllItems; import com.simibubi.create.AllSoundEvents; import com.simibubi.create.AllTags.AllBlockTags; -import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.relays.belt.BeltBlock; import com.simibubi.create.content.contraptions.relays.belt.BeltPart; import com.simibubi.create.content.contraptions.relays.belt.BeltSlope; @@ -65,10 +65,10 @@ import net.minecraft.world.gen.feature.template.Template; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; +import net.minecraftforge.items.wrapper.EmptyHandler; public class SchematicannonTileEntity extends SmartTileEntity implements INamedContainerProvider, IInstanceRendered { @@ -102,7 +102,7 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC public BlockPos target; public BlockPos previousTarget; - public List attachedInventories; + public LinkedHashSet> attachedInventories; public List flyingBlocks; public MaterialChecklist checklist; @@ -137,14 +137,13 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC public SchematicannonTileEntity(TileEntityType tileEntityTypeIn) { super(tileEntityTypeIn); setLazyTickRate(30); - attachedInventories = new LinkedList<>(); + attachedInventories = new LinkedHashSet<>(); flyingBlocks = new LinkedList<>(); inventory = new SchematicannonInventory(this); statusMsg = "idle"; state = State.STOPPED; printingEntityIndex = -1; replaceMode = 2; - neighbourCheckCooldown = NEIGHBOUR_CHECKING; checklist = new MaterialChecklist(); } @@ -164,7 +163,7 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC LazyOptional capability = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite()); if (capability.isPresent()) { - attachedInventories.add(capability.orElse(null)); + attachedInventories.add(capability); } } } @@ -288,7 +287,7 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC public void tick() { super.tick(); - if (neighbourCheckCooldown-- <= 0) { + if (state != State.STOPPED && neighbourCheckCooldown-- <= 0) { neighbourCheckCooldown = NEIGHBOUR_CHECKING; findInventories(); } @@ -577,9 +576,12 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC if (hasCreativeCrate) return true; + attachedInventories.removeIf(cap -> !cap.isPresent()); + // Find and apply damage if (usage == ItemUseType.DAMAGE) { - for (IItemHandler iItemHandler : attachedInventories) { + for (LazyOptional cap : attachedInventories) { + IItemHandler iItemHandler = cap.orElse(EmptyHandler.INSTANCE); for (int slot = 0; slot < iItemHandler.getSlots(); slot++) { ItemStack extractItem = iItemHandler.extractItem(slot, 1, true); if (!ItemRequirement.validate(required, extractItem)) @@ -608,8 +610,8 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC boolean success = false; if (usage == ItemUseType.CONSUME) { int amountFound = 0; - for (IItemHandler iItemHandler : attachedInventories) { - + for (LazyOptional cap : attachedInventories) { + IItemHandler iItemHandler = cap.orElse(EmptyHandler.INSTANCE); amountFound += ItemHelper .extract(iItemHandler, s -> ItemRequirement.validate(required, s), ExtractionCountMode.UPTO, required.getCount(), true) @@ -625,7 +627,8 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC if (!simulate && success) { int amountFound = 0; - for (IItemHandler iItemHandler : attachedInventories) { + for (LazyOptional cap : attachedInventories) { + IItemHandler iItemHandler = cap.orElse(EmptyHandler.INSTANCE); amountFound += ItemHelper .extract(iItemHandler, s -> ItemRequirement.validate(required, s), ExtractionCountMode.UPTO, required.getCount(), false) @@ -916,7 +919,11 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC } } checklist.gathered.clear(); - for (IItemHandler inventory : attachedInventories) { + findInventories(); + for (LazyOptional cap : attachedInventories) { + if (!cap.isPresent()) + continue; + IItemHandler inventory = cap.orElse(EmptyHandler.INSTANCE); for (int slot = 0; slot < inventory.getSlots(); slot++) { ItemStack stackInSlot = inventory.getStackInSlot(slot); if (inventory.extractItem(slot, 1, true) diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java index af79b9005..859346c4a 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java @@ -5,7 +5,6 @@ import com.simibubi.create.foundation.tileEntity.behaviour.BehaviourType; import net.minecraft.block.BlockState; import net.minecraft.nbt.CompoundNBT; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockReader; import net.minecraft.world.World; @@ -47,7 +46,7 @@ public abstract class TileEntityBehaviour { } - public void onNeighborChanged(Direction direction) { + public void onNeighborChanged(BlockPos neighborPos) { } diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java index 04e57538d..2741bbaa1 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/inventory/InvManipulationBehaviour.java @@ -138,6 +138,13 @@ public class InvManipulationBehaviour extends TileEntityBehaviour { findNewNextTick = true; } + @Override + public void onNeighborChanged(BlockPos neighborPos) { + BlockFace targetBlockFace = target.getTarget(getWorld(), tileEntity.getPos(), tileEntity.getBlockState()); + if (targetBlockFace.getConnectedPos().equals(neighborPos)) + onHandlerInvalidated(targetCapability); + } + protected void onHandlerInvalidated(LazyOptional handler) { findNewNextTick = true; targetCapability = LazyOptional.empty(); @@ -167,7 +174,7 @@ public class InvManipulationBehaviour extends TileEntityBehaviour { return amount; } - protected void findNewCapability() { + public void findNewCapability() { BlockFace targetBlockFace = target.getTarget(getWorld(), tileEntity.getPos(), tileEntity.getBlockState()) .getOpposite(); BlockPos pos = targetBlockFace.getPos();