Solved Problem on EntityDamageEvent :3

Discussion in 'Plugin Development' started by girardcome, May 28, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    girardcome

    Hellow guys, I'm not new don't worry ^^
    I have a problem that comes up every time with the EntityDamageEvent, it's due to the instanceof I think, from what I saw in the stacktrace.
    Who could help me better understand why I had this error? Thank you.
    (I don't want any code or sample code, I just want an explanation, I did some little debug messages, but nothing works)..

    ERROR LOGS :
    Code:
     
        [12:21:19] [Server thread/ERROR]: Could not pass event EntityDamageEvent to PluginFight v1.0
                at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
        Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_14_R1.entity.CraftPillager cannot be cast to org.bukkit.entity.Player
                at fr.girardcome.mcpvp.kits.Turtle.onTurtleSneak(Turtle.java:29) ~[?:?]
                at sun.reflect.GeneratedMethodAccessor474.invoke(Unknown Source) ~[?:?]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
                at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_211]
                at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[server.jar:git-Bukkit-f690957]
                ... 32 more
    Code :
    HTML:
    @EventHandler
        public void onTurtleSneak(EntityDamageEvent event) {
            if (!(event.getEntity() instanceof Player)) {
                if (event.getCause() == DamageCause.FALL) {
                    Entity player = event.getEntity();
                    Player p = (Player) player;
                    if (KitManager.hasKit(p, Kit.Turtle)) {
                        if (p.isSneaking() && p.getHealth() > 2) {
                            event.setDamage(2);
                        } else if (p.isSneaking() && p.isBlocking() && p.getHealth() > 1.0D) {
                            event.setDamage(1.0D);
                        }
                    }
                }
            } else if (event.getEntity() instanceof Player) {
                Player p = (Player) event.getEntity();
                if (p.isSneaking() && p.getHealth() > 2) {
                    event.setDamage(2);
                } else if (p.isSneaking() && p.isBlocking() && p.getHealth() > 1.0D) {
                    event.setDamage(1.0D);
                }
            }
    
        }
    Line error: the first "Player p = (Player) player;"
     
    Last edited: May 28, 2019
  2. Offline

    timtower Moderator Moderator

    @girardcome
    if (!(event.getEntity() instanceof Player)) {
    Entity player = event.getEntity();
    Player p = (Player) player;
    Why is that in the same block?
     
  3. Offline

    girardcome

    I'm trying to develop in my own way, but apparently it's not the right one. I know that we can only use an if statement with "e.getEntity () instanceof Player". But what should I do now? please
     
  4. Offline

    timtower Moderator Moderator

    @girardcome The check is fine, but why do you cast to a player after it?
     
  5. Offline

    girardcome

    How can i use my codes if i didn't cast it?
    e.g: the method "if (KitManager.hasKit(p, Kit.Turtle))", if i remove p, i must put "e.getEntity()" and cast it to a Player...
     
  6. Offline

    timtower Moderator Moderator

    But before that you are checking it the entity is NOT a player.
     
  7. Offline

    girardcome

    Yes i solved it thanks!

    That's the new code :
    HTML:
        if (!(event.getEntity() instanceof Player)) {
                return;
            }
            Player p = (Player) event.getEntity();
            if (event.getCause() == DamageCause.FALL) {
                if (KitManager.hasKit(p, Kit.Turtle)) {
                    if (p.isSneaking() && p.getHealth() > 2) {
                        event.setDamage(2);
                    } else if (p.isSneaking() && p.isBlocking() && p.getHealth() > 1.0D) {
                        event.setDamage(1.0D);
                    }
                }
            }
    There's no longer errors on the console, so is it a good way of coding stuff?
     
  8. Offline

    timtower Moderator Moderator

    There are many ways to make the same thing. Multiple are good, some are very bad.
    But most of the time it comes down to the personal preferences of the developer.
     
    girardcome likes this.
Thread Status:
Not open for further replies.

Share This Page