Package net.minecraft.world.chunk

Examples of net.minecraft.world.chunk.Chunk


  @Override
  @Declare
  public void unloadChunkForce(long hash) {
    if (unloadStage0.add(hash)) {
      Chunk chunk = (Chunk) chunks.getValueByKey(hash);
      if (chunk != null) {
        chunk.queuedUnload = true;
      }
    }
  }
View Full Code Here


  public Chunk regenerateChunk(int x, int z) {
    long key = key(x, z);
    AtomicInteger lock = getLock(key);
    synchronized (lock) {
      try {
        Chunk chunk = getChunkIfExists(x, z);
        if (chunk != null) {
          finalizeUnload(key);
          do {
            chunk = getChunkIfExists(x, z);
            if (chunk == null) {
              continue;
            }
            chunk.queuedUnload = true;
            synchronized (chunk) {
              if (chunk.partiallyUnloaded || unloadingChunks.containsItem(key)) {
                continue;
              }
              if (!fireBukkitUnloadEvent(chunk)) {
                Log.warning("Bukkit cancelled chunk unload for regeneration unload of " + x + ", " + z, new Throwable());
              }
              if (lastChunk == chunk) {
                lastChunk = null;
              }
              chunk.partiallyUnloaded = true;
              chunk.onChunkUnloadTT();
              chunk.pendingBlockUpdates = world.getPendingBlockUpdates(chunk, false);
              loadedChunks.remove(chunk);
              chunks.remove(key);
              synchronized (unloadingChunks) {
                unloadingChunks.put(key, chunk);
View Full Code Here

    }
  }

  @Override
  public final Chunk provideChunk(int x, int z) {
    Chunk chunk = getChunkIfExists(x, z);

    if (chunk != null) {
      return chunk;
    }
View Full Code Here

    return defaultEmptyChunk;
  }

  @Override
  public final Chunk loadChunk(int x, int z) {
    Chunk chunk = getChunkAt(x, z, true, false, null);
    chunk.queuedUnload = false;
    return chunk;
  }
View Full Code Here

  }

  @Override
  @Declare
  public final Chunk getChunkIfExists(int x, int z) {
    Chunk chunk = lastChunk;
    if (chunk != null && chunk.xPosition == x && chunk.zPosition == z) {
      return chunk;
    }
    long key = key(x, z);
    chunk = (Chunk) chunks.getValueByKey(key);
View Full Code Here

  }

  @Override
  @Declare
  public final Chunk getChunkAt(final int x, final int z, boolean allowGenerate, boolean regenerate, final Runnable runnable) {
    Chunk chunk = getChunkIfExists(x, z);

    if (chunk != null) {
      if (runnable != null) {
        ThreadedChunkProvider.onChunkLoad(chunk, runnable);
      }
View Full Code Here

    if (!safeToGenerate()) {
      return defaultEmptyChunk;
    }

    long key = key(x, z);
    Chunk chunk;

    final AtomicInteger lock = getLock(key);
    boolean wasGenerated = false;
    try {
      boolean inLoadingMap = false;

      // Lock on the lock for this chunk - prevent multiple instances of the same chunk
      synchronized (lock) {
        chunk = (Chunk) chunks.getValueByKey(key);
        if (chunk != null) {
          return chunk;
        }
        chunk = (Chunk) loadingChunks.getValueByKey(key);
        if (regenerate) {
          if (!allowGenerate) {
            throw new IllegalArgumentException();
          }
          loadingChunks.put(key, defaultEmptyChunk);
        } else if (chunk == null) {
          finalizeUnload(key);
          chunk = safeLoadChunk(x, z);
          if (chunk != null && (chunk.xPosition != x || chunk.zPosition != z)) {
            Log.severe("Chunk at " + chunk.xPosition + ',' + chunk.zPosition + " was stored at " + x + ',' + z + "\nResetting this chunk.");
            chunk = null;
          }
          if (chunk == null) {
            loadingChunks.put(key, defaultEmptyChunk);
            if (!allowGenerate) {
              return null;
            } else if (generator == null) {
              return defaultEmptyChunk;
            }
          } else {
            loadingChunks.put(key, chunk);
            inLoadingMap = true;
            if (!world.loadEventFired) {
              Log.warning("Loaded chunk before world load event fired, this can cause many issues, including loss of multiblock data.", new Throwable());
            }
          }
        } else if (chunk != defaultEmptyChunk) {
          inLoadingMap = true;
        }
      }
      // Unlock this chunk - avoids a deadlock
      // Thread A - requests chunk A - needs genned
      // Thread B - requests chunk B - needs genned
      // In thread A, redpower tries to load chunk B
      // because its marble gen is buggy.
      // Thread B is now waiting for the generate lock,
      // Thread A is waiting for the lock on chunk B

      // Lock the generation lock - ChunkProviderGenerate isn't threadsafe at all

      boolean locked = true;
      generateLock.lock();
      try {
        synchronized (lock) {
          chunk = (Chunk) chunks.getValueByKey(key);
          if (chunk != null) {
            return chunk;
          }
          worldGenInProgress.set(Boolean.TRUE);
          try {
            chunk = (Chunk) loadingChunks.getValueByKey(key);
            if (chunk == null) {
              Log.severe("Failed to load chunk " + chunk + " at " + x + ',' + z + " as it is missing from the loading chunks map.");
              return defaultEmptyChunk;
            }
            if (chunk == defaultEmptyChunk) {
              try {
                chunk = generator.provideChunk(x, z);
                if (chunk == null) {
                  Log.severe("Null chunk was generated for " + x + ',' + z + " by " + generator);
                  return defaultEmptyChunk;
                }
                chunk.isTerrainPopulated = false;
                wasGenerated = true;
              } catch (Throwable t) {
                Log.severe("Failed to generate a chunk in " + Log.name(world) + " at chunk coords " + x + ',' + z);
                throw UnsafeUtil.throwIgnoreChecked(t);
              }
            } else {
              if (generator != null) {
                generator.recreateStructures(x, z);
              }
            }

            if (!inLoadingMap) {
              loadingChunks.put(key, chunk);
            }

            locked = false;
            generateLock.unlock();
            chunk.threadUnsafeChunkLoad();

            chunks.put(key, chunk);
          } finally {
            worldGenInProgress.set(Boolean.FALSE);
          }
        }
      } finally {
        if (locked) {
          generateLock.unlock();
        }
      }
    } finally {
      if (lock.decrementAndGet() == 0) {
        loadingChunks.remove(key);
      }
    }

    loadedChunks.add(chunk);
    chunk.onChunkLoad();
    fireBukkitLoadEvent(chunk, wasGenerated);
    chunkLoadLocks.remove(key);
    tryPopulateChunks(chunk);

    return chunk;
View Full Code Here

  private void tryPopulateChunks(Chunk centerChunk) {
    int cX = centerChunk.xPosition;
    int cZ = centerChunk.zPosition;
    for (int x = cX - populationRange; x <= cX + populationRange; x++) {
      for (int z = cZ - populationRange; z <= cZ + populationRange; z++) {
        Chunk chunk = getChunkFastUnsafe(x, z);
        if (chunk != null && !chunk.queuedUnload && !chunk.partiallyUnloaded && !chunk.isTerrainPopulated && checkChunksExistLoadedNormally(x - populationRange, x + populationRange, z - populationRange, z + populationRange)) {
          populate(chunk);
        }
      }
    }
View Full Code Here

  }

  public boolean checkChunksExistLoadedNormally(int minX, int maxX, int minZ, int maxZ) {
    for (int x = minX; x <= maxX; ++x) {
      for (int z = minZ; z <= maxZ; ++z) {
        Chunk chunk = getChunkFastUnsafe(x, z);
        if (chunk == null || chunk.queuedUnload || chunk.partiallyUnloaded) {
          return false;
        }
      }
    }
View Full Code Here

  protected Chunk safeLoadChunk(int x, int z) {
    if (loader == null) {
      return null;
    }
    try {
      Chunk chunk = loader.loadChunk(world, x, z);

      if (chunk != null) {
        chunk.lastSaveTime = world.getTotalWorldTime();
      }
View Full Code Here

TOP

Related Classes of net.minecraft.world.chunk.Chunk

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.