    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, int par2)
    019    {
    020        super(par1, par2, 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 var5 = par1World.getBlockId(par2, par3, par4);
    071            return (Block.blocksList[var5] != null && Block.blocksList[var5].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 var10 = par9;
    093
    094        if (par5 == 1 && this.canPlaceTorchOn(par1World, par2, par3 - 1, par4))
    095        {
    096            var10 = 5;
    097        }
    098
    099        if (par5 == 2 && par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true))
    100        {
    101            var10 = 4;
    102        }
    103
    104        if (par5 == 3 && par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true))
    105        {
    106            var10 = 3;
    107        }
    108
    109        if (par5 == 4 && par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true))
    110        {
    111            var10 = 2;
    112        }
    113
    114        if (par5 == 5 && par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true))
    115        {
    116            var10 = 1;
    117        }
    118
    119        return var10;
    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);
    145            }
    146            else if (par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true))
    147            {
    148                par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
    149            }
    150            else if (par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true))
    151            {
    152                par1World.setBlockMetadataWithNotify(par2, par3, par4, 3);
    153            }
    154            else if (par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true))
    155            {
    156                par1World.setBlockMetadataWithNotify(par2, par3, par4, 4);
    157            }
    158            else if (this.canPlaceTorchOn(par1World, par2, par3 - 1, par4))
    159            {
    160                par1World.setBlockMetadataWithNotify(par2, par3, par4, 5);
    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        if (this.dropTorchIfCantStay(par1World, par2, par3, par4))
    174        {
    175            int var6 = par1World.getBlockMetadata(par2, par3, par4);
    176            boolean var7 = false;
    177
    178            if (!par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true) && var6 == 1)
    179            {
    180                var7 = true;
    181            }
    182
    183            if (!par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true) && var6 == 2)
    184            {
    185                var7 = true;
    186            }
    187
    188            if (!par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true) && var6 == 3)
    189            {
    190                var7 = true;
    191            }
    192
    193            if (!par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true) && var6 == 4)
    194            {
    195                var7 = true;
    196            }
    197
    198            if (!this.canPlaceTorchOn(par1World, par2, par3 - 1, par4) && var6 == 5)
    199            {
    200                var7 = true;
    201            }
    202
    203            if (var7)
    204            {
    205                this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
    206                par1World.setBlockWithNotify(par2, par3, par4, 0);
    207            }
    208        }
    209    }
    210
    211    /**
    212     * Tests if the block can remain at its current location and will drop as an item if it is unable to stay. Returns
    213     * True if it can stay and False if it drops. Args: world, x, y, z
    214     */
    215    private boolean dropTorchIfCantStay(World par1World, int par2, int par3, int par4)
    216    {
    217        if (!this.canPlaceBlockAt(par1World, par2, par3, par4))
    218        {
    219            if (par1World.getBlockId(par2, par3, par4) == this.blockID)
    220            {
    221                this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
    222                par1World.setBlockWithNotify(par2, par3, par4, 0);
    223            }
    224
    225            return false;
    226        }
    227        else
    228        {
    229            return true;
    230        }
    231    }
    232
    233    /**
    234     * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit. Args: world,
    235     * x, y, z, startVec, endVec
    236     */
    237    public MovingObjectPosition collisionRayTrace(World par1World, int par2, int par3, int par4, Vec3 par5Vec3, Vec3 par6Vec3)
    238    {
    239        int var7 = par1World.getBlockMetadata(par2, par3, par4) & 7;
    240        float var8 = 0.15F;
    241
    242        if (var7 == 1)
    243        {
    244            this.setBlockBounds(0.0F, 0.2F, 0.5F - var8, var8 * 2.0F, 0.8F, 0.5F + var8);
    245        }
    246        else if (var7 == 2)
    247        {
    248            this.setBlockBounds(1.0F - var8 * 2.0F, 0.2F, 0.5F - var8, 1.0F, 0.8F, 0.5F + var8);
    249        }
    250        else if (var7 == 3)
    251        {
    252            this.setBlockBounds(0.5F - var8, 0.2F, 0.0F, 0.5F + var8, 0.8F, var8 * 2.0F);
    253        }
    254        else if (var7 == 4)
    255        {
    256            this.setBlockBounds(0.5F - var8, 0.2F, 1.0F - var8 * 2.0F, 0.5F + var8, 0.8F, 1.0F);
    257        }
    258        else
    259        {
    260            var8 = 0.1F;
    261            this.setBlockBounds(0.5F - var8, 0.0F, 0.5F - var8, 0.5F + var8, 0.6F, 0.5F + var8);
    262        }
    263
    264        return super.collisionRayTrace(par1World, par2, par3, par4, par5Vec3, par6Vec3);
    265    }
    266
    267    @SideOnly(Side.CLIENT)
    268
    269    /**
    270     * A randomly called display update to be able to add particles or other items for display
    271     */
    272    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    273    {
    274        int var6 = par1World.getBlockMetadata(par2, par3, par4);
    275        double var7 = (double)((float)par2 + 0.5F);
    276        double var9 = (double)((float)par3 + 0.7F);
    277        double var11 = (double)((float)par4 + 0.5F);
    278        double var13 = 0.2199999988079071D;
    279        double var15 = 0.27000001072883606D;
    280
    281        if (var6 == 1)
    282        {
    283            par1World.spawnParticle("smoke", var7 - var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    284            par1World.spawnParticle("flame", var7 - var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    285        }
    286        else if (var6 == 2)
    287        {
    288            par1World.spawnParticle("smoke", var7 + var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    289            par1World.spawnParticle("flame", var7 + var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    290        }
    291        else if (var6 == 3)
    292        {
    293            par1World.spawnParticle("smoke", var7, var9 + var13, var11 - var15, 0.0D, 0.0D, 0.0D);
    294            par1World.spawnParticle("flame", var7, var9 + var13, var11 - var15, 0.0D, 0.0D, 0.0D);
    295        }
    296        else if (var6 == 4)
    297        {
    298            par1World.spawnParticle("smoke", var7, var9 + var13, var11 + var15, 0.0D, 0.0D, 0.0D);
    299            par1World.spawnParticle("flame", var7, var9 + var13, var11 + var15, 0.0D, 0.0D, 0.0D);
    300        }
    301        else
    302        {
    303            par1World.spawnParticle("smoke", var7, var9, var11, 0.0D, 0.0D, 0.0D);
    304            par1World.spawnParticle("flame", var7, var9, var11, 0.0D, 0.0D, 0.0D);
    305        }
    306    }
    307}