Search in sources :

Example 1 with InvManipulationBehaviour

use of com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour in project Create by Creators-of-Create.

the class BasinTileEntity method acceptOutputsInner.

private boolean acceptOutputsInner(List<ItemStack> outputItems, List<FluidStack> outputFluids, boolean simulate) {
    BlockState blockState = getBlockState();
    if (!(blockState.getBlock() instanceof BasinBlock))
        return false;
    Direction direction = blockState.getValue(BasinBlock.FACING);
    if (direction != Direction.DOWN) {
        BlockEntity te = level.getBlockEntity(worldPosition.below().relative(direction));
        InvManipulationBehaviour inserter = te == null ? null : TileEntityBehaviour.get(level, te.getBlockPos(), InvManipulationBehaviour.TYPE);
        IItemHandler targetInv = te == null ? null : te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(inserter == null ? null : inserter.getInventory());
        IFluidHandler targetTank = te == null ? null : te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null);
        boolean externalTankNotPresent = targetTank == null;
        if (!outputItems.isEmpty() && targetInv == null)
            return false;
        if (!outputFluids.isEmpty() && externalTankNotPresent) {
            // Special case - fluid outputs but output only accepts items
            targetTank = outputTank.getCapability().orElse(null);
            if (targetTank == null)
                return false;
            if (!acceptFluidOutputsIntoBasin(outputFluids, simulate, targetTank))
                return false;
        }
        if (simulate)
            return true;
        for (ItemStack itemStack : outputItems) {
            if (itemStack.hasContainerItem() && itemStack.getContainerItem().sameItem(itemStack))
                continue;
            spoutputBuffer.add(itemStack.copy());
        }
        if (!externalTankNotPresent)
            for (FluidStack fluidStack : outputFluids) spoutputFluidBuffer.add(fluidStack.copy());
        return true;
    }
    IItemHandler targetInv = outputInventory;
    IFluidHandler targetTank = outputTank.getCapability().orElse(null);
    if (targetInv == null && !outputItems.isEmpty())
        return false;
    if (!acceptItemOutputsIntoBasin(outputItems, simulate, targetInv))
        return false;
    if (outputFluids.isEmpty())
        return true;
    if (targetTank == null)
        return false;
    if (!acceptFluidOutputsIntoBasin(outputFluids, simulate, targetTank))
        return false;
    return true;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) IItemHandler(net.minecraftforge.items.IItemHandler) FluidStack(net.minecraftforge.fluids.FluidStack) InvManipulationBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 2 with InvManipulationBehaviour

use of com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour in project Create by Creators-of-Create.

the class BasinTileEntity method tryClearingSpoutputOverflow.

private void tryClearingSpoutputOverflow() {
    BlockState blockState = getBlockState();
    if (!(blockState.getBlock() instanceof BasinBlock))
        return;
    Direction direction = blockState.getValue(BasinBlock.FACING);
    BlockEntity te = level.getBlockEntity(worldPosition.below().relative(direction));
    FilteringBehaviour filter = null;
    InvManipulationBehaviour inserter = null;
    if (te != null) {
        filter = TileEntityBehaviour.get(level, te.getBlockPos(), FilteringBehaviour.TYPE);
        inserter = TileEntityBehaviour.get(level, te.getBlockPos(), InvManipulationBehaviour.TYPE);
    }
    IItemHandler targetInv = te == null ? null : te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(inserter == null ? null : inserter.getInventory());
    IFluidHandler targetTank = te == null ? null : te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null);
    boolean update = false;
    for (Iterator<ItemStack> iterator = spoutputBuffer.iterator(); iterator.hasNext(); ) {
        ItemStack itemStack = iterator.next();
        if (direction == Direction.DOWN) {
            Block.popResource(level, worldPosition, itemStack);
            iterator.remove();
            update = true;
            continue;
        }
        if (targetInv == null)
            break;
        if (!ItemHandlerHelper.insertItemStacked(targetInv, itemStack, true).isEmpty())
            continue;
        if (filter != null && !filter.test(itemStack))
            continue;
        update = true;
        ItemHandlerHelper.insertItemStacked(targetInv, itemStack.copy(), false);
        iterator.remove();
        visualizedOutputItems.add(IntAttached.withZero(itemStack));
    }
    for (Iterator<FluidStack> iterator = spoutputFluidBuffer.iterator(); iterator.hasNext(); ) {
        FluidStack fluidStack = iterator.next();
        if (direction == Direction.DOWN) {
            iterator.remove();
            update = true;
            continue;
        }
        if (targetTank == null)
            break;
        for (boolean simulate : Iterate.trueAndFalse) {
            FluidAction action = simulate ? FluidAction.SIMULATE : FluidAction.EXECUTE;
            int fill = targetTank instanceof SmartFluidTankBehaviour.InternalFluidHandler ? ((SmartFluidTankBehaviour.InternalFluidHandler) targetTank).forceFill(fluidStack.copy(), action) : targetTank.fill(fluidStack.copy(), action);
            if (fill != fluidStack.getAmount())
                break;
            if (simulate)
                continue;
            update = true;
            iterator.remove();
            visualizedOutputFluids.add(IntAttached.withZero(fluidStack));
        }
    }
    if (update) {
        notifyChangeOfContents();
        sendData();
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) SmartFluidTankBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.fluid.SmartFluidTankBehaviour) FluidStack(net.minecraftforge.fluids.FluidStack) InvManipulationBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour) FluidAction(net.minecraftforge.fluids.capability.IFluidHandler.FluidAction) Direction(net.minecraft.core.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) BlockState(net.minecraft.world.level.block.state.BlockState) FilteringBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour) ItemStack(net.minecraft.world.item.ItemStack) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 3 with InvManipulationBehaviour

use of com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method blockChanged.

public void blockChanged() {
    removeBehaviour(InvManipulationBehaviour.TYPE);
    inserting = new InvManipulationBehaviour(this, this::getTargetFace);
    attachBehaviourLate(inserting);
}
Also used : InvManipulationBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour)

Example 4 with InvManipulationBehaviour

use of com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour in project Create by Creators-of-Create.

the class MechanicalCrafterTileEntity method addBehaviours.

@Override
public void addBehaviours(List<TileEntityBehaviour> behaviours) {
    super.addBehaviours(behaviours);
    inserting = new InvManipulationBehaviour(this, this::getTargetFace);
    connectivity = new EdgeInteractionBehaviour(this, ConnectedInputHandler::toggleConnection).connectivity(ConnectedInputHandler::shouldConnect).require(AllItems.WRENCH.get());
    behaviours.add(inserting);
    behaviours.add(connectivity);
}
Also used : EdgeInteractionBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.edgeInteraction.EdgeInteractionBehaviour) InvManipulationBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour)

Example 5 with InvManipulationBehaviour

use of com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour in project Create by Creators-of-Create.

the class BeltFunnelInteractionHandler method checkForFunnels.

public static boolean checkForFunnels(BeltInventory beltInventory, TransportedItemStack currentItem, float nextOffset) {
    boolean beltMovementPositive = beltInventory.beltMovementPositive;
    int firstUpcomingSegment = (int) Math.floor(currentItem.beltPosition);
    int step = beltMovementPositive ? 1 : -1;
    firstUpcomingSegment = Mth.clamp(firstUpcomingSegment, 0, beltInventory.belt.beltLength - 1);
    for (int segment = firstUpcomingSegment; beltMovementPositive ? segment <= nextOffset : segment + 1 >= nextOffset; segment += step) {
        BlockPos funnelPos = BeltHelper.getPositionForOffset(beltInventory.belt, segment).above();
        Level world = beltInventory.belt.getLevel();
        BlockState funnelState = world.getBlockState(funnelPos);
        if (!(funnelState.getBlock() instanceof BeltFunnelBlock))
            continue;
        Direction funnelFacing = funnelState.getValue(BeltFunnelBlock.HORIZONTAL_FACING);
        Direction movementFacing = beltInventory.belt.getMovementFacing();
        boolean blocking = funnelFacing == movementFacing.getOpposite();
        if (funnelFacing == movementFacing)
            continue;
        if (funnelState.getValue(BeltFunnelBlock.SHAPE) == Shape.PUSHING)
            continue;
        float funnelEntry = segment + .5f;
        if (funnelState.getValue(BeltFunnelBlock.SHAPE) == Shape.EXTENDED)
            funnelEntry += .499f * (beltMovementPositive ? -1 : 1);
        boolean hasCrossed = nextOffset > funnelEntry && beltMovementPositive || nextOffset < funnelEntry && !beltMovementPositive;
        if (!hasCrossed)
            return false;
        if (blocking)
            currentItem.beltPosition = funnelEntry;
        if (world.isClientSide || funnelState.getOptionalValue(BeltFunnelBlock.POWERED).orElse(false))
            if (blocking)
                return true;
            else
                continue;
        BlockEntity te = world.getBlockEntity(funnelPos);
        if (!(te instanceof FunnelTileEntity))
            return true;
        FunnelTileEntity funnelTE = (FunnelTileEntity) te;
        InvManipulationBehaviour inserting = funnelTE.getBehaviour(InvManipulationBehaviour.TYPE);
        FilteringBehaviour filtering = funnelTE.getBehaviour(FilteringBehaviour.TYPE);
        if (inserting == null || filtering != null && !filtering.test(currentItem.stack))
            if (blocking)
                return true;
            else
                continue;
        int amountToExtract = funnelTE.getAmountToExtract();
        ItemStack toInsert = currentItem.stack.copy();
        if (amountToExtract > toInsert.getCount())
            if (blocking)
                return true;
            else
                continue;
        if (amountToExtract != -1) {
            toInsert.setCount(amountToExtract);
            ItemStack remainder = inserting.simulate().insert(toInsert);
            if (!remainder.isEmpty())
                if (blocking)
                    return true;
                else
                    continue;
        }
        ItemStack remainder = inserting.insert(toInsert);
        if (toInsert.equals(remainder, false))
            if (blocking)
                return true;
            else
                continue;
        int notFilled = currentItem.stack.getCount() - toInsert.getCount();
        if (!remainder.isEmpty()) {
            remainder.grow(notFilled);
        } else if (notFilled > 0)
            remainder = ItemHandlerHelper.copyStackWithSize(currentItem.stack, notFilled);
        funnelTE.flap(true);
        funnelTE.onTransfer(toInsert);
        currentItem.stack = remainder;
        beltInventory.belt.sendData();
        if (blocking)
            return true;
    }
    return false;
}
Also used : BeltFunnelBlock(com.simibubi.create.content.logistics.block.funnel.BeltFunnelBlock) InvManipulationBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour) Direction(net.minecraft.core.Direction) FunnelTileEntity(com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity) BlockState(net.minecraft.world.level.block.state.BlockState) FilteringBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

InvManipulationBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour)9 FilteringBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour)6 ItemStack (net.minecraft.world.item.ItemStack)6 BlockState (net.minecraft.world.level.block.state.BlockState)5 Direction (net.minecraft.core.Direction)4 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)4 BlockPos (net.minecraft.core.BlockPos)3 Vec3 (net.minecraft.world.phys.Vec3)3 FluidStack (net.minecraftforge.fluids.FluidStack)3 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)3 SmartTileEntity (com.simibubi.create.foundation.tileEntity.SmartTileEntity)2 TileEntityBehaviour (com.simibubi.create.foundation.tileEntity.TileEntityBehaviour)2 InterfaceProvider (com.simibubi.create.foundation.tileEntity.behaviour.inventory.CapManipulationBehaviourBase.InterfaceProvider)2 TankManipulationBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.inventory.TankManipulationBehaviour)2 List (java.util.List)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 BlockEntityType (net.minecraft.world.level.block.entity.BlockEntityType)2 IItemHandler (net.minecraftforge.items.IItemHandler)2 InstancedRenderDispatcher (com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher)1 AllBlocks (com.simibubi.create.AllBlocks)1