simple-tech

Minecraft: Better than Adventure! mod that adds simple blocks to automate tasks
git clone git://memoryshards.xyz/simple-tech.git
Log | Files | Refs | README | LICENSE

BlockTrappedChest.java (2956B)


      1 package ambos.simpletech.block;
      2 
      3 import net.minecraft.core.block.BlockChest;
      4 import net.minecraft.core.block.material.Material;
      5 import net.minecraft.core.entity.player.EntityPlayer;
      6 import net.minecraft.core.sound.SoundType;
      7 import net.minecraft.core.world.World;
      8 import net.minecraft.core.world.WorldSource;
      9 
     10 import java.util.Random;
     11 
     12 import ambos.simpletech.SimpleTech;
     13 
     14 public class BlockTrappedChest extends BlockChest {
     15     public static final int redstoneOffset = 4;
     16 
     17     public BlockTrappedChest(String key, int id, Material material) {
     18         super(key, id, material);
     19         this.withTexCoords(9, 1, 9, 1, 11, 1, 10, 1, 10, 1, 10, 1);
     20         this.setTicking(true);
     21     }
     22 
     23     @Override
     24     public int tickRate() {
     25         return 20;
     26     }
     27 
     28     @Override
     29     public boolean isSolidRender() {
     30         return false;
     31     }
     32 
     33     @Override
     34     public boolean canProvidePower() {
     35         return true;
     36     }
     37 
     38     @Override
     39     public boolean renderAsNormalBlock() {
     40         return false;
     41     }
     42 
     43     @Override
     44     public boolean isPoweringTo(WorldSource blockAccess, int x, int y, int z, int side) {
     45         return SimpleTech.getRedstoneFromMetadata(blockAccess.getBlockMetadata(x, y, z), redstoneOffset) > 0;
     46     }
     47 
     48     @Override
     49     public void onBlockRemoved(World world, int x, int y, int z, int data) {
     50         if (SimpleTech.getRedstoneFromMetadata(world.getBlockMetadata(x, y, z), redstoneOffset) > 0) {
     51             this.notifyNeighbors(world, x, y, z);
     52         }
     53 
     54         super.onBlockRemoved(world, x, y, z, data);
     55     }
     56 
     57     @Override
     58     public boolean blockActivated(World world, int x, int y, int z, EntityPlayer player) {
     59         this.setState(world, x, y, z, (byte) 1);
     60 
     61         world.scheduleBlockUpdate(x, y, z, this.id, this.tickRate());
     62         world.playSoundEffect(SoundType.GUI_SOUNDS, x + 0.5, y + 0.5, z + 0.5,
     63                 "random.click", 0.3f, 0.6f);
     64 
     65         return super.blockActivated(world, x, y, z, player);
     66     }
     67 
     68     @Override
     69     public void updateTick(World world, int x, int y, int z, Random rand) {
     70         if (!world.isClientSide) {
     71             if (SimpleTech.getRedstoneFromMetadata(world.getBlockMetadata(x, y, z), redstoneOffset) > 0) {
     72                 this.setState(world, x, y, z, (byte) 0);
     73             }
     74         }
     75     }
     76 
     77     private void setState(World world, int x, int y, int z, byte redstone) {
     78         int metadata = world.getBlockMetadata(x, y, z);
     79 
     80         // Recreates metadata using the redstone signal and the old metadata value.
     81         world.setBlockMetadataWithNotify(x, y, z, SimpleTech.getMetaWithRedstone(metadata, redstone, redstoneOffset));
     82 
     83         // Updates block's neighbors.
     84         this.notifyNeighbors(world, x, y, z);
     85 
     86         world.markBlocksDirty(x, y, z, x, y, z);
     87     }
     88 
     89     private void notifyNeighbors(World world, int x, int y, int z) {
     90         world.notifyBlocksOfNeighborChange(x, y, z, this.id);
     91         world.notifyBlocksOfNeighborChange(x, y - 1, z, this.id);
     92     }
     93 }