| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import com.google.common.base.Objects;
|
| 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.PropertyBool;
|
| 8 | import net.minecraft.block.properties.PropertyDirection;
|
| 9 | import net.minecraft.block.state.BlockState;
|
| 10 | import net.minecraft.block.state.IBlockState;
|
| 11 | import net.minecraft.creativetab.CreativeTabs;
|
| 12 | import net.minecraft.entity.EntityLivingBase;
|
| 13 | import net.minecraft.init.Blocks;
|
| 14 | import net.minecraft.item.ItemStack;
|
| 15 | import net.minecraft.util.AxisAlignedBB;
|
| 16 | import net.minecraft.util.BlockPos;
|
| 17 | import net.minecraft.util.EnumFacing;
|
| 18 | import net.minecraft.util.EnumWorldBlockLayer;
|
| 19 | import net.minecraft.world.IBlockAccess;
|
| 20 | import net.minecraft.world.World;
|
| 21 |
|
| 22 | public class BlockTripWireHook extends Block
|
| 23 | {
|
| 24 | public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
|
| 25 | public static final PropertyBool POWERED = PropertyBool.create("powered");
|
| 26 | public static final PropertyBool ATTACHED = PropertyBool.create("attached");
|
| 27 | public static final PropertyBool SUSPENDED = PropertyBool.create("suspended");
|
| 28 |
|
| 29 | public BlockTripWireHook()
|
| 30 | {
|
| 31 | super(Material.circuits);
|
| 32 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(POWERED, Boolean.valueOf(false)).withProperty(ATTACHED, Boolean.valueOf(false)).withProperty(SUSPENDED, Boolean.valueOf(false)));
|
| 33 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 34 | this.setTickRandomly(true);
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * Get the actual Block state of this Block at the given position. This applies properties not visible in the
|
| 39 | * metadata, such as fence connections.
|
| 40 | */
|
| 41 | public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
|
| 42 | {
|
| 43 | return state.withProperty(SUSPENDED, Boolean.valueOf(!World.doesBlockHaveSolidTopSurface(worldIn, pos.down())));
|
| 44 | }
|
| 45 |
|
| 46 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 47 | {
|
| 48 | return null;
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 53 | */
|
| 54 | public boolean isOpaqueCube()
|
| 55 | {
|
| 56 | return false;
|
| 57 | }
|
| 58 |
|
| 59 | public boolean isFullCube()
|
| 60 | {
|
| 61 | return false;
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Check whether this Block can be placed on the given side
|
| 66 | */
|
| 67 | public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side)
|
| 68 | {
|
| 69 | return side.getAxis().isHorizontal() && worldIn.getBlockState(pos.offset(side.getOpposite())).getBlock().isNormalCube();
|
| 70 | }
|
| 71 |
|
| 72 | public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
|
| 73 | {
|
| 74 | for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
|
| 75 | {
|
| 76 | if (worldIn.getBlockState(pos.offset(enumfacing)).getBlock().isNormalCube())
|
| 77 | {
|
| 78 | return true;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 87 | * IBlockstate
|
| 88 | */
|
| 89 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 90 | {
|
| 91 | IBlockState iblockstate = this.getDefaultState().withProperty(POWERED, Boolean.valueOf(false)).withProperty(ATTACHED, Boolean.valueOf(false)).withProperty(SUSPENDED, Boolean.valueOf(false));
|
| 92 |
|
| 93 | if (facing.getAxis().isHorizontal())
|
| 94 | {
|
| 95 | iblockstate = iblockstate.withProperty(FACING, facing);
|
| 96 | }
|
| 97 |
|
| 98 | return iblockstate;
|
| 99 | }
|
| 100 |
|
| 101 | /**
|
| 102 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 103 | */
|
| 104 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 105 | {
|
| 106 | this.func_176260_a(worldIn, pos, state, false, false, -1, (IBlockState)null);
|
| 107 | }
|
| 108 |
|
| 109 | /**
|
| 110 | * Called when a neighboring block changes.
|
| 111 | */
|
| 112 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 113 | {
|
| 114 | if (neighborBlock != this)
|
| 115 | {
|
| 116 | if (this.checkForDrop(worldIn, pos, state))
|
| 117 | {
|
| 118 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 119 |
|
| 120 | if (!worldIn.getBlockState(pos.offset(enumfacing.getOpposite())).getBlock().isNormalCube())
|
| 121 | {
|
| 122 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 123 | worldIn.setBlockToAir(pos);
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | public void func_176260_a(World worldIn, BlockPos pos, IBlockState hookState, boolean p_176260_4_, boolean p_176260_5_, int p_176260_6_, IBlockState p_176260_7_)
|
| 130 | {
|
| 131 | EnumFacing enumfacing = (EnumFacing)hookState.getValue(FACING);
|
| 132 | boolean flag = ((Boolean)hookState.getValue(ATTACHED)).booleanValue();
|
| 133 | boolean flag1 = ((Boolean)hookState.getValue(POWERED)).booleanValue();
|
| 134 | boolean flag2 = !World.doesBlockHaveSolidTopSurface(worldIn, pos.down());
|
| 135 | boolean flag3 = !p_176260_4_;
|
| 136 | boolean flag4 = false;
|
| 137 | int i = 0;
|
| 138 | IBlockState[] aiblockstate = new IBlockState[42];
|
| 139 |
|
| 140 | for (int j = 1; j < 42; ++j)
|
| 141 | {
|
| 142 | BlockPos blockpos = pos.offset(enumfacing, j);
|
| 143 | IBlockState iblockstate = worldIn.getBlockState(blockpos);
|
| 144 |
|
| 145 | if (iblockstate.getBlock() == Blocks.tripwire_hook)
|
| 146 | {
|
| 147 | if (iblockstate.getValue(FACING) == enumfacing.getOpposite())
|
| 148 | {
|
| 149 | i = j;
|
| 150 | }
|
| 151 |
|
| 152 | break;
|
| 153 | }
|
| 154 |
|
| 155 | if (iblockstate.getBlock() != Blocks.tripwire && j != p_176260_6_)
|
| 156 | {
|
| 157 | aiblockstate[j] = null;
|
| 158 | flag3 = false;
|
| 159 | }
|
| 160 | else
|
| 161 | {
|
| 162 | if (j == p_176260_6_)
|
| 163 | {
|
| 164 | iblockstate = (IBlockState)Objects.firstNonNull(p_176260_7_, iblockstate);
|
| 165 | }
|
| 166 |
|
| 167 | boolean flag5 = !((Boolean)iblockstate.getValue(BlockTripWire.DISARMED)).booleanValue();
|
| 168 | boolean flag6 = ((Boolean)iblockstate.getValue(BlockTripWire.POWERED)).booleanValue();
|
| 169 | boolean flag7 = ((Boolean)iblockstate.getValue(BlockTripWire.SUSPENDED)).booleanValue();
|
| 170 | flag3 &= flag7 == flag2;
|
| 171 | flag4 |= flag5 && flag6;
|
| 172 | aiblockstate[j] = iblockstate;
|
| 173 |
|
| 174 | if (j == p_176260_6_)
|
| 175 | {
|
| 176 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 177 | flag3 &= flag5;
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | flag3 = flag3 & i > 1;
|
| 183 | flag4 = flag4 & flag3;
|
| 184 | IBlockState iblockstate1 = this.getDefaultState().withProperty(ATTACHED, Boolean.valueOf(flag3)).withProperty(POWERED, Boolean.valueOf(flag4));
|
| 185 |
|
| 186 | if (i > 0)
|
| 187 | {
|
| 188 | BlockPos blockpos1 = pos.offset(enumfacing, i);
|
| 189 | EnumFacing enumfacing1 = enumfacing.getOpposite();
|
| 190 | worldIn.setBlockState(blockpos1, iblockstate1.withProperty(FACING, enumfacing1), 3);
|
| 191 | this.func_176262_b(worldIn, blockpos1, enumfacing1);
|
| 192 | this.func_180694_a(worldIn, blockpos1, flag3, flag4, flag, flag1);
|
| 193 | }
|
| 194 |
|
| 195 | this.func_180694_a(worldIn, pos, flag3, flag4, flag, flag1);
|
| 196 |
|
| 197 | if (!p_176260_4_)
|
| 198 | {
|
| 199 | worldIn.setBlockState(pos, iblockstate1.withProperty(FACING, enumfacing), 3);
|
| 200 |
|
| 201 | if (p_176260_5_)
|
| 202 | {
|
| 203 | this.func_176262_b(worldIn, pos, enumfacing);
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | if (flag != flag3)
|
| 208 | {
|
| 209 | for (int k = 1; k < i; ++k)
|
| 210 | {
|
| 211 | BlockPos blockpos2 = pos.offset(enumfacing, k);
|
| 212 | IBlockState iblockstate2 = aiblockstate[k];
|
| 213 |
|
| 214 | if (iblockstate2 != null && worldIn.getBlockState(blockpos2).getBlock() != Blocks.air)
|
| 215 | {
|
| 216 | worldIn.setBlockState(blockpos2, iblockstate2.withProperty(ATTACHED, Boolean.valueOf(flag3)), 3);
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | /**
|
| 223 | * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
|
| 224 | */
|
| 225 | public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
|
| 226 | {
|
| 227 | }
|
| 228 |
|
| 229 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 230 | {
|
| 231 | this.func_176260_a(worldIn, pos, state, false, true, -1, (IBlockState)null);
|
| 232 | }
|
| 233 |
|
| 234 | private void func_180694_a(World worldIn, BlockPos pos, boolean p_180694_3_, boolean p_180694_4_, boolean p_180694_5_, boolean p_180694_6_)
|
| 235 | {
|
| 236 | if (p_180694_4_ && !p_180694_6_)
|
| 237 | {
|
| 238 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.1D, (double)pos.getZ() + 0.5D, "random.click", 0.4F, 0.6F);
|
| 239 | }
|
| 240 | else if (!p_180694_4_ && p_180694_6_)
|
| 241 | {
|
| 242 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.1D, (double)pos.getZ() + 0.5D, "random.click", 0.4F, 0.5F);
|
| 243 | }
|
| 244 | else if (p_180694_3_ && !p_180694_5_)
|
| 245 | {
|
| 246 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.1D, (double)pos.getZ() + 0.5D, "random.click", 0.4F, 0.7F);
|
| 247 | }
|
| 248 | else if (!p_180694_3_ && p_180694_5_)
|
| 249 | {
|
| 250 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.1D, (double)pos.getZ() + 0.5D, "random.bowhit", 0.4F, 1.2F / (worldIn.rand.nextFloat() * 0.2F + 0.9F));
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | private void func_176262_b(World worldIn, BlockPos p_176262_2_, EnumFacing p_176262_3_)
|
| 255 | {
|
| 256 | worldIn.notifyNeighborsOfStateChange(p_176262_2_, this);
|
| 257 | worldIn.notifyNeighborsOfStateChange(p_176262_2_.offset(p_176262_3_.getOpposite()), this);
|
| 258 | }
|
| 259 |
|
| 260 | private boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state)
|
| 261 | {
|
| 262 | if (!this.canPlaceBlockAt(worldIn, pos))
|
| 263 | {
|
| 264 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 265 | worldIn.setBlockToAir(pos);
|
| 266 | return false;
|
| 267 | }
|
| 268 | else
|
| 269 | {
|
| 270 | return true;
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | @SuppressWarnings("incomplete-switch")
|
| 275 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 276 | {
|
| 277 | float f = 0.1875F;
|
| 278 |
|
| 279 | switch ((EnumFacing)worldIn.getBlockState(pos).getValue(FACING))
|
| 280 | {
|
| 281 | case EAST:
|
| 282 | this.setBlockBounds(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f);
|
| 283 | break;
|
| 284 |
|
| 285 | case WEST:
|
| 286 | this.setBlockBounds(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f);
|
| 287 | break;
|
| 288 |
|
| 289 | case SOUTH:
|
| 290 | this.setBlockBounds(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F);
|
| 291 | break;
|
| 292 |
|
| 293 | case NORTH:
|
| 294 | this.setBlockBounds(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F);
|
| 295 | }
|
| 296 | }
|
| 297 |
|
| 298 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 299 | {
|
| 300 | boolean flag = ((Boolean)state.getValue(ATTACHED)).booleanValue();
|
| 301 | boolean flag1 = ((Boolean)state.getValue(POWERED)).booleanValue();
|
| 302 |
|
| 303 | if (flag || flag1)
|
| 304 | {
|
| 305 | this.func_176260_a(worldIn, pos, state, true, false, -1, (IBlockState)null);
|
| 306 | }
|
| 307 |
|
| 308 | if (flag1)
|
| 309 | {
|
| 310 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 311 | worldIn.notifyNeighborsOfStateChange(pos.offset(((EnumFacing)state.getValue(FACING)).getOpposite()), this);
|
| 312 | }
|
| 313 |
|
| 314 | super.breakBlock(worldIn, pos, state);
|
| 315 | }
|
| 316 |
|
| 317 | public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 318 | {
|
| 319 | return ((Boolean)state.getValue(POWERED)).booleanValue() ? 15 : 0;
|
| 320 | }
|
| 321 |
|
| 322 | public int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 323 | {
|
| 324 | return !((Boolean)state.getValue(POWERED)).booleanValue() ? 0 : (state.getValue(FACING) == side ? 15 : 0);
|
| 325 | }
|
| 326 |
|
| 327 | /**
|
| 328 | * Can this block provide power. Only wire currently seems to have this change based on its state.
|
| 329 | */
|
| 330 | public boolean canProvidePower()
|
| 331 | {
|
| 332 | return true;
|
| 333 | }
|
| 334 |
|
| 335 | public EnumWorldBlockLayer getBlockLayer()
|
| 336 | {
|
| 337 | return EnumWorldBlockLayer.CUTOUT_MIPPED;
|
| 338 | }
|
| 339 |
|
| 340 | /**
|
| 341 | * Convert the given metadata into a BlockState for this Block
|
| 342 | */
|
| 343 | public IBlockState getStateFromMeta(int meta)
|
| 344 | {
|
| 345 | return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta & 3)).withProperty(POWERED, Boolean.valueOf((meta & 8) > 0)).withProperty(ATTACHED, Boolean.valueOf((meta & 4) > 0));
|
| 346 | }
|
| 347 |
|
| 348 | /**
|
| 349 | * Convert the BlockState into the correct metadata value
|
| 350 | */
|
| 351 | public int getMetaFromState(IBlockState state)
|
| 352 | {
|
| 353 | int i = 0;
|
| 354 | i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();
|
| 355 |
|
| 356 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 357 | {
|
| 358 | i |= 8;
|
| 359 | }
|
| 360 |
|
| 361 | if (((Boolean)state.getValue(ATTACHED)).booleanValue())
|
| 362 | {
|
| 363 | i |= 4;
|
| 364 | }
|
| 365 |
|
| 366 | return i;
|
| 367 | }
|
| 368 |
|
| 369 | protected BlockState createBlockState()
|
| 370 | {
|
| 371 | return new BlockState(this, new IProperty[] {FACING, POWERED, ATTACHED, SUSPENDED});
|
| 372 | }
|
| 373 | }
|
| 374 |
|