| 1 | package net.minecraft.village;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.Iterator;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.block.Block;
|
| 7 | import net.minecraft.block.BlockDoor;
|
| 8 | import net.minecraft.block.material.Material;
|
| 9 | import net.minecraft.nbt.NBTTagCompound;
|
| 10 | import net.minecraft.nbt.NBTTagList;
|
| 11 | import net.minecraft.util.BlockPos;
|
| 12 | import net.minecraft.util.EnumFacing;
|
| 13 | import net.minecraft.world.World;
|
| 14 | import net.minecraft.world.WorldProvider;
|
| 15 | import net.minecraft.world.WorldSavedData;
|
| 16 |
|
| 17 | public class VillageCollection extends WorldSavedData
|
| 18 | {
|
| 19 | private World worldObj;
|
| 20 | private final List<BlockPos> villagerPositionsList = Lists.<BlockPos>newArrayList();
|
| 21 | private final List<VillageDoorInfo> newDoors = Lists.<VillageDoorInfo>newArrayList();
|
| 22 | private final List<Village> villageList = Lists.<Village>newArrayList();
|
| 23 | private int tickCounter;
|
| 24 |
|
| 25 | public VillageCollection(String name)
|
| 26 | {
|
| 27 | super(name);
|
| 28 | }
|
| 29 |
|
| 30 | public VillageCollection(World worldIn)
|
| 31 | {
|
| 32 | super(fileNameForProvider(worldIn.provider));
|
| 33 | this.worldObj = worldIn;
|
| 34 | this.markDirty();
|
| 35 | }
|
| 36 |
|
| 37 | public void setWorldsForAll(World worldIn)
|
| 38 | {
|
| 39 | this.worldObj = worldIn;
|
| 40 |
|
| 41 | for (Village village : this.villageList)
|
| 42 | {
|
| 43 | village.setWorld(worldIn);
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | public void addToVillagerPositionList(BlockPos pos)
|
| 48 | {
|
| 49 | if (this.villagerPositionsList.size() <= 64)
|
| 50 | {
|
| 51 | if (!this.positionInList(pos))
|
| 52 | {
|
| 53 | this.villagerPositionsList.add(pos);
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | /**
|
| 59 | * Runs a single tick for the village collection
|
| 60 | */
|
| 61 | public void tick()
|
| 62 | {
|
| 63 | ++this.tickCounter;
|
| 64 |
|
| 65 | for (Village village : this.villageList)
|
| 66 | {
|
| 67 | village.tick(this.tickCounter);
|
| 68 | }
|
| 69 |
|
| 70 | this.removeAnnihilatedVillages();
|
| 71 | this.dropOldestVillagerPosition();
|
| 72 | this.addNewDoorsToVillageOrCreateVillage();
|
| 73 |
|
| 74 | if (this.tickCounter % 400 == 0)
|
| 75 | {
|
| 76 | this.markDirty();
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | private void removeAnnihilatedVillages()
|
| 81 | {
|
| 82 | Iterator<Village> iterator = this.villageList.iterator();
|
| 83 |
|
| 84 | while (iterator.hasNext())
|
| 85 | {
|
| 86 | Village village = (Village)iterator.next();
|
| 87 |
|
| 88 | if (village.isAnnihilated())
|
| 89 | {
|
| 90 | iterator.remove();
|
| 91 | this.markDirty();
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public List<Village> getVillageList()
|
| 97 | {
|
| 98 | return this.villageList;
|
| 99 | }
|
| 100 |
|
| 101 | public Village getNearestVillage(BlockPos doorBlock, int radius)
|
| 102 | {
|
| 103 | Village village = null;
|
| 104 | double d0 = 3.4028234663852886E38D;
|
| 105 |
|
| 106 | for (Village village1 : this.villageList)
|
| 107 | {
|
| 108 | double d1 = village1.getCenter().distanceSq(doorBlock);
|
| 109 |
|
| 110 | if (d1 < d0)
|
| 111 | {
|
| 112 | float f = (float)(radius + village1.getVillageRadius());
|
| 113 |
|
| 114 | if (d1 <= (double)(f * f))
|
| 115 | {
|
| 116 | village = village1;
|
| 117 | d0 = d1;
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | return village;
|
| 123 | }
|
| 124 |
|
| 125 | private void dropOldestVillagerPosition()
|
| 126 | {
|
| 127 | if (!this.villagerPositionsList.isEmpty())
|
| 128 | {
|
| 129 | this.addDoorsAround((BlockPos)this.villagerPositionsList.remove(0));
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | private void addNewDoorsToVillageOrCreateVillage()
|
| 134 | {
|
| 135 | for (int i = 0; i < this.newDoors.size(); ++i)
|
| 136 | {
|
| 137 | VillageDoorInfo villagedoorinfo = (VillageDoorInfo)this.newDoors.get(i);
|
| 138 | Village village = this.getNearestVillage(villagedoorinfo.getDoorBlockPos(), 32);
|
| 139 |
|
| 140 | if (village == null)
|
| 141 | {
|
| 142 | village = new Village(this.worldObj);
|
| 143 | this.villageList.add(village);
|
| 144 | this.markDirty();
|
| 145 | }
|
| 146 |
|
| 147 | village.addVillageDoorInfo(villagedoorinfo);
|
| 148 | }
|
| 149 |
|
| 150 | this.newDoors.clear();
|
| 151 | }
|
| 152 |
|
| 153 | private void addDoorsAround(BlockPos central)
|
| 154 | {
|
| 155 | int i = 16;
|
| 156 | int j = 4;
|
| 157 | int k = 16;
|
| 158 |
|
| 159 | for (int l = -i; l < i; ++l)
|
| 160 | {
|
| 161 | for (int i1 = -j; i1 < j; ++i1)
|
| 162 | {
|
| 163 | for (int j1 = -k; j1 < k; ++j1)
|
| 164 | {
|
| 165 | BlockPos blockpos = central.add(l, i1, j1);
|
| 166 |
|
| 167 | if (this.isWoodDoor(blockpos))
|
| 168 | {
|
| 169 | VillageDoorInfo villagedoorinfo = this.checkDoorExistence(blockpos);
|
| 170 |
|
| 171 | if (villagedoorinfo == null)
|
| 172 | {
|
| 173 | this.addToNewDoorsList(blockpos);
|
| 174 | }
|
| 175 | else
|
| 176 | {
|
| 177 | villagedoorinfo.func_179849_a(this.tickCounter);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | /**
|
| 186 | * returns the VillageDoorInfo if it exists in any village or in the newDoor list, otherwise returns null
|
| 187 | */
|
| 188 | private VillageDoorInfo checkDoorExistence(BlockPos doorBlock)
|
| 189 | {
|
| 190 | for (VillageDoorInfo villagedoorinfo : this.newDoors)
|
| 191 | {
|
| 192 | if (villagedoorinfo.getDoorBlockPos().getX() == doorBlock.getX() && villagedoorinfo.getDoorBlockPos().getZ() == doorBlock.getZ() && Math.abs(villagedoorinfo.getDoorBlockPos().getY() - doorBlock.getY()) <= 1)
|
| 193 | {
|
| 194 | return villagedoorinfo;
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | for (Village village : this.villageList)
|
| 199 | {
|
| 200 | VillageDoorInfo villagedoorinfo1 = village.getExistedDoor(doorBlock);
|
| 201 |
|
| 202 | if (villagedoorinfo1 != null)
|
| 203 | {
|
| 204 | return villagedoorinfo1;
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | return null;
|
| 209 | }
|
| 210 |
|
| 211 | private void addToNewDoorsList(BlockPos doorBlock)
|
| 212 | {
|
| 213 | EnumFacing enumfacing = BlockDoor.getFacing(this.worldObj, doorBlock);
|
| 214 | EnumFacing enumfacing1 = enumfacing.getOpposite();
|
| 215 | int i = this.countBlocksCanSeeSky(doorBlock, enumfacing, 5);
|
| 216 | int j = this.countBlocksCanSeeSky(doorBlock, enumfacing1, i + 1);
|
| 217 |
|
| 218 | if (i != j)
|
| 219 | {
|
| 220 | this.newDoors.add(new VillageDoorInfo(doorBlock, i < j ? enumfacing : enumfacing1, this.tickCounter));
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | /**
|
| 225 | * Check five blocks in the direction. The centerPos will not be checked.
|
| 226 | */
|
| 227 | private int countBlocksCanSeeSky(BlockPos centerPos, EnumFacing direction, int limitation)
|
| 228 | {
|
| 229 | int i = 0;
|
| 230 |
|
| 231 | for (int j = 1; j <= 5; ++j)
|
| 232 | {
|
| 233 | if (this.worldObj.canSeeSky(centerPos.offset(direction, j)))
|
| 234 | {
|
| 235 | ++i;
|
| 236 |
|
| 237 | if (i >= limitation)
|
| 238 | {
|
| 239 | return i;
|
| 240 | }
|
| 241 | }
|
| 242 | }
|
| 243 |
|
| 244 | return i;
|
| 245 | }
|
| 246 |
|
| 247 | private boolean positionInList(BlockPos pos)
|
| 248 | {
|
| 249 | for (BlockPos blockpos : this.villagerPositionsList)
|
| 250 | {
|
| 251 | if (blockpos.equals(pos))
|
| 252 | {
|
| 253 | return true;
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | return false;
|
| 258 | }
|
| 259 |
|
| 260 | private boolean isWoodDoor(BlockPos doorPos)
|
| 261 | {
|
| 262 | Block block = this.worldObj.getBlockState(doorPos).getBlock();
|
| 263 | return block instanceof BlockDoor ? block.getMaterial() == Material.wood : false;
|
| 264 | }
|
| 265 |
|
| 266 | /**
|
| 267 | * reads in data from the NBTTagCompound into this MapDataBase
|
| 268 | */
|
| 269 | public void readFromNBT(NBTTagCompound nbt)
|
| 270 | {
|
| 271 | this.tickCounter = nbt.getInteger("Tick");
|
| 272 | NBTTagList nbttaglist = nbt.getTagList("Villages", 10);
|
| 273 |
|
| 274 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 275 | {
|
| 276 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
| 277 | Village village = new Village();
|
| 278 | village.readVillageDataFromNBT(nbttagcompound);
|
| 279 | this.villageList.add(village);
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | /**
|
| 284 | * write data to NBTTagCompound from this MapDataBase, similar to Entities and TileEntities
|
| 285 | */
|
| 286 | public void writeToNBT(NBTTagCompound nbt)
|
| 287 | {
|
| 288 | nbt.setInteger("Tick", this.tickCounter);
|
| 289 | NBTTagList nbttaglist = new NBTTagList();
|
| 290 |
|
| 291 | for (Village village : this.villageList)
|
| 292 | {
|
| 293 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 294 | village.writeVillageDataToNBT(nbttagcompound);
|
| 295 | nbttaglist.appendTag(nbttagcompound);
|
| 296 | }
|
| 297 |
|
| 298 | nbt.setTag("Villages", nbttaglist);
|
| 299 | }
|
| 300 |
|
| 301 | public static String fileNameForProvider(WorldProvider provider)
|
| 302 | {
|
| 303 | return "villages" + provider.getInternalNameSuffix();
|
| 304 | }
|
| 305 | }
|
| 306 |
|