Example usage for net.minecraftforge.items ItemHandlerHelper copyStackWithSize

List of usage examples for net.minecraftforge.items ItemHandlerHelper copyStackWithSize

Introduction

In this page you can find the example usage for net.minecraftforge.items ItemHandlerHelper copyStackWithSize.

Prototype

@Nonnull
    public static ItemStack copyStackWithSize(@Nonnull ItemStack itemStack, int size) 

Source Link

Usage

From source file:com.teambr.bookshelf.common.items.InventoryHandlerItem.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into.//from  w  w  w .j a v  a 2s .c o m
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 * May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    checkStackTag();
    if (isItemValidForSlot(slot, stack) || stack.isEmpty() || !isValidSlot(slot))
        return stack;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.setCount(existing.getCount() + (reachedLimit ? limit : stack.getCount()));
        }
        onInventoryChanged(slot);
    }

    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:com.teambr.bookshelf.common.items.InventoryHandlerItem.java

License:Creative Commons License

/**
 * Extracts an ItemStack from the given slot. The returned value must be null
 * if nothing is extracted, otherwise it's stack size must not be greater than amount or the
 * itemstacks getMaxStackSize()./*  w  ww.  j  ava2s . c  o  m*/
 *
 * @param slot     Slot to extract from.
 * @param amount   Amount to extract (may be greater than the current stacks max limit)
 * @param simulate If true, the extraction is only simulated
 * @return ItemStack extracted from the slot, must be null, if nothing can be extracted
 **/
@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
    checkStackTag();
    if (amount == 0)
        return ItemStack.EMPTY;

    if (!isValidSlot(slot))
        return ItemStack.EMPTY;
    ItemStack existing = this.inventoryContents.get(slot);

    if (existing.isEmpty())
        return ItemStack.EMPTY;

    int toExtract = Math.min(amount, existing.getMaxStackSize());

    if (existing.getCount() <= toExtract) {
        if (!simulate) {
            this.inventoryContents.set(slot, ItemStack.EMPTY);
            onInventoryChanged(slot);
        }
        return existing;
    } else {
        if (!simulate) {
            this.inventoryContents.set(slot,
                    ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
            onInventoryChanged(slot);
        }

        return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
    }
}

From source file:com.teambr.bookshelf.common.tiles.InventoryHandler.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into./*from  ww  w  . j  a  v  a2s. c o m*/
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 *         May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (!isItemValidForSlot(slot, stack))
        return stack;

    if (stack.isEmpty() || !isValidSlot(slot))
        return stack;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.setCount(existing.getCount() + (reachedLimit ? limit : stack.getCount()));
        }
        onInventoryChanged(slot);
    }

    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:com.teambr.bookshelf.common.tiles.InventoryHandler.java

License:Creative Commons License

/**
 * Extracts an ItemStack from the given slot. The returned value must be null
 * if nothing is extracted, otherwise it's stack size must not be greater than amount or the
 * itemstacks getMaxStackSize()./*from w ww . j av a2s  .  c o  m*/
 *
 * @param slot     Slot to extract from.
 * @param amount   Amount to extract (may be greater than the current stacks max limit)
 * @param simulate If true, the extraction is only simulated
 * @return ItemStack extracted from the slot, must be null, if nothing can be extracted
 **/
@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
    if (amount == 0)
        return ItemStack.EMPTY;

    if (!isValidSlot(slot))
        return ItemStack.EMPTY;
    ItemStack existing = this.inventoryContents.get(slot);

    if (existing.isEmpty())
        return ItemStack.EMPTY;

    int toExtract = Math.min(amount, existing.getMaxStackSize());

    if (existing.getCount() <= toExtract) {
        if (!simulate) {
            this.inventoryContents.set(slot, ItemStack.EMPTY);
            onInventoryChanged(slot);
        }
        return existing;
    } else {
        if (!simulate) {
            this.inventoryContents.set(slot,
                    ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
            onInventoryChanged(slot);
        }

        return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
    }
}

From source file:com.teambrmodding.neotech.common.tiles.AbstractMachine.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into.//from w ww  . ja  v  a2s  . co  m
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 *         May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (stack.isEmpty() || !isItemValidForSlot(slot, stack))
        return ItemStack.EMPTY;

    if (!isValidSlot(slot))
        return ItemStack.EMPTY;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.grow(reachedLimit ? limit : stack.getCount());
        }
        onInventoryChanged(slot);
    }
    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:com.teambrmodding.neotech.common.tiles.storage.TileEnergyStorage.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into./*w  w  w  .  j a v  a2 s . c  o  m*/
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 *         May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (stack == null || stack.getCount() == 0 || !isItemValidForSlot(slot, stack))
        return ItemStack.EMPTY;

    if (!isValidSlot(slot))
        return ItemStack.EMPTY;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.setCount(existing.getCount() + (reachedLimit ? limit : stack.getCount()));
        }
        onInventoryChanged(slot);
    }

    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:daxum.temporalconvergence.recipes.TimeChestRecipes.java

License:Open Source License

public static void addConversion(ItemStack input, ItemStack output, int time) {
    if (input.isEmpty() || output.isEmpty() || time <= 0) {
        TemporalConvergence.LOGGER.warn("Attempted to register invalid time chest conversion: {} -> {} : {}",
                input, output, time);//from  w w w  .  j  ava2  s.com
        return;
    }

    RECIPES.put(new HashableStack(ItemHandlerHelper.copyStackWithSize(input, 1)),
            new TimeChestRecipe(output.copy(), time));
}

From source file:daxum.temporalconvergence.recipes.TimeChestRecipes.java

License:Open Source License

public static ItemStack getOutput(ItemStack input) {
    HashableStack adjustedInput = new HashableStack(ItemHandlerHelper.copyStackWithSize(input, 1));

    if (RECIPES.containsKey(adjustedInput)) {
        return RECIPES.get(adjustedInput).output;
    }//  www.  ja  v  a  2  s.  c o  m

    return ItemStack.EMPTY;
}

From source file:daxum.temporalconvergence.recipes.TimeChestRecipes.java

License:Open Source License

public static int getTime(ItemStack input) {
    HashableStack adjustedInput = new HashableStack(ItemHandlerHelper.copyStackWithSize(input, 1));

    if (RECIPES.containsKey(adjustedInput)) {
        return RECIPES.get(adjustedInput).time;
    }/*www . j  ava2  s. co  m*/

    return -1;
}

From source file:daxum.temporalconvergence.recipes.TimeFurnaceRecipes.java

License:Open Source License

public static void addRecipe(ItemStack input, PowerRequirements requirements, ItemStack output, int smeltTime) {
    if (!input.isEmpty()) {
        if (smeltTime > 0) {
            RECIPES.add(new TimeFurnaceRecipe(ItemHandlerHelper.copyStackWithSize(input, 1), requirements,
                    ItemHandlerHelper.copyStackWithSize(output, 1), smeltTime));
        } else {//from   w  ww. j  av  a2 s .  c  o  m
            TemporalConvergence.LOGGER.error("Smelt time must be greater than 0");
        }
    } else {
        TemporalConvergence.LOGGER.error("Attempted to register empty input to time furnace recipe");
    }
}