Java Code Examples for net.minecraft.item.ItemStack#grow()

The following examples show how to use net.minecraft.item.ItemStack#grow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RockItem.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand hand)
{
    ItemStack stack = playerIn.getHeldItem(hand);

    if (!playerIn.abilities.isCreativeMode)
    {
        stack.grow(-1);
    }

    worldIn.playSound(null, playerIn.getPosX(), playerIn.getPosY(), playerIn.getPosZ(),
            SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL,
            0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F));

    if (!worldIn.isRemote)
    {
        RockEntity entity = new RockEntity(worldIn, playerIn, this);
        entity.func_234612_a_(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.5F, 1.0F);
        worldIn.addEntity(entity);
    }

    //playerIn.addStat(StatList.getObjectUseStats(this));

    return new ActionResult<>(ActionResultType.SUCCESS, stack);
}
 
Example 2
Source File: SpellUtils.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Gets a list of items required to create the given list of spell module chains.
 *
 * @param modules The list of spell module chains.
 * @return A list of required items.
 */
public static List<ItemStack> getSpellItems(List<List<ModuleInstance>> modules) {
	Deque<ItemStack> items = new ArrayDeque<>();
	for (List<ModuleInstance> moduleList : modules) {
		for (ModuleInstance module : moduleList) {
			ItemStack stack = module.getItemStack();
			ItemStack last = items.peekLast();
			if (last == null)
				items.add(stack.copy());
			else if (items.peekLast().isItemEqual(stack))
				last.grow(stack.getCount());
			else
				items.add(stack.copy());
		}
		items.add(new ItemStack(CODE_LINE_BREAK));
	}
	items.add(new ItemStack(ModItems.PEARL_NACRE));
	return new ArrayList<>(items);
}
 
Example 3
Source File: ContainerCustomSlotClick.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void leftDoubleClickSlot(int slotNum, EntityPlayer player)
{
    SlotItemHandlerGeneric slot = this.getSlotItemHandler(slotNum);
    ItemStack stackCursor = this.inventoryPlayer.getItemStack();

    if (slot != null && stackCursor.isEmpty() == false && this.canMergeSlot(stackCursor, slot))
    {
        // FIXME add a slot-aware version
        ItemStack stackTmp = InventoryUtils.collectItemsFromInventory(
                slot.getItemHandler(), stackCursor, stackCursor.getMaxStackSize() - stackCursor.getCount(), true);

        if (stackTmp.isEmpty() == false)
        {
            stackCursor.grow(stackTmp.getCount());
            this.inventoryPlayer.setItemStack(stackCursor);
        }
    }
}
 
Example 4
Source File: GTHelperStack.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Attempts to sort/merge similar itemstacks in an inventory **/
public static void tryCondenseInventory(TileEntityMachine tile, int startSlot, int endSlot) {
	for (int i = startSlot; i < endSlot; ++i) {
		for (int j = startSlot; j < endSlot; ++j) {
			if (i == j) {
				continue;
			}
			ItemStack stack1 = tile.inventory.get(i);
			ItemStack stack2 = tile.inventory.get(j);
			if (!stack1.isEmpty() && !stack2.isEmpty()
					&& (GTHelperStack.isEqual(stack1, stack2) && stack1.getCount() < stack1.getMaxStackSize())) {
				int max = stack1.getMaxStackSize() - stack1.getCount();
				int available = stack2.getCount();
				int size = GTHelperMath.clip(available, 1, max);
				stack1.grow(size);
				stack2.shrink(size);
			}
			if (stack2.isEmpty() && !stack1.isEmpty() && j < i) {
				tile.inventory.set(j, stack1.copy());
				tile.inventory.set(i, ItemStack.EMPTY);
			}
		}
	}
}
 
Example 5
Source File: MetaTileEntityQuantumChest.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void update() {
    super.update();
    if (!getWorld().isRemote) {
        if (itemsStoredInside < maxStoredItems) {
            ItemStack inputStack = importItems.getStackInSlot(0);
            if (!inputStack.isEmpty() && (itemStack.isEmpty() || areItemStackIdentical(itemStack, inputStack))) {
                int amountOfItemsToInsert = (int) Math.min(inputStack.getCount(), maxStoredItems - itemsStoredInside);
                if (this.itemsStoredInside == 0L || itemStack.isEmpty()) {
                    this.itemStack = GTUtility.copyAmount(1, inputStack);
                }
                inputStack.shrink(amountOfItemsToInsert);
                importItems.setStackInSlot(0, inputStack);
                this.itemsStoredInside += amountOfItemsToInsert;
                markDirty();
            }
        }
        if (itemsStoredInside > 0 && !itemStack.isEmpty()) {
            ItemStack outputStack = exportItems.getStackInSlot(0);
            int maxStackSize = itemStack.getMaxStackSize();
            if (outputStack.isEmpty() || (areItemStackIdentical(itemStack, outputStack) && outputStack.getCount() < maxStackSize)) {
                int amountOfItemsToRemove = (int) Math.min(maxStackSize - outputStack.getCount(), itemsStoredInside);
                if (outputStack.isEmpty()) {
                    outputStack = GTUtility.copyAmount(amountOfItemsToRemove, itemStack);
                } else outputStack.grow(amountOfItemsToRemove);
                exportItems.setStackInSlot(0, outputStack);
                this.itemsStoredInside -= amountOfItemsToRemove;
                if (this.itemsStoredInside == 0) {
                    this.itemStack = ItemStack.EMPTY;
                }
                markDirty();
            }
        }
    }
}
 
Example 6
Source File: SlotItemHandlerCraftResult.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ItemStack onTake(EntityPlayer player, ItemStack stack)
{
    this.onCrafting(stack);

    net.minecraftforge.common.ForgeHooks.setCraftingPlayer(player);
    NonNullList<ItemStack> remainingItems = CraftingManager.getRemainingItems(this.craftMatrix, player.getEntityWorld());
    net.minecraftforge.common.ForgeHooks.setCraftingPlayer(null);

    for (int i = 0; i < remainingItems.size(); i++)
    {
        ItemStack stackInSlot = this.craftMatrix.getStackInSlot(i);
        ItemStack remainingItemsInSlot = remainingItems.get(i);

        if (stackInSlot.isEmpty() == false)
        {
            this.craftMatrix.decrStackSize(i, 1);
            stackInSlot = this.craftMatrix.getStackInSlot(i);
        }

        if (remainingItemsInSlot.isEmpty() == false)
        {
            if (stackInSlot.isEmpty())
            {
                this.craftMatrix.setInventorySlotContents(i, remainingItemsInSlot);
            }
            else if (ItemStack.areItemsEqual(stackInSlot, remainingItemsInSlot) &&
                     ItemStack.areItemStackTagsEqual(stackInSlot, remainingItemsInSlot))
            {
                remainingItemsInSlot.grow(stackInSlot.getCount());
                this.craftMatrix.setInventorySlotContents(i, remainingItemsInSlot);
            }
            else if (this.player.inventory.addItemStackToInventory(remainingItemsInSlot) == false)
            {
                this.player.dropItem(remainingItemsInSlot, false);
            }
        }
    }

    return stack;
}
 
Example 7
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param simulate If set to true, no items will actually be inserted
 * @return The number of items unable to be inserted
 */
public static int insertItem(InventoryRange inv, @Nonnull ItemStack stack, boolean simulate) {
    stack = stack.copy();
    for (int pass = 0; pass < 2; pass++) {
        for (int slot : inv.slots) {
            ItemStack base = inv.inv.getStackInSlot(slot);
            if ((pass == 0) == (base.isEmpty())) {
                continue;
            }
            int fit = fitStackInSlot(inv, slot, stack);
            if (fit == 0) {
                continue;
            }

            if (!base.isEmpty()) {
                stack.shrink(fit);
                if (!simulate) {
                    base.grow(fit);
                    inv.inv.setInventorySlotContents(slot, base);
                }
            } else {
                if (!simulate) {
                    inv.inv.setInventorySlotContents(slot, ItemUtils.copyStack(stack, fit));
                }
                stack.shrink(fit);
            }
            if (stack.getCount() == 0) {
                return 0;
            }
        }
    }
    return stack.getCount();
}
 
Example 8
Source File: GTContainerWorktable.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
ItemStack insert(int slot, ItemStack aStack, final int firstSlot, final int lastSlot) {
	ItemStack craftingStack = aStack;
	int curSlot = firstSlot;
	int maxStackSize = craftingStack.getMaxStackSize();
	int count = craftingStack.getCount();
	int room;
	int toDeliver;
	// Try to first insert into same ItemStacks
	while (curSlot <= lastSlot && count > 0) {
		ItemStack slotStack = this.getStackInSlot(curSlot);
		if (craftingStack.isEmpty()) {
			count = 0;
		}
		if (GTHelperStack.isEqual(craftingStack, slotStack) && slotStack.getCount() < maxStackSize) {
			room = maxStackSize - slotStack.getCount();
			toDeliver = Math.min(room, count);
			slotStack.grow(toDeliver);
			this.setStackInSlot(curSlot, slotStack);
			craftingStack.grow(-toDeliver);
			setStackInSlot(slot, craftingStack);
			if (count >= room) {
				count -= room;
			}
		}
		curSlot++;
	}
	curSlot = firstSlot;
	// Try to deliver into empty slot
	while (curSlot <= lastSlot && count > 0) {
		if (this.getStackInSlot(curSlot).isEmpty()) {
			this.setStackInSlot(curSlot, craftingStack.copy());
			craftingStack.grow(-count);
			setStackInSlot(slot, craftingStack);
			count = 0;
		}
		curSlot++;
	}
	return craftingStack;
}
 
Example 9
Source File: GTTileWorktable.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
ItemStack insert(int slot, ItemStack aStack) {
	ItemStack craftingStack = aStack;
	int curSlot = FIRST_SLOT;
	int maxStackSize = craftingStack.getMaxStackSize();
	int count = craftingStack.getCount();
	int room;
	int toDeliver;
	// Try to first insert into same ItemStacks
	while (curSlot <= LAST_SLOT && count > 0) {
		ItemStack slotStack = this.getStackInSlot(curSlot);
		if (craftingStack.isEmpty()) {
			count = 0;
		}
		if (GTHelperStack.isEqual(craftingStack, slotStack) && slotStack.getCount() < maxStackSize) {
			room = maxStackSize - slotStack.getCount();
			toDeliver = Math.min(room, count);
			slotStack.grow(toDeliver);
			this.setStackInSlot(curSlot, slotStack);
			craftingStack.grow(-toDeliver);
			craftingInventory.set(slot, craftingStack);
			if (count >= room) {
				count -= room;
			}
		}
		curSlot++;
	}
	curSlot = FIRST_SLOT;
	// Try to deliver into empty slot
	while (curSlot <= LAST_SLOT && count > 0) {
		if (this.getStackInSlot(curSlot).isEmpty()) {
			this.setStackInSlot(curSlot, craftingStack.copy());
			craftingStack.grow(-count);
			craftingInventory.set(slot, craftingStack);
			count = 0;
		}
		curSlot++;
	}
	return craftingStack;
}
 
Example 10
Source File: GTTileMatterFabricator.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void checkProgress() {
	// If the progress is full, produce a UU-Matter
	ItemStack output = this.inventory.get(8);
	if (progress >= getMaxProgress()) {
		if (output.isEmpty()) {
			this.inventory.set(8, GTMaterialGen.getIc2(Ic2Items.uuMatter, 1));
		} else if (StackUtil.isStackEqual(output, Ic2Items.uuMatter)) {
			output.grow(1);// Do not copy the stack just grow it. Thats why the functions exist. Also
							// prevents cheating.
		}
		// Not wasting extra EU when overcharging.
		progress -= getMaxProgress();
	}
}
 
Example 11
Source File: ItemUtils.java    From TofuCraftReload with MIT License 5 votes vote down vote up
public static void growOrSetInventoryItem(NonNullList<ItemStack> inventory, ItemStack toSet, int index) {
    ItemStack stack = inventory.get(index);
    if (stack.isEmpty()) {
        inventory.set(index, toSet);
    } else {
        stack.grow(toSet.getCount());
    }
}
 
Example 12
Source File: MetaTileEntityChest.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void sortInventorySlotContents(IItemHandlerModifiable inventory) {
    //stack item stacks with equal items and compounds
    for (int i = 0; i < inventory.getSlots(); i++) {
        for (int j = i + 1; j < inventory.getSlots(); j++) {
            ItemStack stack1 = inventory.getStackInSlot(i);
            ItemStack stack2 = inventory.getStackInSlot(j);
            if (!stack1.isEmpty() && ItemStack.areItemsEqual(stack1, stack2) &&
                ItemStack.areItemStackTagsEqual(stack1, stack2)) {
                int maxStackSize = Math.min(stack1.getMaxStackSize(), inventory.getSlotLimit(i));
                int itemsCanAccept = Math.min(stack2.getCount(), maxStackSize - Math.min(stack1.getCount(), maxStackSize));
                if (itemsCanAccept > 0) {
                    stack1.grow(itemsCanAccept);
                    stack2.shrink(itemsCanAccept);
                }
            }
        }
    }
    //create itemstack pairs and sort them out by attributes
    ArrayList<ItemStack> inventoryContents = new ArrayList<>();
    for (int i = 0; i < inventory.getSlots(); i++) {
        ItemStack itemStack = inventory.getStackInSlot(i);
        if (!itemStack.isEmpty()) {
            inventory.setStackInSlot(i, ItemStack.EMPTY);
            inventoryContents.add(itemStack);
        }
    }
    inventoryContents.sort(GTUtility.createItemStackComparator());
    for (int i = 0; i < inventoryContents.size(); i++) {
        inventory.setStackInSlot(i, inventoryContents.get(i));
    }
}
 
Example 13
Source File: TorchFireEventHandling.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SubscribeEvent
public void onAttackEntity(AttackEntityEvent ev)
{
    if (!ConfigManager.SERVER.enableTorchFire.get())
        return;

    if (!ev.getTarget().func_230279_az_() && !ev.getTarget().world.isRemote)
    {
        PlayerEntity player = ev.getPlayer();
        ItemStack stack = player.getHeldItem(Hand.MAIN_HAND);
        if (stack.getCount() > 0 && stack.getItem() instanceof BlockItem)
        {
            BlockItem b = (BlockItem) stack.getItem();
            Block bl = b.getBlock();
            if (bl == Blocks.TORCH)
            {
                ev.getTarget().setFire(2);
                if (!ev.getPlayer().isCreative() && rnd.nextFloat() > 0.25)
                {
                    stack.grow(-1);
                    if (stack.getCount() <= 0)
                    {
                        player.inventory.setInventorySlotContents(player.inventory.currentItem, ItemStack.EMPTY);
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: TileEntityTFOven.java    From TofuCraftReload with MIT License 5 votes vote down vote up
@Override
public void done() {
    //Output outputs in the cached recipe, and do everything needed.
    ItemStack itemstack = this.inventory.get(0);
    ItemStack itemstack1 = FurnaceRecipes.instance().getSmeltingResult(itemstack);
    ItemStack itemstack2 = this.inventory.get(1);

    if (itemstack2.isEmpty()) {
        this.inventory.set(1, itemstack1.copy());
    } else if (itemstack2.getItem() == itemstack1.getItem()) {
        itemstack2.grow(itemstack1.getCount());
    }

    itemstack.shrink(1);
}
 
Example 15
Source File: TileEntityTFCrasher.java    From TofuCraftReload with MIT License 5 votes vote down vote up
@Override
public void done() {
    //Output outputs in the cached recipe, and do everything needed.
    ItemStack itemstack = this.inventory.get(0);
    ItemStack itemstack1 = CrasherRecipes.getResult(this.inventory.get(0));
    ItemStack itemstack2 = this.inventory.get(1);

    if (itemstack2.isEmpty()) {
        this.inventory.set(1, itemstack1.copy());
    } else if (itemstack2.getItem() == itemstack1.getItem()) {
        itemstack2.grow(itemstack1.getCount());
    }

    itemstack.shrink(1);
}
 
Example 16
Source File: ContainerCustomSlotClick.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected boolean takeItemsFromSlotToCursor(SlotItemHandlerGeneric slot, int amount)
{
    ItemStack stackCursor = this.inventoryPlayer.getItemStack();
    ItemStack stackSlot = slot.getStack();

    if (slot.canTakeStack(this.player) == false || stackSlot.isEmpty() ||
        (stackCursor.isEmpty() == false && InventoryUtils.areItemStacksEqual(stackCursor, stackSlot) == false))
    {
        return false;
    }

    amount = Math.min(amount, stackSlot.getMaxStackSize());
    int spaceAvailable = stackSlot.getMaxStackSize();

    if (stackCursor.isEmpty() == false)
    {
        spaceAvailable = stackCursor.getMaxStackSize() - stackCursor.getCount();
    }

    amount = Math.min(amount, spaceAvailable);

    // only allow taking the whole stack from crafting slots
    if (amount <= 0 || ((slot instanceof SlotItemHandlerCraftResult) && spaceAvailable < stackSlot.getCount()))
    {
        return false;
    }

    stackSlot = slot.decrStackSize(amount);

    if (stackSlot.isEmpty() == false)
    {
        slot.onTake(this.player, stackSlot);

        if (stackCursor.isEmpty())
        {
            stackCursor = stackSlot;
        }
        else
        {
            stackCursor.grow(stackSlot.getCount());
        }

        this.inventoryPlayer.setItemStack(stackCursor);

        return true;
    }

    return false;
}
 
Example 17
Source File: TileEntityCampfirePot.java    From Sakura_mod with MIT License 4 votes vote down vote up
@Override
public void update() {
    boolean flag = this.isBurning();
    boolean flag1 = false;

    if (this.isBurning()) {
        --this.burnTime;
    }
    //check can cook
    if (!this.world.isRemote) {
    	if (this.getTank() != null) {

            ArrayList<ItemStack> inventoryList = Lists.newArrayList();
            for (int i = 0; i < 9; i++) {
              if (!this.inventory.get(i).isEmpty()) {
                inventoryList.add(this.inventory.get(i).copy());
              }
            }
    		ItemStack itemstack = this.inventory.get(9);
      if (isRecipes(this.getTank().getFluid(),inventoryList)){
         	ItemStack result = PotRecipes.getInstance().getResultItemStack(this.getTank().getFluid(), inventoryList);
          	FluidStack fluidStack = PotRecipes.getInstance().getResultFluid(this.getTank().getFluid());
          	if(RecipesUtil.canIncrease(result, itemstack)&&isBurning()) {
              cookTime += 1;
          	}else cookTime = 0;
      
       if (cookTime >= maxCookTimer) {
           cookTime = 0;
           if (itemstack.isEmpty()) {
               this.inventory.set(9, result.copy());
           } else if (itemstack.isItemEqual(result)) {
               itemstack.grow(result.getCount());
           }
           
           //If pot is a recipe that uses a liquid, it consumes only that amount of liquid
           if (fluidStack != null && fluidStack.amount>0) {
               this.tank.drain(fluidStack, true);
           }
           
   	    for(int i=0;i<9;i++){
   	    	if(this.inventory.get(i).getCount()==1&&
   	    	this.inventory.get(i).getItem().getContainerItem(this.inventory.get(i))!=null)
   	    		this.inventory.set(i, this.inventory.get(i).getItem().getContainerItem(this.inventory.get(i)).copy());
   	    	else this.decrStackSize(i, 1);
   	    }
           flag1 = true;
       }
       if (flag != this.isBurning()) {
          flag1 = true;
          BlockCampfirePot.setState(this.isBurning(), this.world, this.pos);
       }
       if (flag1)
       	this.markDirty();
      }else cookTime = 0;
    	}
    }
}
 
Example 18
Source File: TileEntityStoneMortar.java    From Sakura_mod with MIT License 4 votes vote down vote up
@Override
public void update() {
    if (this.getWorld().isRemote) {
        return;
    }
	ItemStack input1 = this.inventorySlotItemStack.get(0);
    ItemStack input2 = this.inventorySlotItemStack.get(1);
    ItemStack input3 = this.inventorySlotItemStack.get(2);
    ItemStack input4 = this.inventorySlotItemStack.get(3);
    ItemStack output1 = this.inventorySlotItemStack.get(4);
    ItemStack output2 = this.inventorySlotItemStack.get(5);
  
    ArrayList<ItemStack> inventoryList = Lists.newArrayList();
    for (int i = 0; i < 4; i++) {
      if (!this.inventorySlotItemStack.get(i).isEmpty()) {
        inventoryList.add(this.inventorySlotItemStack.get(i).copy());
      }
    }

    ItemStack[] result = MortarRecipes.instance().getResult(inventoryList);
    if (result.length>0) {
    	if(RecipesUtil.canIncrease(result[0], output1)){
     	if(result.length>1){
     		if(RecipesUtil.canIncrease(result[1], output2))
     			processTimer += 1;
     		else processTimer = 0;
    		}else processTimer += 1;	
    	}else processTimer = 0;
    }else processTimer = 0;

    if (processTimer >= maxprocessTimer) {
        processTimer = 0;
        
        if (output1.isEmpty()) {
            this.inventorySlotItemStack.set(4, result[0].copy());
        } else if (output1.getItem() == result[0].getItem()) {
        	output1.grow(result[0].getCount());
        }
        if(result.length>1){
         if (output2.isEmpty()) {
             this.inventorySlotItemStack.set(5, result[1].copy());
         } else if (output2.getItem() == result[1].getItem()) {
         	output2.grow(result[1].getCount());
         }
        }
        
        input1.shrink(1);
        input2.shrink(1);
        input3.shrink(1);
        input4.shrink(1);
    }
}
 
Example 19
Source File: ContainerCustomSlotClick.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void rightClickSlot(int slotNum, EntityPlayer player)
{
    SlotItemHandlerGeneric slot = this.getSlotItemHandler(slotNum);

    if (slot == null)
    {
        return;
    }

    ItemStack stackCursor = this.inventoryPlayer.getItemStack();
    ItemStack stackSlot = slot.getStack();

    // Items in cursor
    if (stackCursor.isEmpty() == false)
    {
        // Empty slot or identical items: try to add to slot
        if (stackSlot.isEmpty() || InventoryUtils.areItemStacksEqual(stackCursor, stackSlot))
        {
            // Can put items into the slot
            if (slot.isItemValid(stackCursor))
            {
                if (slot.insertItem(stackCursor.splitStack(1), false).isEmpty() == false)
                {
                    stackCursor.grow(1);
                }

                this.inventoryPlayer.setItemStack(stackCursor.isEmpty() ? ItemStack.EMPTY : stackCursor);
            }
            // Can't put items into the slot (for example a furnace output slot or a crafting output slot); take items instead
            else if (stackSlot.isEmpty() == false)
            {
                this.takeItemsFromSlotToCursor(slot, Math.min(stackSlot.getMaxStackSize() / 2, stackSlot.getCount()));
            }
        }
        // Different items, try to swap the stacks
        else if (slot.canTakeStack(this.player) && slot.canTakeAll() && slot.isItemValid(stackCursor) &&
                stackSlot.getCount() <= stackSlot.getMaxStackSize() &&
                slot.getItemStackLimit(stackCursor) >= stackCursor.getCount())
        {
            this.inventoryPlayer.setItemStack(slot.decrStackSize(stackSlot.getCount()));
            slot.onTake(this.player, stackSlot);
            slot.insertItem(stackCursor, false);
        }
    }
    // Empty cursor, trying to take items from the slot into the cursor
    else if (stackSlot.isEmpty() == false)
    {
        // only allow taking the whole stack from crafting slots
        int amount = stackSlot.getCount();

        if ((slot instanceof SlotItemHandlerCraftResult) == false)
        {
            amount = Math.min((int) Math.ceil((double) stackSlot.getCount() / 2.0d),
                              (int) Math.ceil((double) stackSlot.getMaxStackSize() / 2.0d));
        }

        this.takeItemsFromSlotToCursor(slot, amount);
    }
}
 
Example 20
Source File: ItemStackHandlerBasic.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate)
{
    if (stack.isEmpty() || this.isItemValidForSlot(slot, stack) == false)
    {
        return stack;
    }

    ItemStack existingStack = this.items.get(slot);
    int existingStackSize = existingStack.getCount();
    boolean hasStack = existingStack.isEmpty() == false;
    int max = this.getItemStackLimit(slot, stack);

    if (this.allowCustomStackSizes == false)
    {
        max = Math.min(max, stack.getMaxStackSize());
    }

    // Existing items in the target slot
    if (hasStack)
    {
        // If the slot is already full, or the to-be-inserted item is different
        if (existingStackSize >= max ||
            stack.getItem() != existingStack.getItem() ||
            stack.getMetadata() != existingStack.getMetadata() ||
            ItemStack.areItemStackTagsEqual(stack, existingStack) == false)
        {
            return stack;
        }
    }

    int amount = Math.min(max - existingStackSize, stack.getCount());

    if (amount <= 0)
    {
        return stack;
    }

    if (simulate == false)
    {
        if (hasStack)
        {
            existingStack.grow(amount);
        }
        else
        {
            ItemStack newStack = stack.copy();
            newStack.setCount(amount);
            this.items.set(slot, newStack);
        }

        this.onContentsChanged(slot);
    }

    if (amount < stack.getCount())
    {
        ItemStack stackRemaining = stack.copy();
        stackRemaining.shrink(amount);

        return stackRemaining;
    }
    else
    {
        return ItemStack.EMPTY;
    }
}