BlockFan.java (4361B)
1 package ambos.simpletech.block; 2 3 import net.minecraft.core.block.BlockTileEntity; 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 13 import java.util.Random; 14 15 import ambos.simpletech.SimpleTech; 16 import ambos.simpletech.block.entity.TileEntityFan; 17 18 public class BlockFan extends BlockTileEntity { 19 private final boolean isPowered; 20 21 public BlockFan(String key, int id, Material material, boolean isPowered) { 22 super(key, id, material); 23 this.isPowered = isPowered; 24 } 25 26 @Override 27 public ItemStack[] getBreakResult(World world, EnumDropCause dropCause, int x, int y, int z, int meta, 28 TileEntity tileEntity) { 29 // Only drops unpowered fan when broken. 30 // Should use BlockBuilder.setBlockDrop instead? 31 return dropCause != EnumDropCause.IMPROPER_TOOL ? new ItemStack[] { new ItemStack(SimpleTech.unpoweredFan) } 32 : null; 33 } 34 35 @Override 36 public int tickRate() { 37 return 2; 38 } 39 40 @Override 41 public int getBlockTextureFromSideAndMetadata(Side side, int j) { 42 int direction = SimpleTech.get3DDirectionFromMeta(j); 43 if (direction > Direction.EAST.getId()) { 44 return this.atlasIndices[Side.TOP.getId()]; // Defaults to top/bottom texture. 45 } else if (side.getId() == direction) { 46 return this.atlasIndices[Side.SOUTH.getId()]; // Returns front texture. 47 } else { 48 if (side.getId() == Side.TOP.getId() || side.getId() == Side.BOTTOM.getId()) { 49 return this.atlasIndices[Side.TOP.getId()]; // Returns top/bottom texture. 50 } else { 51 return this.atlasIndices[Side.EAST.getId()]; // Returns one of the sides texture. 52 } 53 } 54 } 55 56 @Override 57 public void randomDisplayTick(World world, int x, int y, int z, Random rand) { 58 // Particle rendering. 59 if (this.isPowered) { 60 int dx = -SimpleTech.getDirectionX(world, x, y, z); 61 int dy = -SimpleTech.getDirectionY(world, x, y, z); 62 int dz = -SimpleTech.getDirectionZ(world, x, y, z); 63 64 for (int i = 1; i < 3; ++i) { 65 double rx = rand.nextDouble() - 0.5; 66 double ry = rand.nextDouble() - 0.5; 67 double rz = rand.nextDouble() - 0.5; 68 world.spawnParticle("smoke", 69 (double) (x + dx) + 0.5 + rx, 70 (double) (y + dy) + 0.5 + ry, 71 (double) (z + dz) + 0.5 + rz, 72 0.2 * (double) dx, 73 0.2 * (double) dy, 74 0.2 * (double) dz); 75 } 76 } 77 } 78 79 @Override 80 public void onNeighborBlockChange(World world, int x, int y, int z, int l) { 81 int direction; 82 83 // If it's currently powered by redstone... 84 if (world.isBlockIndirectlyGettingPowered(x, y, z) || world.isBlockIndirectlyGettingPowered(x, y + 1, z)) { 85 // If it wasn't already powered... 86 if (!this.isPowered) { 87 // Replaces the unpowered fan by its powered counterpart. 88 direction = world.getBlockMetadata(x, y, z); 89 world.setBlockAndMetadataWithNotify(x, y, z, SimpleTech.POWERED_FAN_ID, direction); 90 } 91 92 world.scheduleBlockUpdate(x, y, z, this.id, this.tickRate()); 93 } else if (this.isPowered) { 94 // Replaces the powered fan by its unpowered counterpart. 95 direction = world.getBlockMetadata(x, y, z); 96 world.setBlockAndMetadataWithNotify(x, y, z, SimpleTech.UNPOWERED_FAN_ID, direction); 97 } 98 } 99 100 @Override 101 public void onBlockPlaced(World world, int x, int y, int z, Side side, EntityLiving entity, double sideHeight) { 102 Direction placementDirection = entity.getPlacementDirection(side).getOpposite(); 103 world.setBlockMetadataWithNotify(x, y, z, placementDirection.getId()); 104 } 105 106 @Override 107 protected TileEntity getNewBlockEntity() { 108 return new TileEntityFan(); 109 } 110 }