| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Random;
|
| 6 | import net.minecraft.block.properties.IProperty;
|
| 7 | import net.minecraft.block.properties.PropertyBool;
|
| 8 | import net.minecraft.block.properties.PropertyEnum;
|
| 9 | import net.minecraft.block.state.BlockState;
|
| 10 | import net.minecraft.block.state.IBlockState;
|
| 11 | import net.minecraft.entity.Entity;
|
| 12 | import net.minecraft.entity.EntityMinecartCommandBlock;
|
| 13 | import net.minecraft.entity.item.EntityMinecart;
|
| 14 | import net.minecraft.inventory.Container;
|
| 15 | import net.minecraft.inventory.IInventory;
|
| 16 | import net.minecraft.util.AxisAlignedBB;
|
| 17 | import net.minecraft.util.BlockPos;
|
| 18 | import net.minecraft.util.EntitySelectors;
|
| 19 | import net.minecraft.util.EnumFacing;
|
| 20 | import net.minecraft.world.IBlockAccess;
|
| 21 | import net.minecraft.world.World;
|
| 22 |
|
| 23 | public class BlockRailDetector extends BlockRailBase
|
| 24 | {
|
| 25 | public static final PropertyEnum<BlockRailBase.EnumRailDirection> SHAPE = PropertyEnum.<BlockRailBase.EnumRailDirection>create("shape", BlockRailBase.EnumRailDirection.class, new Predicate<BlockRailBase.EnumRailDirection>()
|
| 26 | {
|
| 27 | public boolean apply(BlockRailBase.EnumRailDirection p_apply_1_)
|
| 28 | {
|
| 29 | return p_apply_1_ != BlockRailBase.EnumRailDirection.NORTH_EAST && p_apply_1_ != BlockRailBase.EnumRailDirection.NORTH_WEST && p_apply_1_ != BlockRailBase.EnumRailDirection.SOUTH_EAST && p_apply_1_ != BlockRailBase.EnumRailDirection.SOUTH_WEST;
|
| 30 | }
|
| 31 | });
|
| 32 | public static final PropertyBool POWERED = PropertyBool.create("powered");
|
| 33 |
|
| 34 | public BlockRailDetector()
|
| 35 | {
|
| 36 | super(true);
|
| 37 | this.setDefaultState(this.blockState.getBaseState().withProperty(POWERED, Boolean.valueOf(false)).withProperty(SHAPE, BlockRailBase.EnumRailDirection.NORTH_SOUTH));
|
| 38 | this.setTickRandomly(true);
|
| 39 | }
|
| 40 |
|
| 41 | /**
|
| 42 | * How many world ticks before ticking
|
| 43 | */
|
| 44 | public int tickRate(World worldIn)
|
| 45 | {
|
| 46 | return 20;
|
| 47 | }
|
| 48 |
|
| 49 | /**
|
| 50 | * Can this block provide power. Only wire currently seems to have this change based on its state.
|
| 51 | */
|
| 52 | public boolean canProvidePower()
|
| 53 | {
|
| 54 | return true;
|
| 55 | }
|
| 56 |
|
| 57 | /**
|
| 58 | * Called When an Entity Collided with the Block
|
| 59 | */
|
| 60 | public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
|
| 61 | {
|
| 62 | if (!worldIn.isRemote)
|
| 63 | {
|
| 64 | if (!((Boolean)state.getValue(POWERED)).booleanValue())
|
| 65 | {
|
| 66 | this.updatePoweredState(worldIn, pos, state);
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | /**
|
| 72 | * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
|
| 73 | */
|
| 74 | public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
|
| 75 | {
|
| 76 | }
|
| 77 |
|
| 78 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 79 | {
|
| 80 | if (!worldIn.isRemote && ((Boolean)state.getValue(POWERED)).booleanValue())
|
| 81 | {
|
| 82 | this.updatePoweredState(worldIn, pos, state);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 87 | {
|
| 88 | return ((Boolean)state.getValue(POWERED)).booleanValue() ? 15 : 0;
|
| 89 | }
|
| 90 |
|
| 91 | public int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 92 | {
|
| 93 | return !((Boolean)state.getValue(POWERED)).booleanValue() ? 0 : (side == EnumFacing.UP ? 15 : 0);
|
| 94 | }
|
| 95 |
|
| 96 | private void updatePoweredState(World worldIn, BlockPos pos, IBlockState state)
|
| 97 | {
|
| 98 | boolean flag = ((Boolean)state.getValue(POWERED)).booleanValue();
|
| 99 | boolean flag1 = false;
|
| 100 | List<EntityMinecart> list = this.<EntityMinecart>findMinecarts(worldIn, pos, EntityMinecart.class, new Predicate[0]);
|
| 101 |
|
| 102 | if (!list.isEmpty())
|
| 103 | {
|
| 104 | flag1 = true;
|
| 105 | }
|
| 106 |
|
| 107 | if (flag1 && !flag)
|
| 108 | {
|
| 109 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true)), 3);
|
| 110 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 111 | worldIn.notifyNeighborsOfStateChange(pos.down(), this);
|
| 112 | worldIn.markBlockRangeForRenderUpdate(pos, pos);
|
| 113 | }
|
| 114 |
|
| 115 | if (!flag1 && flag)
|
| 116 | {
|
| 117 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false)), 3);
|
| 118 | worldIn.notifyNeighborsOfStateChange(pos, this);
|
| 119 | worldIn.notifyNeighborsOfStateChange(pos.down(), this);
|
| 120 | worldIn.markBlockRangeForRenderUpdate(pos, pos);
|
| 121 | }
|
| 122 |
|
| 123 | if (flag1)
|
| 124 | {
|
| 125 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 126 | }
|
| 127 |
|
| 128 | worldIn.updateComparatorOutputLevel(pos, this);
|
| 129 | }
|
| 130 |
|
| 131 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 132 | {
|
| 133 | super.onBlockAdded(worldIn, pos, state);
|
| 134 | this.updatePoweredState(worldIn, pos, state);
|
| 135 | }
|
| 136 |
|
| 137 | public IProperty<BlockRailBase.EnumRailDirection> getShapeProperty()
|
| 138 | {
|
| 139 | return SHAPE;
|
| 140 | }
|
| 141 |
|
| 142 | public boolean hasComparatorInputOverride()
|
| 143 | {
|
| 144 | return true;
|
| 145 | }
|
| 146 |
|
| 147 | public int getComparatorInputOverride(World worldIn, BlockPos pos)
|
| 148 | {
|
| 149 | if (((Boolean)worldIn.getBlockState(pos).getValue(POWERED)).booleanValue())
|
| 150 | {
|
| 151 | List<EntityMinecartCommandBlock> list = this.<EntityMinecartCommandBlock>findMinecarts(worldIn, pos, EntityMinecartCommandBlock.class, new Predicate[0]);
|
| 152 |
|
| 153 | if (!list.isEmpty())
|
| 154 | {
|
| 155 | return ((EntityMinecartCommandBlock)list.get(0)).getCommandBlockLogic().getSuccessCount();
|
| 156 | }
|
| 157 |
|
| 158 | List<EntityMinecart> list1 = this.<EntityMinecart>findMinecarts(worldIn, pos, EntityMinecart.class, new Predicate[] {EntitySelectors.selectInventories});
|
| 159 |
|
| 160 | if (!list1.isEmpty())
|
| 161 | {
|
| 162 | return Container.calcRedstoneFromInventory((IInventory)list1.get(0));
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | return 0;
|
| 167 | }
|
| 168 |
|
| 169 | protected <T extends EntityMinecart> List<T> findMinecarts(World worldIn, BlockPos pos, Class<T> clazz, Predicate<Entity>... filter)
|
| 170 | {
|
| 171 | AxisAlignedBB axisalignedbb = this.getDectectionBox(pos);
|
| 172 | return filter.length != 1 ? worldIn.getEntitiesWithinAABB(clazz, axisalignedbb) : worldIn.getEntitiesWithinAABB(clazz, axisalignedbb, filter[0]);
|
| 173 | }
|
| 174 |
|
| 175 | private AxisAlignedBB getDectectionBox(BlockPos pos)
|
| 176 | {
|
| 177 | float f = 0.2F;
|
| 178 | return new AxisAlignedBB((double)((float)pos.getX() + 0.2F), (double)pos.getY(), (double)((float)pos.getZ() + 0.2F), (double)((float)(pos.getX() + 1) - 0.2F), (double)((float)(pos.getY() + 1) - 0.2F), (double)((float)(pos.getZ() + 1) - 0.2F));
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Convert the given metadata into a BlockState for this Block
|
| 183 | */
|
| 184 | public IBlockState getStateFromMeta(int meta)
|
| 185 | {
|
| 186 | return this.getDefaultState().withProperty(SHAPE, BlockRailBase.EnumRailDirection.byMetadata(meta & 7)).withProperty(POWERED, Boolean.valueOf((meta & 8) > 0));
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * Convert the BlockState into the correct metadata value
|
| 191 | */
|
| 192 | public int getMetaFromState(IBlockState state)
|
| 193 | {
|
| 194 | int i = 0;
|
| 195 | i = i | ((BlockRailBase.EnumRailDirection)state.getValue(SHAPE)).getMetadata();
|
| 196 |
|
| 197 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 198 | {
|
| 199 | i |= 8;
|
| 200 | }
|
| 201 |
|
| 202 | return i;
|
| 203 | }
|
| 204 |
|
| 205 | protected BlockState createBlockState()
|
| 206 | {
|
| 207 | return new BlockState(this, new IProperty[] {SHAPE, POWERED});
|
| 208 | }
|
| 209 | }
|
| 210 |
|