Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1.     @Override
  2.     public void run() {
  3.         for (World curWorld : plugin.getServer().getWorlds()) {
  4.             Boolean renderDay = false;
  5.             Boolean renderNight = false;
  6.             if (curWorld.getEnvironment() == Environment.NORMAL) {
  7.                 if ((curWorld.getTime() > 15000) && (curWorld.getTime() < 20000)) {
  8.                     renderNight = true;
  9.                 }
  10.                 if ((curWorld.getTime() > 2000) && (curWorld.getTime() < 6000)) {
  11.                     renderDay = true;
  12.                 }
  13.             }
  14.             if (curWorld.getEnvironment() == Environment.NETHER) {
  15.                 renderDay = true;
  16.             }
  17.             if ((renderNight || renderDay) && (plugin.lastMapRendering(curWorld.getName(), renderDay) > LiveMap.settingMinutesRedraw)) {
  18.                 // It's a good time to render the map ^^
  19.                 plugin.getServer().broadcastMessage(ChatColor.BLUE+"Rendering server map: "+curWorld.getName()+" ...");
  20.                 System.out.println("Rendering server map: "+curWorld.getName()+" ...");
  21.                 // Surface map, not for nether
  22.                 if (curWorld.getEnvironment() == Environment.NORMAL) {
  23.                     renderWorld(curWorld, renderDay, false);
  24.                     plugin.getServer().broadcastMessage(ChatColor.BLUE+"Surface map done: "+curWorld.getName());
  25.                     System.out.println("Surface map done: "+curWorld.getName());
  26.                 }
  27.                 if (renderDay) {
  28.                     // Cave map, only at day
  29. /* <-- This is line 59 */       renderWorld(curWorld, renderDay, true);
  30.                     plugin.getServer().broadcastMessage(ChatColor.BLUE+"Cave map done: "+curWorld.getName());
  31.                     System.out.println("Cave map done: "+curWorld.getName());
  32.                 }
  33.                 // Force garbage collector to run for freeing the memory as fast as possible
  34.                 System.gc();
  35.                 // Update render time for this world and allow next rendering thread to start
  36.                 plugin.onMapRender(curWorld.getName(), renderDay);
  37.             }
  38.         }
  39.         plugin.onMapRenderingDone();
  40.     }
  41. /* <-- Answer to the ultimate question of life, the universe, and everything */
  42.     private void renderWorld(World targetWorld, Boolean isDay, Boolean isCave) {
  43.         File mapFile = new File("map/"+targetWorld.getName()+(isDay ? "_day" : "_night")+(isCave ? "_cave" : "")+".png");
  44.         BufferedImage mapImage;
  45.         // Load normal map if previous version is available
  46.         if (mapFile.exists()) {
  47.             try {
  48.                 mapImage = ImageIO.read(mapFile);
  49.             } catch (IOException e) {
  50.                 mapImage = new BufferedImage(this.sizeX, this.sizeZ, BufferedImage.TYPE_INT_RGB);
  51.             }
  52.         } else {
  53.             mapImage = new BufferedImage(this.sizeX, this.sizeZ, BufferedImage.TYPE_INT_RGB);
  54.         }
  55.         Integer offsetX = this.sizeX / 2;
  56.         Integer offsetZ = this.sizeZ / 2;
  57.         Boolean isUnderground;
  58.         Block topBlock;
  59.         Material topBlockType;
  60.         byte brightness;
  61.         int iY, color, colorCave;
  62.         // Render map area
  63.         for (int iX = 0; iX < this.sizeX; iX++) {
  64.             for (int iZ = 0; iZ < this.sizeZ; iZ++) {
  65.                 int iWorldX = iX-offsetX;
  66.                 int iWorldZ = iZ-offsetZ;
  67.                 // Check if the chunk of that area is loaded
  68.                 if (targetWorld.isChunkLoaded(iWorldX >> 4, iWorldZ >> 4)) {
  69.                     // Start above (just to be sure) or below the highest block
  70.                     iY = targetWorld.getHighestBlockYAt(iWorldX, iWorldZ) + (isCave ? -1 : 1);
  71.                     topBlockType = targetWorld.getBlockAt(iWorldX, iY, iWorldZ).getType();
  72.                     /* Critical section: without this code it just works fine */
  73.                     if (isCave && (targetWorld.getEnvironment() == Environment.NORMAL)) {
  74.                         while ((iY >= 4) && ((topBlockType == Material.LEAVES) || (topBlockType == Material.GLASS) ||
  75.                                 (topBlockType == Material.AIR) || (topBlockType == Material.LOG))) {
  76.                             topBlockType = targetWorld.getBlockAt(iWorldX, --iY, iWorldZ).getType();
  77.                             System.out.println(iY);
  78.                         }
  79.                     }
  80.                     System.out.println("Done");
  81.                     /* End of critical section */
  82.                     topBlock = targetWorld.getBlockAt(iWorldX, iY, iWorldZ);
  83.                     isUnderground = isCave;
  84.                     // Move downward until a block with a known color was found
  85.                     while (iY >= 4) {
  86.                         if ((topBlockType == Material.AIR) && isUnderground) {
  87.                             // Left solid ground
  88.                             isUnderground = false;
  89.                         }
  90.                         if ((topBlockType != Material.AIR) && !isUnderground) {
  91.                             // Hit a block with air (or unknown blocks) above
  92.                             brightness = (byte)(topBlock.getLightLevel() * 2);
  93.                             color = getBlockColor(topBlock, brightness);
  94.                             colorCave = getBlockColor(topBlock, (byte) (32 + brightness / 2));
  95.                             if (color >= 0) {
  96.                                 // Block color known, draw it
  97.                                 mapImage.setRGB(iZ, (this.sizeX - iX - 1), (isCave ? colorCave : color));
  98.                                 break;
  99.                             } else {
  100.                                 System.out.println("Unknown block: "+topBlockType.toString());
  101.                             }
  102.                         }
  103. /* <-- This is line 131 */          topBlock = targetWorld.getBlockAt(iWorldX, --iY, iWorldZ);
  104.                         topBlockType = topBlock.getType();
  105.                     }
  106.                 }
  107.             }
  108.             if ((iX % 64) == 0) {
  109.                 try {
  110.                     // Short thread pause every 64 blocks to prevent the server from lagging or even timing out
  111.                     LMMapRenderer.sleep(100);
  112.                 } catch (InterruptedException e) {
  113.                     // Forced to resume, e.g. when server is shutting down
  114.                 }
  115.             }
  116.             if ((iX % 1025) == 0) {
  117.                 // Status message at 25, 50 and 75 percent
  118.                 Integer percent = (int)(iX * 100 / this.sizeX);
  119.                 System.out.println("Generating map... "+percent+"%");
  120.             }
  121.         }
  122.         try {
  123.             // Set compression quality to high
  124.             ImageWriteParam iwparam = new JPEGImageWriteParam( Locale.getDefault() );
  125.             iwparam.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ) ;
  126.             iwparam.setCompressionQuality(1);
  127.             // Write day map
  128.             ImageWriter writerDay = ImageIO.getImageWritersByFormatName("png").next();
  129.             ImageOutputStream iosDay = ImageIO.createImageOutputStream(mapFile);
  130.             writerDay.setOutput( iosDay );
  131.             writerDay.write( null, new IIOImage(mapImage, null, null), iwparam );
  132.             ImageIO.write(mapImage, "png", mapFile);
  133.             // Clean up the resources
  134.             iosDay.flush();
  135.             writerDay.dispose();
  136.             iosDay.close();
  137.             mapImage.flush();
  138.         } catch (IOException e) {
  139.             // TODO: Handle IO errors
  140.             e.printStackTrace();
  141.         }
  142.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement