|
| 1 | +package net.minecraft.src; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Random; |
| 5 | + |
| 6 | +public class BlockHutFarmer extends BlockHut { |
| 7 | + |
| 8 | + private EntityFarmer em; |
| 9 | + |
| 10 | + public BlockHutFarmer(int blockID, int _textureID) { |
| 11 | + super(blockID); |
| 12 | + textureID = _textureID; |
| 13 | + hutWidth = 5; |
| 14 | + hutHeight = 3; |
| 15 | + clearingRange = 4; |
| 16 | + halfWidth = hutWidth/2; |
| 17 | + workingRange = 16; |
| 18 | + em=null; |
| 19 | + // Sets the recipe be two planks horizontal to each other |
| 20 | + // CraftingManager.getInstance().addRecipe(new ItemStack(blockID, 1,0), |
| 21 | + // new Object[] { "##", Character.valueOf('#'), Block.dirt,}); |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + public boolean canPlaceBlockAt(World world, int i, int j, int k) |
| 26 | + { |
| 27 | + // check if there are other chests nearby |
| 28 | + Vec3D chestPos = scanForBlockNearPoint(world, mod_MineColony.hutFarmer.blockID, i,j,k, workingRange, 20, workingRange); |
| 29 | + if (chestPos != null) |
| 30 | + return false; |
| 31 | + |
| 32 | + return super.canPlaceBlockAt(world, i, j, k); |
| 33 | + } |
| 34 | + |
| 35 | + public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer) |
| 36 | + { |
| 37 | + // build house when clicked with stick |
| 38 | + ItemStack is = entityplayer.getCurrentEquippedItem(); |
| 39 | + if(is !=null && is.getItem()!=null && (is.getItem().shiftedIndex == mod_MineColony.scepterGold.shiftedIndex)) |
| 40 | + { |
| 41 | + // clean area around house |
| 42 | + for (int x = i - clearingRange; x <= i + clearingRange; x++) |
| 43 | + for (int z = k - clearingRange; z <= k + clearingRange; z++) |
| 44 | + for (int y = j; y < j + hutHeight; y++) { |
| 45 | + if(x!=i || y!=j || z!=k) |
| 46 | + world.setBlockWithNotify(x, y, z, 0); |
| 47 | + } |
| 48 | + |
| 49 | + // floor |
| 50 | + for (int x = i - halfWidth-1; x <= i + halfWidth+1; x++) |
| 51 | + for (int z = k - halfWidth-2; z <= k + halfWidth+2; z++) { |
| 52 | + world.setBlockWithNotify(x, j - 1, z, Block.cobblestone.blockID); |
| 53 | + } |
| 54 | + |
| 55 | + // roof |
| 56 | + for (int x = i - halfWidth-1; x <= i + halfWidth+1; x++) |
| 57 | + for (int z = k - halfWidth-1; z <= k + halfWidth+1; z++) { |
| 58 | + world.setBlockWithNotify(x, j + hutHeight-1, z, Block.planks.blockID); |
| 59 | + } |
| 60 | + |
| 61 | + for (int dy = 0 ; dy <= 5; dy++) |
| 62 | + for (int x = i - halfWidth -1 +dy; x <= i + halfWidth-dy+1; x++) |
| 63 | + for (int z = k - halfWidth-1; z <= k + halfWidth+1; z++) { |
| 64 | + if(dy>=0 && x!=i-halfWidth-1+dy && x!=i+halfWidth-dy+1) |
| 65 | + world.setBlockWithNotify(x, j +hutHeight + dy, z, Block.cloth.blockID); |
| 66 | + else |
| 67 | + { |
| 68 | + if(x==i) |
| 69 | + world.setBlockWithNotify(x, j +hutHeight + dy, z, Block.planks.blockID); |
| 70 | + else |
| 71 | + { |
| 72 | + world.setBlockWithNotify(x, j +hutHeight + dy, z, Block.stairCompactPlanks.blockID); |
| 73 | + if(x>i) |
| 74 | + world.setBlockMetadataWithNotify(x, j +hutHeight + dy, z, 1); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // wall |
| 80 | + for (int z = k - halfWidth-1; z <= k + halfWidth+1; z++) |
| 81 | + for (int y = j; y<= j+1; y++) |
| 82 | + { |
| 83 | + if(z!=k) |
| 84 | + { |
| 85 | + if(y==j) |
| 86 | + world.setBlockWithNotify(i - halfWidth-1, y, z,Block.wood.blockID); |
| 87 | + |
| 88 | + if(y==j+1) |
| 89 | + world.setBlockWithNotify(i - halfWidth-1, y, z,Block.planks.blockID); |
| 90 | + |
| 91 | + world.setBlockWithNotify(i + halfWidth+1, y, z,Block.fence.blockID); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // windows |
| 96 | + // wooden |
| 97 | + world.setBlockWithNotify(i - halfWidth-1, j+1, k-2, Block.stairCompactPlanks.blockID); |
| 98 | + world.setBlockMetadataWithNotify(i - halfWidth-1, j+1, k-2, 3); |
| 99 | + world.setBlockWithNotify(i - halfWidth-1, j+1, k-1, Block.stairCompactPlanks.blockID); |
| 100 | + world.setBlockMetadataWithNotify(i - halfWidth-1, j+1, k-1, 2); |
| 101 | + |
| 102 | + world.setBlockWithNotify(i - halfWidth-1, j+1, k+1, Block.stairCompactPlanks.blockID); |
| 103 | + world.setBlockMetadataWithNotify(i - halfWidth-1, j+1, k+1, 3); |
| 104 | + world.setBlockWithNotify(i - halfWidth-1, j+1, k+2, Block.stairCompactPlanks.blockID); |
| 105 | + world.setBlockMetadataWithNotify(i - halfWidth-1, j+1, k+2, 2); |
| 106 | + |
| 107 | + // fences |
| 108 | + world.setBlockWithNotify(i + halfWidth+1, j+1, k-2, 0); |
| 109 | + world.setBlockWithNotify(i + halfWidth+1, j+1, k+2, 0); |
| 110 | + } |
| 111 | + else if(is !=null && (is.getItem().shiftedIndex == mod_MineColony.scepterSteel.shiftedIndex)) |
| 112 | + { |
| 113 | + // fence |
| 114 | + Vec3D chestPos = Vec3D.createVector(i, j, k); |
| 115 | + for(int dx = i-workingRange; dx<=i+workingRange;dx++) |
| 116 | + for(int dz=k-workingRange; dz<=k+workingRange;dz++) |
| 117 | + { |
| 118 | + int dy = findTopGround(world, dx, dz); |
| 119 | + int groundBlockId = world.getBlockId(dx, dy, dz); |
| 120 | + if(groundBlockId == Block.dirt.blockID || |
| 121 | + groundBlockId == Block.grass.blockID || |
| 122 | + groundBlockId == Block.gravel.blockID || |
| 123 | + groundBlockId == Block.sand.blockID || |
| 124 | + groundBlockId == Block.stone.blockID) |
| 125 | + { |
| 126 | + if(dx == i-workingRange || dx == i+workingRange || |
| 127 | + dz == k-workingRange || dz == k+workingRange) |
| 128 | + world.setBlockWithNotify(dx,dy+1,dz, Block.fence.blockID); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + else |
| 133 | + return super.blockActivated(world, i, j, k, entityplayer); |
| 134 | + |
| 135 | + return true; |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + public void onBlockAdded(World world, int i, int j, int k) { |
| 140 | + super.onBlockAdded(world, i, j, k); |
| 141 | + //world.setWorldTime(0); |
| 142 | + |
| 143 | + // Chest for stuff with 5 stone axes |
| 144 | + world.setBlockWithNotify(i, j, k, mod_MineColony.hutFarmer.blockID); |
| 145 | + TileEntityChest tileentitychest = (TileEntityChest) world |
| 146 | + .getBlockTileEntity(i, j, k); |
| 147 | + |
| 148 | + tileentitychest.setInventorySlotContents(0, new ItemStack(Item.hoeWood, 1)); |
| 149 | + tileentitychest.setInventorySlotContents(1, new ItemStack(Item.shovelWood, 1)); |
| 150 | + tileentitychest.setInventorySlotContents(2, new ItemStack(Item.seeds, 10)); |
| 151 | + |
| 152 | + |
| 153 | + //tileentitychest.setInventorySlotContents(8, new ItemStack(mod_MineColony.scepterGold, 1)); |
| 154 | + |
| 155 | + spawnWorker(world, i, j, k); |
| 156 | + } |
| 157 | + |
| 158 | + public void updateTick(World world, int i, int j, int k, Random random) |
| 159 | + { |
| 160 | + super.updateTick(world, i, j, k, random); |
| 161 | + |
| 162 | + if(getFarmerAround(world, i,j,k)==null) |
| 163 | + { |
| 164 | + if(em!=null) |
| 165 | + em.isDead = true; |
| 166 | + spawnWorker(world, i, j, k); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public void spawnWorker(World world, int i, int j, int k) |
| 171 | + { |
| 172 | + em = (EntityFarmer)EntityList.createEntityInWorld("Farmer", world); |
| 173 | + |
| 174 | + // scan for first free block near chest |
| 175 | + Vec3D spawnPoint = scanForBlockNearPoint(world, 0, i, j, k, 1, 0, 1); |
| 176 | + if(spawnPoint==null) |
| 177 | + spawnPoint = scanForBlockNearPoint(world, Block.snow.blockID, i, j, k, 1, 0, 1); |
| 178 | + |
| 179 | + if(spawnPoint!=null) |
| 180 | + { |
| 181 | + em.setPosition(spawnPoint.xCoord, spawnPoint.yCoord, spawnPoint.zCoord); |
| 182 | + em.setHomePosition(i, j, k); |
| 183 | + world.entityJoinedWorld(em); |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + public void onBlockRemoval(World world, int i, int j, int k) |
| 189 | + { |
| 190 | + em = getFarmerAround(world, i,j,k); |
| 191 | + if(em!=null) |
| 192 | + { |
| 193 | + em.isDead=true; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public EntityFarmer getFarmerAround(World world, int i, int j, int k) |
| 198 | + { |
| 199 | + List list = world.getEntitiesWithinAABB(EntityFarmer.class, this.getCollisionBoundingBoxFromPool(world, i, j, k).expand(workingRange, 20, workingRange)); |
| 200 | + if (list != null) { |
| 201 | + for (int ii = 0; ii < list.size(); ii++) { |
| 202 | + if (list.get(ii) instanceof EntityFarmer) { |
| 203 | + return (EntityFarmer)list.get(ii); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + public int getBlockTextureFromSide(int side) |
| 212 | + { |
| 213 | + if(side==1) |
| 214 | + { |
| 215 | + // Workshop top |
| 216 | + return textureID; |
| 217 | + } |
| 218 | + return blockIndexInTexture; |
| 219 | + } |
| 220 | + |
| 221 | + public int getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l) |
| 222 | + { |
| 223 | + if(l == 1) |
| 224 | + { |
| 225 | + return textureID; |
| 226 | + } |
| 227 | + else |
| 228 | + return super.getBlockTexture(iblockaccess, i, j, k, l); |
| 229 | + } |
| 230 | + |
| 231 | +} |
0 commit comments