Search in sources :

Example 1 with StackKey

use of buildcraft.api.core.StackKey in project BuildCraft by BuildCraft.

the class BptBuilderBlueprint method recomputeNeededItems.

private void recomputeNeededItems() {
    neededItems.clear();
    HashMap<StackKey, Integer> computeStacks = new HashMap<>();
    for (List<BuildingSlotBlock> lb : buildList.values()) {
        for (BuildingSlotBlock slot : lb) {
            if (slot == null) {
                continue;
            }
            List<ItemStack> stacks = new ArrayList<>();
            try {
                stacks = slot.getRequirements(context);
            } catch (Throwable t) {
                // Defensive code against errors in implementers
                t.printStackTrace();
                BCLog.logger.throwing(t);
            }
            for (ItemStack stack : stacks) {
                if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
                    continue;
                }
                StackKey key = new StackKey(stack);
                if (!computeStacks.containsKey(key)) {
                    computeStacks.put(key, stack.stackSize);
                } else {
                    Integer num = computeStacks.get(key);
                    num += stack.stackSize;
                    computeStacks.put(key, num);
                }
            }
        }
    }
    for (BuildingSlotEntity slot : entityList) {
        LinkedList<ItemStack> stacks = new LinkedList<>();
        try {
            stacks = slot.getRequirements(context);
        } catch (Throwable t) {
            // Defensive code against errors in implementers
            t.printStackTrace();
            BCLog.logger.throwing(t);
        }
        for (ItemStack stack : stacks) {
            if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
                continue;
            }
            StackKey key = new StackKey(stack);
            if (!computeStacks.containsKey(key)) {
                computeStacks.put(key, stack.stackSize);
            } else {
                Integer num = computeStacks.get(key);
                num += stack.stackSize;
                computeStacks.put(key, num);
            }
        }
    }
    for (Entry<StackKey, Integer> e : computeStacks.entrySet()) {
        neededItems.add(new RequirementItemStack(e.getKey().stack.copy(), e.getValue()));
    }
    sortNeededItems();
}
Also used : HashMap(java.util.HashMap) StackKey(buildcraft.api.core.StackKey) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock) BuildingSlotEntity(buildcraft.core.builders.BuildingSlotEntity) ItemStack(net.minecraft.item.ItemStack)

Example 2 with StackKey

use of buildcraft.api.core.StackKey in project BuildCraft by BuildCraft.

the class BptBuilderBlueprint method onRemoveBuildingSlotBlock.

protected void onRemoveBuildingSlotBlock(BuildingSlotBlock slot) {
    buildStageOccurences[slot.buildStage]--;
    List<ItemStack> stacks = new ArrayList<>();
    try {
        stacks = slot.getRequirements(context);
    } catch (Throwable t) {
        // Defensive code against errors in implementers
        t.printStackTrace();
        BCLog.logger.throwing(t);
    }
    HashMap<StackKey, Integer> computeStacks = new HashMap<>();
    for (ItemStack stack : stacks) {
        if (stack == null || stack.getItem() == null || stack.stackSize == 0) {
            continue;
        }
        StackKey key = new StackKey(stack);
        if (!computeStacks.containsKey(key)) {
            computeStacks.put(key, stack.stackSize);
        } else {
            Integer num = computeStacks.get(key);
            num += stack.stackSize;
            computeStacks.put(key, num);
        }
    }
    for (RequirementItemStack ris : neededItems) {
        StackKey stackKey = new StackKey(ris.stack);
        if (computeStacks.containsKey(stackKey)) {
            Integer num = computeStacks.get(stackKey);
            if (ris.size <= num) {
                recomputeNeededItems();
                return;
            } else {
                neededItems.set(neededItems.indexOf(ris), new RequirementItemStack(ris.stack, ris.size - num));
            }
        }
    }
    sortNeededItems();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StackKey(buildcraft.api.core.StackKey) ItemStack(net.minecraft.item.ItemStack)

Aggregations

StackKey (buildcraft.api.core.StackKey)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ItemStack (net.minecraft.item.ItemStack)2 BuildingSlotBlock (buildcraft.core.builders.BuildingSlotBlock)1 BuildingSlotEntity (buildcraft.core.builders.BuildingSlotEntity)1 LinkedList (java.util.LinkedList)1