Exemplo n.º 1
0
  public int loadChunks(int max_to_load) {
    if (dw.isLoaded() == false) return 0;
    long t0 = System.nanoTime();
    Object queue = helper.getUnloadQueue(helper.getNMSWorld(w));

    int cnt = 0;
    if (iterator == null) iterator = chunks.listIterator();

    DynmapCore.setIgnoreChunkLoads(true);
    // boolean isnormral = w.getEnvironment() == Environment.NORMAL;
    // Load the required chunks.
    while ((cnt < max_to_load) && iterator.hasNext()) {
      DynmapChunk chunk = iterator.next();
      boolean vis = true;
      if (visible_limits != null) {
        vis = false;
        for (VisibilityLimit limit : visible_limits) {
          if ((chunk.x >= limit.x0)
              && (chunk.x <= limit.x1)
              && (chunk.z >= limit.z0)
              && (chunk.z <= limit.z1)) {
            vis = true;
            break;
          }
        }
      }
      if (vis && (hidden_limits != null)) {
        for (VisibilityLimit limit : hidden_limits) {
          if ((chunk.x >= limit.x0)
              && (chunk.x <= limit.x1)
              && (chunk.z >= limit.z0)
              && (chunk.z <= limit.z1)) {
            vis = false;
            break;
          }
        }
      }
      /* Check if cached chunk snapshot found */
      ChunkSnapshot ss = null;
      DynIntHashMap tileData = null;
      SnapshotRec ssr =
          DynmapPlugin.plugin.sscache.getSnapshot(
              dw.getName(), chunk.x, chunk.z, blockdata, biome, biomeraw, highesty);
      if (ssr != null) {
        ss = ssr.ss;
        if (!vis) {
          if (hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN) ss = STONE;
          else if (hidestyle == HiddenChunkStyle.FILL_OCEAN) ss = OCEAN;
          else ss = EMPTY;
        }
        int idx = (chunk.x - x_min) + (chunk.z - z_min) * x_dim;
        snaparray[idx] = ss;
        snaptile[idx] = ssr.tileData;

        continue;
      }
      chunks_attempted++;
      boolean wasLoaded = w.isChunkLoaded(chunk.x, chunk.z);
      boolean didload = false;
      boolean isunloadpending = false;
      if (queue != null) {
        isunloadpending = helper.isInUnloadQueue(queue, chunk.x, chunk.z);
      }
      if (isunloadpending) {
          /* Workaround: can't be pending if not loaded */
        wasLoaded = true;
      }
      try {
        if (!wasLoaded) {
          didload = w.loadChunk(chunk.x, chunk.z, false);
        } else {
            /* If already was loaded, no need to load */
          didload = true;
        }
      } catch (Throwable t) {
          /* Catch chunk error from Bukkit */
        Log.warning("Bukkit error loading chunk " + chunk.x + "," + chunk.z + " on " + w.getName());
        if (!wasLoaded) {
            /* If wasn't loaded, we loaded it if it now is */
          didload = w.isChunkLoaded(chunk.x, chunk.z);
        }
      }
      boolean didgenerate = false;
      /* If we didn't load, and we're supposed to generate, do it */
      if ((!didload) && do_generate && vis)
        didgenerate = didload = w.loadChunk(chunk.x, chunk.z, true);
      /* If it did load, make cache of it */
      if (didload) {
        tileData = new DynIntHashMap();

        Chunk c = w.getChunkAt(chunk.x, chunk.z); /* Get the chunk */
        /* Test if chunk isn't populated */
        boolean populated = true;
        // TODO: figure out why this doesn't appear to be reliable in Bukkit
        // if((nmschunk != null) && (doneflag != null)) {
        //    try {
        //        populated = doneflag.getBoolean(nmschunk);
        //    } catch (IllegalArgumentException e) {
        //    } catch (IllegalAccessException e) {
        //    }
        // }
        if (!vis) {
          if (hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN) ss = STONE;
          else if (hidestyle == HiddenChunkStyle.FILL_OCEAN) ss = OCEAN;
          else ss = EMPTY;
        } else if (!populated) {
            /* If not populated, treat as empty */
          ss = EMPTY;
        } else {
          if (blockdata || highesty) {
            ss = c.getChunkSnapshot(highesty, biome, biomeraw);
            if (use_spout) {
              ss = checkSpoutData(c, ss);
            }
            /* Get tile entity data */
            List<Object> vals = new ArrayList<Object>();
            Map tileents = helper.getTileEntitiesForChunk(c);
            for (Object t : tileents.values()) {
              int te_x = helper.getTileEntityX(t);
              int te_y = helper.getTileEntityY(t);
              int te_z = helper.getTileEntityZ(t);
              int cx = te_x & 0xF;
              int cz = te_z & 0xF;
              int blkid = ss.getBlockTypeId(cx, te_y, cz);
              int blkdat = ss.getBlockData(cx, te_y, cz);
              String[] te_fields = HDBlockModels.getTileEntityFieldsNeeded(blkid, blkdat);
              if (te_fields != null) {
                Object nbtcompound = helper.readTileEntityNBT(t);

                vals.clear();
                for (String id : te_fields) {
                  Object val = helper.getFieldValue(nbtcompound, id);
                  if (val != null) {
                    vals.add(id);
                    vals.add(val);
                  }
                }
                if (vals.size() > 0) {
                  Object[] vlist = vals.toArray(new Object[vals.size()]);
                  tileData.put(getIndexInChunk(cx, te_y, cz), vlist);
                }
              }
            }
          } else ss = w.getEmptyChunkSnapshot(chunk.x, chunk.z, biome, biomeraw);
          if (ss != null) {
            ssr = new SnapshotRec();
            ssr.ss = ss;
            ssr.tileData = tileData;
            DynmapPlugin.plugin.sscache.putSnapshot(
                dw.getName(), chunk.x, chunk.z, ssr, blockdata, biome, biomeraw, highesty);
          }
        }
        snaparray[(chunk.x - x_min) + (chunk.z - z_min) * x_dim] = ss;
        snaptile[(chunk.x - x_min) + (chunk.z - z_min) * x_dim] = tileData;

        /* If wasn't loaded before, we need to do unload */
        if (!wasLoaded) {
          chunks_read++;
          /* It looks like bukkit "leaks" entities - they don't get removed from the world-level table
           * when chunks are unloaded but not saved - removing them seems to do the trick */
          if (!(didgenerate && do_save)) {
            helper.removeEntitiesFromChunk(c);
          }
          /* Since we only remember ones we loaded, and we're synchronous, no player has
           * moved, so it must be safe (also prevent chunk leak, which appears to happen
           * because isChunkInUse defined "in use" as being within 256 blocks of a player,
           * while the actual in-use chunk area for a player where the chunks are managed
           * by the MC base server is 21x21 (or about a 160 block radius).
           * Also, if we did generate it, need to save it */
          w.unloadChunk(chunk.x, chunk.z, didgenerate && do_save, false);
        } else if (isunloadpending) {
            /* Else, if loaded and unload is pending */
          w.unloadChunkRequest(chunk.x, chunk.z); /* Request new unload */
        }
      }
      cnt++;
    }
    DynmapCore.setIgnoreChunkLoads(false);

    if (iterator.hasNext() == false) {
        /* If we're done */
      isempty = true;
      /* Fill missing chunks with empty dummy chunk */
      for (int i = 0; i < snaparray.length; i++) {
        if (snaparray[i] == null) snaparray[i] = EMPTY;
        else if (snaparray[i] != EMPTY) isempty = false;
      }
    }
    total_loadtime += System.nanoTime() - t0;

    return cnt;
  }