Solved EntityDamageByEntity Error

Discussion in 'Plugin Development' started by shohouku, May 8, 2014.

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

    shohouku

    I'm trying to set the damage of an entity when the entity hits the player, it's getting the setDamage off the config but I'm not sure whats the error about.

    I'm getting the error here:

    Code:java
    1. Skeleton test = (Skeleton) e.getDamager();
    2. String name = test.getCustomName();


    My error:

    Code:java
    1. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.entity.C
    2. raftPlayer cannot be cast to org.bukkit.entity.Skeleton



    CODE:
    Code:java
    1. @EventHandler
    2. public void onDmg(EntityDamageByEntityEvent e) {
    3.  
    4. int lvl1 = monsterdmg.getInt("lvl1");
    5. Skeleton test = (Skeleton) e.getDamager();
    6. String name = test.getCustomName();
    7. if (e.getDamager() instanceof Skeleton) {
    8. if(name.equalsIgnoreCase("LOL")) {
    9.  
    10. e.setDamage(lvl1);
    11. for(Player p : Bukkit.getOnlinePlayers()){
    12. p.sendMessage("" + lvl1);
    13. }
    14. }
    15. }
    16. }
     
  2. Offline

    TGRHavoc

    shohouku
    You're declaring the variable "test" as a skeleton before you've checked if it's an instance of a skeleton.. Therefore, if a player was to damage an entity, Bukkit would try and cast a player as a skeleton (Which is impossible) and throw you an error....
     
  3. Offline

    shohouku



    I'v done this before with just getting the instance of the skeleton which looked like this:

    Code:java
    1. @EventHandler
    2. public void onDmg(EntityDamageByEntityEvent event) {
    3. if (event.getDamager() instanceof Skeleton) {
    4. event.setDamage(1.0);
    5. }
    6. }
    7.  


    But I'm trying to get the custom name of the skeleton, is it possible?

    Can't find any other possible methods to this solution without getting any errors.
     
  4. Offline

    TGRHavoc

    shohouku
    Yes, check whether the damager is a Skeleton, cast it to a variable, check if it has a custom name, check its' custom name, set damage for the event....
     
    shohouku likes this.
  5. Offline

    shohouku


    Thanks!

    Solved.
     
  6. Offline

    TGRHavoc

    shohouku
    No problem, glad I could help :D
     
Thread Status:
Not open for further replies.

Share This Page