/**
* MrCrayfish's Furniture Mod
* Copyright (C) 2016 MrCrayfish (http://www.mrcrayfish.com/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mrcrayfish.furniture.items;
import java.awt.Color;
import com.mrcrayfish.furniture.MrCrayfishFurnitureMod;
import com.mrcrayfish.furniture.init.FurnitureBlocks;
import com.mrcrayfish.furniture.init.FurnitureItems;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemCup extends Item
{
private boolean hasLiquid = false;
private Block cupBlock = FurnitureBlocks.cup;
public ItemCup(boolean hasLiquid)
{
this.hasLiquid = hasLiquid;
if (hasLiquid)
{
setMaxStackSize(1);
}
else
{
setCreativeTab(MrCrayfishFurnitureMod.tabFurniture);
}
}
@Override
public ItemStack onItemUseFinish(ItemStack cup, World world, EntityLivingBase entity)
{
if(entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) entity;
if (hasLiquid)
{
int heal = cup.getTagCompound().getInteger("HealAmount");
player.getFoodStats().addStats(heal, 0.5F);
return new ItemStack(FurnitureItems.itemCup);
}
}
return cup;
}
@Override
public int getMaxItemUseDuration(ItemStack cup)
{
return 32;
}
@Override
public EnumAction getItemUseAction(ItemStack stack)
{
return EnumAction.DRINK;
}
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand hand)
{
if (hasLiquid)
{
if (playerIn.getFoodStats().needFood())
{
playerIn.setActiveHand(hand);
}
}
return new ActionResult(EnumActionResult.PASS, playerIn.getHeldItem(hand));
}
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
IBlockState iblockstate = worldIn.getBlockState(pos);
Block block = iblockstate.getBlock();
if (!block.isReplaceable(worldIn, pos))
{
pos = pos.offset(facing);
}
ItemStack itemstack = player.getHeldItem(hand);
if (!itemstack.isEmpty() && player.canPlayerEdit(pos, facing, itemstack) && worldIn.mayPlace(this.cupBlock, pos, false, facing, (Entity)null))
{
int i = this.getMetadata(itemstack.getMetadata());
IBlockState iblockstate1 = this.cupBlock.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, i, player, hand);
if (placeBlockAt(itemstack, player, worldIn, pos, facing, hitX, hitY, hitZ, iblockstate1))
{
SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, player);
worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
itemstack.shrink(1);
}
return EnumActionResult.SUCCESS;
}
else
{
return EnumActionResult.FAIL;
}
}
@SideOnly(Side.CLIENT)
public int getColorFromCompound(ItemStack cup)
{
if (cup.hasTagCompound())
{
if (cup.getTagCompound().hasKey("Colour"))
{
int[] colour = cup.getTagCompound().getIntArray("Colour");
Color color = new Color(colour[0], colour[1], colour[2]);
return color.getRGB();
}
}
return 16777215;
}
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, IBlockState newState)
{
if (!world.setBlockState(pos, newState, 11)) return false;
IBlockState state = world.getBlockState(pos);
if (state.getBlock() == this.cupBlock)
{
ItemBlock.setTileEntityNBT(world, player, pos, stack);
this.cupBlock.onBlockPlacedBy(world, pos, state, player, stack);
}
return true;
}
}