Search in sources :

Example 1 with IDatabaseSaveable

use of com.bluepowermod.api.item.IDatabaseSaveable in project BluePower by Qmunity.

the class TileCircuitDatabase method copy.

/**
     * Returns true of copy succeeded.
     *
     * @param player
     * @param simulate
     * @return
     */
public boolean copy(EntityPlayer player, ItemStack template, ItemStack target, boolean simulate) {
    if (template != null && target != null) {
        if (template.isItemEqual(target)) {
            IDatabaseSaveable saveable = (IDatabaseSaveable) template.getItem();
            if (saveable.canCopy(template, target)) {
                if (!player.capabilities.isCreativeMode) {
                    List<ItemStack> stacksInTemplate = saveable.getItemsOnStack(template);
                    List<ItemStack> stacksInOutput = saveable.getItemsOnStack(target);
                    if (stacksInTemplate == null)
                        stacksInTemplate = new ArrayList<ItemStack>();
                    if (stacksInOutput == null)
                        stacksInOutput = new ArrayList<ItemStack>();
                    List<ItemStack> traversedItems = new ArrayList<ItemStack>();
                    List<ItemStack> allApplicableItems = new ArrayList<ItemStack>();
                    allApplicableItems.addAll(stacksInTemplate);
                    allApplicableItems.addAll(stacksInOutput);
                    for (ItemStack templateStack : allApplicableItems) {
                        boolean alreadyTraversed = false;
                        for (ItemStack traversedItem : traversedItems) {
                            if (traversedItem.isItemEqual(templateStack) && ItemStack.areItemStackTagsEqual(traversedItem, templateStack)) {
                                alreadyTraversed = true;
                                break;
                            }
                        }
                        if (alreadyTraversed)
                            continue;
                        traversedItems.add(templateStack);
                        int count = 0;
                        for (ItemStack stack : stacksInTemplate) {
                            if (stack.isItemEqual(templateStack) && ItemStack.areItemStackTagsEqual(stack, templateStack)) {
                                count += stack.stackSize;
                            }
                        }
                        for (ItemStack stack : stacksInOutput) {
                            if (stack.isItemEqual(templateStack) && ItemStack.areItemStackTagsEqual(stack, templateStack)) {
                                count -= stack.stackSize;
                            }
                        }
                        // if 5 items are inserted to be copied, the required items are x5.
                        count *= target.stackSize;
                        if (count > 0) {
                            // At this point we need assist from the inventory.
                            ItemStack retrievedStack = templateStack.copy();
                            retrievedStack.stackSize = count;
                            retrievedStack = IOHelper.extract(this, ForgeDirection.UNKNOWN, retrievedStack, true, simulate, 2);
                            if (retrievedStack == null || retrievedStack.stackSize < count)
                                return false;
                        } else if (count < 0) {
                            ItemStack returnedStack = templateStack.copy();
                            returnedStack.stackSize = -count;
                            returnedStack = IOHelper.insert(this, returnedStack, ForgeDirection.UNKNOWN, simulate);
                            if (returnedStack != null && !simulate) {
                                IOHelper.spawnItemInWorld(worldObj, returnedStack, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5);
                            }
                        }
                    }
                }
                if (!simulate) {
                    ItemStack copyStack = template.copy();
                    copyStack.stackSize = target.stackSize;
                    copyInventory.setInventorySlotContents(1, copyStack);
                }
                return true;
            }
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) IDatabaseSaveable(com.bluepowermod.api.item.IDatabaseSaveable)

Example 2 with IDatabaseSaveable

use of com.bluepowermod.api.item.IDatabaseSaveable in project BluePower by Qmunity.

the class TileCircuitDatabase method copy.

/**
 * Returns true of copy succeeded.
 *
 * @param player
 * @param simulate
 * @return
 */
public boolean copy(PlayerEntity player, ItemStack template, ItemStack target, boolean simulate) {
    if (!template.isEmpty() && !target.isEmpty()) {
        if (template.sameItem(target)) {
            IDatabaseSaveable saveable = (IDatabaseSaveable) template.getItem();
            if (saveable.canCopy(template, target)) {
                if (!player.isCreative()) {
                    List<ItemStack> stacksInTemplate = saveable.getItemsOnStack(template);
                    List<ItemStack> stacksInOutput = saveable.getItemsOnStack(target);
                    if (stacksInTemplate.isEmpty())
                        stacksInTemplate = new ArrayList<ItemStack>();
                    if (stacksInOutput.isEmpty())
                        stacksInOutput = new ArrayList<ItemStack>();
                    List<ItemStack> traversedItems = new ArrayList<ItemStack>();
                    List<ItemStack> allApplicableItems = new ArrayList<ItemStack>();
                    allApplicableItems.addAll(stacksInTemplate);
                    allApplicableItems.addAll(stacksInOutput);
                    for (ItemStack templateStack : allApplicableItems) {
                        boolean alreadyTraversed = false;
                        for (ItemStack traversedItem : traversedItems) {
                            if (traversedItem.sameItem(templateStack) && ItemStack.tagMatches(traversedItem, templateStack)) {
                                alreadyTraversed = true;
                                break;
                            }
                        }
                        if (alreadyTraversed)
                            continue;
                        traversedItems.add(templateStack);
                        int count = 0;
                        for (ItemStack stack : stacksInTemplate) {
                            if (stack.sameItem(templateStack) && ItemStack.tagMatches(stack, templateStack)) {
                                count += stack.getCount();
                            }
                        }
                        for (ItemStack stack : stacksInOutput) {
                            if (stack.sameItem(templateStack) && ItemStack.tagMatches(stack, templateStack)) {
                                count -= stack.getCount();
                            }
                        }
                        // if 5 items are inserted to be copied, the required items are x5.
                        count *= target.getCount();
                        if (count > 0) {
                            // At this point we need assist from the inventory.
                            ItemStack retrievedStack = templateStack.copy();
                            retrievedStack.setCount(count);
                            retrievedStack = IOHelper.extract(this, null, retrievedStack, true, simulate, 2);
                            if (retrievedStack.isEmpty() || retrievedStack.getCount() < count)
                                return false;
                        } else if (count < 0) {
                            ItemStack returnedStack = templateStack.copy();
                            returnedStack.setCount(-count);
                            returnedStack = IOHelper.insert(this, returnedStack, null, simulate);
                            if (!returnedStack.isEmpty() && !simulate) {
                                IOHelper.spawnItemInWorld(level, returnedStack, worldPosition.getX() + 0.5, worldPosition.getY() + 0.5, worldPosition.getZ() + 0.5);
                            }
                        }
                    }
                }
                if (!simulate) {
                    ItemStack copyStack = template.copy();
                    copyStack.setCount(target.getCount());
                    copyInventory.setItem(1, copyStack);
                }
                return true;
            }
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) IDatabaseSaveable(com.bluepowermod.api.item.IDatabaseSaveable)

Aggregations

IDatabaseSaveable (com.bluepowermod.api.item.IDatabaseSaveable)2 ArrayList (java.util.ArrayList)2 ItemStack (net.minecraft.item.ItemStack)2