Search in sources :

Example 1 with FunnelTileEntity

use of com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity in project Create by Creators-of-Create.

the class ContentObserverBlock method getStateForPlacement.

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
    BlockState state = defaultBlockState();
    Capability<IItemHandler> itemCap = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
    Capability<IFluidHandler> fluidCap = CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
    Direction preferredFacing = null;
    for (Direction face : Iterate.horizontalDirections) {
        BlockPos offsetPos = context.getClickedPos().relative(face);
        Level world = context.getLevel();
        boolean canDetect = false;
        BlockEntity tileEntity = world.getBlockEntity(offsetPos);
        if (TileEntityBehaviour.get(tileEntity, TransportedItemStackHandlerBehaviour.TYPE) != null)
            canDetect = true;
        else if (TileEntityBehaviour.get(tileEntity, FluidTransportBehaviour.TYPE) != null)
            canDetect = true;
        else if (tileEntity != null && (tileEntity.getCapability(itemCap).isPresent() || tileEntity.getCapability(fluidCap).isPresent()))
            canDetect = true;
        else if (tileEntity instanceof FunnelTileEntity)
            canDetect = true;
        if (canDetect) {
            if (preferredFacing != null) {
                preferredFacing = null;
                break;
            }
            preferredFacing = face;
        }
    }
    if (preferredFacing != null)
        return state.setValue(FACING, preferredFacing);
    return state.setValue(FACING, context.getHorizontalDirection().getOpposite());
}
Also used : FunnelTileEntity(com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity) BlockState(net.minecraft.world.level.block.state.BlockState) IItemHandler(net.minecraftforge.items.IItemHandler) BlockPos(net.minecraft.core.BlockPos) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) Direction(net.minecraft.core.Direction) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 2 with FunnelTileEntity

use of com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity in project Create by Creators-of-Create.

the class DirectBeltInputBehaviour method tryExportingToBeltFunnel.

@Nullable
public ItemStack tryExportingToBeltFunnel(ItemStack stack, @Nullable Direction side, boolean simulate) {
    BlockPos funnelPos = tileEntity.getBlockPos().above();
    Level world = getWorld();
    BlockState funnelState = world.getBlockState(funnelPos);
    if (!(funnelState.getBlock() instanceof BeltFunnelBlock))
        return null;
    if (funnelState.getValue(BeltFunnelBlock.SHAPE) != Shape.PULLING)
        return null;
    if (side != null && FunnelBlock.getFunnelFacing(funnelState) != side)
        return null;
    BlockEntity te = world.getBlockEntity(funnelPos);
    if (!(te instanceof FunnelTileEntity))
        return null;
    if (funnelState.getValue(BeltFunnelBlock.POWERED))
        return stack;
    ItemStack insert = FunnelBlock.tryInsert(world, funnelPos, stack, simulate);
    if (insert.getCount() != stack.getCount() && !simulate)
        ((FunnelTileEntity) te).flap(true);
    return insert;
}
Also used : FunnelTileEntity(com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity) BlockState(net.minecraft.world.level.block.state.BlockState) BeltFunnelBlock(com.simibubi.create.content.logistics.block.funnel.BeltFunnelBlock) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) TransportedItemStack(com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack) ItemStack(net.minecraft.world.item.ItemStack) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Nullable(javax.annotation.Nullable)

Example 3 with FunnelTileEntity

use of com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity 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

FunnelTileEntity (com.simibubi.create.content.logistics.block.funnel.FunnelTileEntity)3 BlockPos (net.minecraft.core.BlockPos)3 Level (net.minecraft.world.level.Level)3 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)3 BlockState (net.minecraft.world.level.block.state.BlockState)3 BeltFunnelBlock (com.simibubi.create.content.logistics.block.funnel.BeltFunnelBlock)2 Direction (net.minecraft.core.Direction)2 ItemStack (net.minecraft.world.item.ItemStack)2 TransportedItemStack (com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack)1 FilteringBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour)1 InvManipulationBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour)1 Nullable (javax.annotation.Nullable)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)1 IItemHandler (net.minecraftforge.items.IItemHandler)1