| 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.material.Material;
|
| 7 | import net.minecraft.block.properties.IProperty;
|
| 8 | import net.minecraft.block.properties.PropertyBool;
|
| 9 | import net.minecraft.block.properties.PropertyEnum;
|
| 10 | import net.minecraft.block.state.BlockState;
|
| 11 | import net.minecraft.block.state.IBlockState;
|
| 12 | import net.minecraft.entity.Entity;
|
| 13 | import net.minecraft.entity.EntityLivingBase;
|
| 14 | import net.minecraft.entity.item.EntityItemFrame;
|
| 15 | import net.minecraft.entity.player.EntityPlayer;
|
| 16 | import net.minecraft.init.Blocks;
|
| 17 | import net.minecraft.init.Items;
|
| 18 | import net.minecraft.item.Item;
|
| 19 | import net.minecraft.tileentity.TileEntity;
|
| 20 | import net.minecraft.tileentity.TileEntityComparator;
|
| 21 | import net.minecraft.util.AxisAlignedBB;
|
| 22 | import net.minecraft.util.BlockPos;
|
| 23 | import net.minecraft.util.EnumFacing;
|
| 24 | import net.minecraft.util.IStringSerializable;
|
| 25 | import net.minecraft.util.StatCollector;
|
| 26 | import net.minecraft.world.IBlockAccess;
|
| 27 | import net.minecraft.world.World;
|
| 28 |
|
| 29 | public class BlockRedstoneComparator extends BlockRedstoneDiode implements ITileEntityProvider
|
| 30 | {
|
| 31 | public static final PropertyBool POWERED = PropertyBool.create("powered");
|
| 32 | public static final PropertyEnum<BlockRedstoneComparator.Mode> MODE = PropertyEnum.<BlockRedstoneComparator.Mode>create("mode", BlockRedstoneComparator.Mode.class);
|
| 33 |
|
| 34 | public BlockRedstoneComparator(boolean powered)
|
| 35 | {
|
| 36 | super(powered);
|
| 37 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(POWERED, Boolean.valueOf(false)).withProperty(MODE, BlockRedstoneComparator.Mode.COMPARE));
|
| 38 | this.isBlockContainer = true;
|
| 39 | }
|
| 40 |
|
| 41 | /**
|
| 42 | * Gets the localized name of this block. Used for the statistics page.
|
| 43 | */
|
| 44 | public String getLocalizedName()
|
| 45 | {
|
| 46 | return StatCollector.translateToLocal("item.comparator.name");
|
| 47 | }
|
| 48 |
|
| 49 | /**
|
| 50 | * Get the Item that this Block should drop when harvested.
|
| 51 | */
|
| 52 | public Item getItemDropped(IBlockState state, Random rand, int fortune)
|
| 53 | {
|
| 54 | return Items.comparator;
|
| 55 | }
|
| 56 |
|
| 57 | public Item getItem(World worldIn, BlockPos pos)
|
| 58 | {
|
| 59 | return Items.comparator;
|
| 60 | }
|
| 61 |
|
| 62 | protected int getDelay(IBlockState state)
|
| 63 | {
|
| 64 | return 2;
|
| 65 | }
|
| 66 |
|
| 67 | protected IBlockState getPoweredState(IBlockState unpoweredState)
|
| 68 | {
|
| 69 | Boolean obool = (Boolean)unpoweredState.getValue(POWERED);
|
| 70 | BlockRedstoneComparator.Mode blockredstonecomparator$mode = (BlockRedstoneComparator.Mode)unpoweredState.getValue(MODE);
|
| 71 | EnumFacing enumfacing = (EnumFacing)unpoweredState.getValue(FACING);
|
| 72 | return Blocks.powered_comparator.getDefaultState().withProperty(FACING, enumfacing).withProperty(POWERED, obool).withProperty(MODE, blockredstonecomparator$mode);
|
| 73 | }
|
| 74 |
|
| 75 | protected IBlockState getUnpoweredState(IBlockState poweredState)
|
| 76 | {
|
| 77 | Boolean obool = (Boolean)poweredState.getValue(POWERED);
|
| 78 | BlockRedstoneComparator.Mode blockredstonecomparator$mode = (BlockRedstoneComparator.Mode)poweredState.getValue(MODE);
|
| 79 | EnumFacing enumfacing = (EnumFacing)poweredState.getValue(FACING);
|
| 80 | return Blocks.unpowered_comparator.getDefaultState().withProperty(FACING, enumfacing).withProperty(POWERED, obool).withProperty(MODE, blockredstonecomparator$mode);
|
| 81 | }
|
| 82 |
|
| 83 | protected boolean isPowered(IBlockState state)
|
| 84 | {
|
| 85 | return this.isRepeaterPowered || ((Boolean)state.getValue(POWERED)).booleanValue();
|
| 86 | }
|
| 87 |
|
| 88 | protected int getActiveSignal(IBlockAccess worldIn, BlockPos pos, IBlockState state)
|
| 89 | {
|
| 90 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 91 | return tileentity instanceof TileEntityComparator ? ((TileEntityComparator)tileentity).getOutputSignal() : 0;
|
| 92 | }
|
| 93 |
|
| 94 | private int calculateOutput(World worldIn, BlockPos pos, IBlockState state)
|
| 95 | {
|
| 96 | return state.getValue(MODE) == BlockRedstoneComparator.Mode.SUBTRACT ? Math.max(this.calculateInputStrength(worldIn, pos, state) - this.getPowerOnSides(worldIn, pos, state), 0) : this.calculateInputStrength(worldIn, pos, state);
|
| 97 | }
|
| 98 |
|
| 99 | protected boolean shouldBePowered(World worldIn, BlockPos pos, IBlockState state)
|
| 100 | {
|
| 101 | int i = this.calculateInputStrength(worldIn, pos, state);
|
| 102 |
|
| 103 | if (i >= 15)
|
| 104 | {
|
| 105 | return true;
|
| 106 | }
|
| 107 | else if (i == 0)
|
| 108 | {
|
| 109 | return false;
|
| 110 | }
|
| 111 | else
|
| 112 | {
|
| 113 | int j = this.getPowerOnSides(worldIn, pos, state);
|
| 114 | return j == 0 ? true : i >= j;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | protected int calculateInputStrength(World worldIn, BlockPos pos, IBlockState state)
|
| 119 | {
|
| 120 | int i = super.calculateInputStrength(worldIn, pos, state);
|
| 121 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 122 | BlockPos blockpos = pos.offset(enumfacing);
|
| 123 | Block block = worldIn.getBlockState(blockpos).getBlock();
|
| 124 |
|
| 125 | if (block.hasComparatorInputOverride())
|
| 126 | {
|
| 127 | i = block.getComparatorInputOverride(worldIn, blockpos);
|
| 128 | }
|
| 129 | else if (i < 15 && block.isNormalCube())
|
| 130 | {
|
| 131 | blockpos = blockpos.offset(enumfacing);
|
| 132 | block = worldIn.getBlockState(blockpos).getBlock();
|
| 133 |
|
| 134 | if (block.hasComparatorInputOverride())
|
| 135 | {
|
| 136 | i = block.getComparatorInputOverride(worldIn, blockpos);
|
| 137 | }
|
| 138 | else if (block.getMaterial() == Material.air)
|
| 139 | {
|
| 140 | EntityItemFrame entityitemframe = this.findItemFrame(worldIn, enumfacing, blockpos);
|
| 141 |
|
| 142 | if (entityitemframe != null)
|
| 143 | {
|
| 144 | i = entityitemframe.func_174866_q();
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | return i;
|
| 150 | }
|
| 151 |
|
| 152 | private EntityItemFrame findItemFrame(World worldIn, final EnumFacing facing, BlockPos pos)
|
| 153 | {
|
| 154 | List<EntityItemFrame> list = worldIn.<EntityItemFrame>getEntitiesWithinAABB(EntityItemFrame.class, new AxisAlignedBB((double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), (double)(pos.getX() + 1), (double)(pos.getY() + 1), (double)(pos.getZ() + 1)), new Predicate<Entity>()
|
| 155 | {
|
| 156 | public boolean apply(Entity p_apply_1_)
|
| 157 | {
|
| 158 | return p_apply_1_ != null && p_apply_1_.getHorizontalFacing() == facing;
|
| 159 | }
|
| 160 | });
|
| 161 | return list.size() == 1 ? (EntityItemFrame)list.get(0) : null;
|
| 162 | }
|
| 163 |
|
| 164 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 165 | {
|
| 166 | if (!playerIn.capabilities.allowEdit)
|
| 167 | {
|
| 168 | return false;
|
| 169 | }
|
| 170 | else
|
| 171 | {
|
| 172 | state = state.cycleProperty(MODE);
|
| 173 | worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, state.getValue(MODE) == BlockRedstoneComparator.Mode.SUBTRACT ? 0.55F : 0.5F);
|
| 174 | worldIn.setBlockState(pos, state, 2);
|
| 175 | this.onStateChange(worldIn, pos, state);
|
| 176 | return true;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | protected void updateState(World worldIn, BlockPos pos, IBlockState state)
|
| 181 | {
|
| 182 | if (!worldIn.isBlockTickPending(pos, this))
|
| 183 | {
|
| 184 | int i = this.calculateOutput(worldIn, pos, state);
|
| 185 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 186 | int j = tileentity instanceof TileEntityComparator ? ((TileEntityComparator)tileentity).getOutputSignal() : 0;
|
| 187 |
|
| 188 | if (i != j || this.isPowered(state) != this.shouldBePowered(worldIn, pos, state))
|
| 189 | {
|
| 190 | if (this.isFacingTowardsRepeater(worldIn, pos, state))
|
| 191 | {
|
| 192 | worldIn.updateBlockTick(pos, this, 2, -1);
|
| 193 | }
|
| 194 | else
|
| 195 | {
|
| 196 | worldIn.updateBlockTick(pos, this, 2, 0);
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | private void onStateChange(World worldIn, BlockPos pos, IBlockState state)
|
| 203 | {
|
| 204 | int i = this.calculateOutput(worldIn, pos, state);
|
| 205 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 206 | int j = 0;
|
| 207 |
|
| 208 | if (tileentity instanceof TileEntityComparator)
|
| 209 | {
|
| 210 | TileEntityComparator tileentitycomparator = (TileEntityComparator)tileentity;
|
| 211 | j = tileentitycomparator.getOutputSignal();
|
| 212 | tileentitycomparator.setOutputSignal(i);
|
| 213 | }
|
| 214 |
|
| 215 | if (j != i || state.getValue(MODE) == BlockRedstoneComparator.Mode.COMPARE)
|
| 216 | {
|
| 217 | boolean flag1 = this.shouldBePowered(worldIn, pos, state);
|
| 218 | boolean flag = this.isPowered(state);
|
| 219 |
|
| 220 | if (flag && !flag1)
|
| 221 | {
|
| 222 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false)), 2);
|
| 223 | }
|
| 224 | else if (!flag && flag1)
|
| 225 | {
|
| 226 | worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true)), 2);
|
| 227 | }
|
| 228 |
|
| 229 | this.notifyNeighbors(worldIn, pos, state);
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 234 | {
|
| 235 | if (this.isRepeaterPowered)
|
| 236 | {
|
| 237 | worldIn.setBlockState(pos, this.getUnpoweredState(state).withProperty(POWERED, Boolean.valueOf(true)), 4);
|
| 238 | }
|
| 239 |
|
| 240 | this.onStateChange(worldIn, pos, state);
|
| 241 | }
|
| 242 |
|
| 243 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 244 | {
|
| 245 | super.onBlockAdded(worldIn, pos, state);
|
| 246 | worldIn.setTileEntity(pos, this.createNewTileEntity(worldIn, 0));
|
| 247 | }
|
| 248 |
|
| 249 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 250 | {
|
| 251 | super.breakBlock(worldIn, pos, state);
|
| 252 | worldIn.removeTileEntity(pos);
|
| 253 | this.notifyNeighbors(worldIn, pos, state);
|
| 254 | }
|
| 255 |
|
| 256 | /**
|
| 257 | * Called on both Client and Server when World#addBlockEvent is called
|
| 258 | */
|
| 259 | public boolean onBlockEventReceived(World worldIn, BlockPos pos, IBlockState state, int eventID, int eventParam)
|
| 260 | {
|
| 261 | super.onBlockEventReceived(worldIn, pos, state, eventID, eventParam);
|
| 262 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 263 | return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
|
| 264 | }
|
| 265 |
|
| 266 | /**
|
| 267 | * Returns a new instance of a block's tile entity class. Called on placing the block.
|
| 268 | */
|
| 269 | public TileEntity createNewTileEntity(World worldIn, int meta)
|
| 270 | {
|
| 271 | return new TileEntityComparator();
|
| 272 | }
|
| 273 |
|
| 274 | /**
|
| 275 | * Convert the given metadata into a BlockState for this Block
|
| 276 | */
|
| 277 | public IBlockState getStateFromMeta(int meta)
|
| 278 | {
|
| 279 | return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(POWERED, Boolean.valueOf((meta & 8) > 0)).withProperty(MODE, (meta & 4) > 0 ? BlockRedstoneComparator.Mode.SUBTRACT : BlockRedstoneComparator.Mode.COMPARE);
|
| 280 | }
|
| 281 |
|
| 282 | /**
|
| 283 | * Convert the BlockState into the correct metadata value
|
| 284 | */
|
| 285 | public int getMetaFromState(IBlockState state)
|
| 286 | {
|
| 287 | int i = 0;
|
| 288 | i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();
|
| 289 |
|
| 290 | if (((Boolean)state.getValue(POWERED)).booleanValue())
|
| 291 | {
|
| 292 | i |= 8;
|
| 293 | }
|
| 294 |
|
| 295 | if (state.getValue(MODE) == BlockRedstoneComparator.Mode.SUBTRACT)
|
| 296 | {
|
| 297 | i |= 4;
|
| 298 | }
|
| 299 |
|
| 300 | return i;
|
| 301 | }
|
| 302 |
|
| 303 | protected BlockState createBlockState()
|
| 304 | {
|
| 305 | return new BlockState(this, new IProperty[] {FACING, MODE, POWERED});
|
| 306 | }
|
| 307 |
|
| 308 | /**
|
| 309 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 310 | * IBlockstate
|
| 311 | */
|
| 312 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 313 | {
|
| 314 | return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(POWERED, Boolean.valueOf(false)).withProperty(MODE, BlockRedstoneComparator.Mode.COMPARE);
|
| 315 | }
|
| 316 |
|
| 317 | public static enum Mode implements IStringSerializable
|
| 318 | {
|
| 319 | COMPARE("compare"),
|
| 320 | SUBTRACT("subtract");
|
| 321 |
|
| 322 | private final String name;
|
| 323 |
|
| 324 | private Mode(String name)
|
| 325 | {
|
| 326 | this.name = name;
|
| 327 | }
|
| 328 |
|
| 329 | public String toString()
|
| 330 | {
|
| 331 | return this.name;
|
| 332 | }
|
| 333 |
|
| 334 | public String getName()
|
| 335 | {
|
| 336 | return this.name;
|
| 337 | }
|
| 338 | }
|
| 339 | }
|
| 340 |
|