Finding out whether an entity is an animal or a mob

Discussion in 'Plugin Development' started by SamusAranX, Jul 25, 2011.

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

    SamusAranX

    Currently I'm doing it like this:
    Code:
    if(event.getEntity() instanceof org.bukkit.entity.Chicken || event.getEntity() instanceof org.bukkit.entity.Cow || event.getEntity() instanceof org.bukkit.entity.Creeper || event.getEntity() instanceof org.bukkit.entity.Pig || event.getEntity() instanceof org.bukkit.entity.PigZombie || event.getEntity() instanceof org.bukkit.entity.Sheep || event.getEntity() instanceof org.bukkit.entity.Skeleton || event.getEntity() instanceof org.bukkit.entity.Slime || event.getEntity() instanceof org.bukkit.entity.Spider || event.getEntity() instanceof org.bukkit.entity.Squid || event.getEntity() instanceof org.bukkit.entity.Wolf || event.getEntity() instanceof org.bukkit.entity.Zombie) { //cringeworthy ^ 100
    But there's no way that this is the best method to do it. :D
    My question is: Can I replace that clusterfuck of code with org.bukkit.entity.Animals and org.bukkit.entity.Monster while retaining it's function?
     
  2. Offline

    DrBowe

    Code:java
    1.  
    2. if(event.getEntity() instanceof Animals || event.getEntity() instanceof Monster){
    3. //do stuff
    4. }
    5.  
     
  3. Offline

    SamusAranX

    OK, thanks :)
     
  4. Offline

    nisovin

    Note that squids, ghasts, and slimes are not animals or monsters.
     
  5. Offline

    Nitnelave

    You could try with livingEntity and then filter out the players, to include slimes, ghast, etc..
     
  6. Offline

    Taco

    Code:
    if(event.getEntity() instanceof LivingEntity && !event.getEntity() instanceof Player)
     
  7. Offline

    SamusAranX

    @nisovin @Nitnelave Squids are SeaMobs or WaterMobs, I think (But I don't have Eclipse open atm, so I don't know exactly), and Ghasts and Slimes are disabled on my server anyway. :p Out of curiosity, what are Ghasts and Slimes then?

    @Taco I mainly programmed in VB.NET and a bit of C# before I started with Java, and I yet have to wrap my head around all this instanceof stuff, thanks >.>
     
  8. Offline

    nisovin

    The instanceof concept exists in VB.Net and C#. In C# I believe it's the "is" keyword, and in VB.Net you'd do something like "TypeOf [var] Is [type]".
     
Thread Status:
Not open for further replies.

Share This Page