| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.block.material.Material;
|
| 6 | import net.minecraft.block.properties.IProperty;
|
| 7 | import net.minecraft.block.properties.PropertyDirection;
|
| 8 | import net.minecraft.block.state.BlockState;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.creativetab.CreativeTabs;
|
| 11 | import net.minecraft.entity.EntityLivingBase;
|
| 12 | import net.minecraft.init.Blocks;
|
| 13 | import net.minecraft.util.AxisAlignedBB;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.util.EnumFacing;
|
| 16 | import net.minecraft.util.EnumParticleTypes;
|
| 17 | import net.minecraft.util.EnumWorldBlockLayer;
|
| 18 | import net.minecraft.util.MovingObjectPosition;
|
| 19 | import net.minecraft.util.Vec3;
|
| 20 | import net.minecraft.world.World;
|
| 21 |
|
| 22 | public class BlockTorch extends Block
|
| 23 | {
|
| 24 | public static final PropertyDirection FACING = PropertyDirection.create("facing", new Predicate<EnumFacing>()
|
| 25 | {
|
| 26 | public boolean apply(EnumFacing p_apply_1_)
|
| 27 | {
|
| 28 | return p_apply_1_ != EnumFacing.DOWN;
|
| 29 | }
|
| 30 | });
|
| 31 |
|
| 32 | protected BlockTorch()
|
| 33 | {
|
| 34 | super(Material.circuits);
|
| 35 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.UP));
|
| 36 | this.setTickRandomly(true);
|
| 37 | this.setCreativeTab(CreativeTabs.tabDecorations);
|
| 38 | }
|
| 39 |
|
| 40 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 41 | {
|
| 42 | return null;
|
| 43 | }
|
| 44 |
|
| 45 | /**
|
| 46 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 47 | */
|
| 48 | public boolean isOpaqueCube()
|
| 49 | {
|
| 50 | return false;
|
| 51 | }
|
| 52 |
|
| 53 | public boolean isFullCube()
|
| 54 | {
|
| 55 | return false;
|
| 56 | }
|
| 57 |
|
| 58 | private boolean canPlaceOn(World worldIn, BlockPos pos)
|
| 59 | {
|
| 60 | if (World.doesBlockHaveSolidTopSurface(worldIn, pos))
|
| 61 | {
|
| 62 | return true;
|
| 63 | }
|
| 64 | else
|
| 65 | {
|
| 66 | Block block = worldIn.getBlockState(pos).getBlock();
|
| 67 | return block instanceof BlockFence || block == Blocks.glass || block == Blocks.cobblestone_wall || block == Blocks.stained_glass;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
|
| 72 | {
|
| 73 | for (EnumFacing enumfacing : FACING.getAllowedValues())
|
| 74 | {
|
| 75 | if (this.canPlaceAt(worldIn, pos, enumfacing))
|
| 76 | {
|
| 77 | return true;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | return false;
|
| 82 | }
|
| 83 |
|
| 84 | private boolean canPlaceAt(World worldIn, BlockPos pos, EnumFacing facing)
|
| 85 | {
|
| 86 | BlockPos blockpos = pos.offset(facing.getOpposite());
|
| 87 | boolean flag = facing.getAxis().isHorizontal();
|
| 88 | return flag && worldIn.isBlockNormalCube(blockpos, true) || facing.equals(EnumFacing.UP) && this.canPlaceOn(worldIn, blockpos);
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 93 | * IBlockstate
|
| 94 | */
|
| 95 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 96 | {
|
| 97 | if (this.canPlaceAt(worldIn, pos, facing))
|
| 98 | {
|
| 99 | return this.getDefaultState().withProperty(FACING, facing);
|
| 100 | }
|
| 101 | else
|
| 102 | {
|
| 103 | for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
|
| 104 | {
|
| 105 | if (worldIn.isBlockNormalCube(pos.offset(enumfacing.getOpposite()), true))
|
| 106 | {
|
| 107 | return this.getDefaultState().withProperty(FACING, enumfacing);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | return this.getDefaultState();
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 116 | {
|
| 117 | this.checkForDrop(worldIn, pos, state);
|
| 118 | }
|
| 119 |
|
| 120 | /**
|
| 121 | * Called when a neighboring block changes.
|
| 122 | */
|
| 123 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 124 | {
|
| 125 | this.onNeighborChangeInternal(worldIn, pos, state);
|
| 126 | }
|
| 127 |
|
| 128 | protected boolean onNeighborChangeInternal(World worldIn, BlockPos pos, IBlockState state)
|
| 129 | {
|
| 130 | if (!this.checkForDrop(worldIn, pos, state))
|
| 131 | {
|
| 132 | return true;
|
| 133 | }
|
| 134 | else
|
| 135 | {
|
| 136 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 137 | EnumFacing.Axis enumfacing$axis = enumfacing.getAxis();
|
| 138 | EnumFacing enumfacing1 = enumfacing.getOpposite();
|
| 139 | boolean flag = false;
|
| 140 |
|
| 141 | if (enumfacing$axis.isHorizontal() && !worldIn.isBlockNormalCube(pos.offset(enumfacing1), true))
|
| 142 | {
|
| 143 | flag = true;
|
| 144 | }
|
| 145 | else if (enumfacing$axis.isVertical() && !this.canPlaceOn(worldIn, pos.offset(enumfacing1)))
|
| 146 | {
|
| 147 | flag = true;
|
| 148 | }
|
| 149 |
|
| 150 | if (flag)
|
| 151 | {
|
| 152 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 153 | worldIn.setBlockToAir(pos);
|
| 154 | return true;
|
| 155 | }
|
| 156 | else
|
| 157 | {
|
| 158 | return false;
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | protected boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state)
|
| 164 | {
|
| 165 | if (state.getBlock() == this && this.canPlaceAt(worldIn, pos, (EnumFacing)state.getValue(FACING)))
|
| 166 | {
|
| 167 | return true;
|
| 168 | }
|
| 169 | else
|
| 170 | {
|
| 171 | if (worldIn.getBlockState(pos).getBlock() == this)
|
| 172 | {
|
| 173 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 174 | worldIn.setBlockToAir(pos);
|
| 175 | }
|
| 176 |
|
| 177 | return false;
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Ray traces through the blocks collision from start vector to end vector returning a ray trace hit.
|
| 183 | */
|
| 184 | public MovingObjectPosition collisionRayTrace(World worldIn, BlockPos pos, Vec3 start, Vec3 end)
|
| 185 | {
|
| 186 | EnumFacing enumfacing = (EnumFacing)worldIn.getBlockState(pos).getValue(FACING);
|
| 187 | float f = 0.15F;
|
| 188 |
|
| 189 | if (enumfacing == EnumFacing.EAST)
|
| 190 | {
|
| 191 | this.setBlockBounds(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f);
|
| 192 | }
|
| 193 | else if (enumfacing == EnumFacing.WEST)
|
| 194 | {
|
| 195 | this.setBlockBounds(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f);
|
| 196 | }
|
| 197 | else if (enumfacing == EnumFacing.SOUTH)
|
| 198 | {
|
| 199 | this.setBlockBounds(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F);
|
| 200 | }
|
| 201 | else if (enumfacing == EnumFacing.NORTH)
|
| 202 | {
|
| 203 | this.setBlockBounds(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F);
|
| 204 | }
|
| 205 | else
|
| 206 | {
|
| 207 | f = 0.1F;
|
| 208 | this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.6F, 0.5F + f);
|
| 209 | }
|
| 210 |
|
| 211 | return super.collisionRayTrace(worldIn, pos, start, end);
|
| 212 | }
|
| 213 |
|
| 214 | public void randomDisplayTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 215 | {
|
| 216 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 217 | double d0 = (double)pos.getX() + 0.5D;
|
| 218 | double d1 = (double)pos.getY() + 0.7D;
|
| 219 | double d2 = (double)pos.getZ() + 0.5D;
|
| 220 | double d3 = 0.22D;
|
| 221 | double d4 = 0.27D;
|
| 222 |
|
| 223 | if (enumfacing.getAxis().isHorizontal())
|
| 224 | {
|
| 225 | EnumFacing enumfacing1 = enumfacing.getOpposite();
|
| 226 | worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4 * (double)enumfacing1.getFrontOffsetX(), d1 + d3, d2 + d4 * (double)enumfacing1.getFrontOffsetZ(), 0.0D, 0.0D, 0.0D, new int[0]);
|
| 227 | worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4 * (double)enumfacing1.getFrontOffsetX(), d1 + d3, d2 + d4 * (double)enumfacing1.getFrontOffsetZ(), 0.0D, 0.0D, 0.0D, new int[0]);
|
| 228 | }
|
| 229 | else
|
| 230 | {
|
| 231 | worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0, d1, d2, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 232 | worldIn.spawnParticle(EnumParticleTypes.FLAME, d0, d1, d2, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | public EnumWorldBlockLayer getBlockLayer()
|
| 237 | {
|
| 238 | return EnumWorldBlockLayer.CUTOUT;
|
| 239 | }
|
| 240 |
|
| 241 | /**
|
| 242 | * Convert the given metadata into a BlockState for this Block
|
| 243 | */
|
| 244 | public IBlockState getStateFromMeta(int meta)
|
| 245 | {
|
| 246 | IBlockState iblockstate = this.getDefaultState();
|
| 247 |
|
| 248 | switch (meta)
|
| 249 | {
|
| 250 | case 1:
|
| 251 | iblockstate = iblockstate.withProperty(FACING, EnumFacing.EAST);
|
| 252 | break;
|
| 253 |
|
| 254 | case 2:
|
| 255 | iblockstate = iblockstate.withProperty(FACING, EnumFacing.WEST);
|
| 256 | break;
|
| 257 |
|
| 258 | case 3:
|
| 259 | iblockstate = iblockstate.withProperty(FACING, EnumFacing.SOUTH);
|
| 260 | break;
|
| 261 |
|
| 262 | case 4:
|
| 263 | iblockstate = iblockstate.withProperty(FACING, EnumFacing.NORTH);
|
| 264 | break;
|
| 265 |
|
| 266 | case 5:
|
| 267 | default:
|
| 268 | iblockstate = iblockstate.withProperty(FACING, EnumFacing.UP);
|
| 269 | }
|
| 270 |
|
| 271 | return iblockstate;
|
| 272 | }
|
| 273 |
|
| 274 | /**
|
| 275 | * Convert the BlockState into the correct metadata value
|
| 276 | */
|
| 277 | public int getMetaFromState(IBlockState state)
|
| 278 | {
|
| 279 | int i = 0;
|
| 280 |
|
| 281 | switch ((EnumFacing)state.getValue(FACING))
|
| 282 | {
|
| 283 | case EAST:
|
| 284 | i = i | 1;
|
| 285 | break;
|
| 286 |
|
| 287 | case WEST:
|
| 288 | i = i | 2;
|
| 289 | break;
|
| 290 |
|
| 291 | case SOUTH:
|
| 292 | i = i | 3;
|
| 293 | break;
|
| 294 |
|
| 295 | case NORTH:
|
| 296 | i = i | 4;
|
| 297 | break;
|
| 298 |
|
| 299 | case DOWN:
|
| 300 | case UP:
|
| 301 | default:
|
| 302 | i = i | 5;
|
| 303 | }
|
| 304 |
|
| 305 | return i;
|
| 306 | }
|
| 307 |
|
| 308 | protected BlockState createBlockState()
|
| 309 | {
|
| 310 | return new BlockState(this, new IProperty[] {FACING});
|
| 311 | }
|
| 312 | }
|
| 313 |
|