BlockLightSensor.java (2906B)
1 package ambos.simpletech.block; 2 3 import ambos.simpletech.SimpleTech; 4 import ambos.simpletech.block.entity.TileEntityLightSensor; 5 import net.minecraft.core.block.BlockTileEntity; 6 import net.minecraft.core.block.entity.TileEntity; 7 import net.minecraft.core.block.material.Material; 8 import net.minecraft.core.entity.player.EntityPlayer; 9 import net.minecraft.core.world.World; 10 import net.minecraft.core.world.WorldSource; 11 12 public class BlockLightSensor extends BlockTileEntity { 13 public static final int invertedOffset = 0; 14 public static final int redstoneOffset = 4; 15 16 public BlockLightSensor(String key, int id, Material material) { 17 super(key, id, material); 18 } 19 20 public boolean isInverted(World world, int x, int y, int z) { 21 return SimpleTech.getInvertedFromMetadata(world.getBlockMetadata(x, y, z), invertedOffset) != 0; 22 } 23 24 @Override 25 public boolean renderAsNormalBlock() { 26 return false; 27 } 28 29 @Override 30 public boolean isSolidRender() { 31 return false; 32 } 33 34 @Override 35 public boolean canProvidePower() { 36 return true; 37 } 38 39 @Override 40 public TileEntity getNewBlockEntity() { 41 return new TileEntityLightSensor(); 42 } 43 44 @Override 45 public boolean isPoweringTo(WorldSource blockAccess, int x, int y, int z, int side) { 46 return SimpleTech.getRedstoneFromMetadata(blockAccess.getBlockMetadata(x, y, z), redstoneOffset) > 0; 47 } 48 49 @Override 50 public void setBlockBoundsBasedOnState(World world, int x, int y, int z) { 51 // Sets block shape when placed. 52 this.setBlockBounds(0.0f, 0.0f, 0.0f, 1.0f, 0.25f, 1.0f); 53 } 54 55 @Override 56 public void setBlockBoundsForItemRender() { 57 // Sets block shape when rendered inside containers. 58 this.setBlockBounds(0.0f, 0.0f, 0.0f, 1.0f, 0.25f, 1.0f); 59 } 60 61 @Override 62 public boolean blockActivated(World world, int x, int y, int z, EntityPlayer player) { 63 int metadata = world.getBlockMetadata(x, y, z); 64 int isInverted = !isInverted(world, x, y, z) ? 1 : 0; 65 66 // Recreates metadata using the inverted state and the old metadata value. 67 world.setBlockMetadataWithNotify(x, y, z, SimpleTech.getMetaWithInverted(metadata, isInverted, invertedOffset)); 68 69 return true; 70 } 71 72 public void updateSensor(World world, int x, int y, int z, boolean powering) { 73 int metadata = world.getBlockMetadata(x, y, z); 74 int redstone = powering ? 1 : 0; 75 76 // Recreates metadata using the redstone signal and the old metadata value. 77 world.setBlockMetadataWithNotify(x, y, z, SimpleTech.getMetaWithRedstone(metadata, redstone, redstoneOffset)); 78 79 // Updates block's neighbors. 80 world.notifyBlocksOfNeighborChange(x, y, z, this.id); 81 world.notifyBlocksOfNeighborChange(x, y - 1, z, this.id); 82 83 world.markBlocksDirty(x, y, z, x, y, z); 84 } 85 }