How can I make a mob invincible?

Discussion in 'Plugin Development' started by PerezHD, Nov 3, 2014.

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

    PerezHD

    Hey guys, I am trying to make just a zombie only invincible, I am not sure how to do this, but I need someones help about it.

    What I have now:
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e) {
    3. setCancelled(true);
     
  2. Offline

    ChaddTheMan

    Yes it is possible, and you're on the right track!
    If needed I could post some code but for now try this:
    Get the mob that was hurt.
    If it was a zombie, then "setCancelled(true)".
    else return;

    Edit: Try out this code, it should work as a guideline.

    Code:java
    1. package your.package.here;
    2.  
    3. import org.bukkit.entity.EntityType;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7.  
    8. public class PluginMobDamageListener implements Listener {
    9.  
    10. @EventHandler
    11. public void onMobDamage(EntityDamageEvent e) {
    12.  
    13. EntityType type = e.getEntityType();
    14.  
    15. if( !(type == EntityType.ZOMBIE) ) return; //If it's not a zombie then return..Don't do damage.
    16.  
    17. e.setCancelled(true); //It has to be a zombie to get to this point, cancelling the damage.
    18.  
    19. }
    20.  
    21. }
    22.  
     
  3. Offline

    PerezHD

    My part on where I am confused at is how could I make it detect if its a zombie? Please post the code?
     
  4. Offline

    ChaddTheMan


    Check my reply update
     
  5. Offline

    PerezHD

    Thx, it worked perfectly.
     
  6. Offline

    mamifsidtect

    Just a suggestion for your code ChaddTheMan I think it would be more preferable and probably adhere to Java coding conventions to do this instead (no major changes and code would still work without it, just syntax):

    Code:java
    1. package your.package.here;
    2.  
    3. import org.bukkit.entity.EntityType;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7.  
    8. public class PluginMobDamageListener implements Listener {
    9.  
    10. @EventHandler
    11. public void onEntityDamage(EntityDamageEvent event) {
    12. EntityType entityType = e.getEntityType();
    13. if (type != EntityType.ZOMBIE) return;
    14.  
    15. //or you can do
    16. //if (type != EntityType.ZOMBIE) {
    17. // return;
    18. //}
    19.  
    20. event.setCancelled(true);
    21. }
    22. }
     
Thread Status:
Not open for further replies.

Share This Page