001package net.minecraft.item;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import net.minecraft.block.Block;
006import net.minecraft.block.BlockHalfSlab;
007import net.minecraft.entity.player.EntityPlayer;
008import net.minecraft.util.Icon;
009import net.minecraft.world.World;
010
011public class ItemSlab extends ItemBlock
012{
013    private final boolean isFullBlock;
014
015    /** Instance of BlockHalfSlab. */
016    private final BlockHalfSlab theHalfSlab;
017
018    /** The double-slab block corresponding to this item. */
019    private final BlockHalfSlab doubleSlab;
020
021    public ItemSlab(int par1, BlockHalfSlab par2BlockHalfSlab, BlockHalfSlab par3BlockHalfSlab, boolean par4)
022    {
023        super(par1);
024        this.theHalfSlab = par2BlockHalfSlab;
025        this.doubleSlab = par3BlockHalfSlab;
026        this.isFullBlock = par4;
027        this.setMaxDamage(0);
028        this.setHasSubtypes(true);
029    }
030
031    @SideOnly(Side.CLIENT)
032
033    /**
034     * Gets an icon index based on an item's damage value
035     */
036    public Icon getIconFromDamage(int par1)
037    {
038        return Block.blocksList[this.itemID].getBlockTextureFromSideAndMetadata(2, par1);
039    }
040
041    /**
042     * Returns the metadata of the block which this Item (ItemBlock) can place
043     */
044    public int getMetadata(int par1)
045    {
046        return par1;
047    }
048
049    /**
050     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
051     * different names based on their damage or NBT.
052     */
053    public String getUnlocalizedName(ItemStack par1ItemStack)
054    {
055        return this.theHalfSlab.getFullSlabName(par1ItemStack.getItemDamage());
056    }
057
058    /**
059     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
060     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
061     */
062    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
063    {
064        if (this.isFullBlock)
065        {
066            return super.onItemUse(par1ItemStack, par2EntityPlayer, par3World, par4, par5, par6, par7, par8, par9, par10);
067        }
068        else if (par1ItemStack.stackSize == 0)
069        {
070            return false;
071        }
072        else if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack))
073        {
074            return false;
075        }
076        else
077        {
078            int i1 = par3World.getBlockId(par4, par5, par6);
079            int j1 = par3World.getBlockMetadata(par4, par5, par6);
080            int k1 = j1 & 7;
081            boolean flag = (j1 & 8) != 0;
082
083            if ((par7 == 1 && !flag || par7 == 0 && flag) && i1 == this.theHalfSlab.blockID && k1 == par1ItemStack.getItemDamage())
084            {
085                if (par3World.checkIfAABBIsClear(this.doubleSlab.getCollisionBoundingBoxFromPool(par3World, par4, par5, par6)) && par3World.setBlock(par4, par5, par6, this.doubleSlab.blockID, k1, 3))
086                {
087                    par3World.playSoundEffect((double)((float)par4 + 0.5F), (double)((float)par5 + 0.5F), (double)((float)par6 + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getPitch() * 0.8F);
088                    --par1ItemStack.stackSize;
089                }
090
091                return true;
092            }
093            else
094            {
095                return this.func_77888_a(par1ItemStack, par2EntityPlayer, par3World, par4, par5, par6, par7) ? true : super.onItemUse(par1ItemStack, par2EntityPlayer, par3World, par4, par5, par6, par7, par8, par9, par10);
096            }
097        }
098    }
099
100    @SideOnly(Side.CLIENT)
101
102    /**
103     * Returns true if the given ItemBlock can be placed on the given side of the given block position.
104     */
105    public boolean canPlaceItemBlockOnSide(World par1World, int par2, int par3, int par4, int par5, EntityPlayer par6EntityPlayer, ItemStack par7ItemStack)
106    {
107        int i1 = par2;
108        int j1 = par3;
109        int k1 = par4;
110        int l1 = par1World.getBlockId(par2, par3, par4);
111        int i2 = par1World.getBlockMetadata(par2, par3, par4);
112        int j2 = i2 & 7;
113        boolean flag = (i2 & 8) != 0;
114
115        if ((par5 == 1 && !flag || par5 == 0 && flag) && l1 == this.theHalfSlab.blockID && j2 == par7ItemStack.getItemDamage())
116        {
117            return true;
118        }
119        else
120        {
121            if (par5 == 0)
122            {
123                --par3;
124            }
125
126            if (par5 == 1)
127            {
128                ++par3;
129            }
130
131            if (par5 == 2)
132            {
133                --par4;
134            }
135
136            if (par5 == 3)
137            {
138                ++par4;
139            }
140
141            if (par5 == 4)
142            {
143                --par2;
144            }
145
146            if (par5 == 5)
147            {
148                ++par2;
149            }
150
151            l1 = par1World.getBlockId(par2, par3, par4);
152            i2 = par1World.getBlockMetadata(par2, par3, par4);
153            j2 = i2 & 7;
154            flag = (i2 & 8) != 0;
155            return l1 == this.theHalfSlab.blockID && j2 == par7ItemStack.getItemDamage() ? true : super.canPlaceItemBlockOnSide(par1World, i1, j1, k1, par5, par6EntityPlayer, par7ItemStack);
156        }
157    }
158
159    private boolean func_77888_a(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7)
160    {
161        if (par7 == 0)
162        {
163            --par5;
164        }
165
166        if (par7 == 1)
167        {
168            ++par5;
169        }
170
171        if (par7 == 2)
172        {
173            --par6;
174        }
175
176        if (par7 == 3)
177        {
178            ++par6;
179        }
180
181        if (par7 == 4)
182        {
183            --par4;
184        }
185
186        if (par7 == 5)
187        {
188            ++par4;
189        }
190
191        int i1 = par3World.getBlockId(par4, par5, par6);
192        int j1 = par3World.getBlockMetadata(par4, par5, par6);
193        int k1 = j1 & 7;
194
195        if (i1 == this.theHalfSlab.blockID && k1 == par1ItemStack.getItemDamage())
196        {
197            if (par3World.checkIfAABBIsClear(this.doubleSlab.getCollisionBoundingBoxFromPool(par3World, par4, par5, par6)) && par3World.setBlock(par4, par5, par6, this.doubleSlab.blockID, k1, 3))
198            {
199                par3World.playSoundEffect((double)((float)par4 + 0.5F), (double)((float)par5 + 0.5F), (double)((float)par6 + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getPitch() * 0.8F);
200                --par1ItemStack.stackSize;
201            }
202
203            return true;
204        }
205        else
206        {
207            return false;
208        }
209    }
210}