Search in sources :

Example 1 with ISpecialInsertion

use of logisticspipes.interfaces.ISpecialInsertion in project LogisticsPipes by RS485.

the class SlotFinderOpenGuiPacket method processPacket.

@Override
public void processPacket(EntityPlayer player) {
    // hack to avoid wrenching blocks
    int savedEquipped = player.inventory.currentItem;
    boolean foundSlot = false;
    // try to find a empty slot
    for (int i = 0; i < 9; i++) {
        if (player.inventory.getStackInSlot(i).isEmpty()) {
            foundSlot = true;
            player.inventory.currentItem = i;
            break;
        }
    }
    // okay, anything that's a block?
    if (!foundSlot) {
        for (int i = 0; i < 9; i++) {
            ItemStack is = player.inventory.getStackInSlot(i);
            if (!is.isEmpty() && is.getItem() instanceof ItemBlock) {
                foundSlot = true;
                player.inventory.currentItem = i;
                break;
            }
        }
    }
    // give up and select whatever is right of the current slot
    if (!foundSlot) {
        player.inventory.currentItem = (player.inventory.currentItem + 1) % 9;
    }
    boolean openedGui = false;
    final LogisticsTileGenericPipe genericPipe = getPipe(player.world, LTGPCompletionCheck.PIPE);
    if (genericPipe.isRoutingPipe()) {
        openedGui = genericPipe.getRoutingPipe().getAvailableAdjacent().inventories().stream().filter(neighbor -> LPNeighborTileEntityKt.getInventoryUtil(neighbor) instanceof ISpecialInsertion).anyMatch(neighbor -> {
            for (ICraftingRecipeProvider provider : SimpleServiceLocator.craftingRecipeProviders) {
                if (provider.canOpenGui(neighbor.getTileEntity())) {
                    return true;
                }
            }
            Block block = neighbor.getTileEntity().getBlockType();
            final BlockPos blockPos = neighbor.getTileEntity().getPos();
            final IBlockState blockState = player.world.getBlockState(blockPos);
            if (!block.isAir(blockState, player.world, blockPos)) {
                int xCoord = blockPos.getX();
                int yCoord = blockPos.getY();
                int zCoord = blockPos.getZ();
                if (SimpleServiceLocator.enderStorageProxy.isEnderChestBlock(block)) {
                    SimpleServiceLocator.enderStorageProxy.openEnderChest(player.world, xCoord, yCoord, zCoord, player);
                    MainProxy.sendPacketToPlayer(PacketHandler.getPacket(SlotFinderActivatePacket.class).setTagetPosX(xCoord).setTagetPosY(yCoord).setTagetPosZ(zCoord).setSlot(getSlot()).setPacketPos(this), player);
                    return true;
                }
                if (block.onBlockActivated(player.world, blockPos, blockState, player, EnumHand.MAIN_HAND, EnumFacing.UP, 0, 0, 0)) {
                    MainProxy.sendPacketToPlayer(PacketHandler.getPacket(SlotFinderActivatePacket.class).setTagetPosX(xCoord).setTagetPosY(yCoord).setTagetPosZ(zCoord).setSlot(getSlot()).setPacketPos(this), player);
                    return true;
                }
            }
            return false;
        });
    }
    if (!openedGui) {
        LogisticsPipes.log.warn("Ignored SlotFinderOpenGuiPacket from " + player.toString() + ", because of failing preconditions");
    }
    player.inventory.currentItem = savedEquipped;
}
Also used : ModernPacket(logisticspipes.network.abstractpackets.ModernPacket) Setter(lombok.Setter) ISpecialInsertion(logisticspipes.interfaces.ISpecialInsertion) Getter(lombok.Getter) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) LPDataInput(network.rs485.logisticspipes.util.LPDataInput) LogisticsPipes(logisticspipes.LogisticsPipes) EnumHand(net.minecraft.util.EnumHand) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) PacketHandler(logisticspipes.network.PacketHandler) MainProxy(logisticspipes.proxy.MainProxy) LPDataOutput(network.rs485.logisticspipes.util.LPDataOutput) IBlockState(net.minecraft.block.state.IBlockState) ItemStack(net.minecraft.item.ItemStack) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) Block(net.minecraft.block.Block) EntityPlayer(net.minecraft.entity.player.EntityPlayer) StaticResolve(logisticspipes.utils.StaticResolve) LPNeighborTileEntityKt(network.rs485.logisticspipes.connection.LPNeighborTileEntityKt) ItemBlock(net.minecraft.item.ItemBlock) ModuleCoordinatesPacket(logisticspipes.network.abstractpackets.ModuleCoordinatesPacket) ICraftingRecipeProvider(logisticspipes.proxy.interfaces.ICraftingRecipeProvider) ISpecialInsertion(logisticspipes.interfaces.ISpecialInsertion) IBlockState(net.minecraft.block.state.IBlockState) ICraftingRecipeProvider(logisticspipes.proxy.interfaces.ICraftingRecipeProvider) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) Block(net.minecraft.block.Block) ItemBlock(net.minecraft.item.ItemBlock) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) ItemBlock(net.minecraft.item.ItemBlock)

Example 2 with ISpecialInsertion

use of logisticspipes.interfaces.ISpecialInsertion in project LogisticsPipes by RS485.

the class PipeTransportLogistics method handleTileReachedServer_internal.

protected final void handleTileReachedServer_internal(LPTravelingItemServer arrivingItem, TileEntity tile, ForgeDirection dir) {
    if (isRouted && getPipe().container.tilePart.getBCPipePluggable(dir) != null && getPipe().container.tilePart.getBCPipePluggable(dir).isAcceptingItems(arrivingItem)) {
        LPTravelingItemServer remainingItem = getPipe().container.tilePart.getBCPipePluggable(dir).handleItem(arrivingItem);
        if (remainingItem != null) {
            getRoutedPipe().getRouter().update(true, getRoutedPipe());
            this.injectItem(remainingItem, dir);
        }
        return;
    }
    if (getPipe() instanceof PipeItemsFluidSupplier) {
        ((PipeItemsFluidSupplier) getPipe()).endReached(arrivingItem, tile);
        if (arrivingItem.getItemIdentifierStack().getStackSize() <= 0) {
            return;
        }
    }
    markChunkModified(tile);
    if (MainProxy.isServer(getWorld()) && arrivingItem.getInfo() != null && arrivingItem.getArrived() && isRouted) {
        getRoutedPipe().notifyOfItemArival(arrivingItem.getInfo());
    }
    if (getPipe() instanceof FluidRoutedPipe) {
        if (((FluidRoutedPipe) getPipe()).endReached(arrivingItem, tile)) {
            return;
        }
    }
    boolean isSpecialConnectionInformationTransition = false;
    if (MainProxy.isServer(getWorld())) {
        if (SimpleServiceLocator.specialtileconnection.needsInformationTransition(tile)) {
            isSpecialConnectionInformationTransition = true;
            SimpleServiceLocator.specialtileconnection.transmit(tile, arrivingItem);
        }
    }
    if (SimpleServiceLocator.pipeInformationManager.isItemPipe(tile)) {
        if (passToNextPipe(arrivingItem, tile)) {
            return;
        }
    } else if (tile instanceof IInventory && isRouted) {
        getRoutedPipe().getCacheHolder().trigger(CacheTypes.Inventory);
        // items.scheduleRemoval(arrivingItem);
        if (MainProxy.isServer(getWorld())) {
            // destroy the item on exit if it isn't exitable
            if (!isSpecialConnectionInformationTransition && !isItemExitable(arrivingItem.getItemIdentifierStack())) {
                return;
            }
            // last chance for chassi to back out
            if (arrivingItem != null) {
                if (arrivingItem.getTransportMode() != TransportMode.Active && !getRoutedPipe().getTransportLayer().stillWantItem(arrivingItem)) {
                    reverseItem(arrivingItem);
                    return;
                }
            }
            ISlotUpgradeManager slotManager;
            {
                ModulePositionType slot = null;
                int positionInt = -1;
                if (arrivingItem.getInfo().targetInfo instanceof ChassiTargetInformation) {
                    positionInt = ((ChassiTargetInformation) arrivingItem.getInfo().targetInfo).getModuleSlot();
                    slot = ModulePositionType.SLOT;
                } else if (LPConstants.DEBUG && container.pipe instanceof PipeLogisticsChassi) {
                    System.out.println(arrivingItem);
                    new RuntimeException("[ItemInsertion] Information weren't ment for a chassi pipe").printStackTrace();
                }
                slotManager = getRoutedPipe().getUpgradeManager(slot, positionInt);
            }
            boolean tookSome = false;
            if (arrivingItem.getAdditionalTargetInformation() instanceof ITargetSlotInformation) {
                ITargetSlotInformation information = (ITargetSlotInformation) arrivingItem.getAdditionalTargetInformation();
                IInventory inv = (IInventory) tile;
                if (inv instanceof ISidedInventory) {
                    inv = new SidedInventoryMinecraftAdapter((ISidedInventory) inv, ForgeDirection.UNKNOWN, false);
                }
                IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv);
                if (util instanceof ISpecialInsertion) {
                    int slot = information.getTargetSlot();
                    int amount = information.getAmount();
                    if (util.getSizeInventory() > slot) {
                        ItemStack content = util.getStackInSlot(slot);
                        ItemStack toAdd = arrivingItem.getItemIdentifierStack().makeNormalStack();
                        toAdd.stackSize = Math.min(toAdd.stackSize, Math.max(0, amount - (content != null ? content.stackSize : 0)));
                        if (toAdd.stackSize > 0) {
                            if (util.getSizeInventory() > slot) {
                                int added = ((ISpecialInsertion) util).addToSlot(toAdd, slot);
                                arrivingItem.getItemIdentifierStack().lowerStackSize(added);
                                if (added > 0) {
                                    tookSome = true;
                                }
                            }
                        }
                    }
                    if (information.isLimited()) {
                        if (arrivingItem.getItemIdentifierStack().getStackSize() > 0) {
                            reverseItem(arrivingItem);
                        }
                        return;
                    }
                }
            }
            // sneaky insertion
            if (!getRoutedPipe().getUpgradeManager().hasCombinedSneakyUpgrade() || slotManager.hasOwnSneakyUpgrade()) {
                ForgeDirection insertion = arrivingItem.output.getOpposite();
                if (slotManager.hasSneakyUpgrade()) {
                    insertion = slotManager.getSneakyOrientation();
                }
                ItemStack added = InventoryHelper.getTransactorFor(tile, insertion).add(arrivingItem.getItemIdentifierStack().makeNormalStack(), insertion, true);
                arrivingItem.getItemIdentifierStack().lowerStackSize(added.stackSize);
                if (added.stackSize > 0 && arrivingItem instanceof IRoutedItem) {
                    tookSome = true;
                    arrivingItem.setBufferCounter(0);
                }
                ItemRoutingInformation info;
                if (arrivingItem.getItemIdentifierStack().getStackSize() > 0) {
                    // we have some leftovers, we are splitting the stack, we need to clone the info
                    info = arrivingItem.getInfo().clone();
                    // For InvSysCon
                    info.getItem().setStackSize(added.stackSize);
                    insertedItemStack(info, tile);
                } else {
                    info = arrivingItem.getInfo();
                    info.getItem().setStackSize(added.stackSize);
                    // For InvSysCon
                    insertedItemStack(info, tile);
                    // every item has been inserted.
                    return;
                }
            } else {
                ForgeDirection[] dirs = getRoutedPipe().getUpgradeManager().getCombinedSneakyOrientation();
                for (ForgeDirection insertion : dirs) {
                    if (insertion == null) {
                        continue;
                    }
                    ItemStack added = InventoryHelper.getTransactorFor(tile, insertion).add(arrivingItem.getItemIdentifierStack().makeNormalStack(), insertion, true);
                    arrivingItem.getItemIdentifierStack().lowerStackSize(added.stackSize);
                    if (added.stackSize > 0) {
                        tookSome = true;
                        arrivingItem.setBufferCounter(0);
                    }
                    ItemRoutingInformation info;
                    if (arrivingItem.getItemIdentifierStack().getStackSize() > 0) {
                        // we have some leftovers, we are splitting the stack, we need to clone the info
                        info = arrivingItem.getInfo().clone();
                        // For InvSysCon
                        info.getItem().setStackSize(added.stackSize);
                        insertedItemStack(info, tile);
                    } else {
                        info = arrivingItem.getInfo();
                        info.getItem().setStackSize(added.stackSize);
                        // For InvSysCon
                        insertedItemStack(info, tile);
                        // every item has been inserted.
                        return;
                    }
                }
            }
            if (arrivingItem.getItemIdentifierStack().getStackSize() > 0) {
                reverseItem(arrivingItem);
            }
        }
        // the item is handled
        return;
    }
    // end of insert into IInventory
    dropItem(arrivingItem);
}
Also used : IInventory(net.minecraft.inventory.IInventory) ITargetSlotInformation(logisticspipes.interfaces.routing.ITargetSlotInformation) SidedInventoryMinecraftAdapter(logisticspipes.utils.SidedInventoryMinecraftAdapter) FluidRoutedPipe(logisticspipes.pipes.basic.fluid.FluidRoutedPipe) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) ModulePositionType(logisticspipes.modules.abstractmodules.LogisticsModule.ModulePositionType) ISidedInventory(net.minecraft.inventory.ISidedInventory) PipeItemsFluidSupplier(logisticspipes.pipes.PipeItemsFluidSupplier) ISpecialInsertion(logisticspipes.interfaces.ISpecialInsertion) ChassiTargetInformation(logisticspipes.pipes.PipeLogisticsChassi.ChassiTargetInformation) PipeLogisticsChassi(logisticspipes.pipes.PipeLogisticsChassi) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ItemRoutingInformation(logisticspipes.routing.ItemRoutingInformation) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemStack(net.minecraft.item.ItemStack) ISlotUpgradeManager(logisticspipes.interfaces.ISlotUpgradeManager) LPTravelingItemServer(logisticspipes.transport.LPTravelingItem.LPTravelingItemServer)

Example 3 with ISpecialInsertion

use of logisticspipes.interfaces.ISpecialInsertion in project LogisticsPipes by RS485.

the class PipeTransportLogistics method handleTileReachedServer_internal.

protected final void handleTileReachedServer_internal(LPTravelingItemServer arrivingItem, TileEntity tile, EnumFacing dir) {
    if (getPipe() instanceof PipeItemsFluidSupplier) {
        ((PipeItemsFluidSupplier) getPipe()).endReached(arrivingItem, tile);
        if (arrivingItem.getItemIdentifierStack().getStackSize() <= 0) {
            return;
        }
    }
    markChunkModified(tile);
    if (arrivingItem.getInfo() != null && arrivingItem.getArrived() && isRouted) {
        getRoutedPipe().notifyOfItemArival(arrivingItem.getInfo());
    }
    if (getPipe() instanceof FluidRoutedPipe) {
        if (((FluidRoutedPipe) getPipe()).endReached(arrivingItem, tile)) {
            return;
        }
    }
    boolean isSpecialConnectionInformationTransition = false;
    if (SimpleServiceLocator.specialtileconnection.needsInformationTransition(tile)) {
        isSpecialConnectionInformationTransition = true;
        SimpleServiceLocator.specialtileconnection.transmit(tile, arrivingItem);
    }
    if (SimpleServiceLocator.pipeInformationManager.isItemPipe(tile)) {
        if (passToNextPipe(arrivingItem, tile)) {
            return;
        }
    } else {
        IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(tile, dir.getOpposite());
        if (util != null && isRouted) {
            getRoutedPipe().getCacheHolder().trigger(CacheTypes.Inventory);
            // destroy the item on exit if it isn't exitable
            if (!isSpecialConnectionInformationTransition && isItemUnwanted(arrivingItem.getItemIdentifierStack())) {
                return;
            }
            // last chance for chassi to back out
            if (arrivingItem.getTransportMode() != TransportMode.Active && !getRoutedPipe().getTransportLayer().stillWantItem(arrivingItem)) {
                reverseItem(arrivingItem);
                return;
            }
            final ISlotUpgradeManager slotManager;
            {
                ModulePositionType slot = null;
                int positionInt = -1;
                if (arrivingItem.getInfo().targetInfo instanceof ChassiTargetInformation) {
                    positionInt = ((ChassiTargetInformation) arrivingItem.getInfo().targetInfo).getModuleSlot();
                    slot = ModulePositionType.SLOT;
                } else if (LogisticsPipes.isDEBUG() && container.pipe instanceof PipeLogisticsChassis) {
                    System.out.println(arrivingItem);
                    new RuntimeException("[ItemInsertion] Information weren't ment for a chassi pipe").printStackTrace();
                }
                slotManager = getRoutedPipe().getUpgradeManager(slot, positionInt);
            }
            if (arrivingItem.getAdditionalTargetInformation() instanceof ITargetSlotInformation) {
                ITargetSlotInformation information = (ITargetSlotInformation) arrivingItem.getAdditionalTargetInformation();
                if (util instanceof ISpecialInsertion) {
                    int slot = information.getTargetSlot();
                    int amount = information.getAmount();
                    if (util.getSizeInventory() > slot) {
                        ItemStack content = util.getStackInSlot(slot);
                        ItemStack toAdd = arrivingItem.getItemIdentifierStack().makeNormalStack();
                        final int amountLeft = Math.max(0, amount - content.getCount());
                        toAdd.setCount(Math.min(toAdd.getCount(), amountLeft));
                        if (toAdd.getCount() > 0) {
                            if (util.getSizeInventory() > slot) {
                                int added = ((ISpecialInsertion) util).addToSlot(toAdd, slot);
                                arrivingItem.getItemIdentifierStack().lowerStackSize(added);
                            }
                        }
                    }
                    if (information.isLimited()) {
                        if (arrivingItem.getItemIdentifierStack().getStackSize() > 0) {
                            reverseItem(arrivingItem);
                        }
                        return;
                    }
                }
            }
            // sneaky insertion
            if (!getRoutedPipe().getUpgradeManager().hasCombinedSneakyUpgrade() || slotManager.hasOwnSneakyUpgrade()) {
                EnumFacing insertion = arrivingItem.output.getOpposite();
                if (slotManager.hasSneakyUpgrade()) {
                    insertion = slotManager.getSneakyOrientation();
                }
                if (insertArrivingItem(arrivingItem, tile, insertion))
                    return;
            } else {
                EnumFacing[] dirs = getRoutedPipe().getUpgradeManager().getCombinedSneakyOrientation();
                for (EnumFacing insertion : dirs) {
                    if (insertion == null) {
                        continue;
                    }
                    if (insertArrivingItem(arrivingItem, tile, insertion))
                        return;
                }
            }
            if (arrivingItem.getItemIdentifierStack().getStackSize() > 0) {
                reverseItem(arrivingItem);
            }
            // the item is handled
            return;
        }
    // end of insert into IInventory
    }
    dropItem(arrivingItem);
}
Also used : ITargetSlotInformation(logisticspipes.interfaces.routing.ITargetSlotInformation) FluidRoutedPipe(logisticspipes.pipes.basic.fluid.FluidRoutedPipe) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) EnumFacing(net.minecraft.util.EnumFacing) PipeLogisticsChassis(logisticspipes.pipes.PipeLogisticsChassis) ModulePositionType(logisticspipes.modules.LogisticsModule.ModulePositionType) PipeItemsFluidSupplier(logisticspipes.pipes.PipeItemsFluidSupplier) ISpecialInsertion(logisticspipes.interfaces.ISpecialInsertion) ChassiTargetInformation(logisticspipes.pipes.PipeLogisticsChassis.ChassiTargetInformation) ItemStack(net.minecraft.item.ItemStack) ISlotUpgradeManager(logisticspipes.interfaces.ISlotUpgradeManager)

Aggregations

ISpecialInsertion (logisticspipes.interfaces.ISpecialInsertion)3 ItemStack (net.minecraft.item.ItemStack)3 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)2 ISlotUpgradeManager (logisticspipes.interfaces.ISlotUpgradeManager)2 ITargetSlotInformation (logisticspipes.interfaces.routing.ITargetSlotInformation)2 PipeItemsFluidSupplier (logisticspipes.pipes.PipeItemsFluidSupplier)2 FluidRoutedPipe (logisticspipes.pipes.basic.fluid.FluidRoutedPipe)2 EnumFacing (net.minecraft.util.EnumFacing)2 LogisticsPipes (logisticspipes.LogisticsPipes)1 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)1 ModulePositionType (logisticspipes.modules.LogisticsModule.ModulePositionType)1 ModulePositionType (logisticspipes.modules.abstractmodules.LogisticsModule.ModulePositionType)1 PacketHandler (logisticspipes.network.PacketHandler)1 ModernPacket (logisticspipes.network.abstractpackets.ModernPacket)1 ModuleCoordinatesPacket (logisticspipes.network.abstractpackets.ModuleCoordinatesPacket)1 PipeLogisticsChassi (logisticspipes.pipes.PipeLogisticsChassi)1 ChassiTargetInformation (logisticspipes.pipes.PipeLogisticsChassi.ChassiTargetInformation)1 PipeLogisticsChassis (logisticspipes.pipes.PipeLogisticsChassis)1 ChassiTargetInformation (logisticspipes.pipes.PipeLogisticsChassis.ChassiTargetInformation)1 LogisticsTileGenericPipe (logisticspipes.pipes.basic.LogisticsTileGenericPipe)1