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

BlockRedstoneNotGate.java (7304B)


      1 package ambos.simpletech.block;
      2 
      3 import net.minecraft.core.block.Block;
      4 import net.minecraft.core.block.entity.TileEntity;
      5 import net.minecraft.core.block.material.Material;
      6 import net.minecraft.core.entity.EntityLiving;
      7 import net.minecraft.core.enums.EnumDropCause;
      8 import net.minecraft.core.item.ItemStack;
      9 import net.minecraft.core.util.helper.Direction;
     10 import net.minecraft.core.util.helper.Side;
     11 import net.minecraft.core.world.World;
     12 import net.minecraft.core.world.WorldSource;
     13 
     14 import java.util.Random;
     15 
     16 import ambos.simpletech.SimpleTech;
     17 
     18 public class BlockRedstoneNotGate extends Block {
     19     private final boolean isPowered;
     20 
     21     public BlockRedstoneNotGate(String key, int id, Material material, boolean isPowered) {
     22         super(key, id, material);
     23         this.isPowered = isPowered;
     24     }
     25 
     26     @Override
     27     public void setBlockBoundsBasedOnState(World world, int x, int y, int z) {
     28         // Sets block shape when placed.
     29         this.setBlockBounds(0.0f, 0.0f, 0.0f, 1.0f, 0.125f, 1.0f);
     30     }
     31 
     32     @Override
     33     public void setBlockBoundsForItemRender() {
     34         // Sets block shape when rendered inside containers.
     35         this.setBlockBounds(0.0f, 0.0f, 0.0f, 1.0f, 0.125f, 1.0f);
     36     }
     37 
     38     @Override
     39     public boolean isSolidRender() {
     40         return false;
     41     }
     42 
     43     @Override
     44     public boolean renderAsNormalBlock() {
     45         return false;
     46     }
     47 
     48     @Override
     49     public boolean canPlaceBlockAt(World world, int x, int y, int z) {
     50         return !world.canPlaceOnSurfaceOfBlock(x, y - 1, z) ? false : super.canPlaceBlockAt(world, x, y, z);
     51     }
     52 
     53     @Override
     54     public ItemStack[] getBreakResult(World world, EnumDropCause dropCause, int x, int y, int z, int meta,
     55             TileEntity tileEntity) {
     56         return new ItemStack[] { new ItemStack(SimpleTech.notGate) };
     57     }
     58 
     59     @Override
     60     public boolean canBlockStay(World world, int x, int y, int z) {
     61         return !world.canPlaceOnSurfaceOfBlock(x, y - 1, z) ? false : super.canBlockStay(world, x, y, z);
     62     }
     63 
     64     @Override
     65     public void updateTick(World world, int x, int y, int z, Random rand) {
     66         int metadata = world.getBlockMetadata(x, y, z);
     67         boolean shouldPower = this.shouldPowerAdjacentBlocks(world, x, y, z, metadata);
     68         if (this.isPowered && !shouldPower) {
     69             world.setBlockAndMetadataWithNotify(x, y, z, SimpleTech.notGateIdle.id, metadata);
     70         } else if (!this.isPowered) {
     71             world.setBlockAndMetadataWithNotify(x, y, z, SimpleTech.notGateActive.id, metadata);
     72         }
     73     }
     74 
     75     @Override
     76     public int getBlockTextureFromSideAndMetadata(Side side, int j) {
     77         if (side == Side.BOTTOM) {
     78             return !this.isPowered ? texCoordToIndex(3, 7) : texCoordToIndex(3, 6);
     79         } else if (side == Side.TOP) {
     80             return !this.isPowered ? texCoordToIndex(3, 8) : texCoordToIndex(3, 9);
     81         } else {
     82             return texCoordToIndex(5, 0);
     83         }
     84     }
     85 
     86     @Override
     87     public boolean shouldSideBeRendered(WorldSource blockAccess, int x, int y, int z, int side) {
     88         // Don't render bottom and top textures to avoid z-fighting with modified
     89         // renderer.
     90         return side != Side.BOTTOM.getId() && side != Side.TOP.getId();
     91     }
     92 
     93     @Override
     94     public boolean isIndirectlyPoweringTo(World world, int x, int y, int z, int side) {
     95         return this.isPoweringTo(world, x, y, z, side);
     96     }
     97 
     98     @Override
     99     public boolean isPoweringTo(WorldSource blockAccess, int x, int y, int z, int side) {
    100         int direction = blockAccess.getBlockMetadata(x, y, z) & 3;
    101         if (!this.isPowered) {
    102             if (direction == Direction.EAST.getHorizontalIndex() && side == Side.EAST.getId()) {
    103                 return false;
    104             } else if (direction == Direction.NORTH.getHorizontalIndex() && side == Side.NORTH.getId()) {
    105                 return false;
    106             } else if (direction == Direction.SOUTH.getHorizontalIndex() && side == Side.SOUTH.getId()) {
    107                 return false;
    108             } else if (direction == Direction.WEST.getHorizontalIndex() && side == Side.WEST.getId()) {
    109                 return false;
    110             } else {
    111                 return true;
    112             }
    113         } else {
    114             return false;
    115         }
    116     }
    117 
    118     @Override
    119     public void onNeighborBlockChange(World world, int x, int y, int z, int blockId) {
    120         if (!this.canBlockStay(world, x, y, z)) {
    121             this.dropBlockWithCause(world, EnumDropCause.WORLD, x, y, z, world.getBlockMetadata(x, y, z), null);
    122             world.setBlockWithNotify(x, y, z, 0);
    123         } else {
    124             int metadata = world.getBlockMetadata(x, y, z);
    125             boolean shouldPower = this.shouldPowerAdjacentBlocks(world, x, y, z, metadata);
    126             if (this.isPowered && !shouldPower) {
    127                 world.scheduleBlockUpdate(x, y, z, this.id, 1);
    128             } else if (!this.isPowered && shouldPower) {
    129                 world.scheduleBlockUpdate(x, y, z, this.id, 1);
    130             }
    131         }
    132     }
    133 
    134     @Override
    135     public boolean canProvidePower() {
    136         return false;
    137     }
    138 
    139     @Override
    140     public void onBlockPlaced(World world, int x, int y, int z, Side side, EntityLiving entity, double sideHeight) {
    141         int metadata = entity.getHorizontalPlacementDirection(side).getHorizontalIndex();
    142         world.setBlockMetadataWithNotify(x, y, z, metadata);
    143         boolean shouldPower = this.shouldPowerAdjacentBlocks(world, x, y, z, metadata);
    144         if (shouldPower) {
    145             world.scheduleBlockUpdate(x, y, z, this.id, 1);
    146         }
    147     }
    148 
    149     @Override
    150     public void onBlockAdded(World world, int i, int j, int k) {
    151         world.notifyBlocksOfNeighborChange(i + 1, j, k, this.id);
    152         world.notifyBlocksOfNeighborChange(i - 1, j, k, this.id);
    153         world.notifyBlocksOfNeighborChange(i, j, k + 1, this.id);
    154         world.notifyBlocksOfNeighborChange(i, j, k - 1, this.id);
    155         world.notifyBlocksOfNeighborChange(i, j - 1, k, this.id);
    156         world.notifyBlocksOfNeighborChange(i, j + 1, k, this.id);
    157     }
    158 
    159     private boolean shouldPowerAdjacentBlocks(World world, int i, int j, int k, int metadata) {
    160         int direction = metadata & 3;
    161         switch (direction) {
    162             case 0:
    163                 return world.isBlockIndirectlyProvidingPowerTo(i, j, k + 1, 3) ||
    164                         world.getBlockId(i, j, k + 1) == Block.wireRedstone.id &&
    165                                 world.getBlockMetadata(i, j, k + 1) > 0;
    166             case 1:
    167                 return world.isBlockIndirectlyProvidingPowerTo(i - 1, j, k, 4) ||
    168                         world.getBlockId(i - 1, j, k) == Block.wireRedstone.id &&
    169                                 world.getBlockMetadata(i - 1, j, k) > 0;
    170             case 2:
    171                 return world.isBlockIndirectlyProvidingPowerTo(i, j, k - 1, 2) ||
    172                         world.getBlockId(i, j, k - 1) == Block.wireRedstone.id &&
    173                                 world.getBlockMetadata(i, j, k - 1) > 0;
    174             case 3:
    175                 return world.isBlockIndirectlyProvidingPowerTo(i + 1, j, k, 5) ||
    176                         world.getBlockId(i + 1, j, k) == Block.wireRedstone.id &&
    177                                 world.getBlockMetadata(i + 1, j, k) > 0;
    178             default:
    179                 return false;
    180         }
    181     }
    182 }