Solved EntityDamageEvent - Player - Mob errors spamming?

Discussion in 'Spigot Plugin Development' started by PurrfectMistake_, Apr 14, 2017.

Thread Status:
Not open for further replies.
  1. Here's my little piece of code, essentially.. i'm trying to disable fly when a player gets damaged, and the jump effect is to prevent fall damage. Either way, I'm getting spammed in console whenever a mob / other entity gets damaged. Any idea why? It looks fine to me.

    Here's the error: https://hastebin.com/ecelolivis.vbs

    Code (Text):
          @EventHandler(priority = EventPriority.HIGH)
          public void DamageFly(EntityDamageEvent event)
          {
              Player p = (Player)event.getEntity();
                if (event.getEntity() instanceof Player){
                      {
                  if(p.isFlying())
                  {
                      p.setAllowFlight(false);
                      p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 120, -1));
                  }else{
                      // do nothing
                  }
                 
                  if(p.getAllowFlight() == true);
                  {
                      p.setAllowFlight(false);
                  }
                  }
                      }else{
                          // do nothing
                }
          }
     
  2. Code (Text):
    @EventHandler(priority = EventPriority.HIGH)
          public void DamageFly(EntityDamageEvent event) {
              Player p = (Player)event.getEntity();
              if (event.getEntity() instanceof Player) {
                 [...]
     
    You have to check if the entity is a player FIRST, then cast it.

    Fixed Code:
    Code (Text):
        @EventHandler(priority = EventPriority.HIGH)
        public void DamageFly(EntityDamageEvent event) {
            if (event.getEntity() instanceof Player) {
                Player p = (Player)event.getEntity();
                if(p.isFlying()) {
                      p.setAllowFlight(false);
                      p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 120, -1));
                  } else {
                      // do nothing
                  }
                  if (p.getAllowFlight() == true) {
                      p.setAllowFlight(false);
                  }
            }
        }
     
     
  3. Hahhahah i knew it was something small that I did wrong.
    Cheers mate :)
     
Thread Status:
Not open for further replies.