    001package net.minecraft.item;
    002
    003import net.minecraft.block.Block;
    004import net.minecraft.block.material.Material;
    005import net.minecraft.creativetab.CreativeTabs;
    006import net.minecraft.entity.player.EntityPlayer;
    007import net.minecraft.util.MathHelper;
    008import net.minecraft.world.World;
    009
    010public class ItemDoor extends Item
    011{
    012    private Material doorMaterial;
    013
    014    public ItemDoor(int par1, Material par2Material)
    015    {
    016        super(par1);
    017        this.doorMaterial = par2Material;
    018        this.maxStackSize = 1;
    019        this.setCreativeTab(CreativeTabs.tabRedstone);
    020    }
    021
    022    /**
    023     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
    024     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
    025     */
    026    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    027    {
    028        if (par7 != 1)
    029        {
    030            return false;
    031        }
    032        else
    033        {
    034            ++par5;
    035            Block block;
    036
    037            if (this.doorMaterial == Material.wood)
    038            {
    039                block = Block.doorWood;
    040            }
    041            else
    042            {
    043                block = Block.doorIron;
    044            }
    045
    046            if (par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack) && par2EntityPlayer.canPlayerEdit(par4, par5 + 1, par6, par7, par1ItemStack))
    047            {
    048                if (!block.canPlaceBlockAt(par3World, par4, par5, par6))
    049                {
    050                    return false;
    051                }
    052                else
    053                {
    054                    int i1 = MathHelper.floor_double((double)((par2EntityPlayer.rotationYaw + 180.0F) * 4.0F / 360.0F) - 0.5D) & 3;
    055                    placeDoorBlock(par3World, par4, par5, par6, i1, block);
    056                    --par1ItemStack.stackSize;
    057                    return true;
    058                }
    059            }
    060            else
    061            {
    062                return false;
    063            }
    064        }
    065    }
    066
    067    public static void placeDoorBlock(World par0World, int par1, int par2, int par3, int par4, Block par5Block)
    068    {
    069        byte b0 = 0;
    070        byte b1 = 0;
    071
    072        if (par4 == 0)
    073        {
    074            b1 = 1;
    075        }
    076
    077        if (par4 == 1)
    078        {
    079            b0 = -1;
    080        }
    081
    082        if (par4 == 2)
    083        {
    084            b1 = -1;
    085        }
    086
    087        if (par4 == 3)
    088        {
    089            b0 = 1;
    090        }
    091
    092        int i1 = (par0World.isBlockNormalCube(par1 - b0, par2, par3 - b1) ? 1 : 0) + (par0World.isBlockNormalCube(par1 - b0, par2 + 1, par3 - b1) ? 1 : 0);
    093        int j1 = (par0World.isBlockNormalCube(par1 + b0, par2, par3 + b1) ? 1 : 0) + (par0World.isBlockNormalCube(par1 + b0, par2 + 1, par3 + b1) ? 1 : 0);
    094        boolean flag = par0World.getBlockId(par1 - b0, par2, par3 - b1) == par5Block.blockID || par0World.getBlockId(par1 - b0, par2 + 1, par3 - b1) == par5Block.blockID;
    095        boolean flag1 = par0World.getBlockId(par1 + b0, par2, par3 + b1) == par5Block.blockID || par0World.getBlockId(par1 + b0, par2 + 1, par3 + b1) == par5Block.blockID;
    096        boolean flag2 = false;
    097
    098        if (flag && !flag1)
    099        {
    100            flag2 = true;
    101        }
    102        else if (j1 > i1)
    103        {
    104            flag2 = true;
    105        }
    106
    107        par0World.setBlock(par1, par2, par3, par5Block.blockID, par4, 2);
    108        par0World.setBlock(par1, par2 + 1, par3, par5Block.blockID, 8 | (flag2 ? 1 : 0), 2);
    109        par0World.notifyBlocksOfNeighborChange(par1, par2, par3, par5Block.blockID);
    110        par0World.notifyBlocksOfNeighborChange(par1, par2 + 1, par3, par5Block.blockID);
    111    }
    112}