Image in a Map(Item)

Discussion in 'Plugin Development' started by Adriano3ds, Jul 4, 2013.

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

    Adriano3ds

    Hello :D

    How can I do this:

    Show Spoiler

    [​IMG]


    Have any library?

    Thanks
     
  2. Offline

    reesylou

    It is not a mod/plugin.... take a look at this:
     
  3. Offline

    Minnymin3

    Theres a library for this I think its called the MapAPI or something like that.

    Did some research and the Bukkit api supports this now. Take a look at this: http://wombat.platymuus.com/minecraft/mapapi/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Offline

    Compressions

    Adriano3ds MapInitializeEvent.
    Code:
    MapView map = event.getMap();
     
    for(MapRenderer r : map.getRenderers()) {
    map.removeRenderer(r);
    }
     
    map.addRenderer(new MapRenderer() {
    public void render(MapView view, MapCanvas canvas, Player player) {
    canvas.drawImage(10, 10, new ImageIcon(getDataFolder().getPath() + "/yourImage.png").getimage());
    }
    });
    JavaDocs
     
  5. Offline

    chasechocolate

    Yes, what Compressions said, or you can read it from a URL (such as Imgur) by using ImageIO.read(new URL("some.url")).
     
  6. Offline

    Adriano3ds



    Hum..
    Nice :D


    I don't understand.. can you help me? I do:
    Show Spoiler

    Code:
    @EventHandler
    public void onMapInitialize(MapInitializeEvent event){
    MapView map = event.getMap();
     
    for(MapRenderer r : map.getRenderers()) {
    map.removeRenderer(r);
    }
     
    map.addRenderer(new MapRenderer() {
    public void render(MapView view, MapCanvas canvas, Player player) {
    canvas.drawImage(10, 10, new ImageIcon(getDataFolder().getPath() + "/yourImage.png").getimage());
    }
    

    but don't work
     
  7. Offline

    Minnymin3

    Adriano3ds
    What exactly doesn't work? I think that its the image icon thats the problem.
     
  8. Offline

    chasechocolate

    Adriano3ds here's a nice tutorial from MylesIsCool (I do believe this is the second time I have mentioned this video :p)
     
    MylesIsCool likes this.
  9. Offline

    Compressions

    Adriano3ds What doesn't work is that you didn't properly place the image and properly name the path... Ignorance...
     
  10. Offline

    Adriano3ds


    the path is: /plugins/myplugin?


    The map generates the world "image"..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  11. Offline

    DarkBladee12

    Adriano3ds You have to remove the normal renderers and then add yours like this:

    Code:java
    1. for(MapRenderer mr : MAPVIEW.getRenderers()){
    2. MAPVIEW.removeRenderer(mr);
    3. }
    4. MAPVIEW.addRenderer(new YOURRENDERER(...));


    Example renderer:

    Code:java
    1. import java.awt.image.BufferedImage;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.map.MapCanvas;
    5. import org.bukkit.map.MapRenderer;
    6. import org.bukkit.map.MapView;
    7.  
    8. public class ExampleRenderer extends MapRenderer {
    9. private BufferedImage image;
    10. private boolean rendered;
    11.  
    12. public ExampleRenderer(BufferedImage image) {
    13. this.image = image;
    14. }
    15.  
    16. public void render(MapView mv, MapCanvas mc, Player p) {
    17. if (rendered || image == null) {
    18. return;
    19. }
    20. mc.drawImage(0, 0, image);
    21. rendered = true;
    22. }
    23.  
    24. public void setImage(BufferedImage image) {
    25. this.image = image;
    26. setRendered(false);
    27. }
    28.  
    29. public BufferedImage getImage() {
    30. return this.image;
    31. }
    32.  
    33. public void setRendered(boolean rendered) {
    34. this.rendered = rendered;
    35. }
    36.  
    37. public boolean hasRendered() {
    38. return this.rendered;
    39. }
    40. }
     
  12. Offline

    Adriano3ds


    How I set the image?? I don't know much java
     
  13. Offline

    DarkBladee12

    Adriano3ds You'll have to read it eather from a file or from the resource:

    Code:java
    1. BufferedImage i = ImageIO.read(new File(PATH));
    2. //
    3. BufferedImage i = ImageIO.read(getClass().getResourceAsStream(FILENAME)); // Image has to be in the same package as the class
     
  14. Offline

    desht

    That will kill your server's performance. It's loading the image from file every tick for every player who's holding the map with that renderer attached.

    Right way to do it (@DarkBladee12 's code is good):
    • Load the image once, at plugin startup (or perhaps via some kind of reload command)
    • Only call canvas.drawImage() once - the image will stay on the map. canvas.drawImage() doesn't need to be called again unless the image actually changes (e.g. via aforementioned reload command)
    • Using ImageIcon is also overkill; the BufferedImage class has all the necessary functionality
     
Thread Status:
Not open for further replies.

Share This Page