Search in sources :

Example 6 with IPipeServiceProvider

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

the class ModuleProvider method tick.

@Override
public void tick() {
    final IPipeServiceProvider service = _service;
    if (service == null)
        return;
    if (service.isNthTick(6)) {
        checkUpdate(null);
    }
    int itemsleft = itemsToExtract();
    int stacksleft = stacksToExtract();
    LogisticsItemOrder firstOrder = null;
    LogisticsItemOrder order = null;
    while (itemsleft > 0 && stacksleft > 0 && service.getItemOrderManager().hasOrders(ResourceType.PROVIDER) && (firstOrder == null || firstOrder != order)) {
        if (firstOrder == null) {
            firstOrder = order;
        }
        order = service.getItemOrderManager().peekAtTopRequest(ResourceType.PROVIDER);
        int sent = sendStack(order.getResource().stack, itemsleft, order.getDestination().getRouter().getSimpleID(), order.getInformation());
        if (sent < 0) {
            break;
        }
        service.spawnParticle(Particles.VioletParticle, 3);
        stacksleft -= 1;
        itemsleft -= sent;
    }
}
Also used : IPipeServiceProvider(logisticspipes.interfaces.IPipeServiceProvider) LogisticsItemOrder(logisticspipes.routing.order.LogisticsItemOrder)

Example 7 with IPipeServiceProvider

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

the class ModuleActiveSupplier method itemLost.

@Override
public void itemLost(ItemIdentifierStack item, IAdditionalTargetInformation info) {
    final IPipeServiceProvider service = Objects.requireNonNull(_service);
    service.getDebug().log("Supplier: Registered Item Lost: " + item);
    decreaseRequested(item);
}
Also used : IPipeServiceProvider(logisticspipes.interfaces.IPipeServiceProvider)

Example 8 with IPipeServiceProvider

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

the class ModuleActiveSupplier method createSupplyRequest.

private void createSupplyRequest(IInventoryUtil invUtil) {
    final IPipeServiceProvider service = Objects.requireNonNull(_service);
    service.getDebug().log("Supplier: Start calculating supply request");
    // How many do I want?
    HashMap<ItemIdentifier, Integer> needed = new HashMap<>(inventory.getItemsAndCount());
    service.getDebug().log("Supplier: Needed: " + needed);
    // How many do I have?
    Map<ItemIdentifier, Integer> have = invUtil.getItemsAndCount();
    service.getDebug().log("Supplier: Have:   " + have);
    // How many do I have?
    HashMap<ItemIdentifier, Integer> haveUndamaged = new HashMap<>();
    for (Entry<ItemIdentifier, Integer> item : have.entrySet()) {
        haveUndamaged.merge(item.getKey().getUndamaged(), item.getValue(), Integer::sum);
    }
    // Reduce what I have and what have been requested already
    for (Entry<ItemIdentifier, Integer> item : needed.entrySet()) {
        Integer haveCount = haveUndamaged.get(item.getKey().getUndamaged());
        if (haveCount == null) {
            haveCount = 0;
        }
        int spaceAvailable = invUtil.roomForItem(item.getKey().unsafeMakeNormalStack(Integer.MAX_VALUE));
        if (requestMode.getValue() == SupplyMode.Infinite) {
            Integer requestedCount = _requestedItems.get(item.getKey());
            if (requestedCount != null) {
                spaceAvailable -= requestedCount;
            }
            item.setValue(Math.min(item.getKey().getMaxStackSize(), Math.max(0, spaceAvailable)));
            continue;
        }
        if (spaceAvailable < 1 || (requestMode.getValue() == SupplyMode.Bulk50 && haveCount > item.getValue() / 2) || (requestMode.getValue() == SupplyMode.Bulk100 && haveCount >= item.getValue())) {
            item.setValue(0);
            continue;
        }
        if (haveCount > 0) {
            item.setValue(item.getValue() - haveCount);
            // so that 1 damaged item can't satisfy a request for 2 other damage values.
            haveUndamaged.put(item.getKey().getUndamaged(), haveCount - item.getValue());
        }
        Integer requestedCount = _requestedItems.get(item.getKey());
        if (requestedCount != null) {
            item.setValue(item.getValue() - requestedCount);
        }
    }
    service.getDebug().log("Supplier: Missing:   " + needed);
    setRequestFailed(false);
    // Make request
    for (Entry<ItemIdentifier, Integer> need : needed.entrySet()) {
        Integer amountRequested = need.getValue();
        if (amountRequested == null || amountRequested < 1) {
            continue;
        }
        int neededCount = amountRequested;
        if (!service.useEnergy(10)) {
            break;
        }
        boolean success = false;
        IAdditionalTargetInformation targetInformation = new SupplierTargetInformation();
        if (requestMode.getValue() != SupplyMode.Full) {
            service.getDebug().log("Supplier: Requesting partial: " + need.getKey().makeStack(neededCount));
            neededCount = RequestTree.requestPartial(need.getKey().makeStack(neededCount), this, targetInformation);
            service.getDebug().log("Supplier: Requested: " + need.getKey().makeStack(neededCount));
            if (neededCount > 0) {
                success = true;
            }
        } else {
            service.getDebug().log("Supplier: Requesting: " + need.getKey().makeStack(neededCount));
            success = RequestTree.request(need.getKey().makeStack(neededCount), this, null, targetInformation);
            if (success) {
                service.getDebug().log("Supplier: Request success");
            } else {
                service.getDebug().log("Supplier: Request failed");
            }
        }
        if (success) {
            Integer currentRequest = _requestedItems.get(need.getKey());
            if (currentRequest == null) {
                _requestedItems.put(need.getKey(), neededCount);
                service.getDebug().log("Supplier: Inserting Requested Items: " + neededCount);
            } else {
                _requestedItems.put(need.getKey(), currentRequest + neededCount);
                service.getDebug().log("Supplier: Raising Requested Items from: " + currentRequest + " to: " + currentRequest + neededCount);
            }
        } else {
            setRequestFailed(true);
        }
    }
}
Also used : ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) HashMap(java.util.HashMap) IPipeServiceProvider(logisticspipes.interfaces.IPipeServiceProvider) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation)

Example 9 with IPipeServiceProvider

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

the class ModuleActiveSupplier method createPatternRequest.

private void createPatternRequest(IInventoryUtil invUtil) {
    final IPipeServiceProvider service = Objects.requireNonNull(_service);
    service.getDebug().log("Supplier: Start calculating pattern request");
    setRequestFailed(false);
    for (int i = 0; i < SUPPLIER_SLOTS; i++) {
        ItemIdentifierStack needed = inventory.getIDStackInSlot(i);
        if (needed == null) {
            continue;
        }
        final Integer slotAssignedTo = slotAssignmentPattern.get(i);
        if (invUtil.getSizeInventory() <= slotAssignedTo) {
            continue;
        }
        ItemStack stack = invUtil.getStackInSlot(slotAssignedTo);
        ItemIdentifierStack have = null;
        if (!stack.isEmpty()) {
            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.getValue() == PatternMode.Bulk50 && haveCount > needed.getStackSize() / 2) || (patternMode.getValue() == 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(slotAssignedTo, needed.getStackSize());
        if (patternMode.getValue() != 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 : IPipeServiceProvider(logisticspipes.interfaces.IPipeServiceProvider) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) ItemStack(net.minecraft.item.ItemStack) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation)

Example 10 with IPipeServiceProvider

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

the class ModuleActiveSupplier method decreaseRequested.

private void decreaseRequested(ItemIdentifierStack item) {
    final IPipeServiceProvider service = Objects.requireNonNull(_service);
    int remaining = item.getStackSize();
    // see if we can get an exact match
    Integer count = _requestedItems.get(item.getItem());
    if (count != null) {
        service.getDebug().log("Supplier: Exact match. Still missing: " + Math.max(0, count - remaining));
        if (count - remaining > 0) {
            _requestedItems.put(item.getItem(), count - remaining);
        } else {
            _requestedItems.remove(item.getItem());
        }
        remaining -= count;
    }
    if (remaining <= 0) {
        return;
    }
    // still remaining... was from fuzzyMatch on a crafter
    Iterator<Entry<ItemIdentifier, Integer>> it = _requestedItems.entrySet().iterator();
    while (it.hasNext()) {
        Entry<ItemIdentifier, Integer> e = it.next();
        if (e.getKey().equalsWithoutNBT(item.getItem())) {
            int expected = e.getValue();
            service.getDebug().log("Supplier: Fuzzy match with" + e + ". Still missing: " + Math.max(0, expected - remaining));
            if (expected - remaining > 0) {
                e.setValue(expected - remaining);
            } else {
                it.remove();
            }
            remaining -= expected;
        }
        if (remaining <= 0) {
            return;
        }
    }
    // we have no idea what this is, log it.
    service.getDebug().log("Supplier: supplier got unexpected item " + item);
}
Also used : Entry(java.util.Map.Entry) StatusEntry(logisticspipes.pipes.basic.debug.StatusEntry) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IPipeServiceProvider(logisticspipes.interfaces.IPipeServiceProvider)

Aggregations

IPipeServiceProvider (logisticspipes.interfaces.IPipeServiceProvider)27 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)14 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)14 ItemStack (net.minecraft.item.ItemStack)13 Nonnull (javax.annotation.Nonnull)11 SinkReply (logisticspipes.utils.SinkReply)10 IAdditionalTargetInformation (logisticspipes.interfaces.routing.IAdditionalTargetInformation)9 DictResource (logisticspipes.request.resources.DictResource)9 List (java.util.List)8 Objects (java.util.Objects)8 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)8 ISlotUpgradeManager (logisticspipes.interfaces.ISlotUpgradeManager)8 LogisticsItemOrder (logisticspipes.routing.order.LogisticsItemOrder)8 Pair (logisticspipes.utils.tuples.Pair)8 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 ItemResource (logisticspipes.request.resources.ItemResource)7 Property (network.rs485.logisticspipes.property.Property)7 ImmutableList (com.google.common.collect.ImmutableList)6 Map (java.util.Map)6