HashMap always returns 2.

Discussion in 'Spigot Plugin Development' started by Bladian, Jun 19, 2015.

  1. I currently have this code:

    Code (Text):

        @EventHandler
        public void onBreak(BlockBreakEvent e) {

            Player p = e.getPlayer();
            Block block = e.getBlock();

            if(block.getType() == Material.GOLD_ORE) {

                for (Player specs : interactions) {
                    if (gold.get(p) == null) {
                        gold.put(p, 1);
                        specs.sendMessage(pi + "§e" + p.getName() + " §9§l| §eGOLD ORE" + " §9§l| §e1");
                    } else {
                        int get = gold.get(p);
                        int times = get+1;
                        gold.put(specs, times);
                        specs.sendMessage(pi + "§e" + p.getName() + " §9§l| §eGOLD ORE" + " §9§l| §e" + times);
                    }
                }
            }
            else if (block.getType() == Material.DIAMOND_ORE) {

                for (Player specs : interactions) {
                    if (diamond.get(p) == null) {
                        diamond.put(p, 1);
                        specs.sendMessage(pi + "§e" + p.getName() + " §9§l| §eDIAMOND ORE" + " §9§l| §e1");
                    } else {
                        int times = diamond.get(p) + 1;
                        diamond.put(specs, times);
                        specs.sendMessage(pi + "§e" + p.getName() + " §9§l| §eDIAMOND ORE" + " §9§l| §e" + times);
                    }
                }
            }
        }
    }
    It works as expected except the hashmap always returns 2 after the second time. No matter what I do.

    Any help?
     
  2. Because you dumped p in "gold" as 1, then you're triggering the else block every time and adding one to that. Your code is extremely difficult to reason about, I suggest renaming some of those variables to something more meaningful.
     
  3. Just noticed how badly set up my code is, I'll change it now.