| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 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.state.BlockState;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.entity.Entity;
|
| 11 | import net.minecraft.entity.player.EntityPlayer;
|
| 12 | import net.minecraft.init.Blocks;
|
| 13 | import net.minecraft.init.Items;
|
| 14 | import net.minecraft.item.Item;
|
| 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 BlockTripWire extends Block
|
| 23 | {
|
| 24 | public static final PropertyBool POWERED = PropertyBool.create("powered");
|
| 25 | public static final PropertyBool SUSPENDED = PropertyBool.create("suspended");
|
| 26 | public static final PropertyBool ATTACHED = PropertyBool.create("attached");
|
| 27 | public static final PropertyBool DISARMED = PropertyBool.create("disarmed");
|
| 28 | public static final PropertyBool NORTH = PropertyBool.create("north");
|
| 29 | public static final PropertyBool EAST = PropertyBool.create("east");
|
| 30 | public static final PropertyBool SOUTH = PropertyBool.create("south");
|
| 31 | public static final PropertyBool WEST = PropertyBool.create("west");
|
| 32 |
|
| 33 | public BlockTripWire()
|
| 34 | {
|
| 35 | super(Material.circuits);
|
| 36 | this.setDefaultState(this.blockState.getBaseState().withProperty(POWERED, Boolean.valueOf(false)).withProperty(SUSPENDED, Boolean.valueOf(false)).withProperty(ATTACHED, Boolean.valueOf(false)).withProperty(DISARMED, Boolean.valueOf(false)).withProperty(NORTH, Boolean.valueOf(false)).withProperty(EAST, Boolean.valueOf(false)).withProperty(SOUTH, Boolean.valueOf(false)).withProperty(WEST, Boolean.valueOf(false)));
|
| 37 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.15625F, 1.0F);
|
| 38 | this.setTickRandomly(true);
|
| 39 | }
|
| 40 |
|
| 41 | /**
|
| 42 | * Get the actual Block state of this Block at the given position. This applies properties not visible in the
|
| 43 | * metadata, such as fence connections.
|
| 44 | */
|
| 45 | public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
|
| 46 | {
|
| 47 | return state.withProperty(NORTH, Boolean.valueOf(isConnectedTo(worldIn, pos, state, EnumFacing.NORTH))).withProperty(EAST, Boolean.valueOf(isConnectedTo(worldIn, pos, state, EnumFacing.EAST))).withProperty(SOUTH, Boolean.valueOf(isConnectedTo(worldIn, pos, state, EnumFacing.SOUTH))).withProperty(WEST, Boolean.valueOf(isConnectedTo(worldIn, pos, state, EnumFacing.WEST)));
|
| 48 | }
|
| 49 |
|
| 50 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 51 | {
|
| 52 | return null;
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 57 | */
|
| 58 | public boolean isOpaqueCube()
|
| 59 | {
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | public boolean isFullCube()
|
| 64 | {
|
| 65 | return false;
|
| 66 | }
|
| 67 |
|
| 68 | public EnumWorldBlockLayer getBlockLayer()
|
| 69 | {
|
| 70 | return EnumWorldBlockLayer.TRANSLUCENT;
|
| 71 | }
|
| 72 |
|
| 73 | /**
|
| 74 | * Get the Item that this Block should drop when harvested.
|
| 75 | */
|
| 76 | public Item getItemDropped(IBlockState state, Random rand, int fortune)
|
| 77 | {
|
| 78 | return Items.string;
|
| 79 | }
|
| 80 |
|
| 81 | public Item getItem(World worldIn, BlockPos pos)
|
| 82 | {
|
| 83 | return Items.string;
|
| 84 | }
|
| 85 |
|
| 86 | /**
|
| 87 | * Called when a neighboring block changes.
|
| 88 | */
|
| 89 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 90 | {
|
| 91 | boolean flag = ((Boolean)state.getValue(SUSPENDED)).booleanValue();
|
| 92 | boolean flag1 = !World.doesBlockHaveSolidTopSurface(worldIn, pos.down());
|
| 93 |
|
| 94 | if (flag != flag1)
|
| 95 | {
|
| 96 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 97 | worldIn.setBlockToAir(pos);
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 102 | {
|
| 103 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 104 | boolean flag = ((Boolean)iblockstate.getValue(ATTACHED)).booleanValue();
|
| 105 | boolean flag1 = ((Boolean)iblockstate.getValue(SUSPENDED)).booleanValue();
|
| 106 |
|
| 107 | if (!flag1)
|
| 108 | {
|
| 109 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.09375F, 1.0F);
|
| 110 | }
|
| 111 | else if (!flag)
|
| 112 | {
|
| 113 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
|
| 114 | }
|
| 115 | else
|
| 116 | {
|
| 117 | this.setBlockBounds(0.0F, 0.0625F, 0.0F, 1.0F, 0.15625F, 1.0F);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 122 | {
|
| 123 | state = state.withProperty(SUSPENDED, Boolean.valueOf(!World.doesBlockHaveSolidTopSurface(worldIn, pos.down())));
|
| 124 | worldIn.setBlockState(pos, state, 3);
|
| 125 | this.notifyHook(worldIn, pos, state);
|
| 126 | }
|
| 127 |
|
| 128 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 129 | {
|
| 130 | this.notifyHook(worldIn, pos, state.withProperty(POWERED, Boolean.valueOf(true)));
|
| 131 | }
|
| 132 |
|
| 133 | public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
|
| 134 | {
|
| 135 | if (!worldIn.isRemote)
|
| 136 | {
|
| 137 | if (player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() == Items.shears)
|
| 138 | {
|
| 139 | worldIn.setBlockState(pos, state.withProperty(DISARMED, Boolean.valueOf(true)), 4);
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | private void notifyHook(World worldIn, BlockPos pos, IBlockState state)
|
| 145 | {
|
| 146 | for (EnumFacing enumfacing : new EnumFacing[] {EnumFacing.SOUTH, EnumFacing.WEST})
|
| 147 | {
|
| 148 | for (int i = 1; i < 42; ++i)
|
| 149 | {
|
| 150 | BlockPos blockpos = pos.offset(enumfacing, i);
|
| 151 | IBlockState iblockstate = worldIn.getBlockState(blockpos);
|
| 152 |
|
| 153 | if (iblockstate.getBlock() == Blocks.tripwire_hook)
|
| 154 | {
|
| 155 | if (iblockstate.getValue(BlockTripWireHook.FACING) == enumfacing.getOpposite())
|
| 156 | {
|
| 157 | Blocks.tripwire_hook.func_176260_a(worldIn, blockpos, iblockstate, false, true, i, state);
|
| 158 | }
|
| 159 |
|
| 160 | break;
|
| 161 | }
|
| 162 |
|
| 163 | if (iblockstate.getBlock() != Blocks.tripwire)
|
| 164 | {
|
| 165 | break;
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | /**
|
| 172 | * Called When an Entity Collided with the Block
|
| 173 | */
|
| 174 | public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
|
| 175 | {
|
| 176 | if (!worldIn.isRemote)
|
| 177 | {
|
| 178 | if (!((Boolean)state.getValue(POWERED)).booleanValue())
|
| 179 | {
|
| 180 | this.updateState(worldIn, pos);
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | /**
|
| 186 | * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
|
| 187 | */
|
| 188 | public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
|
| 189 | {
|
| 190 | }
|
| 191 |
|
| 192 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 193 | {
|
| 194 | if (!worldIn.isRemote)
|
| 195 | {
|
| 196 | if (((Boolean)worldIn.getBlockState(pos).getValue(POWERED)).booleanValue())
|
| 197 | {
|
| 198 | this.updateState(worldIn, pos);
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | private void updateState(World worldIn, BlockPos pos)
|
| 204 | {
|
| 205 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 206 | boolean flag = ((Boolean)iblockstate.getValue(POWERED)).booleanValue();
|
| 207 | boolean flag1 = false;
|
| 208 | List <? extends Entity > list = worldIn.getEntitiesWithinAABBExcludingEntity((Entity)null, new AxisAlignedBB((double)pos.getX() + this.minX, (double)pos.getY() + this.minY, (double)pos.getZ() + this.minZ, (double)pos.getX() + this.maxX, (double)pos.getY() + this.maxY, (double)pos.getZ() + this.maxZ));
|
| 209 |
|
| 210 | if (!list.isEmpty())
|
| 211 | {
|
| 212 | for (Entity entity : list)
|
| 213 | {
|
| 214 | if (!entity.doesEntityNotTriggerPressurePlate())
|
| 215 | {
|
| 216 | flag1 = true;
|
| 217 | break;
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | if (flag1 != flag)
|
| 223 | {
|
| 224 | iblockstate = iblockstate.withProperty(POWERED, Boolean.valueOf(flag1));
|
| 225 | worldIn.setBlockState(pos, iblockstate, 3);
|
| 226 | this.notifyHook(worldIn, pos, iblockstate);
|
| 227 | }
|
| 228 |
|
| 229 | if (flag1)
|
| 230 | {
|
| 231 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | public static boolean isConnectedTo(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing direction)
|
| 236 | {
|
| 237 | BlockPos blockpos = pos.offset(direction);
|
| 238 | IBlockState iblockstate = worldIn.getBlockState(blockpos);
|
| 239 | Block block = iblockstate.getBlock();
|
| 240 |
|
| 241 | if (block == Blocks.tripwire_hook)
|
| 242 | {
|
| 243 | EnumFacing enumfacing = direction.getOpposite();
|
| 244 | return iblockstate.getValue(BlockTripWireHook.FACING) == enumfacing;
|
| 245 | }
|
| 246 | else if (block == Blocks.tripwire)
|
| 247 | {
|
| 248 | boolean flag = ((Boolean)state.getValue(SUSPENDED)).booleanValue();
|
| 249 | boolean flag1 = ((Boolean)iblockstate.getValue(SUSPENDED)).booleanValue();
|
| 250 | return flag == flag1;
|
| 251 | }
|
| 252 | else
|
| 253 | {
|
| 254 | return false;
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | /**
|
| 259 | * Convert the given metadata into a BlockState for this Block
|
| 260 | */
|
| 261 | public IBlockState getStateFromMeta(int meta)
|
| 262 | {
|
| 263 | return this.getDefaultState().withProperty(POWERED, Boolean.valueOf((meta & 1) > 0)).withProperty(SUSPENDED, Boolean.valueOf((meta & 2) > 0)).withProperty(ATTACHED, Boolean.valueOf((meta & 4) > 0)).withProperty(DISARMED, Boolean.valueOf((meta & 8) > 0));
|
| 264 | }
|
| 265 |
|
| 266 | /**
|
| 267 | * Convert the BlockState into the correct metadata value
|
| 268 | */
|
| 269 | public int getMetaFromState(IBlockState state)
|
| 270 | {
|
| 271 | int i = 0;
|
| 272 |
|
| 273 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 274 | {
|
| 275 | i |= 1;
|
| 276 | }
|
| 277 |
|
| 278 | if (((Boolean)state.getValue(SUSPENDED)).booleanValue())
|
| 279 | {
|
| 280 | i |= 2;
|
| 281 | }
|
| 282 |
|
| 283 | if (((Boolean)state.getValue(ATTACHED)).booleanValue())
|
| 284 | {
|
| 285 | i |= 4;
|
| 286 | }
|
| 287 |
|
| 288 | if (((Boolean)state.getValue(DISARMED)).booleanValue())
|
| 289 | {
|
| 290 | i |= 8;
|
| 291 | }
|
| 292 |
|
| 293 | return i;
|
| 294 | }
|
| 295 |
|
| 296 | protected BlockState createBlockState()
|
| 297 | {
|
| 298 | return new BlockState(this, new IProperty[] {POWERED, SUSPENDED, ATTACHED, DISARMED, NORTH, EAST, WEST, SOUTH});
|
| 299 | }
|
| 300 | }
|
| 301 |
|