HashMap problems

Discussion in 'Plugin Development' started by alexisngo1605, Oct 20, 2016.

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

    alexisngo1605

    Hello ! So my actual script doesn't work, I removed some lines of codes and saw that the problem was the HashMap, it sent me errors in the console too, here they are:

    Code:
    private HashMap<Player, Integer> detect;
    
    @EventHandler
        public void SpawnAndDetect_UraniumOre(PlayerMoveEvent e) {
            if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                    && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                return;
    
            Random r = new Random();
            Player p = e.getPlayer();
            Location l = p.getLocation();
    
            int randomBlock = r.nextInt(10) - 2;
            int randomBlock2 = r.nextInt(10) - 2;
            int random = r.nextInt(100000);
    
            if (random > 150 && random < 200 && l.getY() < 20 && l.getY() > 10) {
    
                l.setX(l.getX() + randomBlock);
                l.setZ(l.getZ() + randomBlock2);
                l.setY(l.getY());
    
                if (l.getBlock().getType() != Material.STONE && l.getBlock().getType() != Material.GRAVEL
                        && l.getBlock().getType() != Material.DIRT)
                    return;
    
                l.getBlock().setType(Material.EMERALD_BLOCK);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore as spawned !");
                p.playSound(l, Sound.LEVEL_UP, 100, 0);
    
                Block b = l.getBlock();
                b.setMetadata("uranium", new FixedMetadataValue(this, null));
            } else {
                if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                        && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                    return;
                int startx = (int) l.getX() - 8, starty = (int) l.getY() - 8, startz = (int) l.getZ() - 8;
                int endx = (int) l.getX() + 8, endy = (int) l.getY() + 8, endz = (int) l.getZ() + 8;
                for (int x = startx; x < endx; x++) {
                    for (int y = starty; y < endy; y++) {
                        for (int z = startz; z < endz; z++) {
                            Block block = p.getWorld().getBlockAt(x, y, z);
                            if (block.getType() == Material.EMERALD_BLOCK && block.hasMetadata("uranium")) {
                    detect.put(p, detect.get(p)+1);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore in a radius of 10 blocks:" + ChatColor.RED + detect.get(p) + " ores.");
                detect.remove(p);
                               
    
                            }
    
                        }
                    }
                }
            }
        }
    Errors:
    Code:
    [14:09:57 ERROR]: Could not pass event PlayerMoveEvent to Plugin v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[craftbukkit.jar:git-Bukkit-17a3db7]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-17a3db7]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-17a3db7]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:242) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.PacketPlayInFlying.a(SourceFile:126) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.PacketPlayInPosition.a(SourceFile:57) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-17a3db7]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:609) [craftbukkit.jar:git-Bukkit-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:517) [craftbukkit.jar:git-Bukkit-17a3db7]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
            at Package.Class.onPlayerWalk(Class.java:206) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:295) ~[craftbukkit.jar:git-Bukkit-17a3db7]
            ... 14 more
    Thanks for your help :) !
     
  2. Online

    timtower Administrator Administrator Moderator

    @alexisngo1605 1. Please use UUID's instead of Player objects.
    2. Initialize your hashmap
     
  3. Offline

    alexisngo1605

    Like this ?

    Code:
       private static final Map<UUID, Integer> detect;
      static
      {
      detect = new HashMap<UUID, Integer>();
      }
    
        @EventHandler
        public void SpawnAndDetect_UraniumOre(PlayerMoveEvent e) {
            if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                    && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                return;
    
            Random r = new Random();
            Player p = e.getPlayer();
            Location l = p.getLocation();
    
            int randomBlock = r.nextInt(10) - 2;
            int randomBlock2 = r.nextInt(10) - 2;
            int random = r.nextInt(100000);
    
            if (random > 150 && random < 200 && l.getY() < 20 && l.getY() > 10) {
    
                l.setX(l.getX() + randomBlock);
                l.setZ(l.getZ() + randomBlock2);
                l.setY(l.getY());
    
                if (l.getBlock().getType() != Material.STONE && l.getBlock().getType() != Material.GRAVEL
                        && l.getBlock().getType() != Material.DIRT)
                    return;
    
                l.getBlock().setType(Material.EMERALD_BLOCK);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore as spawned !");
                p.playSound(l, Sound.LEVEL_UP, 100, 0);
    
                Block b = l.getBlock();
                b.setMetadata("uranium", new FixedMetadataValue(this, null));
            } else {
                if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                        && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                    return;
                int startx = (int) l.getX() - 8, starty = (int) l.getY() - 8, startz = (int) l.getZ() - 8;
                int endx = (int) l.getX() + 8, endy = (int) l.getY() + 8, endz = (int) l.getZ() + 8;
                for (int x = startx; x < endx; x++) {
                    for (int y = starty; y < endy; y++) {
                        for (int z = startz; z < endz; z++) {
                            Block block = p.getWorld().getBlockAt(x, y, z);
                            if (block.getType() == Material.EMERALD_BLOCK && block.hasMetadata("uranium")) {
             detect.put(p.getUniqueId(), detect.get(p.getUniqueId())+1);
           p.sendMessage(ChatColor.GREEN + "Radioactive ore in a radius of 10 blocks:" + ChatColor.RED + detect.get(p.getUniqueId()) + " ores.");
           detect.remove(p.getUniqueId());
                              
    
                            }
    
                        }
                    }
                }
            }
        }
     
  4. Online

    timtower Administrator Administrator Moderator

    @alexisngo1605 Remove the static, just add it after the declaration.
     
  5. Offline

    alexisngo1605

    I'm not sure if I did what you meant, I did this:

    Code:
        private static Map<UUID, Integer> detect;
    
        @EventHandler
        public void SpawnAndDetect_UraniumOre(PlayerMoveEvent e) {
            if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                    && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                return;
    
            Random r = new Random();
            Player p = e.getPlayer();
            Location l = p.getLocation();
    
            int randomBlock = r.nextInt(10) - 2;
            int randomBlock2 = r.nextInt(10) - 2;
            int random = r.nextInt(100000);
    
            if (random > 150 && random < 200 && l.getY() < 20 && l.getY() > 10) {
    
                l.setX(l.getX() + randomBlock);
                l.setZ(l.getZ() + randomBlock2);
                l.setY(l.getY());
    
                if (l.getBlock().getType() != Material.STONE && l.getBlock().getType() != Material.GRAVEL
                        && l.getBlock().getType() != Material.DIRT)
                    return;
    
                l.getBlock().setType(Material.EMERALD_BLOCK);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore as spawned !");
                p.playSound(l, Sound.LEVEL_UP, 100, 0);
    
                Block b = l.getBlock();
                b.setMetadata("uranium", new FixedMetadataValue(this, null));
            } else {
                if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                        && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                    return;
                int startx = (int) l.getX() - 8, starty = (int) l.getY() - 8, startz = (int) l.getZ() - 8;
                int endx = (int) l.getX() + 8, endy = (int) l.getY() + 8, endz = (int) l.getZ() + 8;
                for (int x = startx; x < endx; x++) {
                    for (int y = starty; y < endy; y++) {
                        for (int z = startz; z < endz; z++) {
                            Block block = p.getWorld().getBlockAt(x, y, z);
                            if (block.getType() == Material.EMERALD_BLOCK && block.hasMetadata("uranium")) {
                    detect.put(p.getUniqueId(), detect.get(p.getUniqueId())+1);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore in a radius of 10 blocks:" + ChatColor.RED + detect.get(p.getUniqueId()) + " ores.");
                detect.remove(p.getUniqueId());
                 {
                     detect = new HashMap<UUID, Integer>();
                    }
     
  6. Online

    timtower Administrator Administrator Moderator

    @alexisngo1605 Now put the code that you had in the static, put it after detect.
    And remove the static.
     
  7. Offline

    alexisngo1605

    detect is in the 9th line, if it can help you to find it

    Code:
        private Map<UUID, Integer> detect;
    
    @EventHandler
        public void SpawnAndDetect_UraniumOre(PlayerMoveEvent e) {
            if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                    && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                return;
    
            detect = new HashMap<UUID, Integer>(); { //new HashMap
            Random r = new Random();
            Player p = e.getPlayer();
            Location l = p.getLocation();
    
            int randomBlock = r.nextInt(10) - 2;
            int randomBlock2 = r.nextInt(10) - 2;
            int random = r.nextInt(100000);
    
            if (random > 150 && random < 200 && l.getY() < 20 && l.getY() > 10) {
    
                l.setX(l.getX() + randomBlock);
                l.setZ(l.getZ() + randomBlock2);
                l.setY(l.getY());
    
                if (l.getBlock().getType() != Material.STONE && l.getBlock().getType() != Material.GRAVEL
                        && l.getBlock().getType() != Material.DIRT)
                    return;
    
                l.getBlock().setType(Material.EMERALD_BLOCK);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore as spawned !");
                p.playSound(l, Sound.LEVEL_UP, 100, 0);
    
                Block b = l.getBlock();
                b.setMetadata("uranium", new FixedMetadataValue(this, null));
            } else {
                if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
                        && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
                    return;
                int startx = (int) l.getX() - 8, starty = (int) l.getY() - 8, startz = (int) l.getZ() - 8;
                int endx = (int) l.getX() + 8, endy = (int) l.getY() + 8, endz = (int) l.getZ() + 8;
                for (int x = startx; x < endx; x++) {
                    for (int y = starty; y < endy; y++) {
                        for (int z = startz; z < endz; z++) {
                            Block block = p.getWorld().getBlockAt(x, y, z);
                            if (block.getType() == Material.EMERALD_BLOCK && block.hasMetadata("uranium")) {
                    detect.put(p.getUniqueId(), detect.get(p.getUniqueId())+1);
                p.sendMessage(ChatColor.GREEN + "Radioactive ore in a radius of 10 blocks:" + ChatColor.RED + detect.get(p.getUniqueId()) + " ores.");
                detect.remove(p.getUniqueId());
                    }
                            }
    
                        }
                    }
                }
             }
        }
     
  8. Online

    timtower Administrator Administrator Moderator

    @alexisngo1605 Don't do that there, do that on the first line.
    Else it will be empty every single time.
     
  9. Offline

    alexisngo1605

    In the first line here ?
    Code:
        private Map<UUID, Integer> detect; {
        detect = new HashMap<UUID, Integer>(); {
        }
        }
    Or the first line in the event ?
    Code:
       @EventHandler
       public void SpawnAndDetect_UraniumOre(PlayerMoveEvent e) {
        detect = new HashMap<UUID, Integer>(); {
         if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
             && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
           return;
    
         Random r = new Random();
         Player p = e.getPlayer();
         Location l = p.getLocation();
    
         int randomBlock = r.nextInt(10) - 2;
         int randomBlock2 = r.nextInt(10) - 2;
         int random = r.nextInt(100000);
    
         if (random > 150 && random < 200 && l.getY() < 20 && l.getY() > 10) {
    
           l.setX(l.getX() + randomBlock);
           l.setZ(l.getZ() + randomBlock2);
           l.setY(l.getY());
    
           if (l.getBlock().getType() != Material.STONE && l.getBlock().getType() != Material.GRAVEL
               && l.getBlock().getType() != Material.DIRT)
             return;
    
           l.getBlock().setType(Material.EMERALD_BLOCK);
           p.sendMessage(ChatColor.GREEN + "Radioactive ore as spawned");
           p.playSound(l, Sound.LEVEL_UP, 100, 0);
    
           Block b = l.getBlock();
           b.setMetadata("uranium", new FixedMetadataValue(this, null));
         } else {
           if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY()
               && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
             return;
           int startx = (int) l.getX() - 8, starty = (int) l.getY() - 8, startz = (int) l.getZ() - 8;
           int endx = (int) l.getX() + 8, endy = (int) l.getY() + 8, endz = (int) l.getZ() + 8;
           for (int x = startx; x < endx; x++) {
             for (int y = starty; y < endy; y++) {
               for (int z = startz; z < endz; z++) {
                 Block block = p.getWorld().getBlockAt(x, y, z);
                 if (block.getType() == Material.EMERALD_BLOCK && block.hasMetadata("uranium")) {
             detect.put(p.getUniqueId(), detect.get(p.getUniqueId())+1);
           p.sendMessage(ChatColor.GREEN + "Radioactive ore in a radius of 10 blocks:" + ChatColor.RED + detect.get(p.getUniqueId()) + " ores.");
           detect.remove(p.getUniqueId());
             }
                 }
               }
             }
           }
          }
    }
     
  10. Offline

    Lolmewn

    Pro-tip: Learn Java before you make plugins.
    (you do it in the constructor or in the field itself)
     
  11. Online

    timtower Administrator Administrator Moderator

    @alexisngo1605 Ever tried
    Code:
    private Map<UUID, Integer> detect = new HashMap<UUID,Integer>();
    ?
    And outside the events, in the class
     
  12. Offline

    alexisngo1605

    @Lolmewn Oh that's why my codes are messy ! I made smalls plugins for now 2 years and I only learnt Bukkit coding and not Java by looking youtubers. Do you have a book or a website to recommand, so I can learn Java ?

    @timtower I remember did a copy/paste of this method for one of my plugin, I never knew there was a difference between my method and this one, thank you !
     
  13. Offline

    Lolmewn

    @alexisngo1605 Just general java tutorials really, the ones from Oracle are pretty decent.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page