Search in sources :

Example 11 with ItemIdentifierStack

use of logisticspipes.utils.item.ItemIdentifierStack in project LogisticsPipes by RS485.

the class ModuleCrafter method tick.

@Override
public void tick() {
    enabledUpdateEntity();
    if (_lostItems.isEmpty()) {
        return;
    }
    // if(true) return;
    DelayedGeneric<Pair<ItemIdentifierStack, IAdditionalTargetInformation>> lostItem = _lostItems.poll();
    int rerequested = 0;
    while (lostItem != null && rerequested < 100) {
        Pair<ItemIdentifierStack, IAdditionalTargetInformation> pair = lostItem.get();
        if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING)) {
            SinkReply reply = LogisticsManager.canSink(getRouter(), null, true, pair.getValue1().getItem(), null, true, true);
            if (reply == null || reply.maxNumberOfItems < 1) {
                _lostItems.add(new DelayedGeneric<>(pair, 9000 + (int) (Math.random() * 2000)));
                lostItem = _lostItems.poll();
                continue;
            }
        }
        int received = RequestTree.requestPartial(pair.getValue1(), (CoreRoutedPipe) _service, pair.getValue2());
        rerequested++;
        if (received < pair.getValue1().getStackSize()) {
            pair.getValue1().setStackSize(pair.getValue1().getStackSize() - received);
            _lostItems.add(new DelayedGeneric<>(pair, 4500 + (int) (Math.random() * 1000)));
        }
        lostItem = _lostItems.poll();
    }
}
Also used : SinkReply(logisticspipes.utils.SinkReply) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) Pair(logisticspipes.utils.tuples.Pair) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation)

Example 12 with ItemIdentifierStack

use of logisticspipes.utils.item.ItemIdentifierStack in project LogisticsPipes by RS485.

the class ModuleActiveSupplier method createPatternRequest.

private void createPatternRequest(IInventoryUtil invUtil) {
    _service.getDebug().log("Supplier: Start calculating pattern request");
    setRequestFailed(false);
    for (int i = 0; i < 9; i++) {
        ItemIdentifierStack needed = dummyInventory.getIDStackInSlot(i);
        if (needed == null) {
            continue;
        }
        if (invUtil.getSizeInventory() <= slotArray[i]) {
            continue;
        }
        ItemStack stack = invUtil.getStackInSlot(slotArray[i]);
        ItemIdentifierStack have = null;
        if (stack != null) {
            have = ItemIdentifierStack.getFromStack(stack);
        }
        int haveCount = 0;
        if (have != null) {
            if (!have.getItem().equals(needed.getItem())) {
                _service.getDebug().log("Supplier: Slot for " + i + ", " + needed + " already taken by " + have);
                setRequestFailed(true);
                continue;
            }
            haveCount = have.getStackSize();
        }
        if ((_patternMode == PatternMode.Bulk50 && haveCount > needed.getStackSize() / 2) || (_patternMode == PatternMode.Bulk100 && haveCount >= needed.getStackSize())) {
            continue;
        }
        Integer requestedCount = _requestedItems.get(needed.getItem());
        if (requestedCount != null) {
            haveCount += requestedCount;
        }
        int neededCount = needed.getStackSize() - haveCount;
        if (neededCount < 1) {
            continue;
        }
        ItemIdentifierStack toRequest = new ItemIdentifierStack(needed.getItem(), neededCount);
        _service.getDebug().log("Supplier: Missing for slot " + i + ": " + toRequest);
        if (!_service.useEnergy(10)) {
            break;
        }
        boolean success = false;
        IAdditionalTargetInformation targetInformation = new PatternSupplierTargetInformation(slotArray[i], needed.getStackSize());
        if (_patternMode != PatternMode.Full) {
            _service.getDebug().log("Supplier: Requesting partial: " + toRequest);
            neededCount = RequestTree.requestPartial(toRequest, this, targetInformation);
            _service.getDebug().log("Supplier: Requested: " + toRequest.getItem().makeStack(neededCount));
            if (neededCount > 0) {
                success = true;
            }
        } else {
            _service.getDebug().log("Supplier: Requesting: " + toRequest);
            success = RequestTree.request(toRequest, this, null, targetInformation);
            if (success) {
                _service.getDebug().log("Supplier: Request success");
            } else {
                _service.getDebug().log("Supplier: Request failed");
            }
        }
        if (success) {
            Integer currentRequest = _requestedItems.get(toRequest.getItem());
            if (currentRequest == null) {
                _requestedItems.put(toRequest.getItem(), neededCount);
            } else {
                _requestedItems.put(toRequest.getItem(), currentRequest + neededCount);
            }
        } else {
            setRequestFailed(true);
        }
    }
}
Also used : ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) ItemStack(net.minecraft.item.ItemStack) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation)

Example 13 with ItemIdentifierStack

use of logisticspipes.utils.item.ItemIdentifierStack in project LogisticsPipes by RS485.

the class ModuleProvider method checkUpdate.

private void checkUpdate(EntityPlayer player) {
    if (localModeWatchers.size() == 0 && player == null) {
        return;
    }
    displayList.clear();
    displayMap.clear();
    getAllItems(displayMap, new ArrayList<>(0));
    displayList.ensureCapacity(displayMap.size());
    displayList.addAll(displayMap.entrySet().stream().map(item -> new ItemIdentifierStack(item.getKey(), item.getValue())).collect(Collectors.toList()));
    if (!oldList.equals(displayList)) {
        oldList.clear();
        oldList.ensureCapacity(displayList.size());
        oldList.addAll(displayList);
        MainProxy.sendToPlayerList(PacketHandler.getPacket(ModuleInventory.class).setIdentList(displayList).setModulePos(this).setCompressable(true), localModeWatchers);
    } else if (player != null) {
        MainProxy.sendPacketToPlayer(PacketHandler.getPacket(ModuleInventory.class).setIdentList(displayList).setModulePos(this).setCompressable(true), player);
    }
}
Also used : ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) ModuleInventory(logisticspipes.network.packets.module.ModuleInventory)

Example 14 with ItemIdentifierStack

use of logisticspipes.utils.item.ItemIdentifierStack in project LogisticsPipes by RS485.

the class ModuleCrafter method enabledUpdateEntity.

public void enabledUpdateEntity() {
    if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
        if (_service.isNthTick(6)) {
            cacheAreAllOrderesToBuffer();
        }
        if (_service.getItemOrderManager().isFirstOrderWatched()) {
            TileEntity tile = lastAccessedCrafter.get();
            if (tile != null) {
                _service.getItemOrderManager().setMachineProgress(SimpleServiceLocator.machineProgressProvider.getProgressForTile(tile));
            } else {
                _service.getItemOrderManager().setMachineProgress((byte) 0);
            }
        }
    } else {
        cachedAreAllOrderesToBuffer = false;
    }
    if (!_service.isNthTick(6)) {
        return;
    }
    waitingForCraft = false;
    if ((!_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA))) {
        if (getUpgradeManager().getCrafterCleanup() > 0) {
            List<AdjacentTileEntity> crafters = locateCrafters();
            ItemStack extracted = null;
            for (AdjacentTileEntity adjacentCrafter : crafters) {
                extracted = extractFiltered(adjacentCrafter, _cleanupInventory, cleanupModeIsExclude, getUpgradeManager().getCrafterCleanup() * 3);
                if (extracted != null && extracted.stackSize > 0) {
                    break;
                }
            }
            if (extracted != null && extracted.stackSize > 0) {
                _service.queueRoutedItem(SimpleServiceLocator.routedItemHelper.createNewTravelItem(extracted), ForgeDirection.UP);
                _service.getCacheHolder().trigger(CacheTypes.Inventory);
            }
        }
        return;
    }
    waitingForCraft = true;
    List<AdjacentTileEntity> adjacentCrafters = locateCrafters();
    if (adjacentCrafters.size() < 1) {
        if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
            _service.getItemOrderManager().sendFailed();
        }
        return;
    }
    List<ItemIdentifierStack> wanteditem = getCraftedItems();
    if (wanteditem == null || wanteditem.isEmpty()) {
        return;
    }
    _service.spawnParticle(Particles.VioletParticle, 2);
    int itemsleft = itemsToExtract();
    int stacksleft = stacksToExtract();
    while (itemsleft > 0 && stacksleft > 0 && (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA))) {
        // fetch but not remove.
        LogisticsItemOrder nextOrder = _service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, ResourceType.EXTRA);
        int maxtosend = Math.min(itemsleft, nextOrder.getResource().stack.getStackSize());
        maxtosend = Math.min(nextOrder.getResource().getItem().getMaxStackSize(), maxtosend);
        // retrieve the new crafted items
        ItemStack extracted = null;
        AdjacentTileEntity adjacent = null;
        for (AdjacentTileEntity adjacentCrafter : adjacentCrafters) {
            adjacent = adjacentCrafter;
            extracted = extract(adjacent, nextOrder.getResource(), maxtosend);
            if (extracted != null && extracted.stackSize > 0) {
                break;
            }
        }
        if (extracted == null || extracted.stackSize == 0) {
            _service.getItemOrderManager().deferSend();
            break;
        }
        _service.getCacheHolder().trigger(CacheTypes.Inventory);
        lastAccessedCrafter = new WeakReference<>(adjacent.tileEntity);
        // send the new crafted items to the destination
        ItemIdentifier extractedID = ItemIdentifier.get(extracted);
        while (extracted.stackSize > 0) {
            if (!doesExtractionMatch(nextOrder, extractedID)) {
                LogisticsItemOrder startOrder = nextOrder;
                if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
                    do {
                        _service.getItemOrderManager().deferSend();
                        nextOrder = _service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, ResourceType.EXTRA);
                    } while (!doesExtractionMatch(nextOrder, extractedID) && startOrder != nextOrder);
                }
                if (startOrder == nextOrder) {
                    int numtosend = Math.min(extracted.stackSize, extractedID.getMaxStackSize());
                    if (numtosend == 0) {
                        break;
                    }
                    stacksleft -= 1;
                    itemsleft -= numtosend;
                    ItemStack stackToSend = extracted.splitStack(numtosend);
                    //Route the unhandled item
                    _service.sendStack(stackToSend, -1, ItemSendMode.Normal, null);
                    continue;
                }
            }
            int numtosend = Math.min(extracted.stackSize, extractedID.getMaxStackSize());
            numtosend = Math.min(numtosend, nextOrder.getResource().stack.getStackSize());
            if (numtosend == 0) {
                break;
            }
            stacksleft -= 1;
            itemsleft -= numtosend;
            ItemStack stackToSend = extracted.splitStack(numtosend);
            if (nextOrder.getDestination() != null) {
                SinkReply reply = LogisticsManager.canSink(nextOrder.getDestination().getRouter(), null, true, ItemIdentifier.get(stackToSend), null, true, false);
                boolean defersend = false;
                if (reply == null || reply.bufferMode != BufferMode.NONE || reply.maxNumberOfItems < 1) {
                    defersend = true;
                }
                IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stackToSend);
                item.setDestination(nextOrder.getDestination().getRouter().getSimpleID());
                item.setTransportMode(TransportMode.Active);
                item.setAdditionalTargetInformation(nextOrder.getInformation());
                _service.queueRoutedItem(item, adjacent.direction);
                _service.getItemOrderManager().sendSuccessfull(stackToSend.stackSize, defersend, item);
            } else {
                _service.sendStack(stackToSend, -1, ItemSendMode.Normal, nextOrder.getInformation());
                _service.getItemOrderManager().sendSuccessfull(stackToSend.stackSize, false, null);
            }
            if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
                // fetch but not remove.
                nextOrder = _service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, ResourceType.EXTRA);
            }
        }
    }
}
Also used : LogisticsItemOrder(logisticspipes.routing.order.LogisticsItemOrder) AdjacentTileEntity(network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity) LogisticsCraftingTableTileEntity(logisticspipes.blocks.crafting.LogisticsCraftingTableTileEntity) AdjacentTileEntity(network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) SinkReply(logisticspipes.utils.SinkReply) ItemStack(net.minecraft.item.ItemStack) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack)

Example 15 with ItemIdentifierStack

use of logisticspipes.utils.item.ItemIdentifierStack in project LogisticsPipes by RS485.

the class ModuleCrafterMK3 method tick.

@Override
public void tick() {
    super.tick();
    if (inv.isEmpty()) {
        return;
    }
    if (!_service.isNthTick(6)) {
        return;
    }
    //Add from internal buffer
    List<AdjacentTileEntity> crafters = locateCrafters();
    boolean change = false;
    for (AdjacentTileEntity adjacent : crafters) {
        for (int i = inv.getSizeInventory() - 1; i >= 0; i--) {
            ItemIdentifierStack slot = inv.getIDStackInSlot(i);
            if (slot == null) {
                continue;
            }
            ForgeDirection insertion = adjacent.direction.getOpposite();
            if (getUpgradeManager().hasSneakyUpgrade()) {
                insertion = getUpgradeManager().getSneakyOrientation();
            }
            ItemIdentifierStack toadd = slot.clone();
            toadd.setStackSize(Math.min(toadd.getStackSize(), toadd.getItem().getMaxStackSize()));
            if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING)) {
                toadd.setStackSize(Math.min(toadd.getStackSize(), ((IInventory) adjacent.tileEntity).getInventoryStackLimit()));
                ItemStack added = InventoryHelper.getTransactorFor(adjacent.tileEntity, adjacent.direction.getOpposite()).add(toadd.makeNormalStack(), insertion, true);
                slot.setStackSize(slot.getStackSize() - added.stackSize);
                if (added.stackSize != 0) {
                    change = true;
                }
            } else {
                _service.queueRoutedItem(SimpleServiceLocator.routedItemHelper.createNewTravelItem(toadd), adjacent.direction.getOpposite());
                slot.setStackSize(slot.getStackSize() - toadd.getStackSize());
                change = true;
            }
            if (slot.getStackSize() <= 0) {
                inv.clearInventorySlotContents(i);
            } else {
                inv.setInventorySlotContents(i, slot);
            }
        }
    }
    if (change) {
        inv.markDirty();
        _service.getCacheHolder().trigger(CacheTypes.Inventory);
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) ItemStack(net.minecraft.item.ItemStack) AdjacentTileEntity(network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity)

Aggregations

ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)58 ItemStack (net.minecraft.item.ItemStack)21 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)18 ArrayList (java.util.ArrayList)9 Pair (logisticspipes.utils.tuples.Pair)8 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 LinkedList (java.util.LinkedList)5 NBTTagList (net.minecraft.nbt.NBTTagList)5 List (java.util.List)4 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)4 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)4 DictResource (logisticspipes.request.resources.DictResource)4 IResource (logisticspipes.request.resources.IResource)4 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)4 HashMap (java.util.HashMap)3 TreeSet (java.util.TreeSet)3 LogisticsCraftingTableTileEntity (logisticspipes.blocks.crafting.LogisticsCraftingTableTileEntity)3 IAdditionalTargetInformation (logisticspipes.interfaces.routing.IAdditionalTargetInformation)3 ItemResource (logisticspipes.request.resources.ItemResource)3 IRouter (logisticspipes.routing.IRouter)3