Search in sources :

Example 1 with StackKey

use of mods.railcraft.common.util.collections.StackKey in project Railcraft by Railcraft.

the class InvTools method findMatchingItems.

/**
     * Returns all items from the inventory that match the
     * filter, but does not remove them.
     * The resulting set will be populated with a single instance of each item type.
     *
     * @param inv    the inventory    The inventory
     * @param filter EnumItemType to match against
     * @return A Set of ItemStacks
     */
@Nonnull
public static Set<StackKey> findMatchingItems(IInventoryComposite inv, Predicate<ItemStack> filter) {
    Set<StackKey> items = CollectionTools.createItemStackSet();
    for (IInventoryObject inventoryObject : inv) {
        for (IInvSlot slot : InventoryIterator.getRailcraft(inventoryObject)) {
            ItemStack stack = slot.getStack();
            if (!isEmpty(stack) && filter.test(stack)) {
                stack = stack.copy();
                stack.stackSize = 1;
                items.add(StackKey.make(stack));
            }
        }
    }
    return items;
}
Also used : IInvSlot(mods.railcraft.common.util.inventory.iterators.IInvSlot) IInventoryObject(mods.railcraft.common.util.inventory.wrappers.IInventoryObject) StackKey(mods.railcraft.common.util.collections.StackKey) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 2 with StackKey

use of mods.railcraft.common.util.collections.StackKey in project Railcraft by Railcraft.

the class InvTools method populateManifest.

private static void populateManifest(Multiset<StackKey> manifest, IInventoryObject inv) {
    for (ItemStack stack : InventoryIterator.getRailcraft(inv).getStacks()) {
        StackKey key = StackKey.make(stack);
        manifest.add(key, stack.stackSize);
    }
}
Also used : StackKey(mods.railcraft.common.util.collections.StackKey) ItemStack(net.minecraft.item.ItemStack)

Example 3 with StackKey

use of mods.railcraft.common.util.collections.StackKey in project Railcraft by Railcraft.

the class InvTools method createManifest.

/**
     * Returns a Multiset that lists the total
     * number of each type of item in the inventory.
     *
     * @param invs the inventories to generate the manifest for
     * @return A <code>Multiset</code> that lists how many of each item is in the inventories
     */
@Nonnull
public static Multiset<StackKey> createManifest(IInventoryComposite invs, Collection<StackKey> manifestEntries) {
    Multiset<StackKey> manifest = HashMultiset.create();
    for (StackKey entry : manifestEntries) {
        Predicate<ItemStack> filter = StackFilters.matches(entry.get());
        invs.forEach(inv -> {
            for (ItemStack stack : InventoryIterator.getRailcraft(inv).getStacks()) {
                if (filter.test(stack))
                    manifest.add(entry, stack.stackSize);
            }
        });
    }
    return manifest;
}
Also used : StackKey(mods.railcraft.common.util.collections.StackKey) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 4 with StackKey

use of mods.railcraft.common.util.collections.StackKey in project Railcraft by Railcraft.

the class TileItemManipulator method processCart.

@Override
protected void processCart(EntityMinecart cart) {
    chests.clear();
    chests.add(invBuffer);
    chests.addAll(invCache.getAdjacentInventories());
    InventoryComposite cartInv = InventoryComposite.of(InventoryFactory.get(cart, getFacing().getOpposite()));
    if (cartInv.isEmpty()) {
        sendCart(cart);
        return;
    }
    this.cart = cartInv;
    switch(getMode()) {
        case TRANSFER:
            {
                Multiset<StackKey> filterManifest = InvTools.createManifest(getItemFilters());
                filterManifest.entrySet().stream().filter(entry -> transferredItems.count(entry.getElement()) < entry.getCount()).anyMatch(entry -> {
                    ItemStack moved = InvTools.moveOneItem(getSource(), getDestination(), StackFilters.matches(entry.getElement().get()));
                    if (!InvTools.isEmpty(moved)) {
                        setProcessing(true);
                        transferredItems.add(entry.getElement());
                        return true;
                    }
                    return false;
                });
                break;
            }
        case STOCK:
            {
                Multiset<StackKey> filterManifest = InvTools.createManifest(getItemFilters());
                Multiset<StackKey> destManifest = InvTools.createManifest(getDestination(), filterManifest.elementSet());
                moveItem(filterManifest.entrySet().stream().filter(entry -> destManifest.count(entry.getElement()) < entry.getCount()));
                break;
            }
        case EXCESS:
            {
                Multiset<StackKey> filterManifest = InvTools.createManifest(getItemFilters());
                Multiset<StackKey> sourceManifest = InvTools.createManifest(getSource(), filterManifest.elementSet());
                moveItem(filterManifest.entrySet().stream().filter(entry -> sourceManifest.count(entry.getElement()) > entry.getCount()));
                if (!isProcessing()) {
                    Predicate<ItemStack> keep = filterManifest.elementSet().stream().map(e -> StackFilters.matches(e.get())).reduce(StackFilters.none(), Predicate::or);
                    ItemStack moved = InvTools.moveOneItemExcept(getSource(), getDestination(), keep);
                    itemMoved(moved);
                }
                break;
            }
        case ALL:
            {
                Multiset<StackKey> filterManifest = InvTools.createManifest(getItemFilters());
                if (filterManifest.isEmpty()) {
                    ItemStack moved = InvTools.moveOneItem(getSource(), getDestination());
                    itemMoved(moved);
                } else {
                    moveItem(filterManifest.entrySet().stream());
                }
                break;
            }
    }
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IInventoryObject(mods.railcraft.common.util.inventory.wrappers.IInventoryObject) EnumMap(java.util.EnumMap) Multiset(com.google.common.collect.Multiset) Predicate(java.util.function.Predicate) EntityMinecart(net.minecraft.entity.item.EntityMinecart) IOException(java.io.IOException) RailcraftInputStream(mods.railcraft.common.util.network.RailcraftInputStream) StackFilters(mods.railcraft.common.util.inventory.filters.StackFilters) InventoryMapper(mods.railcraft.common.util.inventory.wrappers.InventoryMapper) RailcraftOutputStream(mods.railcraft.common.util.network.RailcraftOutputStream) ItemStack(net.minecraft.item.ItemStack) InventoryComposite(mods.railcraft.common.util.inventory.wrappers.InventoryComposite) Stream(java.util.stream.Stream) HashMultiset(com.google.common.collect.HashMultiset) StackKey(mods.railcraft.common.util.collections.StackKey) MultiButtonController(mods.railcraft.common.gui.buttons.MultiButtonController) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Slot(net.minecraft.inventory.Slot) mods.railcraft.common.util.inventory(mods.railcraft.common.util.inventory) IInventoryComposite(mods.railcraft.common.util.inventory.wrappers.IInventoryComposite) Nullable(javax.annotation.Nullable) Multiset(com.google.common.collect.Multiset) HashMultiset(com.google.common.collect.HashMultiset) InventoryComposite(mods.railcraft.common.util.inventory.wrappers.InventoryComposite) IInventoryComposite(mods.railcraft.common.util.inventory.wrappers.IInventoryComposite) ItemStack(net.minecraft.item.ItemStack) Predicate(java.util.function.Predicate)

Aggregations

StackKey (mods.railcraft.common.util.collections.StackKey)4 ItemStack (net.minecraft.item.ItemStack)4 Nonnull (javax.annotation.Nonnull)2 IInventoryObject (mods.railcraft.common.util.inventory.wrappers.IInventoryObject)2 HashMultiset (com.google.common.collect.HashMultiset)1 Multiset (com.google.common.collect.Multiset)1 IOException (java.io.IOException)1 EnumMap (java.util.EnumMap)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 MultiButtonController (mods.railcraft.common.gui.buttons.MultiButtonController)1 mods.railcraft.common.util.inventory (mods.railcraft.common.util.inventory)1 StackFilters (mods.railcraft.common.util.inventory.filters.StackFilters)1 IInvSlot (mods.railcraft.common.util.inventory.iterators.IInvSlot)1 IInventoryComposite (mods.railcraft.common.util.inventory.wrappers.IInventoryComposite)1 InventoryComposite (mods.railcraft.common.util.inventory.wrappers.InventoryComposite)1 InventoryMapper (mods.railcraft.common.util.inventory.wrappers.InventoryMapper)1 RailcraftInputStream (mods.railcraft.common.util.network.RailcraftInputStream)1 RailcraftOutputStream (mods.railcraft.common.util.network.RailcraftOutputStream)1