简体   繁体   中英

Java HashMap returns 0 even though a value was set

Hopefully this isn't a super stupid question, but I am new to programming and have no idea why my HashMap returns a value of 0 even though I set values to the same key I look up. If it's important, this is code for a Spigot plugin.

What it does is that, while I'm still sneaking, the charge value keeps going up like normal, but upon exiting sneak mode it returns 0.

public class Leaping extends Enchant{

    public Leaping(){
        super("leaping");
    }

    public HashMap<Player, Integer> charge = new HashMap<Player, Integer>();

    @Override
    public void setDefaults(){
        typesAllowed.add("leggings");
        displayName = "&7Leaping {enchantlevel}";
        maxLevel = 20;
        event = "onSneak";
        permission = "eg.enchant.leaping.#";
        crystal = new Crystal(this);
        crystal.displayName = "&3Leaping {enchantlevel}";
        crystal.material = new MaterialData(Material.EMERALD);
    }

    @Override
    public void callEvent(ItemStack item, final Player player, Entity target, double value, Block block){
        if(EnchantManager.hasEnchant(item, this.name)) {
            int level = EnchantManager.getEnchantLevel(item, this);
            if(value == 0.0) {
                new BukkitRunnable() {
                    public void run() {
                        if((!(getCharge(player) >= level)) && player.isSneaking()) {
                            setCharge(player, getCharge(player) + 1);
                            player.sendMessage(Integer.toString(getCharge(player)));
                            player.playSound(player.getLocation(), Sound.BLOCK_NOTE_CHIME, 1, 1);
                        }else {
                            cancel();
                        }
                    }
                }.runTaskTimer(Main.plugin, 20, 20);
            }
            if(value == 1.0) {
                jump(player, getCharge(player));
            }
        }
    }

    private void setCharge(Player p, int value) {
        if(charge.containsKey(p)) charge.remove(p);
        charge.put(p, value);
    }

    private int getCharge(Player p) {
        if(!charge.containsKey(p)) {setCharge(p, 0);}
        return charge.get(p);
    }

    private void jump(Player p, int i) {
        p.sendMessage(Integer.toString(i));
        p.setVelocity(p.getLocation().getDirection().multiply(i));
        p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1, 1);
        new BukkitRunnable() {
            public void run() {
                setCharge(p, 0);
            }
        }.runTaskLater(Main.plugin, 20);
    }   
}

public class SneakEvent implements Listener{

Plugin plugin;

public SneakEvent(Plugin pl) {
    this.plugin = pl;
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@EventHandler
public static void onSneak(PlayerToggleSneakEvent e) {
    Player p = e.getPlayer();

    if(e.isSneaking()) {
        EnchantManager.callEvent(p.getInventory().getLeggings(), "onSneak", p, null, 0.0, p.getLocation().getBlock().getRelative(BlockFace.DOWN));
    }else {
        EnchantManager.callEvent(p.getInventory().getLeggings(), "onSneak", p, null, 1.0, p.getLocation().getBlock().getRelative(BlockFace.DOWN));
    }
}

Any type of help would be appreciated. Thank you in advance for your time.

I cannot understand the situation.

But, I suggest that you add log in the setCharge() to make sure the value is not 0 .

for example

private void setCharge(Player p, int value) {
    System.out.println("key is " + p + "; value is " + value);
    if(charge.containsKey(p)) charge.remove(p);
    charge.put(p, value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM