Solved HashMap not working?

Discussion in 'Spigot Plugin Development' started by Waldxn, May 21, 2017.

  1. First off, I'm sure I've made a simple mistake, but I can't find it.

    Here is the code:
    Code (Text):
    package com.waldxn.needs.regeneration;

    import com.waldxn.needs.Needs;
    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.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;

    import java.util.HashMap;

    public class Trees implements Listener {

        public Trees(Needs plugin) {
            Needs plugin1 = plugin;
        }

        @EventHandler
        public void woodBreak(BlockBreakEvent event) {

            Player player = event.getPlayer();
            Material block = event.getBlock().getType();
            HashMap<Location, Integer> durability = new HashMap<>();

            if (block == Material.LEAVES || block == Material.LEAVES_2) {
                event.setCancelled(true);
                player.sendMessage("You can only break the wood!");
            }
            if (block == Material.LOG || block == Material.LOG_2) {
                Location loc = event.getBlock().getLocation();
                if (!(durability.containsKey(loc))) {
                    durability.put(loc, 120);
                    player.sendMessage("Tree durability is " + durability.get(loc));
                    event.setCancelled(true);
                    if (durability.containsKey(loc)) {
                        System.out.println("Does contain");
                    }
                } else if (durability.get(loc) == 0) {
                    if (block == Material.LOG) {
                        ItemStack item = new ItemStack(Material.LOG, 64);
                        player.getInventory().addItem(item);
                        player.sendMessage("You have received 64 logs!");
                    } else {
                        ItemStack item = new ItemStack(Material.LOG_2, 64);
                        player.getInventory().addItem(item);
                        player.sendMessage("You have received 64 logs!");
                    }
                } else if (durability.containsKey(loc)) {
                    int dur = durability.get(loc);
                    dur -= 5;
                    durability.replace(loc, dur);
                    player.sendMessage("Tree durability is yuh " + durability.get(loc));
                    event.setCancelled(true);
                }
            }
        }
    }
     
    So it's basically just not realizing that the hashmap DOES contain the key. I tested it here:

    Code (Text):
    if (durability.containsKey(loc)) {
                        System.out.println("Does contain");
                    }
    It is printing to the console that it does contain it. But when breaking the block again it isn't registering it as being in the hashmap.

    Anyone see where I messed up?
     
  2. Put your Map outside of the event constructor. Otherwise it would still generate a new map again and again.
     
    • Informative Informative x 1
  3. Thanks so much! Knew it was something simple!