Stopping players from taking any damage(at all!)

Discussion in 'Plugin Development' started by Sabersamus, Feb 17, 2012.

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

    Sabersamus

    I have done this, by using

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDamageEvent(EntityDamageEvent event){
    4. Player player = (Player) event.getEntity();
    5. if(event.getEntity() instanceof Player){
    6. if(player.hasPermission("basic.god")){
    7. event.setCancelled(true);
    8. }
    9. }else{
    10. event.setCancelled(false);
    11. }
    12. }
    13.  


    upon testing it, when ever i get hurt, no problem. no errors, no damage, nothing.

    should i go attack a pig, lets say. 1 million errors saying stuff like this

    Code:
            ... 11 more
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.entity.CraftPig
    cannot be cast to org.bukkit.entity.Player
            at com.github.Sabersamus.Basic.Listeners.GodModeListener.onPlayerDamageE
    vent(GodModeListener.java:19)
    

    here is line 19 :/
    Code:java
    1. Player player = (Player) event.getEntity();


    so what im asking is, whats a better way to do it, so that players take no damage, but entities dont FLIP out at me when they do?
     
  2. Offline

    Roadkill909

    If the entity is not an instance of Player, it'll throw that exception. Move the casting line (line 19) inside your if statement where you check instance.

    Edit: Also the else statement portion is unnecessary. I recommend it be removed.

    Second edit: Here's the code, didn't think I explained it clearly..

    Code:java
    1. @EventHandler
    2. public void onPlayerDamageEvent(EntityDamageEvent event){
    3. if(event.getEntity() instanceof Player){
    4. Player player = (Player) event.getEntity();
    5. if(player.hasPermission("basic.god")){
    6. event.setCancelled(true);
    7. }
    8. }
    9. }
     
  3. why do you cast it before you check if it's the class you want it to be casted to?
     
  4. Offline

    Sabersamus

    how exactly would that look? :confused:

    EDIT: nvm i feel retarded now -_- im sorry XD

    i need some coffee :3

    thanks guys :) it works, no more MILLIONS of errors whenever a stupid pig gets in my way XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  5. Offline

    Roadkill909

    Also the reason that error was firing a million times is any time a skeleton fell off a cliff or zombie caught on fire in sunlight it will throw an error.

    Glad you got it working
     
  6. Offline

    Sabersamus

    thanks for the help ;)
     
  7. Code:
    @EventHandler
    public void onPlayerDamageEvent(PlayerDamegeEvent ev)
    {
        Player p = ev.getPlayer();
        if(p.hasPermission("permission.node.here")
        {
              ev.setCancelled(true);
        }
     
     
    }
    That simply cancels the event if they have the permission.

    oops, i didn't copy tour code roadkill, i just didnt read the entire post :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  8. Offline

    mushroomhostage

    Canceling the damage event works for the most part, but doesn't stop other plugins from damaging the player. Is there any way to solve this?
     
Thread Status:
Not open for further replies.

Share This Page