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.creativetab.CreativeTabs;
008import net.minecraft.util.AxisAlignedBB;
009import net.minecraft.util.MovingObjectPosition;
010import net.minecraft.util.Vec3;
011import net.minecraft.world.World;
012
013import net.minecraftforge.common.ForgeDirection;
014import static net.minecraftforge.common.ForgeDirection.*;
015
016public class BlockTorch extends Block
017{
018    protected BlockTorch(int par1)
019    {
020        super(par1, Material.circuits);
021        this.setTickRandomly(true);
022        this.setCreativeTab(CreativeTabs.tabDecorations);
023    }
024
025    /**
026     * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
027     * cleared to be reused)
028     */
029    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
030    {
031        return null;
032    }
033
034    /**
035     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
036     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
037     */
038    public boolean isOpaqueCube()
039    {
040        return false;
041    }
042
043    /**
044     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
045     */
046    public boolean renderAsNormalBlock()
047    {
048        return false;
049    }
050
051    /**
052     * The type of render function that is called for this block
053     */
054    public int getRenderType()
055    {
056        return 2;
057    }
058
059    /**
060     * Gets if we can place a torch on a block.
061     */
062    private boolean canPlaceTorchOn(World par1World, int par2, int par3, int par4)
063    {
064        if (par1World.doesBlockHaveSolidTopSurface(par2, par3, par4))
065        {
066            return true;
067        }
068        else
069        {
070            int l = par1World.getBlockId(par2, par3, par4);
071            return (Block.blocksList[l] != null && Block.blocksList[l].canPlaceTorchOnTop(par1World, par2, par3, par4));
072        }
073    }
074
075    /**
076     * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
077     */
078    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
079    {
080        return par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST,  true) ||
081               par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST,  true) ||
082               par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true) ||
083               par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true) ||
084               canPlaceTorchOn(par1World, par2, par3 - 1, par4);
085    }
086
087    /**
088     * Called when a block is placed using its ItemBlock. Args: World, X, Y, Z, side, hitX, hitY, hitZ, block metadata
089     */
090    public int onBlockPlaced(World par1World, int par2, int par3, int par4, int par5, float par6, float par7, float par8, int par9)
091    {
092        int j1 = par9;
093
094        if (par5 == 1 && this.canPlaceTorchOn(par1World, par2, par3 - 1, par4))
095        {
096            j1 = 5;
097        }
098
099        if (par5 == 2 && par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true))
100        {
101            j1 = 4;
102        }
103
104        if (par5 == 3 && par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true))
105        {
106            j1 = 3;
107        }
108
109        if (par5 == 4 && par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true))
110        {
111            j1 = 2;
112        }
113
114        if (par5 == 5 && par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true))
115        {
116            j1 = 1;
117        }
118
119        return j1;
120    }
121
122    /**
123     * Ticks the block if it's been scheduled
124     */
125    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
126    {
127        super.updateTick(par1World, par2, par3, par4, par5Random);
128
129        if (par1World.getBlockMetadata(par2, par3, par4) == 0)
130        {
131            this.onBlockAdded(par1World, par2, par3, par4);
132        }
133    }
134
135    /**
136     * Called whenever the block is added into the world. Args: world, x, y, z
137     */
138    public void onBlockAdded(World par1World, int par2, int par3, int par4)
139    {
140        if (par1World.getBlockMetadata(par2, par3, par4) == 0)
141        {
142            if (par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true))
143            {
144                par1World.setBlockMetadataWithNotify(par2, par3, par4, 1, 2);
145            }
146            else if (par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true))
147            {
148                par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
149            }
150            else if (par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true))
151            {
152                par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
153            }
154            else if (par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true))
155            {
156                par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
157            }
158            else if (this.canPlaceTorchOn(par1World, par2, par3 - 1, par4))
159            {
160                par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
161            }
162        }
163
164        this.dropTorchIfCantStay(par1World, par2, par3, par4);
165    }
166
167    /**
168     * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
169     * their own) Args: x, y, z, neighbor blockID
170     */
171    public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
172    {
173        this.func_94397_d(par1World, par2, par3, par4, par5);
174    }
175
176    protected boolean func_94397_d(World par1World, int par2, int par3, int par4, int par5)
177    {
178        if (this.dropTorchIfCantStay(par1World, par2, par3, par4))
179        {
180            int i1 = par1World.getBlockMetadata(par2, par3, par4);
181            boolean flag = false;
182
183            if (!par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true) && i1 == 1)
184            {
185                flag = true;
186            }
187
188            if (!par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true) && i1 == 2)
189            {
190                flag = true;
191            }
192
193            if (!par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true) && i1 == 3)
194            {
195                flag = true;
196            }
197
198            if (!par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true) && i1 == 4)
199            {
200                flag = true;
201            }
202
203            if (!this.canPlaceTorchOn(par1World, par2, par3 - 1, par4) && i1 == 5)
204            {
205                flag = true;
206            }
207
208            if (flag)
209            {
210                this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
211                par1World.setBlockToAir(par2, par3, par4);
212                return true;
213            }
214            else
215            {
216                return false;
217            }
218        }
219        else
220        {
221            return true;
222        }
223    }
224
225    /**
226     * Tests if the block can remain at its current location and will drop as an item if it is unable to stay. Returns
227     * True if it can stay and False if it drops. Args: world, x, y, z
228     */
229    protected boolean dropTorchIfCantStay(World par1World, int par2, int par3, int par4)
230    {
231        if (!this.canPlaceBlockAt(par1World, par2, par3, par4))
232        {
233            if (par1World.getBlockId(par2, par3, par4) == this.blockID)
234            {
235                this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
236                par1World.setBlockToAir(par2, par3, par4);
237            }
238
239            return false;
240        }
241        else
242        {
243            return true;
244        }
245    }
246
247    /**
248     * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit. Args: world,
249     * x, y, z, startVec, endVec
250     */
251    public MovingObjectPosition collisionRayTrace(World par1World, int par2, int par3, int par4, Vec3 par5Vec3, Vec3 par6Vec3)
252    {
253        int l = par1World.getBlockMetadata(par2, par3, par4) & 7;
254        float f = 0.15F;
255
256        if (l == 1)
257        {
258            this.setBlockBounds(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f);
259        }
260        else if (l == 2)
261        {
262            this.setBlockBounds(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f);
263        }
264        else if (l == 3)
265        {
266            this.setBlockBounds(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F);
267        }
268        else if (l == 4)
269        {
270            this.setBlockBounds(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F);
271        }
272        else
273        {
274            f = 0.1F;
275            this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.6F, 0.5F + f);
276        }
277
278        return super.collisionRayTrace(par1World, par2, par3, par4, par5Vec3, par6Vec3);
279    }
280
281    @SideOnly(Side.CLIENT)
282
283    /**
284     * A randomly called display update to be able to add particles or other items for display
285     */
286    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
287    {
288        int l = par1World.getBlockMetadata(par2, par3, par4);
289        double d0 = (double)((float)par2 + 0.5F);
290        double d1 = (double)((float)par3 + 0.7F);
291        double d2 = (double)((float)par4 + 0.5F);
292        double d3 = 0.2199999988079071D;
293        double d4 = 0.27000001072883606D;
294
295        if (l == 1)
296        {
297            par1World.spawnParticle("smoke", d0 - d4, d1 + d3, d2, 0.0D, 0.0D, 0.0D);
298            par1World.spawnParticle("flame", d0 - d4, d1 + d3, d2, 0.0D, 0.0D, 0.0D);
299        }
300        else if (l == 2)
301        {
302            par1World.spawnParticle("smoke", d0 + d4, d1 + d3, d2, 0.0D, 0.0D, 0.0D);
303            par1World.spawnParticle("flame", d0 + d4, d1 + d3, d2, 0.0D, 0.0D, 0.0D);
304        }
305        else if (l == 3)
306        {
307            par1World.spawnParticle("smoke", d0, d1 + d3, d2 - d4, 0.0D, 0.0D, 0.0D);
308            par1World.spawnParticle("flame", d0, d1 + d3, d2 - d4, 0.0D, 0.0D, 0.0D);
309        }
310        else if (l == 4)
311        {
312            par1World.spawnParticle("smoke", d0, d1 + d3, d2 + d4, 0.0D, 0.0D, 0.0D);
313            par1World.spawnParticle("flame", d0, d1 + d3, d2 + d4, 0.0D, 0.0D, 0.0D);
314        }
315        else
316        {
317            par1World.spawnParticle("smoke", d0, d1, d2, 0.0D, 0.0D, 0.0D);
318            par1World.spawnParticle("flame", d0, d1, d2, 0.0D, 0.0D, 0.0D);
319        }
320    }
321}