    001package net.minecraft.block;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.Random;
    006import net.minecraft.block.material.Material;
    007import net.minecraft.entity.player.EntityPlayer;
    008import net.minecraft.item.Item;
    009import net.minecraft.item.ItemStack;
    010import net.minecraft.world.World;
    011
    012public class BlockFlowerPot extends Block
    013{
    014    public BlockFlowerPot(int par1)
    015    {
    016        super(par1, Material.circuits);
    017        this.setBlockBoundsForItemRender();
    018    }
    019
    020    /**
    021     * Sets the block's bounds for rendering it as an item
    022     */
    023    public void setBlockBoundsForItemRender()
    024    {
    025        float f = 0.375F;
    026        float f1 = f / 2.0F;
    027        this.setBlockBounds(0.5F - f1, 0.0F, 0.5F - f1, 0.5F + f1, f, 0.5F + f1);
    028    }
    029
    030    /**
    031     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
    032     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
    033     */
    034    public boolean isOpaqueCube()
    035    {
    036        return false;
    037    }
    038
    039    /**
    040     * The type of render function that is called for this block
    041     */
    042    public int getRenderType()
    043    {
    044        return 33;
    045    }
    046
    047    /**
    048     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
    049     */
    050    public boolean renderAsNormalBlock()
    051    {
    052        return false;
    053    }
    054
    055    /**
    056     * Called upon block activation (right click on the block.)
    057     */
    058    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    059    {
    060        ItemStack itemstack = par5EntityPlayer.inventory.getCurrentItem();
    061
    062        if (itemstack == null)
    063        {
    064            return false;
    065        }
    066        else if (par1World.getBlockMetadata(par2, par3, par4) != 0)
    067        {
    068            return false;
    069        }
    070        else
    071        {
    072            int i1 = getMetaForPlant(itemstack);
    073
    074            if (i1 > 0)
    075            {
    076                par1World.setBlockMetadataWithNotify(par2, par3, par4, i1, 2);
    077
    078                if (!par5EntityPlayer.capabilities.isCreativeMode && --itemstack.stackSize <= 0)
    079                {
    080                    par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, (ItemStack)null);
    081                }
    082
    083                return true;
    084            }
    085            else
    086            {
    087                return false;
    088            }
    089        }
    090    }
    091
    092    @SideOnly(Side.CLIENT)
    093
    094    /**
    095     * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
    096     */
    097    public int idPicked(World par1World, int par2, int par3, int par4)
    098    {
    099        ItemStack itemstack = getPlantForMeta(par1World.getBlockMetadata(par2, par3, par4));
    100        return itemstack == null ? Item.flowerPot.itemID : itemstack.itemID;
    101    }
    102
    103    /**
    104     * Get the block's damage value (for use with pick block).
    105     */
    106    public int getDamageValue(World par1World, int par2, int par3, int par4)
    107    {
    108        ItemStack itemstack = getPlantForMeta(par1World.getBlockMetadata(par2, par3, par4));
    109        return itemstack == null ? Item.flowerPot.itemID : itemstack.getItemDamage();
    110    }
    111
    112    @SideOnly(Side.CLIENT)
    113    public boolean func_82505_u_()
    114    {
    115        return true;
    116    }
    117
    118    /**
    119     * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
    120     */
    121    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
    122    {
    123        return super.canPlaceBlockAt(par1World, par2, par3, par4) && par1World.doesBlockHaveSolidTopSurface(par2, par3 - 1, par4);
    124    }
    125
    126    /**
    127     * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
    128     * their own) Args: x, y, z, neighbor blockID
    129     */
    130    public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
    131    {
    132        if (!par1World.doesBlockHaveSolidTopSurface(par2, par3 - 1, par4))
    133        {
    134            this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
    135            par1World.setBlockToAir(par2, par3, par4);
    136        }
    137    }
    138
    139    /**
    140     * Drops the block items with a specified chance of dropping the specified items
    141     */
    142    public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
    143    {
    144        super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, par7);
    145
    146        if (par5 > 0)
    147        {
    148            ItemStack itemstack = getPlantForMeta(par5);
    149
    150            if (itemstack != null)
    151            {
    152                this.dropBlockAsItem_do(par1World, par2, par3, par4, itemstack);
    153            }
    154        }
    155    }
    156
    157    /**
    158     * Returns the ID of the items to drop on destruction.
    159     */
    160    public int idDropped(int par1, Random par2Random, int par3)
    161    {
    162        return Item.flowerPot.itemID;
    163    }
    164
    165    /**
    166     * Return the item associated with the specified flower pot metadata value.
    167     */
    168    public static ItemStack getPlantForMeta(int par0)
    169    {
    170        switch (par0)
    171        {
    172            case 1:
    173                return new ItemStack(Block.plantRed);
    174            case 2:
    175                return new ItemStack(Block.plantYellow);
    176            case 3:
    177                return new ItemStack(Block.sapling, 1, 0);
    178            case 4:
    179                return new ItemStack(Block.sapling, 1, 1);
    180            case 5:
    181                return new ItemStack(Block.sapling, 1, 2);
    182            case 6:
    183                return new ItemStack(Block.sapling, 1, 3);
    184            case 7:
    185                return new ItemStack(Block.mushroomRed);
    186            case 8:
    187                return new ItemStack(Block.mushroomBrown);
    188            case 9:
    189                return new ItemStack(Block.cactus);
    190            case 10:
    191                return new ItemStack(Block.deadBush);
    192            case 11:
    193                return new ItemStack(Block.tallGrass, 1, 2);
    194            default:
    195                return null;
    196        }
    197    }
    198
    199    /**
    200     * Return the flower pot metadata value associated with the specified item.
    201     */
    202    public static int getMetaForPlant(ItemStack par0ItemStack)
    203    {
    204        int i = par0ItemStack.getItem().itemID;
    205
    206        if (i == Block.plantRed.blockID)
    207        {
    208            return 1;
    209        }
    210        else if (i == Block.plantYellow.blockID)
    211        {
    212            return 2;
    213        }
    214        else if (i == Block.cactus.blockID)
    215        {
    216            return 9;
    217        }
    218        else if (i == Block.mushroomBrown.blockID)
    219        {
    220            return 8;
    221        }
    222        else if (i == Block.mushroomRed.blockID)
    223        {
    224            return 7;
    225        }
    226        else if (i == Block.deadBush.blockID)
    227        {
    228            return 10;
    229        }
    230        else
    231        {
    232            if (i == Block.sapling.blockID)
    233            {
    234                switch (par0ItemStack.getItemDamage())
    235                {
    236                    case 0:
    237                        return 3;
    238                    case 1:
    239                        return 4;
    240                    case 2:
    241                        return 5;
    242                    case 3:
    243                        return 6;
    244                }
    245            }
    246
    247            if (i == Block.tallGrass.blockID)
    248            {
    249                switch (par0ItemStack.getItemDamage())
    250                {
    251                    case 2:
    252                        return 11;
    253                }
    254            }
    255
    256            return 0;
    257        }
    258    }
    259}