How to block killing a certain mobs

Discussion in 'Plugin Development' started by nnx, Jul 7, 2020.

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

    nnx

    PS English from Google Translate:

    Hello good day! I would like to know how it is possible to block players from killing certain mobs if they are not allowed.

    Example:
    kill.cow (if the player does not have this permission, he will not be able to kill cows).

    I've been trying to do this for a few days and I couldn't. I thank those who help!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @nnx EntityDamageEntityEvent
    Check if the one doing the damage is a player, if so: cancel the event.
    Do check projectiles and their shooters as well.
     
  3. Offline

    nnx

    English from Google Translate

    If it doesn't matter, could you make a small example for you to be aware of? I am a beginner in Java. Thank you!
    PS: 1.8
     
    Last edited: Jul 7, 2020
  4. Offline

    timtower Administrator Administrator Moderator

    @nnx I don't spoonfeed.
    I will help change your code, but I won't write it for you.
     
  5. Offline

    bowlerguy66

  6. Offline

    mAndAle


    Code:
        private String perm = "killer.cow";
       
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e) {
            Player p = (Player) e.getDamager();
            if (p instanceof Cow) {
                if (!p.hasPermission(perm)) {
                   e.setCancelled(true);
                  return;
            }
                 return;
        }
    }
    that's not all you asked for but I think this help is enough and you go ahead, try to improve the code I just wrote to you and also to understand it.
     
Thread Status:
Not open for further replies.

Share This Page