package org.cyclops.cyclopscore.helper;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

import java.util.Random;

/**
 * Contains helper methods for various itemstack specific things.
 * @author rubensworks
 */
public final class ItemStackHelpers {

    private static final Random RANDOM = new Random();

    /**
     * Get the tag compound from an item safely.
     * If it does not exist yet, it will create and save a new tag compound.
     * @param itemStack The item to get the tag compound from.
     * @return The tag compound.
     */
    public static NBTTagCompound getSafeTagCompound(ItemStack itemStack) {
        if(!itemStack.hasTagCompound()) {
            itemStack.setTagCompound(new NBTTagCompound());
        }
        return itemStack.getTagCompound();
    }

    /**
     * Spawn an itemstack into the world.
     * @param world The world
     * @param pos The position
     * @param itemStack the item stack
     */
    public static void spawnItemStack(World world, BlockPos pos, ItemStack itemStack) {
        spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemStack);
    }

    /**
     * Spawn an itemstack into the world.
     * @param world The world
     * @param x X
     * @param y Y
     * @param z Z
     * @param itemStack the item stack
     */
    public static void spawnItemStack(World world, double x, double y, double z, ItemStack itemStack) {
        float offsetX = RANDOM.nextFloat() * 0.8F + 0.1F;
        float offsetY = RANDOM.nextFloat() * 0.8F + 0.1F;
        float offsetZ = RANDOM.nextFloat() * 0.8F + 0.1F;

        while (itemStack.stackSize > 0) {
            int i = RANDOM.nextInt(21) + 10;

            if (i > itemStack.stackSize) {
                i = itemStack.stackSize;
            }

            itemStack.stackSize -= i;
            EntityItem entityitem = new EntityItem(world, x + (double)offsetX, y + (double)offsetY,
                    z + (double)offsetZ, new ItemStack(itemStack.getItem(), i, itemStack.getMetadata()));

            if (itemStack.hasTagCompound()) {
                entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemStack.getTagCompound().copy());
            }

            float motion = 0.05F;
            entityitem.motionX = RANDOM.nextGaussian() * (double)motion;
            entityitem.motionY = RANDOM.nextGaussian() * (double)motion + 0.2D;
            entityitem.motionZ = RANDOM.nextGaussian() * (double)motion;
            world.spawnEntityInWorld(entityitem);
        }
    }

}

@@ -79,16 +79,65 @@
 tyItem;%0A
+import net.minecraft.entity.player.EntityPlayer;%0A
 import n
@@ -2691,10 +2691,1568 @@
 %0A    %7D%0A%0A
+    /**%0A     * Spawn an entity in the direction of a player without setting a pickup delay.%0A     * @param world The world%0A     * @param pos The position to spawn an%0A     * @param stack The stack to spawn%0A     * @param player The player to direct the motion to%0A     */%0A    public static void spawnItemStackToPlayer(World world, BlockPos pos, ItemStack stack, EntityPlayer player) %7B%0A        if (!world.isRemote) %7B%0A            float f = 0.5F;%0A%0A            double xo = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;%0A            double yo = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;%0A            double zo = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;%0A            EntityItem entityitem = new EntityItem(world, (double)pos.getX() + xo, (double)pos.getY() + yo, (double)pos.getZ() + zo, stack);%0A%0A            double d0 = 8.0D;%0A            double d1 = (player.posX - entityitem.posX) / d0;%0A            double d2 = (player.posY + (double)player.getEyeHeight() - entityitem.posY) / d0;%0A            double d3 = (player.posZ - entityitem.posZ) / d0;%0A            double d4 = Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3);%0A            double d5 = 1.0D - d4;%0A%0A            if (d5 %3E 0.0D) %7B%0A                d5 *= d5;%0A                entityitem.motionX += d1 / d4 * d5 * 0.1D;%0A                entityitem.motionY += d2 / d4 * d5 * 0.1D;%0A                entityitem.motionZ += d3 / d4 * d5 * 0.1D;%0A            %7D%0A%0A            entityitem.setNoPickupDelay();%0A            world.spawnEntityInWorld(entityitem);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add helper method to direct an item spawn to a player