Error with adding a potion effect when an arrow shoots a player

Discussion in 'Spigot Plugin Development' started by MangoCodes, Jun 27, 2016.

  1. The title explains it basically...

    Classes:

    Code (Text):


    Set<UUID> set = new HashSet<>();

    Random random = new Random();
    int chance = random.nextInt(100) +1;

    @EventHandler
    public void Chill(EntityShootBowEvent e) {
        Player p = (Player) e.getEntity();
        if (p.getInventory().getItemInHand() == null) return;
        if (p.getInventory().getItemInHand().getItemMeta() == null) return;
        if (p.getInventory().getItemInHand().getItemMeta().getLore() == null) return;
        if (p.getInventory().getItemInHand().getType() == null) return;

        Entity proj = e.getProjectile();

        if (p.getInventory().getItemInHand().getItemMeta().getLore().contains(ChatColor.GOLD + "Chill I")) {
            if (chance <= 100) {
                set.add(proj.getUniqueId());
            }
        }
    }

    @EventHandler
    public void onEntityDamage(EntityDamageByEntityEvent e) {
        if (set.contains(e.getDamager().getUniqueId())) {
            if (e.getEntity() instanceof Player) {
                if (e.getDamager() instanceof Arrow) {

                    Player p = (Player) e.getDamager();

                    Player damaged = (Player) e.getEntity();

                    if (p.getInventory().getItemInHand().getItemMeta().getLore().contains(ChatColor.GOLD + "Chill I")) {
                        damaged.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 4, 10));
                        set.remove(e.getDamager().getUniqueId());
                    }

                }
            }
        }
    }
     
    Error:

    Code (Text):

    [21:44:08] [Server thread/ERROR]: Could not pass event EntityShootBowEvent to CEnchants v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityShootBowEvent(CraftEventFactory.java:246) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntitySkeleton.a(EntitySkeleton.java:256) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PathfinderGoalArrowAttack.e(SourceFile:89) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PathfinderGoalSelector.a(PathfinderGoalSelector.java:91) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityInsentient.doTick(EntityInsentient.java:473) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.m(EntityLiving.java:1626) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityInsentient.m(EntityInsentient.java:338) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityMonster.m(EntityMonster.java:20) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntitySkeleton.m(EntitySkeleton.java:110) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.t_(EntityLiving.java:1487) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityInsentient.t_(EntityInsentient.java:193) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityMonster.t_(EntityMonster.java:24) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.entityJoinedWorld(World.java:1607) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.g(World.java:1582) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.tickEntities(World.java:1425) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.WorldServer.tickEntities(WorldServer.java:597) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:786) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftSkeleton cannot be cast to org.bukkit.entity.Player
        at com.MangoCodes.CEnchant.Enchantments.Chill.Chill(Chill.java:39) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_73]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 24 more
     
     
  2. You need to make sure it's a player - that event is also thrown by skeletons.
     
  3. Sorry for the late response, but where do I need to make sure it's the player?
     
  4. right when the event fires,

    if(e.getEntity() instanceof Player) {
    // cast
     
  5. On the EntityShootBowEvent?
     
  6. Yeah
     
  7. Tried that still an error..

    Code (Text):

    [15:04:44] [Server thread/ERROR]: Could not pass event EntityShootBowEvent to CEnchants v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityShootBowEvent(CraftEventFactory.java:246) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntitySkeleton.a(EntitySkeleton.java:256) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PathfinderGoalArrowAttack.e(SourceFile:89) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PathfinderGoalSelector.a(PathfinderGoalSelector.java:91) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityInsentient.doTick(EntityInsentient.java:473) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.m(EntityLiving.java:1626) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityInsentient.m(EntityInsentient.java:338) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityMonster.m(EntityMonster.java:20) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntitySkeleton.m(EntitySkeleton.java:110) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.t_(EntityLiving.java:1487) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityInsentient.t_(EntityInsentient.java:193) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityMonster.t_(EntityMonster.java:24) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.entityJoinedWorld(World.java:1607) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.g(World.java:1582) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.tickEntities(World.java:1425) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.WorldServer.tickEntities(WorldServer.java:597) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:786) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftSkeleton cannot be cast to org.bukkit.entity.Player
        at com.MangoCodes.CEnchant.Enchantments.Chill.Chill(Chill.java:39) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_73]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 24 more
     
     
  8. at com.MangoCodes.CEnchant.Enchantments.Chill.Chill(Chill.java:39) ~[?:?]

    Chill line #39
     
  9. Ok fixed that now it's have errors on Line 62.. but that is just

    Code (Text):

    Player p = (Player) e.getDamager();
     
     
  10. Do the same thing as before, check if the damager is an instanceof Player before casting :)
     
  11. Did that.. still not working..??

    Code (Text):

    [15:13:18] [Server thread/ERROR]: Could not pass event EntityDamageByEntityEvent to CEnchants v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEvent(CraftEventFactory.java:87) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:553) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:466) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:585) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.d(EntityLiving.java:1102) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityHuman.d(EntityHuman.java:859) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:743) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityHuman.damageEntity(EntityHuman.java:800) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityPlayer.damageEntity(EntityPlayer.java:496) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityArrow.t_(EntityArrow.java:242) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.entityJoinedWorld(World.java:1607) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.g(World.java:1582) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.tickEntities(World.java:1425) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.WorldServer.tickEntities(WorldServer.java:597) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:786) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftArrow cannot be cast to org.bukkit.entity.Player
        at com.MangoCodes.CEnchant.Enchantments.Chill.onEntityDamage(Chill.java:62) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_73]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 22 more
     
    Code..

    Code (Text):

    56 - @EventHandler
    57 - public void onEntityDamage(EntityDamageByEntityEvent e) {
       58 - if (!(e.getEntity() instanceof Player)) return;
        59 - if (!(e.getDamager() instanceof Arrow)) return;
        60 - if (!(set.contains(e.getDamager().getUniqueId()))) return;
    61
        62 - Player p = (Player) e.getDamager();
        63 - Player damaged = (Player) e.getEntity();
    64
        65 -  if (p.getInventory().getItemInHand().getItemMeta().getLore().contains(ChatColor.GOLD + "Chill I")) {
             66 - damaged.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 4, 10));
           67 - set.remove(e.getDamager().getUniqueId());
      68 - }
    69 - }
     
     
  12. You're casting an Arrow (damager) to Player.


    New Code:
    Code (Text):

    56 - @EventHandler
    57 - public void onEntityDamage(EntityDamageByEntityEvent e) {
       58 - if (!(e.getEntity() instanceof Player)) return;
        59 - if (!(e.getDamager() instanceof Arrow)) return;
        60 - if (!(set.contains(e.getDamager().getUniqueId()))) return;
    61
        62 - Projectile arrow = (Arrow)e.getDamager();
               Player p = arrow.getShooter();
        63 - Player damaged = (Player) e.getEntity();
    64
        65 -  if (p.getInventory().getItemInHand().getItemMeta().getLore().contains(ChatColor.GOLD + "Chill I")) {
             66 - damaged.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 4, 10));
           67 - set.remove(e.getDamager().getUniqueId());
      68 - }
    69 - }
     
    Untested, might work though.
    The problem is that the damager is an Arrow, and you're casting it to a player.
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftArrow cannot be cast to org.bukkit.entity.Player
     
  13. It works.. except for the Potion Effect seems to like disappear even though I had set it for 4 seconds.
     
  14. It's in ticks, 20 ticks = 1 second

    80 ticks = 4 seconds