How can i broadcastMessage() and exclude specific players?

Discussion in 'Plugin Development' started by The Zombie Blarger, Nov 12, 2011.

Thread Status:
Not open for further replies.
  1. I have a plugin im making, and its for emoting. For example

    player 1: /derp player2
    to player 1: you derped player2!
    to player2: player2 derped you!
    to everyone else: player1 derped player2!

    However, i need to exclude that broadcast from showing to player1 and player2. How would i do this?
     
  2. Offline

    matter123

    for(Player p:Server.getOnlinePlayers()) {
    if(p!=player1||p!=player2)p.sendMessage(message):
    }
    not im half asleep so this amay have bugs
     
  3. Perfect, appreciate it!
     
  4. Offline

    matter123

    ok now that im awake dont use != use !(p.equals(player1))
     
    pyraetos likes this.
  5. Offline

    Baba43

    Where is the difference between != and equals?
     
  6. Offline

    Acrobot

    You use == (and !=) for comparing REFERENCES, NOT VALUES.
    Generally, you should only use == for primitive data types, checking if the object IS THE SAME OBJECT as the 2nd one and comparing 2 enum values.

    .equals() compares VALUES.
    o.equals(o) method can be over-written when creating a new object, so it can be compared differently.
    You generally want to use this for Objects, such as Strings or custom objects.
     
    Baba43 likes this.
  7. Offline

    matter123

    i just want to add that o.equals(o2) is different from o==o2 only if the class that o belongs to overrides equals
     
  8. Offline

    halley

    You are correct, but this way of stating it may be misleading. If your class doesn't override equals(), then equals() and == are equivalent comparisons, but both are now comparing REFERENCES, still not comparing VALUES.

    Two different object instances with exactly the same state internally are not seen as equal, unless the class overrides equals() and compares the internal state of both objects for their values.
     
  9. Offline

    matter123

    ok thats more corrcet then what i said
     
Thread Status:
Not open for further replies.

Share This Page