Solved Check if the entity is a player

Discussion in 'Plugin Development' started by jebbo, Nov 5, 2013.

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

    jebbo

    How can i simply check if the entity, that i attack is a Player?
     
  2. Offline

    adam753

    if(entity instanceof Player) {
     
  3. Offline

    Janmm14

    jebbo
    Edit:
    Also possibl:
    if (entity.getType() == EntityType.Player) {
     
    jebbo likes this.
  4. Offline

    jebbo

    Worked,
    but now, if I get damage:
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.entity.C
    raftCreeper cannot be cast to org.bukkit.entity.Player
            at me.iraphi.irfriede.Main.onDamage(Main.java:126)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 22 more
    Line 124-126

    Code:
            if(en instanceof Player) {
                Player pl = (CraftPlayer)e.getEntity();
                Player to = (Player)e.getDamager();
     
  5. Offline

    Janmm14

    jebbo
    You have to check both. And why you need craftplayer? use it only if you need to.
     
  6. Offline

    jebbo

    Now I have

    Entity en = e.getEntity();
    if(en instanceof Player){
    //code
    }else{

    }

    But I still get a error in the console saying

    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.entity.C
    raftZombie cannot be cast to org.bukkit.entity.Player

    What do i have to check?
     
  7. Offline

    Janmm14

    jebbo
    what type is en?

    if (e.getEntity() instanceof Player && e.getDamager () instanceof Player) {
     
    jebbo likes this.
  8. Offline

    jebbo


    Its working, thank you.
     
Thread Status:
Not open for further replies.

Share This Page