Solved Event exception entity damage.

Discussion in 'Spigot Plugin Development' started by dentoffe, Jun 15, 2016.

  1. Hi,
    I made an event that would stop players fly when they start PvPing.
    But it spams the console with this error when I enable mobs (because they are also entity's)
    So I added this:
    Code (Text):
    if ((!(e.getDamager() instanceof Arrow)) && (!(e.getDamager() instanceof Player))) {
                  return;
                }
            if (!(e.getEntity() instanceof Player)) {
                return;
            }
    But that did not work (full code of the even in spoiler code)

    Code (Text):
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e) {
            if (!Main.plugin.getConfig().getBoolean("StopflyOnPvP")) {
                return;
            }
            if ((!(e.getDamager() instanceof Arrow)) && (!(e.getDamager() instanceof Player))) {
                  return;
                }
            if (!(e.getEntity() instanceof Player)) {
                return;
            }
            Player p = null;
            try {
                p = (Player) e.getEntity();
            }
            catch (NullPointerException en) {
                en.printStackTrace();
             
            }
            if (p != null) {
                Main.FlyingPlayers.remove(p.getName());
                p.setFlying(false);
                p.setAllowFlight(false);
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("Messages.On-PvP")));
                Main.Fall.add(p.getName());
            }
            Player plr = null;
            try {
                plr = (Player) e.getDamager();
            }
            catch (NullPointerException en) {
                en.printStackTrace();
             
            }
            if (plr != null) {
                Main.FlyingPlayers.remove(plr.getName());
                plr.setFlying(false);
                plr.setAllowFlight(false);
                plr.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("Messages.On-PvP")));
                Main.Fall.add(plr.getName());
            }
        }
    Code (Text):
    [15:24:14] [Server thread/ERROR]: Could not pass event EntityDamageEvent to xpfly v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:89) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:572) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:558) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:604) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.EntityLiving.damageEntity0(EntityLiving.java:1238) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.EntityLiving.damageEntity(EntityLiving.java:834) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.EntityLiving.U(EntityLiving.java:191) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.EntityInsentient.U(EntityInsentient.java:180) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.Entity.m(Entity.java:303) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.EntityLiving.m(EntityLiving.java:1744) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.EntityInsentient.m(EntityInsentient.java:240) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.World.entityJoinedWorld(World.java:1621) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.World.g(World.java:1596) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.World.tickEntities(World.java:1430) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.WorldServer.tickEntities(WorldServer.java:632) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:804) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_74]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.entity.CraftVillager cannot be cast to org.bukkit.entity.Player
    at me.universe.xpfly.events.onPlayerFall(events.java:65) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_74]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_74]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_74]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_74]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.10.jar:git-Spigot-3ccadba-fb568fd]
    ... 23 more
     
     
  2. (!(e.getDamager() instanceof Arrow)) && (!(e.getDamager() instanceof Player)) will always return false. The same value cannot possibly be an instanceof Arrow and Player at the same time. However, your bug isn't even in that method. Can you show us onPlayerFall and point line 65?

    Also, don't use try/catch for NPEs, just use if(object != null). And class names start by a Capital letter.
     
    • Like Like x 1
    • Useful Useful x 1
  3. Could you post which line is line 65 in your code.
     
  4. Code (Text):
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            if (Main.plugin.getConfig().getBoolean("Disable-fly-on-join") == true) {
                Player p = (Player) e.getPlayer();
                p.setFlying(false);
                p.setAllowFlight(false);
                Main.Fall.add(p.getName());
                p.sendMessage(ChatColor.translateAlternateColorCodes('&',Main.plugin.getConfig().getString("Messages.Fly-on-join")));
                try {
                   
               
                Main.FlyingPlayers.remove(p.getName());
                }
                catch (NullPointerException en)
                {
                    en.printStackTrace();
                }
            }
           
        }
        @EventHandler
        public void onPlayerFall(EntityDamageEvent e) {
            Player p = (Player) e.getEntity();
            if (e.getCause() == DamageCause.FALL){
                if (Main.Fall.contains(p.getName())) {
                    p.setFallDistance(0);
                    Main.Fall.remove(p.getName());
                    e.setCancelled(true);
                    return;
                }
            } else {
                return;
            }
     
  5. public void onPlayerFall(EntityDamageEvent e) {
    Player p = (Player) e.getEntity();

    You blindly cast the entity to a player. That doesn't work, as EntityDamageEvent will be called for every type of entity taking damage. For exemple, if a villager takes damage, you're trying to cast that villager to a Player, that doesn't make sense. Instead, try using if (e.getEntity() instanceof Player) before everything.
     
  6. MiniDigger

    Supporter

    this is a redundant call, you can just do if(boolean), no need to do if(boolean == true)
     
    • Useful Useful x 1
  7. Yeah I know I didn't saw the mistake was there anyway I fixed it thank you so much for helping me <3
    Also thanks for the tips you gave me :)
    Code (Text):
    public class Events implements Listener{
       
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e) {
            if (!Main.plugin.getConfig().getBoolean("StopflyOnPvP")) {
                return;
            }
            if ((!(e.getDamager() instanceof Arrow)) || (!(e.getDamager() instanceof Player)) || (!(e.getEntity() instanceof Player))) {
                  return;
                }
            Player p = (Player) e.getEntity();
            if (p != null) {
                Main.FlyingPlayers.remove(p.getName());
                p.setFlying(false);
                p.setAllowFlight(false);
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("Messages.On-PvP")));
                Main.Fall.add(p.getName());
            }
            Player plr = (Player) e.getDamager();
            if (plr != null) {
                Main.FlyingPlayers.remove(plr.getName());
                plr.setFlying(false);
                plr.setAllowFlight(false);
                plr.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("Messages.On-PvP")));
                Main.Fall.add(plr.getName());
            }
        }
        @EventHandler
        public void onPlayerFall(EntityDamageEvent e) {
            if (!(e.getEntity() instanceof Player)) {
                return;
            }
            Player p = (Player) e.getEntity();
            if (p != null) {
                if (e.getCause() == DamageCause.FALL){
                    if (Main.Fall.contains(p.getName())) {
                        p.setFallDistance(0);
                        Main.Fall.remove(p.getName());
                        e.setCancelled(true);
                        return;
                    }
                } else {
                    return;
                }
            }
           
        }
    }
     
     
  8. Yeah I also thought that but when I'm looking for my bug I always try do 1000 of things so it would actualy work. ;) Thanks for your reply btw