| 1 | package net.minecraft.world.gen;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.IOException;
|
| 5 | import java.util.Collections;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.concurrent.ConcurrentHashMap;
|
| 9 | import net.minecraft.crash.CrashReport;
|
| 10 | import net.minecraft.crash.CrashReportCategory;
|
| 11 | import net.minecraft.entity.EnumCreatureType;
|
| 12 | import net.minecraft.util.BlockPos;
|
| 13 | import net.minecraft.util.IProgressUpdate;
|
| 14 | import net.minecraft.util.LongHashMap;
|
| 15 | import net.minecraft.util.ReportedException;
|
| 16 | import net.minecraft.world.ChunkCoordIntPair;
|
| 17 | import net.minecraft.world.MinecraftException;
|
| 18 | import net.minecraft.world.World;
|
| 19 | import net.minecraft.world.WorldServer;
|
| 20 | import net.minecraft.world.biome.BiomeGenBase;
|
| 21 | import net.minecraft.world.chunk.Chunk;
|
| 22 | import net.minecraft.world.chunk.EmptyChunk;
|
| 23 | import net.minecraft.world.chunk.IChunkProvider;
|
| 24 | import net.minecraft.world.chunk.storage.IChunkLoader;
|
| 25 | import org.apache.logging.log4j.LogManager;
|
| 26 | import org.apache.logging.log4j.Logger;
|
| 27 |
|
| 28 | public class ChunkProviderServer implements IChunkProvider
|
| 29 | {
|
| 30 | private static final Logger logger = LogManager.getLogger();
|
| 31 | private Set<Long> droppedChunksSet = Collections.<Long>newSetFromMap(new ConcurrentHashMap());
|
| 32 |
|
| 33 | /** a dummy chunk, returned in place of an actual chunk. */
|
| 34 | private Chunk dummyChunk;
|
| 35 |
|
| 36 | /**
|
| 37 | * chunk generator object. Calls to load nonexistent chunks are forwarded to this object.
|
| 38 | */
|
| 39 | private IChunkProvider serverChunkGenerator;
|
| 40 | private IChunkLoader chunkLoader;
|
| 41 |
|
| 42 | /**
|
| 43 | * if set, this flag forces a request to load a chunk to load the chunk rather than defaulting to the dummy if
|
| 44 | * possible
|
| 45 | */
|
| 46 | public boolean chunkLoadOverride = true;
|
| 47 | private LongHashMap<Chunk> id2ChunkMap = new LongHashMap();
|
| 48 | private List<Chunk> loadedChunks = Lists.<Chunk>newArrayList();
|
| 49 | private WorldServer worldObj;
|
| 50 |
|
| 51 | public ChunkProviderServer(WorldServer p_i1520_1_, IChunkLoader p_i1520_2_, IChunkProvider p_i1520_3_)
|
| 52 | {
|
| 53 | this.dummyChunk = new EmptyChunk(p_i1520_1_, 0, 0);
|
| 54 | this.worldObj = p_i1520_1_;
|
| 55 | this.chunkLoader = p_i1520_2_;
|
| 56 | this.serverChunkGenerator = p_i1520_3_;
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * Checks to see if a chunk exists at x, z
|
| 61 | */
|
| 62 | public boolean chunkExists(int x, int z)
|
| 63 | {
|
| 64 | return this.id2ChunkMap.containsItem(ChunkCoordIntPair.chunkXZ2Int(x, z));
|
| 65 | }
|
| 66 |
|
| 67 | public List<Chunk> func_152380_a()
|
| 68 | {
|
| 69 | return this.loadedChunks;
|
| 70 | }
|
| 71 |
|
| 72 | public void dropChunk(int x, int z)
|
| 73 | {
|
| 74 | if (this.worldObj.provider.canRespawnHere())
|
| 75 | {
|
| 76 | if (!this.worldObj.isSpawnChunk(x, z))
|
| 77 | {
|
| 78 | this.droppedChunksSet.add(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(x, z)));
|
| 79 | }
|
| 80 | }
|
| 81 | else
|
| 82 | {
|
| 83 | this.droppedChunksSet.add(Long.valueOf(ChunkCoordIntPair.chunkXZ2Int(x, z)));
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | /**
|
| 88 | * marks all chunks for unload, ignoring those near the spawn
|
| 89 | */
|
| 90 | public void unloadAllChunks()
|
| 91 | {
|
| 92 | for (Chunk chunk : this.loadedChunks)
|
| 93 | {
|
| 94 | this.dropChunk(chunk.xPosition, chunk.zPosition);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | /**
|
| 99 | * loads or generates the chunk at the chunk location specified
|
| 100 | *
|
| 101 | * @param chunkX x coord of the chunk to load (block coord >> 4)
|
| 102 | * @param chunkZ z coord of the chunk to load (block coord >> 4)
|
| 103 | */
|
| 104 | public Chunk loadChunk(int chunkX, int chunkZ)
|
| 105 | {
|
| 106 | long i = ChunkCoordIntPair.chunkXZ2Int(chunkX, chunkZ);
|
| 107 | this.droppedChunksSet.remove(Long.valueOf(i));
|
| 108 | Chunk chunk = (Chunk)this.id2ChunkMap.getValueByKey(i);
|
| 109 |
|
| 110 | if (chunk == null)
|
| 111 | {
|
| 112 | chunk = this.loadChunkFromFile(chunkX, chunkZ);
|
| 113 |
|
| 114 | if (chunk == null)
|
| 115 | {
|
| 116 | if (this.serverChunkGenerator == null)
|
| 117 | {
|
| 118 | chunk = this.dummyChunk;
|
| 119 | }
|
| 120 | else
|
| 121 | {
|
| 122 | try
|
| 123 | {
|
| 124 | chunk = this.serverChunkGenerator.provideChunk(chunkX, chunkZ);
|
| 125 | }
|
| 126 | catch (Throwable throwable)
|
| 127 | {
|
| 128 | CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Exception generating new chunk");
|
| 129 | CrashReportCategory crashreportcategory = crashreport.makeCategory("Chunk to be generated");
|
| 130 | crashreportcategory.addCrashSection("Location", String.format("%d,%d", new Object[] {Integer.valueOf(chunkX), Integer.valueOf(chunkZ)}));
|
| 131 | crashreportcategory.addCrashSection("Position hash", Long.valueOf(i));
|
| 132 | crashreportcategory.addCrashSection("Generator", this.serverChunkGenerator.makeString());
|
| 133 | throw new ReportedException(crashreport);
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | this.id2ChunkMap.add(i, chunk);
|
| 139 | this.loadedChunks.add(chunk);
|
| 140 | chunk.onChunkLoad();
|
| 141 | chunk.populateChunk(this, this, chunkX, chunkZ);
|
| 142 | }
|
| 143 |
|
| 144 | return chunk;
|
| 145 | }
|
| 146 |
|
| 147 | /**
|
| 148 | * Will return back a chunk, if it doesn't exist and its not a MP client it will generates all the blocks for the
|
| 149 | * specified chunk from the map seed and chunk seed
|
| 150 | */
|
| 151 | public Chunk provideChunk(int x, int z)
|
| 152 | {
|
| 153 | Chunk chunk = (Chunk)this.id2ChunkMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(x, z));
|
| 154 | return chunk == null ? (!this.worldObj.isFindingSpawnPoint() && !this.chunkLoadOverride ? this.dummyChunk : this.loadChunk(x, z)) : chunk;
|
| 155 | }
|
| 156 |
|
| 157 | private Chunk loadChunkFromFile(int x, int z)
|
| 158 | {
|
| 159 | if (this.chunkLoader == null)
|
| 160 | {
|
| 161 | return null;
|
| 162 | }
|
| 163 | else
|
| 164 | {
|
| 165 | try
|
| 166 | {
|
| 167 | Chunk chunk = this.chunkLoader.loadChunk(this.worldObj, x, z);
|
| 168 |
|
| 169 | if (chunk != null)
|
| 170 | {
|
| 171 | chunk.setLastSaveTime(this.worldObj.getTotalWorldTime());
|
| 172 |
|
| 173 | if (this.serverChunkGenerator != null)
|
| 174 | {
|
| 175 | this.serverChunkGenerator.recreateStructures(chunk, x, z);
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | return chunk;
|
| 180 | }
|
| 181 | catch (Exception exception)
|
| 182 | {
|
| 183 | logger.error((String)"Couldn\'t load chunk", (Throwable)exception);
|
| 184 | return null;
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | private void saveChunkExtraData(Chunk chunkIn)
|
| 190 | {
|
| 191 | if (this.chunkLoader != null)
|
| 192 | {
|
| 193 | try
|
| 194 | {
|
| 195 | this.chunkLoader.saveExtraChunkData(this.worldObj, chunkIn);
|
| 196 | }
|
| 197 | catch (Exception exception)
|
| 198 | {
|
| 199 | logger.error((String)"Couldn\'t save entities", (Throwable)exception);
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | private void saveChunkData(Chunk chunkIn)
|
| 205 | {
|
| 206 | if (this.chunkLoader != null)
|
| 207 | {
|
| 208 | try
|
| 209 | {
|
| 210 | chunkIn.setLastSaveTime(this.worldObj.getTotalWorldTime());
|
| 211 | this.chunkLoader.saveChunk(this.worldObj, chunkIn);
|
| 212 | }
|
| 213 | catch (IOException ioexception)
|
| 214 | {
|
| 215 | logger.error((String)"Couldn\'t save chunk", (Throwable)ioexception);
|
| 216 | }
|
| 217 | catch (MinecraftException minecraftexception)
|
| 218 | {
|
| 219 | logger.error((String)"Couldn\'t save chunk; already in use by another instance of Minecraft?", (Throwable)minecraftexception);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | /**
|
| 225 | * Populates chunk with ores etc etc
|
| 226 | */
|
| 227 | public void populate(IChunkProvider chunkProvider, int x, int z)
|
| 228 | {
|
| 229 | Chunk chunk = this.provideChunk(x, z);
|
| 230 |
|
| 231 | if (!chunk.isTerrainPopulated())
|
| 232 | {
|
| 233 | chunk.func_150809_p();
|
| 234 |
|
| 235 | if (this.serverChunkGenerator != null)
|
| 236 | {
|
| 237 | this.serverChunkGenerator.populate(chunkProvider, x, z);
|
| 238 | chunk.setChunkModified();
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | public boolean populateChunk(IChunkProvider chunkProvider, Chunk chunkIn, int x, int z)
|
| 244 | {
|
| 245 | if (this.serverChunkGenerator != null && this.serverChunkGenerator.populateChunk(chunkProvider, chunkIn, x, z))
|
| 246 | {
|
| 247 | Chunk chunk = this.provideChunk(x, z);
|
| 248 | chunk.setChunkModified();
|
| 249 | return true;
|
| 250 | }
|
| 251 | else
|
| 252 | {
|
| 253 | return false;
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | /**
|
| 258 | * Two modes of operation: if passed true, save all Chunks in one go. If passed false, save up to two chunks.
|
| 259 | * Return true if all chunks have been saved.
|
| 260 | */
|
| 261 | public boolean saveChunks(boolean saveAllChunks, IProgressUpdate progressCallback)
|
| 262 | {
|
| 263 | int i = 0;
|
| 264 | List<Chunk> list = Lists.newArrayList(this.loadedChunks);
|
| 265 |
|
| 266 | for (int j = 0; j < ((List)list).size(); ++j)
|
| 267 | {
|
| 268 | Chunk chunk = (Chunk)list.get(j);
|
| 269 |
|
| 270 | if (saveAllChunks)
|
| 271 | {
|
| 272 | this.saveChunkExtraData(chunk);
|
| 273 | }
|
| 274 |
|
| 275 | if (chunk.needsSaving(saveAllChunks))
|
| 276 | {
|
| 277 | this.saveChunkData(chunk);
|
| 278 | chunk.setModified(false);
|
| 279 | ++i;
|
| 280 |
|
| 281 | if (i == 24 && !saveAllChunks)
|
| 282 | {
|
| 283 | return false;
|
| 284 | }
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | return true;
|
| 289 | }
|
| 290 |
|
| 291 | /**
|
| 292 | * Save extra data not associated with any Chunk. Not saved during autosave, only during world unload. Currently
|
| 293 | * unimplemented.
|
| 294 | */
|
| 295 | public void saveExtraData()
|
| 296 | {
|
| 297 | if (this.chunkLoader != null)
|
| 298 | {
|
| 299 | this.chunkLoader.saveExtraData();
|
| 300 | }
|
| 301 | }
|
| 302 |
|
| 303 | /**
|
| 304 | * Unloads chunks that are marked to be unloaded. This is not guaranteed to unload every such chunk.
|
| 305 | */
|
| 306 | public boolean unloadQueuedChunks()
|
| 307 | {
|
| 308 | if (!this.worldObj.disableLevelSaving)
|
| 309 | {
|
| 310 | for (int i = 0; i < 100; ++i)
|
| 311 | {
|
| 312 | if (!this.droppedChunksSet.isEmpty())
|
| 313 | {
|
| 314 | Long olong = (Long)this.droppedChunksSet.iterator().next();
|
| 315 | Chunk chunk = (Chunk)this.id2ChunkMap.getValueByKey(olong.longValue());
|
| 316 |
|
| 317 | if (chunk != null)
|
| 318 | {
|
| 319 | chunk.onChunkUnload();
|
| 320 | this.saveChunkData(chunk);
|
| 321 | this.saveChunkExtraData(chunk);
|
| 322 | this.id2ChunkMap.remove(olong.longValue());
|
| 323 | this.loadedChunks.remove(chunk);
|
| 324 | }
|
| 325 |
|
| 326 | this.droppedChunksSet.remove(olong);
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | if (this.chunkLoader != null)
|
| 331 | {
|
| 332 | this.chunkLoader.chunkTick();
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | return this.serverChunkGenerator.unloadQueuedChunks();
|
| 337 | }
|
| 338 |
|
| 339 | /**
|
| 340 | * Returns if the IChunkProvider supports saving.
|
| 341 | */
|
| 342 | public boolean canSave()
|
| 343 | {
|
| 344 | return !this.worldObj.disableLevelSaving;
|
| 345 | }
|
| 346 |
|
| 347 | /**
|
| 348 | * Converts the instance data to a readable string.
|
| 349 | */
|
| 350 | public String makeString()
|
| 351 | {
|
| 352 | return "ServerChunkCache: " + this.id2ChunkMap.getNumHashElements() + " Drop: " + this.droppedChunksSet.size();
|
| 353 | }
|
| 354 |
|
| 355 | public List<BiomeGenBase.SpawnListEntry> getPossibleCreatures(EnumCreatureType creatureType, BlockPos pos)
|
| 356 | {
|
| 357 | return this.serverChunkGenerator.getPossibleCreatures(creatureType, pos);
|
| 358 | }
|
| 359 |
|
| 360 | public BlockPos getStrongholdGen(World worldIn, String structureName, BlockPos position)
|
| 361 | {
|
| 362 | return this.serverChunkGenerator.getStrongholdGen(worldIn, structureName, position);
|
| 363 | }
|
| 364 |
|
| 365 | public int getLoadedChunkCount()
|
| 366 | {
|
| 367 | return this.id2ChunkMap.getNumHashElements();
|
| 368 | }
|
| 369 |
|
| 370 | public void recreateStructures(Chunk chunkIn, int x, int z)
|
| 371 | {
|
| 372 | }
|
| 373 |
|
| 374 | public Chunk provideChunk(BlockPos blockPosIn)
|
| 375 | {
|
| 376 | return this.provideChunk(blockPosIn.getX() >> 4, blockPosIn.getZ() >> 4);
|
| 377 | }
|
| 378 | }
|
| 379 |
|