Get if player is sneaking and get if player is PVPing

Discussion in 'Plugin Development' started by Willbbz, Sep 22, 2012.

Thread Status:
Not open for further replies.
  1. As the title suggests. I'm trying to disable PvP for players when they call up the sneak event. I've scoured this forum for a solution and putting code together with no luck

    So far:
    Code:
        @EventHandler
        public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) {
          Player player = event.getPlayer();
          if(player.isSneaking()) {
              @EventHandler
                public void onEntityDamage(EntityDamageEvent event) {
                  if (event.getEntity() instanceof Player) {
                  Player p = (Player) event.getEntity();
                  if (p.getPassenger() instanceof Player) {
                  p.sendMessage("You can't attack players when croutched!");
                  event.setCancelled(true);
                  }
                  }
                  }
              }
          }
    The error I'm getting is on this line
    Code:
                public void onEntityDamage(EntityDamageEvent event) {
    
    I think it might be because I'm pulling up another event handler within the first @EventHandler

    ---
    Also rather than make a new thread, would anybody know how to enable a plugin if only another specific plugin is enabled?

    Thanks!
     
  2. you cant put 2 event handlers indie each other
     
  3. Like ferrybig told you can't write a method in a method.
    But there's no need to listen for the PlayerToggleSneakEvent at all cause you can do the p.isSneaking() check in the EntityDamageEvent, too (it's a method of Player, not PlayerToggleSneakEvent. But all in all your code is wrong. p.getPassenger() is a passenger (most likely null), not the attacked player (hint: EntityDamageByEntityEvent).
     
  4. Thanks! I'm pretty n00by with this.

    I've changed it to
    Code:
        @EventHandler
            public void onEntityDamage(EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                Player Player = (Player) event.getEntity();
                if(Player.isSneaking()) {
                    event.setCancelled(true);
                      }
                      }
                    }
        }
    Compiles fine but players still take damage from other players so I'm missing something
     
  5. Offline

    Tirelessly

    Try putting it at highest priority and see if that's causing a problem.
     
  6. No luck unfortunately. I thought it might be a problem with my parenthesis so changed to this
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
            public void onEntityDamage(EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                Player Player = (Player) event.getEntity();
                if(Player.isSneaking()){
                    Player.sendMessage("You can't attack players when croutched!");
                    event.setCancelled(true);
                     
                      }
                    }
        }
    }
    Players still take damage when I hit them and the sendMessage doesn't print to my client
     
  7. Player Player <- this is probably the worst thing you can do. Please make it "Player player"
    Other than that it seems fine to me. Please add some debugging lines, like this:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    2. public void onEntityDamage(EntityDamageByEntityEvent event) {
    3. System.out.print("Event started!");
    4. if (event.getEntity() instanceof Player) {
    5. System.out.print("Entity is a Player!");
    6. Player player = (Player) event.getEntity();
    7. if(player.isSneaking()){
    8. player.sendMessage("You can't attack players when croutched!");
    9. event.setCancelled(true);
    10. }
    11. }
    12. }

    Also make sure you registred the Listener correctly: http://wiki.bukkit.org/Introduction_to_the_New_Event_System#Registering_Events_in_Plugin
     
  8. Thanks for your help, very appreciated! What's the difference between Player and player?

    The plugin works but the player who is crouched is not able to receive damage but can inflict damage on another player. I was hoping to invert this. The sendMessage also is printed to the person who is sneaking when they get hit when it should be printed to them while they're sneaking and also be hitting somebody else.
     
  9. Offline

    The_Coder

    he is casting (Player) to event.getEntity(), which makes things easier. The reason that is works is that I has already gone past the check. If there was not check you would get a lot of errors.
     
  10. Player is a bukkit object, player is a variable you defined.
    By giving the variable the same name as the bukkit object things getting confused...
    Variable names shouldn't start uppercase anyway.

    Hint: event.getDamager()
     
Thread Status:
Not open for further replies.

Share This Page