| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.Random;
|
| 4 | import net.minecraft.block.material.Material;
|
| 5 | import net.minecraft.block.state.IBlockState;
|
| 6 | import net.minecraft.entity.EntityLivingBase;
|
| 7 | import net.minecraft.init.Blocks;
|
| 8 | import net.minecraft.item.ItemStack;
|
| 9 | import net.minecraft.util.BlockPos;
|
| 10 | import net.minecraft.util.EnumFacing;
|
| 11 | import net.minecraft.util.EnumWorldBlockLayer;
|
| 12 | import net.minecraft.world.IBlockAccess;
|
| 13 | import net.minecraft.world.World;
|
| 14 |
|
| 15 | public abstract class BlockRedstoneDiode extends BlockDirectional
|
| 16 | {
|
| 17 | /** Tells whether the repeater is powered or not */
|
| 18 | protected final boolean isRepeaterPowered;
|
| 19 |
|
| 20 | protected BlockRedstoneDiode(boolean powered)
|
| 21 | {
|
| 22 | super(Material.circuits);
|
| 23 | this.isRepeaterPowered = powered;
|
| 24 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.125F, 1.0F);
|
| 25 | }
|
| 26 |
|
| 27 | public boolean isFullCube()
|
| 28 | {
|
| 29 | return false;
|
| 30 | }
|
| 31 |
|
| 32 | public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
|
| 33 | {
|
| 34 | return World.doesBlockHaveSolidTopSurface(worldIn, pos.down()) ? super.canPlaceBlockAt(worldIn, pos) : false;
|
| 35 | }
|
| 36 |
|
| 37 | public boolean canBlockStay(World worldIn, BlockPos pos)
|
| 38 | {
|
| 39 | return World.doesBlockHaveSolidTopSurface(worldIn, pos.down());
|
| 40 | }
|
| 41 |
|
| 42 | /**
|
| 43 | * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
|
| 44 | */
|
| 45 | public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
|
| 46 | {
|
| 47 | }
|
| 48 |
|
| 49 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 50 | {
|
| 51 | if (!this.isLocked(worldIn, pos, state))
|
| 52 | {
|
| 53 | boolean flag = this.shouldBePowered(worldIn, pos, state);
|
| 54 |
|
| 55 | if (this.isRepeaterPowered && !flag)
|
| 56 | {
|
| 57 | worldIn.setBlockState(pos, this.getUnpoweredState(state), 2);
|
| 58 | }
|
| 59 | else if (!this.isRepeaterPowered)
|
| 60 | {
|
| 61 | worldIn.setBlockState(pos, this.getPoweredState(state), 2);
|
| 62 |
|
| 63 | if (!flag)
|
| 64 | {
|
| 65 | worldIn.updateBlockTick(pos, this.getPoweredState(state).getBlock(), this.getTickDelay(state), -1);
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public boolean shouldSideBeRendered(IBlockAccess worldIn, BlockPos pos, EnumFacing side)
|
| 72 | {
|
| 73 | return side.getAxis() != EnumFacing.Axis.Y;
|
| 74 | }
|
| 75 |
|
| 76 | protected boolean isPowered(IBlockState state)
|
| 77 | {
|
| 78 | return this.isRepeaterPowered;
|
| 79 | }
|
| 80 |
|
| 81 | public int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 82 | {
|
| 83 | return this.getWeakPower(worldIn, pos, state, side);
|
| 84 | }
|
| 85 |
|
| 86 | public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 87 | {
|
| 88 | return !this.isPowered(state) ? 0 : (state.getValue(FACING) == side ? this.getActiveSignal(worldIn, pos, state) : 0);
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * Called when a neighboring block changes.
|
| 93 | */
|
| 94 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 95 | {
|
| 96 | if (this.canBlockStay(worldIn, pos))
|
| 97 | {
|
| 98 | this.updateState(worldIn, pos, state);
|
| 99 | }
|
| 100 | else
|
| 101 | {
|
| 102 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 103 | worldIn.setBlockToAir(pos);
|
| 104 |
|
| 105 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 106 | {
|
| 107 | worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | protected void updateState(World worldIn, BlockPos pos, IBlockState state)
|
| 113 | {
|
| 114 | if (!this.isLocked(worldIn, pos, state))
|
| 115 | {
|
| 116 | boolean flag = this.shouldBePowered(worldIn, pos, state);
|
| 117 |
|
| 118 | if ((this.isRepeaterPowered && !flag || !this.isRepeaterPowered && flag) && !worldIn.isBlockTickPending(pos, this))
|
| 119 | {
|
| 120 | int i = -1;
|
| 121 |
|
| 122 | if (this.isFacingTowardsRepeater(worldIn, pos, state))
|
| 123 | {
|
| 124 | i = -3;
|
| 125 | }
|
| 126 | else if (this.isRepeaterPowered)
|
| 127 | {
|
| 128 | i = -2;
|
| 129 | }
|
| 130 |
|
| 131 | worldIn.updateBlockTick(pos, this, this.getDelay(state), i);
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public boolean isLocked(IBlockAccess worldIn, BlockPos pos, IBlockState state)
|
| 137 | {
|
| 138 | return false;
|
| 139 | }
|
| 140 |
|
| 141 | protected boolean shouldBePowered(World worldIn, BlockPos pos, IBlockState state)
|
| 142 | {
|
| 143 | return this.calculateInputStrength(worldIn, pos, state) > 0;
|
| 144 | }
|
| 145 |
|
| 146 | protected int calculateInputStrength(World worldIn, BlockPos pos, IBlockState state)
|
| 147 | {
|
| 148 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 149 | BlockPos blockpos = pos.offset(enumfacing);
|
| 150 | int i = worldIn.getRedstonePower(blockpos, enumfacing);
|
| 151 |
|
| 152 | if (i >= 15)
|
| 153 | {
|
| 154 | return i;
|
| 155 | }
|
| 156 | else
|
| 157 | {
|
| 158 | IBlockState iblockstate = worldIn.getBlockState(blockpos);
|
| 159 | return Math.max(i, iblockstate.getBlock() == Blocks.redstone_wire ? ((Integer)iblockstate.getValue(BlockRedstoneWire.POWER)).intValue() : 0);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | protected int getPowerOnSides(IBlockAccess worldIn, BlockPos pos, IBlockState state)
|
| 164 | {
|
| 165 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 166 | EnumFacing enumfacing1 = enumfacing.rotateY();
|
| 167 | EnumFacing enumfacing2 = enumfacing.rotateYCCW();
|
| 168 | return Math.max(this.getPowerOnSide(worldIn, pos.offset(enumfacing1), enumfacing1), this.getPowerOnSide(worldIn, pos.offset(enumfacing2), enumfacing2));
|
| 169 | }
|
| 170 |
|
| 171 | protected int getPowerOnSide(IBlockAccess worldIn, BlockPos pos, EnumFacing side)
|
| 172 | {
|
| 173 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 174 | Block block = iblockstate.getBlock();
|
| 175 | return this.canPowerSide(block) ? (block == Blocks.redstone_wire ? ((Integer)iblockstate.getValue(BlockRedstoneWire.POWER)).intValue() : worldIn.getStrongPower(pos, side)) : 0;
|
| 176 | }
|
| 177 |
|
| 178 | /**
|
| 179 | * Can this block provide power. Only wire currently seems to have this change based on its state.
|
| 180 | */
|
| 181 | public boolean canProvidePower()
|
| 182 | {
|
| 183 | return true;
|
| 184 | }
|
| 185 |
|
| 186 | /**
|
| 187 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 188 | * IBlockstate
|
| 189 | */
|
| 190 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 191 | {
|
| 192 | return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
|
| 193 | }
|
| 194 |
|
| 195 | /**
|
| 196 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 197 | */
|
| 198 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 199 | {
|
| 200 | if (this.shouldBePowered(worldIn, pos, state))
|
| 201 | {
|
| 202 | worldIn.scheduleUpdate(pos, this, 1);
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 207 | {
|
| 208 | this.notifyNeighbors(worldIn, pos, state);
|
| 209 | }
|
| 210 |
|
| 211 | protected void notifyNeighbors(World worldIn, BlockPos pos, IBlockState state)
|
| 212 | {
|
| 213 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 214 | BlockPos blockpos = pos.offset(enumfacing.getOpposite());
|
| 215 | worldIn.notifyBlockOfStateChange(blockpos, this);
|
| 216 | worldIn.notifyNeighborsOfStateExcept(blockpos, this, enumfacing);
|
| 217 | }
|
| 218 |
|
| 219 | /**
|
| 220 | * Called when a player destroys this Block
|
| 221 | */
|
| 222 | public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state)
|
| 223 | {
|
| 224 | if (this.isRepeaterPowered)
|
| 225 | {
|
| 226 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 227 | {
|
| 228 | worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | super.onBlockDestroyedByPlayer(worldIn, pos, state);
|
| 233 | }
|
| 234 |
|
| 235 | /**
|
| 236 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 237 | */
|
| 238 | public boolean isOpaqueCube()
|
| 239 | {
|
| 240 | return false;
|
| 241 | }
|
| 242 |
|
| 243 | protected boolean canPowerSide(Block blockIn)
|
| 244 | {
|
| 245 | return blockIn.canProvidePower();
|
| 246 | }
|
| 247 |
|
| 248 | protected int getActiveSignal(IBlockAccess worldIn, BlockPos pos, IBlockState state)
|
| 249 | {
|
| 250 | return 15;
|
| 251 | }
|
| 252 |
|
| 253 | public static boolean isRedstoneRepeaterBlockID(Block blockIn)
|
| 254 | {
|
| 255 | return Blocks.unpowered_repeater.isAssociated(blockIn) || Blocks.unpowered_comparator.isAssociated(blockIn);
|
| 256 | }
|
| 257 |
|
| 258 | public boolean isAssociated(Block other)
|
| 259 | {
|
| 260 | return other == this.getPoweredState(this.getDefaultState()).getBlock() || other == this.getUnpoweredState(this.getDefaultState()).getBlock();
|
| 261 | }
|
| 262 |
|
| 263 | public boolean isFacingTowardsRepeater(World worldIn, BlockPos pos, IBlockState state)
|
| 264 | {
|
| 265 | EnumFacing enumfacing = ((EnumFacing)state.getValue(FACING)).getOpposite();
|
| 266 | BlockPos blockpos = pos.offset(enumfacing);
|
| 267 | return isRedstoneRepeaterBlockID(worldIn.getBlockState(blockpos).getBlock()) ? worldIn.getBlockState(blockpos).getValue(FACING) != enumfacing : false;
|
| 268 | }
|
| 269 |
|
| 270 | protected int getTickDelay(IBlockState state)
|
| 271 | {
|
| 272 | return this.getDelay(state);
|
| 273 | }
|
| 274 |
|
| 275 | protected abstract int getDelay(IBlockState state);
|
| 276 |
|
| 277 | protected abstract IBlockState getPoweredState(IBlockState unpoweredState);
|
| 278 |
|
| 279 | protected abstract IBlockState getUnpoweredState(IBlockState poweredState);
|
| 280 |
|
| 281 | public boolean isAssociatedBlock(Block other)
|
| 282 | {
|
| 283 | return this.isAssociated(other);
|
| 284 | }
|
| 285 |
|
| 286 | public EnumWorldBlockLayer getBlockLayer()
|
| 287 | {
|
| 288 | return EnumWorldBlockLayer.CUTOUT;
|
| 289 | }
|
| 290 | }
|
| 291 |
|