Need help about map item

Discussion in 'Plugin Development' started by Jaknog, Aug 15, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Jaknog

    I need to saverendered map of map item.

    how can i export my map?


    Result export with this code
    Code:
    package jn.pubg;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PUBG extends JavaPlugin {
       
        public static boolean save;
        Command command = new Command(this);
        public void onEnable() {
            getServer().getPluginManager().registerEvents(plane, this);
            for(Player p : getServer().getOnlinePlayers()) {
                p.setFlySpeed(1f);
            }
        }
    
    }
    
    Code:
    package jn.pubg;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.world.ChunkLoadEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.map.MapView;
    import org.bukkit.map.MapView.Scale;
    
    
    public class Command implements Listener{
       
        PUBG pubg;
        public Command(PUBG pub) {
            pubg = pub;
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onCommand(PlayerCommandPreprocessEvent e) throws InterruptedException {
            if(e.getMessage().equalsIgnoreCase("/s")) {
                PUBG.save=true;
                e.setCancelled(true);
            }
        } 
    }
    
    
    Code:
    package jn.pubg;
    
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    import org.bukkit.entity.Player;
    import org.bukkit.map.MapCanvas;
    import org.bukkit.map.MapCursor;
    import org.bukkit.map.MapRenderer;
    import org.bukkit.map.MapView;
    import org.bukkit.map.MinecraftFont;
    
    public class Renderer extends MapRenderer {
    
        @SuppressWarnings("deprecation")
        @Override
        public void render(MapView view, MapCanvas canvas, Player p) {
            Double x = Math.ceil((((p.getLocation().getX()+1000) / 2000)*255)-128);
            Double z = Math.ceil((((p.getLocation().getZ()+1000) / 2000)*255)-128);
    
            while(canvas.getCursors().size() > 0)
                canvas.getCursors().removeCursor(canvas.getCursors().getCursor(0));
            MapCursor mc = new MapCursor(x.byteValue(),z.byteValue(),(byte)2,MapCursor.Type.WHITE_CIRCLE.getValue(),true);
            canvas.getCursors().addCursor(mc);
            for(int i=0;i<255;i++)
                for(int j=0;j<255;j++)
                    canvas.setPixel(i, j, canvas.getPixel(i, j));
            String string = "PUBG in MC v2017-08-16";
            int width = MinecraftFont.Font.getWidth(string);
            int height = MinecraftFont.Font.getWidth(string);
            canvas.drawText((128-width)/2, (128-height)/2, MinecraftFont.Font, string);
           
            if(PUBG.save == true) {
               final BufferedImage res = new BufferedImage( 255, 255, BufferedImage.TYPE_INT_RGB );
               for (int xxxxx = 0; xxxxx < res.getWidth(); xxxxx++){
                   for (int yyyyy = 0; yyyyy < res.getHeight(); yyyyy++){
                       res.setRGB(xxxxx, yyyyy, canvas.getPixel(xxxxx, yyyyy));
                   }
               }
               try {
                  RenderedImage ri = res;
                    ImageIO.write(ri, "PNG", new File("map.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
               PUBG.save=false;
            }
        }
    
    }
    
    
    [​IMG]

    [​IMG]
     
  2. Offline

    Zombie_Striker

    @Jaknog
    Why do you want to export the map? If you need it as an image file, just take the buffered image and save it has a new file. If you just want to be able to be able to use the same map instance, instead of creating a new map instance and setting the renderer every time, get one map id and reset it's map renderer every time the server reloads.
     
Thread Status:
Not open for further replies.

Share This Page