How to disable a player from hitting other players while sneaking

Discussion in 'Plugin Development' started by Mr_Gibbles, Jan 17, 2014.

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

    Mr_Gibbles

    Here is my code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onTurtleAttack(EntityDamageEvent event) {
    4. if (event.getEntity() instanceof Player) {
    5. Player p = (Player)event.getEntity();
    6. if (turtle.contains(p.getName())) {
    7. if (p.isSneaking()) {
    8. event.setDamage(2);
    9. }
    10. }
    11. }
    12. }

    I want to make them unable to attack other players under event.setDamage(2);

    This is probably something really simple and dumb, but I'm new at coding and I've been at this for hours :/
     
  2. Offline

    Doamax

    event.setCancelled(true); is used to cancel the event. I'm not 100% sure what you want though.
     
  3. Offline

    ArthurMaker

    EntityDamageByEntityEvent is what you need.
     
  4. Offline

    Mr_Gibbles

    I want it so that when a player is sneaking they can only take 2 hearts of damage and they can't attack other players.

    So basically make it so the player cant pvp

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

    The_IcATaRiX

    Code:java
    1. @EventHandler
    2. public void onSneak(EntityDamageEvent e) {
    3. Player p = (Player)e.getEntity();
    4. if (p.isSneaking()) {
    5. e.setDamage(2.0D);
    6. }
    7. }
    8.  
    9. @EventHandler
    10. public void onHit(EntityDamageByEntity e) {
    11. Player damager = (Player)e.getDamager();
    12. if (damager.isSneaking()) {
    13. e.setCancelled(true);
    14. }
    15. }


    I'm pretty sure, that's what you want :)
     
  6. Offline

    Doamax

    Like ArthurMaker said, you will need the EntityDamageByEntityEvent. Check if the attacked entity is a player and crouching, if they are set the damage to 2, if they aren't then check if the attacker is a player and crouching, then cancel the event.
     
  7. Offline

    Mr_Gibbles

    Yeah, that works, thanks :)

    I see what you guys mean now, thanks :)
     
Thread Status:
Not open for further replies.

Share This Page