package squeek.veganoption.items;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionHelper;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import squeek.veganoption.ModInfo;
import squeek.veganoption.blocks.BlockRawEnder;
import squeek.veganoption.helpers.BlockHelper;

public class ItemFrozenBubble extends Item
{
	public static IIcon partiallyFilledIcon;

	public ItemFrozenBubble()
	{
		super();
		setHasSubtypes(true);
	}

	@Override
	public String getPotionEffect(ItemStack itemStack)
	{
		// water breathing
		return PotionHelper.field_151423_m;
	}

	@Override
	public boolean onEntityItemUpdate(EntityItem entityItem)
	{
		tryFillWithRawEnderFromWorld(entityItem);
		return super.onEntityItemUpdate(entityItem);
	}

	public static boolean isFull(ItemStack itemStack)
	{
		return getPercentFilled(itemStack) >= 1;
	}

	public static boolean isEmpty(ItemStack itemStack)
	{
		return getPercentFilled(itemStack) <= 0;
	}

	public static ItemStack fill(ItemStack itemStack, int amount)
	{
		itemStack.setItemDamage(itemStack.getItemDamage() + amount);
		if (isFull(itemStack))
		{
			return new ItemStack(Items.ender_pearl);
		}
		return itemStack;
	}

	public static float getPercentFilled(ItemStack itemStack)
	{
		return itemStack.getItemDamage() / 8f;
	}

	@Override
	public boolean showDurabilityBar(ItemStack itemStack)
	{
		return !isEmpty(itemStack);
	}

	@Override
	public double getDurabilityForDisplay(ItemStack itemStack)
	{
		return 1.0f - getPercentFilled(itemStack);
	}

	public static boolean tryFillWithRawEnderFromWorld(EntityItem entityItem)
	{
		if (entityItem.worldObj.isRemote || entityItem == null || entityItem.getEntityItem() == null)
			return false;

		if (!isFull(entityItem.getEntityItem()) && entityItem.isInsideOfMaterial(BlockRawEnder.materialRawEnder))
		{
			EntityItem entityItemToFill = entityItem;
			ItemStack bubbleToFill = entityItemToFill.getEntityItem();

			if (entityItemToFill.getEntityItem().stackSize > 1)
			{
				bubbleToFill = entityItem.getEntityItem().splitStack(1);
				entityItemToFill = new EntityItem(entityItemToFill.worldObj, entityItemToFill.posX, entityItemToFill.posY, entityItemToFill.posZ, bubbleToFill);
				entityItemToFill.delayBeforeCanPickup = 10;
				entityItemToFill.worldObj.spawnEntityInWorld(entityItemToFill);
			}

			BlockHelper.BlockPos fluidBlockPos = BlockHelper.blockPos(entityItem.worldObj, MathHelper.floor_double(entityItem.posX), MathHelper.floor_double(entityItem.posY), MathHelper.floor_double(entityItem.posZ));
			int newMeta = fluidBlockPos.getMeta() - 1;

			if (newMeta >= 0)
				fluidBlockPos.world.setBlockMetadataWithNotify(fluidBlockPos.x, fluidBlockPos.y, fluidBlockPos.z, newMeta, 3);
			else
				fluidBlockPos.world.setBlockToAir(fluidBlockPos.x, fluidBlockPos.y, fluidBlockPos.z);

			ItemStack filledItemStack = fill(bubbleToFill, 1);
			entityItemToFill.setEntityItemStack(filledItemStack);

			return true;
		}
		return false;
	}

	@Override
	public String getUnlocalizedName(ItemStack itemStack)
	{
		return super.getUnlocalizedName(itemStack) + (!isEmpty(itemStack) ? "Filled" : "");
	}

	@Override
	public IIcon getIconFromDamage(int meta)
	{
		return meta > 0 ? partiallyFilledIcon : super.getIconFromDamage(meta);
	}

	@Override
	public void registerIcons(IIconRegister iconRegister)
	{
		super.registerIcons(iconRegister);
		partiallyFilledIcon = iconRegister.registerIcon(ModInfo.MODID_LOWER + ":frozen_bubble_filled");
	}
}

@@ -413,23 +413,24 @@
 ion.
-blocks.BlockRaw
+content.modules.
 Ende
@@ -478,16 +478,63 @@
 kHelper;
+%0Aimport squeek.veganoption.helpers.FluidHelper;
 %0A%0Apublic
@@ -1987,68 +1987,217 @@
  && 
-entityItem.isInsideOfMaterial(BlockRawEnder.materialRawEnder
+Ender.fluidRawEnder == FluidHelper.getFluidTypeOfBlock(entityItem.worldObj.getBlock(MathHelper.floor_double(entityItem.posX), MathHelper.floor_double(entityItem.posY), MathHelper.floor_double(entityItem.posZ))
 ))%0A%09

Fix frozen bubbles being able to be filled by any liquid * Whoops!