即使设置了值,Java HashMap也会返回0

时间:2018-04-03 01:23:36

标签: java minecraft

希望这不是一个超级愚蠢的问题,但我是编程的新手,并且不知道为什么我的HashMap返回值0,即使我将值设置为我查找的相同键。如果这很重要,那就是Spigot插件的代码。

它的作用是,虽然我还在偷偷摸摸,但是充电值会像往常一样上升,但是在退出潜行模式时,它会返回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));
    }
}

任何类型的帮助将不胜感激。提前感谢您的时间。

1 个答案:

答案 0 :(得分:-2)

我无法理解这种情况。

但是,我建议您在setCharge()中添加日志,以确保该值不是0

例如

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);
}