Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void run() {
- for (World curWorld : plugin.getServer().getWorlds()) {
- Boolean renderDay = false;
- Boolean renderNight = false;
- if (curWorld.getEnvironment() == Environment.NORMAL) {
- if ((curWorld.getTime() > 15000) && (curWorld.getTime() < 20000)) {
- renderNight = true;
- }
- if ((curWorld.getTime() > 2000) && (curWorld.getTime() < 6000)) {
- renderDay = true;
- }
- }
- if (curWorld.getEnvironment() == Environment.NETHER) {
- renderDay = true;
- }
- if ((renderNight || renderDay) && (plugin.lastMapRendering(curWorld.getName(), renderDay) > LiveMap.settingMinutesRedraw)) {
- // It's a good time to render the map ^^
- plugin.getServer().broadcastMessage(ChatColor.BLUE+"Rendering server map: "+curWorld.getName()+" ...");
- System.out.println("Rendering server map: "+curWorld.getName()+" ...");
- // Surface map, not for nether
- if (curWorld.getEnvironment() == Environment.NORMAL) {
- renderWorld(curWorld, renderDay, false);
- plugin.getServer().broadcastMessage(ChatColor.BLUE+"Surface map done: "+curWorld.getName());
- System.out.println("Surface map done: "+curWorld.getName());
- }
- if (renderDay) {
- // Cave map, only at day
- /* <-- This is line 59 */ renderWorld(curWorld, renderDay, true);
- plugin.getServer().broadcastMessage(ChatColor.BLUE+"Cave map done: "+curWorld.getName());
- System.out.println("Cave map done: "+curWorld.getName());
- }
- // Force garbage collector to run for freeing the memory as fast as possible
- System.gc();
- // Update render time for this world and allow next rendering thread to start
- plugin.onMapRender(curWorld.getName(), renderDay);
- }
- }
- plugin.onMapRenderingDone();
- }
- /* <-- Answer to the ultimate question of life, the universe, and everything */
- private void renderWorld(World targetWorld, Boolean isDay, Boolean isCave) {
- File mapFile = new File("map/"+targetWorld.getName()+(isDay ? "_day" : "_night")+(isCave ? "_cave" : "")+".png");
- BufferedImage mapImage;
- // Load normal map if previous version is available
- if (mapFile.exists()) {
- try {
- mapImage = ImageIO.read(mapFile);
- } catch (IOException e) {
- mapImage = new BufferedImage(this.sizeX, this.sizeZ, BufferedImage.TYPE_INT_RGB);
- }
- } else {
- mapImage = new BufferedImage(this.sizeX, this.sizeZ, BufferedImage.TYPE_INT_RGB);
- }
- Integer offsetX = this.sizeX / 2;
- Integer offsetZ = this.sizeZ / 2;
- Boolean isUnderground;
- Block topBlock;
- Material topBlockType;
- byte brightness;
- int iY, color, colorCave;
- // Render map area
- for (int iX = 0; iX < this.sizeX; iX++) {
- for (int iZ = 0; iZ < this.sizeZ; iZ++) {
- int iWorldX = iX-offsetX;
- int iWorldZ = iZ-offsetZ;
- // Check if the chunk of that area is loaded
- if (targetWorld.isChunkLoaded(iWorldX >> 4, iWorldZ >> 4)) {
- // Start above (just to be sure) or below the highest block
- iY = targetWorld.getHighestBlockYAt(iWorldX, iWorldZ) + (isCave ? -1 : 1);
- topBlockType = targetWorld.getBlockAt(iWorldX, iY, iWorldZ).getType();
- /* Critical section: without this code it just works fine */
- if (isCave && (targetWorld.getEnvironment() == Environment.NORMAL)) {
- while ((iY >= 4) && ((topBlockType == Material.LEAVES) || (topBlockType == Material.GLASS) ||
- (topBlockType == Material.AIR) || (topBlockType == Material.LOG))) {
- topBlockType = targetWorld.getBlockAt(iWorldX, --iY, iWorldZ).getType();
- System.out.println(iY);
- }
- }
- System.out.println("Done");
- /* End of critical section */
- topBlock = targetWorld.getBlockAt(iWorldX, iY, iWorldZ);
- isUnderground = isCave;
- // Move downward until a block with a known color was found
- while (iY >= 4) {
- if ((topBlockType == Material.AIR) && isUnderground) {
- // Left solid ground
- isUnderground = false;
- }
- if ((topBlockType != Material.AIR) && !isUnderground) {
- // Hit a block with air (or unknown blocks) above
- brightness = (byte)(topBlock.getLightLevel() * 2);
- color = getBlockColor(topBlock, brightness);
- colorCave = getBlockColor(topBlock, (byte) (32 + brightness / 2));
- if (color >= 0) {
- // Block color known, draw it
- mapImage.setRGB(iZ, (this.sizeX - iX - 1), (isCave ? colorCave : color));
- break;
- } else {
- System.out.println("Unknown block: "+topBlockType.toString());
- }
- }
- /* <-- This is line 131 */ topBlock = targetWorld.getBlockAt(iWorldX, --iY, iWorldZ);
- topBlockType = topBlock.getType();
- }
- }
- }
- if ((iX % 64) == 0) {
- try {
- // Short thread pause every 64 blocks to prevent the server from lagging or even timing out
- LMMapRenderer.sleep(100);
- } catch (InterruptedException e) {
- // Forced to resume, e.g. when server is shutting down
- }
- }
- if ((iX % 1025) == 0) {
- // Status message at 25, 50 and 75 percent
- Integer percent = (int)(iX * 100 / this.sizeX);
- System.out.println("Generating map... "+percent+"%");
- }
- }
- try {
- // Set compression quality to high
- ImageWriteParam iwparam = new JPEGImageWriteParam( Locale.getDefault() );
- iwparam.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ) ;
- iwparam.setCompressionQuality(1);
- // Write day map
- ImageWriter writerDay = ImageIO.getImageWritersByFormatName("png").next();
- ImageOutputStream iosDay = ImageIO.createImageOutputStream(mapFile);
- writerDay.setOutput( iosDay );
- writerDay.write( null, new IIOImage(mapImage, null, null), iwparam );
- ImageIO.write(mapImage, "png", mapFile);
- // Clean up the resources
- iosDay.flush();
- writerDay.dispose();
- iosDay.close();
- mapImage.flush();
- } catch (IOException e) {
- // TODO: Handle IO errors
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement