EntityDamageEvent Help - No Errors In Console

Discussion in 'Plugin Development' started by CrazymanJR, Sep 11, 2014.

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

    CrazymanJR

    I need help trying to fix spectators from not being able to hit other players, and players being able to hit specators. The code doesn't work, but has no error.

    Code:
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent event) {
    3. if (event.getEntity() instanceof Player) {
    4. Player p = (Player) event.getEntity();
    5.  
    6. if (spectators.contains(p.getName())) {
    7. event.setCancelled(true);
    8. }
    9.  
    10. }
    11. }
    12.  
    13. @EventHandler
    14. public void onHit(EntityDamageByEntityEvent event) {
    15. if (event.getDamager() instanceof Player) {
    16. Player p = (Player) event.getDamager();
    17.  
    18. if (spectators.contains(p.getName())) {
    19. event.setCancelled(true);
    20. }
    21. }
    22. }
     
  2. Offline

    teej107

    CrazymanJR You do know that you could put all of your posted code inside the EntityDamageByEntityEvent listener, right?
     
  3. Offline

    CrazymanJR

    teej107
    I know, but i just like seeing things like this, plus it doesn't work either when i put them in the same listener either.
     
  4. Offline

    mythbusterma

    CrazymanJR

    Are you registering your listeners, and are you certain "spectators" actually contains anything?
     
    PePsiGam3r likes this.
  5. Offline

    PePsiGam3r

    Make sure you've registered your listeners, also make sure that spectators is a real list and it does contain names (as mythbusterma said)
     
  6. Offline

    4thegame3

    Try to do a little debug like this.

    for(String spectator : spectators){
    System.out.println(spectator);
    }

    then check in the console the values of spectators
     
  7. Offline

    CrazymanJR

    I have already registered my listeners, but they don't cowteal.

    I really need somebody to help me out here...

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

    teej107

    CrazymanJR
    The only reason the event won't cancel is because your if statement is false. "spectators" does not contain the player's name and that is why the event isn't cancelling. This is of coarse assuming that there are no errors and you did register your events.
     
  9. Offline

    FabeGabeMC

    CrazymanJR
    You might want to do event.getDamager();
    event.getEntity() returns the one getting damaged.
     
  10. Offline

    mythbusterma

    teej107

    Isn't that what I said?
     
  11. Offline

    teej107

    Yes. The OP is still asking for help even though you told him what the problem was. Hopefully my re-worded explanation helped him understand his problem since he didn't seem to show signs of following yours.
    CrazymanJR
     
  12. Offline

    CrazymanJR

    Nothing seems to work out, even your help. Just gonna not do what i was gonna do.
     
  13. Offline

    teej107

  14. Offline

    shmkane

    Do a bit of testing, throw an else statement around

    Code:
    if (spectators.contains(p.getName())) {
            event.setCancelled(true);
          }
    And add a cute message

    PHP:
    if (spectators.contains(p.getName())) {
            
    event.setCancelled(true);
          }else{
    //send someone a message saying something dun goofed!
    }
    If you see the message, you'll know the location of your problem ;)
     
  15. Offline

    4thegame3

    Ok try this.
    Code:java
    1. for (String spectator : spectators){
    2. if(spectator.equalsIgnoreCase(p.getName()){
    3. System.out.println("yes");
    4. }
    5. }
     
Thread Status:
Not open for further replies.

Share This Page