Map issues

Discussion in 'Plugin Development' started by deathknife, Oct 12, 2013.

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

    deathknife

    I'm messing a bit with maps and I have came to an issue.

    I'm trying to make, so everytime you change your held item it will update map.
    However, I get error in console. As well as that, only one map will update, the first map in taskbar will update while others not. for example, if my taskbar is empty and I get a map to slot 2(taskbar) it will update, if i get another map to slot 1, the map on slot 1 will update but the one on slot 2 won't anymore.

    stack-trace: http://prntscr.com/1wvq6a
    Line 143 in Cookies Class:
    Code:java
    1. public static void UpdateMap(Player player)
    2. {
    3. Map.setRedrawNeed();
    4. player.sendMessage("Before IF");
    5. if (player.getItemInHand().getType().equals(Material.MAP)) {
    6. player.sendMessage("Map");
    7. //Line 143 below:
    8. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Test")) {
    9. player.sendMessage("Pass the IF");
    10. MapView map = Bukkit.getServer().getMap(player.getItemInHand().getDurability());
    11. for (MapRenderer r:map.getRenderers()){
    12. map.removeRenderer(r);
    13. }
    14. map.addRenderer(new Map());
    15. return;
    16.  
    17. }else {
    18. player.sendMessage("Fail If");
    19. }
    20. return;
    21. }
    22. return;
    23. }

    If I remove line 143, it doens't give me any error. But with line 143, it won't even execute the else.

    My Listener class/Event that calls on player item held change.
    Code:java
    1. @EventHandler
    2. public void onItemHeldChange(PlayerItemHeldEvent event)
    3. {
    4. if(event.getPlayer() == null)
    5. {
    6. return;
    7. }
    8. Cookies.UpdateMap(event.getPlayer());
    9. return;
    10. }
    11.  
    12. }
    13.  


    My Map class:
    Code:java
    1. public class Map extends MapRenderer{
    2. static boolean redrawNeeded = false;
    3.  
    4. public static void setRedrawNeed()
    5. {
    6. redrawNeeded = true;
    7. }
    8.  
    9. @Override
    10. public void render(MapView map, MapCanvas canvas, Player player) {
    11. if(!redrawNeeded)
    12. return;
    13.  
    14. setMap(map, canvas, player);
    15. }
    16.  
    17. @SuppressWarnings("deprecation")
    18. public static void setMap(MapView map, MapCanvas canvas, Player player) {
    19. for(int x = 0; x < 128; x++) {
    20. for(int y = 0; y < 128; y++) {
    21. canvas.setPixel(x, y, MapPalette.matchColor(255, 132, 255));
    22. }
    23. }
    24.  
    25.  
    26. Damageable dplayer = player;
    27. String hp = "Health: " + (int) dplayer.getHealth();
    28. String name = "Name: " + player.getName();
    29.  
    30. canvas.drawText(3, 5, MinecraftFont.Font, "Profile");
    31. canvas.drawText(3, 25, MinecraftFont.Font, name);
    32. canvas.drawText(3, 35, MinecraftFont.Font, hp);
    33.  
    34. redrawNeeded = false;
    35. player.sendMap(map);
    36. }
    37. }
    38.  



    So the 2 issues im having is;
    When updating map it throws error in console & only one map in my inventory will update, the one that's in the 'closest' slot.
     
  2. Offline

    deathknife

  3. Offline

    MiniDigger

    This is because you get a NPE in that line. That means that player, getItemInHand(), getItemMeta() or getDisplayName() is null.

    I think that the item has no item meta or the item meta has no display name. You should check that.
     
    deathknife likes this.
  4. Offline

    deathknife

    MiniDigger
    I have added
    player.getItemInHand().getItemMeta().getDisplayName() != null
    as if statement and it doesn't throw me that error anymore.
    I still need help for second issue, Only one map will update, and it doesnt seem to check if it's displayname is 'Test' but updates it no matter what.
     
  5. Offline

    MiniDigger

    deathknife can you please show me the updated version of your UpdateMap()?
     
  6. Offline

    deathknife

    MiniDigger
    Code:java
    1. public static void UpdateMap(Player player)
    2. {
    3. Map.setRedrawNeed();
    4. //player.sendMessage("Before IF");
    5. if (player.getItemInHand().getType().equals(Material.MAP) && player.getItemInHand().getItemMeta().getDisplayName() != null) {
    6. //player.sendMessage("Map");
    7. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Test")) {
    8. //player.sendMessage("Pass the IF");
    9. MapView map = Bukkit.getServer().getMap(player.getItemInHand().getDurability());
    10. for (MapRenderer r:map.getRenderers()){
    11. map.removeRenderer(r);
    12. }
    13. map.addRenderer(new Map());
    14. return;
    15.  
    16. }else {
    17. //player.sendMessage("Fail If");
    18. }
    19. return;
    20. }
    21. return;
    22. }

    Sorry, forgot. Here it is.
     
  7. Offline

    MiniDigger

    deathknife I think it only changes the map, the player is holding in is hand. You always just check this.
     
  8. Offline

    deathknife

    MiniDigger Yes, but let's say player has 2 maps. One is on slot 1 and one is on slot 2. The UpdateMap is activated by command. The player is holding map 1, types the command and it updates. The player is holding map two, types the command and it won't update. If the player swaps map 1 with map 2 (map 1 == slot 2, map 2 == slot 1), then only map 2(slot 1) will render on command, while map 1(slot2) won't.
     
  9. Offline

    MiniDigger

    deathknife well that's interesting :D. Does UpdateMap() gets called for map in the second slot? If so, where does it stop executing?
     
  10. Offline

    deathknife

    MiniDigger
    Code:java
    1. Bukkit.broadcastMessage("test" + map);
    2.  
    3. if(!redrawNeeded)
    4. return;


    I have this inside Map class in render, the redrawNeeded is for map to render instantly. It also broadcastes all maps I have in inventory, not the one in hand.
    I have came up with one theory;
    On command, it sets redrawNeeded to true. Then it proceeds to render all maps, but it only renders the first one as when one map gets render it sets redrawNeeded to false (Code in OP). And it needs redrawNeeded to true to do anything. Something like this.

    I also forgot to mention, that the other maps are blank.
     
Thread Status:
Not open for further replies.

Share This Page