Search in sources :

Example 11 with ItemIdentifier

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

the class ModuleActiveSupplier method createSupplyRequest.

private void createSupplyRequest(IInventoryUtil invUtil) {
    _service.getDebug().log("Supplier: Start calculating supply request");
    //How many do I want?
    HashMap<ItemIdentifier, Integer> needed = new HashMap<>(dummyInventory.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()) {
        Integer n = haveUndamaged.get(item.getKey().getUndamaged());
        if (n == null) {
            haveUndamaged.put(item.getKey().getUndamaged(), item.getValue());
        } else {
            haveUndamaged.put(item.getKey().getUndamaged(), item.getValue() + n);
        }
    }
    //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());
        if (_requestMode == SupplyMode.Infinite) {
            Integer requestedCount = _requestedItems.get(item.getKey());
            if (requestedCount != null) {
                spaceAvailable -= requestedCount;
            }
            item.setValue(Math.min(item.getKey().getMaxStackSize(), spaceAvailable));
            continue;
        }
        if (spaceAvailable == 0 || (_requestMode == SupplyMode.Bulk50 && haveCount > item.getValue() / 2) || (_requestMode == 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 != 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) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation)

Example 12 with ItemIdentifier

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

the class ModuleAdvancedExtractor method checkExtract.

private void checkExtract(IInventoryUtil invUtil) {
    Map<ItemIdentifier, Integer> items = invUtil.getItemsAndCount();
    for (Entry<ItemIdentifier, Integer> item : items.entrySet()) {
        if (!CanExtract(item.getKey().makeNormalStack(item.getValue()))) {
            continue;
        }
        List<Integer> jamList = new LinkedList<>();
        Pair<Integer, SinkReply> reply = _service.hasDestination(item.getKey(), true, jamList);
        if (reply == null) {
            continue;
        }
        int itemsleft = itemsToExtract();
        while (reply != null) {
            int count = Math.min(itemsleft, item.getValue());
            count = Math.min(count, item.getKey().getMaxStackSize());
            if (reply.getValue2().maxNumberOfItems > 0) {
                count = Math.min(count, reply.getValue2().maxNumberOfItems);
            }
            while (!_service.useEnergy(neededEnergy() * count) && count > 0) {
                _service.spawnParticle(Particles.OrangeParticle, 2);
                count--;
            }
            if (count <= 0) {
                break;
            }
            ItemStack stackToSend = invUtil.getMultipleItems(item.getKey(), count);
            if (stackToSend == null || stackToSend.stackSize == 0) {
                break;
            }
            count = stackToSend.stackSize;
            _service.sendStack(stackToSend, reply, itemSendMode());
            itemsleft -= count;
            if (itemsleft <= 0) {
                break;
            }
            jamList.add(reply.getValue1());
            reply = _service.hasDestination(item.getKey(), true, jamList);
        }
        return;
    }
}
Also used : ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) SinkReply(logisticspipes.utils.SinkReply) ItemStack(net.minecraft.item.ItemStack) LinkedList(java.util.LinkedList)

Example 13 with ItemIdentifier

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

the class ModuleProvider method canProvide.

@Override
public void canProvide(RequestTreeNode tree, RequestTree root, List<IFilter> filters) {
    List<ItemIdentifier> possible = new ArrayList<>();
    if (tree.getRequestType() instanceof ItemResource) {
        possible.add(((ItemResource) tree.getRequestType()).getItem());
    } else if (tree.getRequestType() instanceof DictResource) {
        IInventoryUtil inv = _service.getPointedInventory(_extractionMode, true);
        if (inv != null) {
            Map<ItemIdentifier, Integer> currentInv = inv.getItemsAndCount();
            possible.addAll(currentInv.keySet().stream().filter(item -> tree.getRequestType().matches(item, IResource.MatchSettings.NORMAL)).collect(Collectors.toList()));
        }
    }
    for (ItemIdentifier item : possible) {
        int canProvide = getAvailableItemCount(item);
        canProvide -= root.getAllPromissesFor((IProvideItems) _service, item);
        canProvide = Math.min(canProvide, tree.getMissingAmount());
        if (canProvide < 1) {
            return;
        }
        LogisticsPromise promise = new LogisticsPromise(item, canProvide, (IProvideItems) _service, ResourceType.PROVIDER);
        tree.addPromise(promise);
    }
}
Also used : ModuleInHandGuiProvider(logisticspipes.network.abstractguis.ModuleInHandGuiProvider) IRouter(logisticspipes.routing.IRouter) LogisticsModule(logisticspipes.modules.abstractmodules.LogisticsModule) Particles(logisticspipes.pipefxhandlers.Particles) CCType(logisticspipes.proxy.computers.interfaces.CCType) MainProxy(logisticspipes.proxy.MainProxy) PlayerCollectionList(logisticspipes.utils.PlayerCollectionList) SinkReply(logisticspipes.utils.SinkReply) HUDStopModuleWatchingPacket(logisticspipes.network.packets.hud.HUDStopModuleWatchingPacket) Map(java.util.Map) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand) IAdditionalTargetInformation(logisticspipes.interfaces.routing.IAdditionalTargetInformation) IIconRegister(net.minecraft.client.renderer.texture.IIconRegister) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IProvideItems(logisticspipes.interfaces.routing.IProvideItems) NewGuiHandler(logisticspipes.network.NewGuiHandler) Side(cpw.mods.fml.relauncher.Side) SideOnly(cpw.mods.fml.relauncher.SideOnly) Collection(java.util.Collection) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) LogisticsManager(logisticspipes.logistics.LogisticsManager) ItemIdentifierInventory(logisticspipes.utils.item.ItemIdentifierInventory) IModuleInventoryReceive(logisticspipes.interfaces.IModuleInventoryReceive) PacketHandler(logisticspipes.network.PacketHandler) RequestTreeNode(logisticspipes.request.RequestTreeNode) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) DictResource(logisticspipes.request.resources.DictResource) Collectors(java.util.stream.Collectors) ProviderModuleInHand(logisticspipes.network.guis.module.inhand.ProviderModuleInHand) List(java.util.List) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) IFilter(logisticspipes.interfaces.routing.IFilter) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Entry(java.util.Map.Entry) IHUDModuleHandler(logisticspipes.interfaces.IHUDModuleHandler) LogisticsSneakyDirectionModule(logisticspipes.modules.abstractmodules.LogisticsSneakyDirectionModule) ItemResource(logisticspipes.request.resources.ItemResource) ModuleCoordinatesGuiProvider(logisticspipes.network.abstractguis.ModuleCoordinatesGuiProvider) LogisticsOrder(logisticspipes.routing.order.LogisticsOrder) ResourceType(logisticspipes.routing.order.IOrderInfoProvider.ResourceType) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) IClientInformationProvider(logisticspipes.interfaces.IClientInformationProvider) IRequestItems(logisticspipes.interfaces.routing.IRequestItems) ProviderModuleGuiProvider(logisticspipes.network.guis.module.inpipe.ProviderModuleGuiProvider) IModuleWatchReciver(logisticspipes.interfaces.IModuleWatchReciver) ExtractionMode(logisticspipes.logisticspipes.ExtractionMode) ExtractorModuleMode(logisticspipes.network.packets.modules.ExtractorModuleMode) IHUDModuleRenderer(logisticspipes.interfaces.IHUDModuleRenderer) LogisticsItemOrder(logisticspipes.routing.order.LogisticsItemOrder) LogisticsPromise(logisticspipes.routing.LogisticsPromise) ItemSendMode(logisticspipes.pipes.basic.CoreRoutedPipe.ItemSendMode) RequestTree(logisticspipes.request.RequestTree) HUDProviderModule(logisticspipes.gui.hud.modules.HUDProviderModule) IIcon(net.minecraft.util.IIcon) HUDStartModuleWatchingPacket(logisticspipes.network.packets.hud.HUDStartModuleWatchingPacket) IResource(logisticspipes.request.resources.IResource) ModuleInventory(logisticspipes.network.packets.module.ModuleInventory) TreeMap(java.util.TreeMap) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) IInventory(net.minecraft.inventory.IInventory) ILegacyActiveModule(logisticspipes.interfaces.ILegacyActiveModule) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IProvideItems(logisticspipes.interfaces.routing.IProvideItems) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) ArrayList(java.util.ArrayList) DictResource(logisticspipes.request.resources.DictResource) LogisticsPromise(logisticspipes.routing.LogisticsPromise) ItemResource(logisticspipes.request.resources.ItemResource) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 14 with ItemIdentifier

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

the class ModuleSatellite method spaceFor.

private int spaceFor(ItemIdentifier item, boolean includeInTransit) {
    WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(pipe.container);
    //@formatter:off
    int count = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> {
        IInventory inv = (IInventory) adjacent.tileEntity;
        if (inv instanceof net.minecraft.inventory.ISidedInventory) {
            inv = new SidedInventoryMinecraftAdapter((net.minecraft.inventory.ISidedInventory) inv, adjacent.direction.getOpposite(), false);
        }
        IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, adjacent.direction);
        return util.roomForItem(item, 9999);
    }).reduce(Integer::sum).orElse(0);
    if (includeInTransit) {
        count -= pipe.countOnRoute(item);
    }
    return count;
}
Also used : IIconRegister(net.minecraft.client.renderer.texture.IIconRegister) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Side(cpw.mods.fml.relauncher.Side) ConnectionPipeType(logisticspipes.routing.pathfinder.IPipeInformationProvider.ConnectionPipeType) SideOnly(cpw.mods.fml.relauncher.SideOnly) LogisticsModule(logisticspipes.modules.abstractmodules.LogisticsModule) Collection(java.util.Collection) FixedPriority(logisticspipes.utils.SinkReply.FixedPriority) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IPipeServiceProvider(logisticspipes.interfaces.IPipeServiceProvider) IIcon(net.minecraft.util.IIcon) SinkReply(logisticspipes.utils.SinkReply) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) SidedInventoryMinecraftAdapter(logisticspipes.utils.SidedInventoryMinecraftAdapter) IWorldProvider(logisticspipes.interfaces.IWorldProvider) IInventory(net.minecraft.inventory.IInventory) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) IInventory(net.minecraft.inventory.IInventory) SidedInventoryMinecraftAdapter(logisticspipes.utils.SidedInventoryMinecraftAdapter) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper)

Example 15 with ItemIdentifier

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

the class ModuleCrafter method extractFromIInventory.

private ItemStack extractFromIInventory(IInventory inv, IResource wanteditem, int count, ForgeDirection dir) {
    IInventoryUtil invUtil = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, dir);
    ItemIdentifier itemToExtract = null;
    if (wanteditem instanceof ItemResource) {
        itemToExtract = ((ItemResource) wanteditem).getItem();
    } else if (wanteditem instanceof DictResource) {
        int max = Integer.MIN_VALUE;
        ItemIdentifier toExtract = null;
        for (Map.Entry<ItemIdentifier, Integer> content : invUtil.getItemsAndCount().entrySet()) {
            if (wanteditem.matches(content.getKey(), IResource.MatchSettings.NORMAL)) {
                if (content.getValue() > max) {
                    max = content.getValue();
                    toExtract = content.getKey();
                }
            }
        }
        if (toExtract == null) {
            return null;
        }
        itemToExtract = toExtract;
    }
    int available = invUtil.itemCount(itemToExtract);
    if (available == 0) {
        return null;
    }
    if (!_service.useEnergy(neededEnergy() * Math.min(count, available))) {
        return null;
    }
    return invUtil.getMultipleItems(itemToExtract, Math.min(count, available));
}
Also used : ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IInventoryUtil(logisticspipes.interfaces.IInventoryUtil) DictResource(logisticspipes.request.resources.DictResource) ItemResource(logisticspipes.request.resources.ItemResource)

Aggregations

ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)69 ItemStack (net.minecraft.item.ItemStack)37 ArrayList (java.util.ArrayList)23 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)23 HashMap (java.util.HashMap)14 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)13 TreeSet (java.util.TreeSet)12 IFilter (logisticspipes.interfaces.routing.IFilter)11 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)11 LinkedList (java.util.LinkedList)10 List (java.util.List)10 Map (java.util.Map)10 SinkReply (logisticspipes.utils.SinkReply)10 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)9 IRouter (logisticspipes.routing.IRouter)9 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)9 BitSet (java.util.BitSet)8 Entry (java.util.Map.Entry)7 LogisticsModule (logisticspipes.modules.abstractmodules.LogisticsModule)7 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)7