    001    package net.minecraft.block;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.Random;
    006    import net.minecraft.block.material.Material;
    007    import net.minecraft.creativetab.CreativeTabs;
    008    import net.minecraft.util.AxisAlignedBB;
    009    import net.minecraft.util.MovingObjectPosition;
    010    import net.minecraft.util.Vec3;
    011    import net.minecraft.world.World;
    012
    013    import net.minecraftforge.common.ForgeDirection;
    014    import static net.minecraftforge.common.ForgeDirection.*;
    015
    016    public 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        public int func_85104_a(World par1World, int par2, int par3, int par4, int par5, float par6, float par7, float par8, int par9)
    088        {
    089            int var10 = par9;
    090
    091            if (par5 == 1 && this.canPlaceTorchOn(par1World, par2, par3 - 1, par4))
    092            {
    093                var10 = 5;
    094            }
    095
    096            if (par5 == 2 && par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true))
    097            {
    098                var10 = 4;
    099            }
    100
    101            if (par5 == 3 && par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true))
    102            {
    103                var10 = 3;
    104            }
    105
    106            if (par5 == 4 && par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true))
    107            {
    108                var10 = 2;
    109            }
    110
    111            if (par5 == 5 && par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true))
    112            {
    113                var10 = 1;
    114            }
    115
    116            return var10;
    117        }
    118
    119        /**
    120         * Ticks the block if it's been scheduled
    121         */
    122        public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
    123        {
    124            super.updateTick(par1World, par2, par3, par4, par5Random);
    125
    126            if (par1World.getBlockMetadata(par2, par3, par4) == 0)
    127            {
    128                this.onBlockAdded(par1World, par2, par3, par4);
    129            }
    130        }
    131
    132        /**
    133         * Called whenever the block is added into the world. Args: world, x, y, z
    134         */
    135        public void onBlockAdded(World par1World, int par2, int par3, int par4)
    136        {
    137            if (par1World.getBlockMetadata(par2, par3, par4) == 0)
    138            {
    139                if (par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true))
    140                {
    141                    par1World.setBlockMetadataWithNotify(par2, par3, par4, 1);
    142                }
    143                else if (par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true))
    144                {
    145                    par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
    146                }
    147                else if (par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true))
    148                {
    149                    par1World.setBlockMetadataWithNotify(par2, par3, par4, 3);
    150                }
    151                else if (par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true))
    152                {
    153                    par1World.setBlockMetadataWithNotify(par2, par3, par4, 4);
    154                }
    155                else if (this.canPlaceTorchOn(par1World, par2, par3 - 1, par4))
    156                {
    157                    par1World.setBlockMetadataWithNotify(par2, par3, par4, 5);
    158                }
    159            }
    160
    161            this.dropTorchIfCantStay(par1World, par2, par3, par4);
    162        }
    163
    164        /**
    165         * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
    166         * their own) Args: x, y, z, neighbor blockID
    167         */
    168        public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
    169        {
    170            if (this.dropTorchIfCantStay(par1World, par2, par3, par4))
    171            {
    172                int var6 = par1World.getBlockMetadata(par2, par3, par4);
    173                boolean var7 = false;
    174
    175                if (!par1World.isBlockSolidOnSide(par2 - 1, par3, par4, EAST, true) && var6 == 1)
    176                {
    177                    var7 = true;
    178                }
    179
    180                if (!par1World.isBlockSolidOnSide(par2 + 1, par3, par4, WEST, true) && var6 == 2)
    181                {
    182                    var7 = true;
    183                }
    184
    185                if (!par1World.isBlockSolidOnSide(par2, par3, par4 - 1, SOUTH, true) && var6 == 3)
    186                {
    187                    var7 = true;
    188                }
    189
    190                if (!par1World.isBlockSolidOnSide(par2, par3, par4 + 1, NORTH, true) && var6 == 4)
    191                {
    192                    var7 = true;
    193                }
    194
    195                if (!this.canPlaceTorchOn(par1World, par2, par3 - 1, par4) && var6 == 5)
    196                {
    197                    var7 = true;
    198                }
    199
    200                if (var7)
    201                {
    202                    this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
    203                    par1World.setBlockWithNotify(par2, par3, par4, 0);
    204                }
    205            }
    206        }
    207
    208        /**
    209         * Tests if the block can remain at its current location and will drop as an item if it is unable to stay. Returns
    210         * True if it can stay and False if it drops. Args: world, x, y, z
    211         */
    212        private boolean dropTorchIfCantStay(World par1World, int par2, int par3, int par4)
    213        {
    214            if (!this.canPlaceBlockAt(par1World, par2, par3, par4))
    215            {
    216                if (par1World.getBlockId(par2, par3, par4) == this.blockID)
    217                {
    218                    this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
    219                    par1World.setBlockWithNotify(par2, par3, par4, 0);
    220                }
    221
    222                return false;
    223            }
    224            else
    225            {
    226                return true;
    227            }
    228        }
    229
    230        /**
    231         * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit. Args: world,
    232         * x, y, z, startVec, endVec
    233         */
    234        public MovingObjectPosition collisionRayTrace(World par1World, int par2, int par3, int par4, Vec3 par5Vec3, Vec3 par6Vec3)
    235        {
    236            int var7 = par1World.getBlockMetadata(par2, par3, par4) & 7;
    237            float var8 = 0.15F;
    238
    239            if (var7 == 1)
    240            {
    241                this.setBlockBounds(0.0F, 0.2F, 0.5F - var8, var8 * 2.0F, 0.8F, 0.5F + var8);
    242            }
    243            else if (var7 == 2)
    244            {
    245                this.setBlockBounds(1.0F - var8 * 2.0F, 0.2F, 0.5F - var8, 1.0F, 0.8F, 0.5F + var8);
    246            }
    247            else if (var7 == 3)
    248            {
    249                this.setBlockBounds(0.5F - var8, 0.2F, 0.0F, 0.5F + var8, 0.8F, var8 * 2.0F);
    250            }
    251            else if (var7 == 4)
    252            {
    253                this.setBlockBounds(0.5F - var8, 0.2F, 1.0F - var8 * 2.0F, 0.5F + var8, 0.8F, 1.0F);
    254            }
    255            else
    256            {
    257                var8 = 0.1F;
    258                this.setBlockBounds(0.5F - var8, 0.0F, 0.5F - var8, 0.5F + var8, 0.6F, 0.5F + var8);
    259            }
    260
    261            return super.collisionRayTrace(par1World, par2, par3, par4, par5Vec3, par6Vec3);
    262        }
    263
    264        @SideOnly(Side.CLIENT)
    265
    266        /**
    267         * A randomly called display update to be able to add particles or other items for display
    268         */
    269        public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    270        {
    271            int var6 = par1World.getBlockMetadata(par2, par3, par4);
    272            double var7 = (double)((float)par2 + 0.5F);
    273            double var9 = (double)((float)par3 + 0.7F);
    274            double var11 = (double)((float)par4 + 0.5F);
    275            double var13 = 0.2199999988079071D;
    276            double var15 = 0.27000001072883606D;
    277
    278            if (var6 == 1)
    279            {
    280                par1World.spawnParticle("smoke", var7 - var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    281                par1World.spawnParticle("flame", var7 - var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    282            }
    283            else if (var6 == 2)
    284            {
    285                par1World.spawnParticle("smoke", var7 + var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    286                par1World.spawnParticle("flame", var7 + var15, var9 + var13, var11, 0.0D, 0.0D, 0.0D);
    287            }
    288            else if (var6 == 3)
    289            {
    290                par1World.spawnParticle("smoke", var7, var9 + var13, var11 - var15, 0.0D, 0.0D, 0.0D);
    291                par1World.spawnParticle("flame", var7, var9 + var13, var11 - var15, 0.0D, 0.0D, 0.0D);
    292            }
    293            else if (var6 == 4)
    294            {
    295                par1World.spawnParticle("smoke", var7, var9 + var13, var11 + var15, 0.0D, 0.0D, 0.0D);
    296                par1World.spawnParticle("flame", var7, var9 + var13, var11 + var15, 0.0D, 0.0D, 0.0D);
    297            }
    298            else
    299            {
    300                par1World.spawnParticle("smoke", var7, var9, var11, 0.0D, 0.0D, 0.0D);
    301                par1World.spawnParticle("flame", var7, var9, var11, 0.0D, 0.0D, 0.0D);
    302            }
    303        }
    304    }