Solved Could not pass event EntityDamageByEntityEvent

Discussion in 'Spigot Plugin Development' started by Hacker333333, Jun 23, 2022 at 4:19 PM.

  1. Hello. I have my own plugin, it adds PvP bans (blocks pvp and crystal explosions for some players)
    This plugin sometimes spamming errors in console
    EventHandler code:
    Code (Java):
    @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
            if (e.getEntity() instanceof Player || e.getEntity() instanceof EnderCrystal) {
                if(e.getDamager() instanceof Player || e.getDamager() instanceof Arrow) {
                    FileConfiguration config = this.getConfig();
                    TextComponent msg = new TextComponent("У вас PvP-бан");
                    msg.setBold(true);
                    msg.setColor(ChatColor.DARK_RED);
                    if(e.getDamager() instanceof Arrow) {
                        Arrow arr = (Arrow)e.getDamager();
                        Player shooter = (Player)arr.getShooter();
                        if(config.getList("banlist").contains(shooter.getName())){
                            e.setCancelled(true);
                            shooter.spigot().sendMessage(ChatMessageType.ACTION_BAR, msg);
                        }
                    } else {
                        if(config.getList("banlist").contains(e.getDamager().getName())){
                            e.setCancelled(true);
                            Player pl = Bukkit.getPlayer(e.getDamager().getName());
                            pl.spigot().sendMessage(ChatMessageType.ACTION_BAR, msg);
                        }
                    }
                }
            }
        }
    Error in console:
    Code (Text):
    [18:15:26] [Server thread/ERROR]: Could not pass event EntityDamageByEntityEvent to MultiJA v1.3.2
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1620]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched_1.12.2.jar:git-Paper-1620]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1620]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1620]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:94) ~[patched_1.12.2.jar:git-Paper-1620]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:620) ~[patched_1.12.2.jar:git-Paper-1620]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:523) ~[patched_1.12.2.jar:git-Paper-1620]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:652) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity0(EntityLiving.java:1415) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityHuman.damageEntity0(EntityHuman.java:860) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity(EntityLiving.java:888) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityHuman.damageEntity(EntityHuman.java:773) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityPlayer.damageEntity(EntityPlayer.java:629) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityArrow.a(EntityArrow.java:295) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityArrow.B_(EntityArrow.java:203) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.EntityTippedArrow.B_(EntityTippedArrow.java:86) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.World.entityJoinedWorld(World.java:1797) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.World.h(World.java:1767) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.World.tickEntities(World.java:1574) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.WorldServer.tickEntities(WorldServer.java:682) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:921) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1620]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1620]
    at java.lang.Thread.run(Thread.java:750) [?:1.8.0_332]
    Caused by: java.lang.ClassCastException
    Any ideas how to fix that?
     
  2. So,
    1. You should save players in the config with their UUIDs and not names
    2. I think that the problem is, that you are getting a list of objects from the config and not a list of strings, so use #getStringList then #getList
     
    • Like Like x 1
  3. Ok, thank you, i fix it!
    But this old version of code do not cause any errors
    Code (Java):
        public void onHit(EntityDamageByEntityEvent e) {
            if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                FileConfiguration config = this.getConfig();
                if(config.getList("banlist").contains(e.getDamager().getName())){
                    e.setCancelled(true);
                    e.getDamager().sendMessage(ChatColor.RED + "You have PvP-ban");
    }
    }
    }
     
     
  4. I fixed it. Added checking for player cast
    Final code:
    Code (Java):
        @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
            if (e.getEntity() instanceof Player || e.getEntity() instanceof EnderCrystal) {
                if(e.getDamager() instanceof Player || e.getDamager() instanceof Arrow) {
                    FileConfiguration config = this.getConfig();
                    TextComponent msg = new TextComponent("У вас PvP-бан");
                    msg.setBold(true);
                    msg.setColor(ChatColor.DARK_RED);
                    if(e.getDamager() instanceof Arrow) {
                        Arrow arr = (Arrow)e.getDamager();
                        if(arr.getShooter() instanceof Player) {
                            Player shooter = (Player)arr.getShooter();
                            if(config.getStringList("banlist").contains(shooter.getName())){
                                e.setCancelled(true);
                                shooter.spigot().sendMessage(ChatMessageType.ACTION_BAR, msg);
                            }
                        }
                    } else {
                        if(config.getStringList("banlist").contains(e.getDamager().getName())){
                            e.setCancelled(true);
                            Player pl = Bukkit.getPlayer(e.getDamager().getName());
                            pl.spigot().sendMessage(ChatMessageType.ACTION_BAR, msg);
                        }
                    }
                }
            }
        }