Could not pass event PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by Jartsu, Jun 9, 2016.

  1. Hey,

    So I have an code and that's error: Could not pass event PlayerInteractEvent, problem is here:
    Code (Text):
    ammo.put(e.getPlayer(), ammo.get(e.getPlayer()) - 1);
    Here's the full part:
    Code (Text):

    public void onPlayerInteract(PlayerInteractEvent e) throws InterruptedException{
        if(e.getAction() == Action.RIGHT_CLICK_AIR){
            if(e.getItem().getType() == Material.IRON_SPADE){
                Snowball s = e.getPlayer().launchProjectile(Snowball.class);
                ammo.put(e.getPlayer(), ammo.get(e.getPlayer()) - 1);
                e.getPlayer().sendMessage(String.valueOf(ammo.get(e.getPlayer())));
            }
        }
    }
     
     
  2. You need to check if your hashmap contains the object before trying to get it.

    Code (Text):

    ammo.put(e.getPlayer(), ammo.get(e.getPlayer()) - 1);
     
    If ammo doesn't contain your player object then it returns null, which I believe causes the error. Make sure to check if it doesn't contain it then add it to the hashmap.
     
  3. Oh.. Thank You! I forgot that, I added that code into the OnJoin, if player join it adds him there :/.