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;
}
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);
}
}
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;
}
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;
}
}
}
Aggregations