Solved Playing a sound server wide (or to every players)

Discussion in 'Plugin Development' started by Thej0y, Apr 15, 2013.

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

    Thej0y

    Hi,
    I'm looking for a way to play a sound to everybody (whos online) on the server.
    I just can't find how to do it... I tryed so many things and havent found anything concluding on google.

    Code:
    for(Player p : Bukkit.getOnlinePlayers()){
    p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0);
    }
    
    This code is what I made that (I think) is the closer the what I want to do... But i'm also affraid that it could cause major noise if too many player where in same place.

    Anyone have an idea?
    Thank you :)
     
  2. world.playSound()?
     
  3. Thej0y
    This won't cause noise, since you send the sound to each player, other players won't even hear that sound. If you use world.playSound() then it would cause a huge sound wave, cuz everyone would hear that. So your method is the best to use.
     
  4. Offline

    Thej0y

    I'm trying thing around this for about a day now :p (i'm really new to programming -.-)

    do you mean something like this?
    Code:
    for(org.bukkit.World w : Bukkit.getWorlds()){
    w.playSound(null, Sound.CAT_MEOW, 1, 0);
    }
    
     
  5. Thej0y
    Don't do that, expecially since you're giving a null value as location which would be a problem.
    As Hellsing said, your initial code is good.
     
  6. Offline

    Burnett1

    He means use the Player p: Bukkit.... one. And yes the first post was right.

    Edit: Ninja'd
     
  7. Offline

    Thej0y

    Thanks a lot for this info! :)

    but still, my method simpy does nothing. No error, no message and nothing happpend... :( must be something I did wrong.
     
  8. Offline

    Burnett1

    The whole code please.
     
  9. Offline

    Thej0y

    lol thanks anyway! :)

    The whole plugin code or the whole command? here is the command:
    Code:
    if(label.equalsIgnoreCase("sendall")) {
            if(Bukkit.getOfflinePlayer(sender.getName()).isOp()) {     
    for(Player p : Bukkit.getOnlinePlayers()){
    p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0);
    }
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  10. Offline

    Burnett1

    Your checking if the ONLINE player's name is equal to an OFFLINE player's name...? What do you actually want it to do? If the sender is op play the sound? Or if the console sent it?
     
  11. Offline

    Thej0y

    if the sender is op(even if hes offline,so it doesnt conflict with my remote console that somethime shows me as an offline player), then send sound to everyone online.

    I cant see where I compare online vs offline as i set p to online player after checking if the sender is op

    edit: in doubt, I just re-tested,
    Code:
    if(Bukkit.getOfflinePlayer(sender.getName()).isOp()) 
    this code work for checking if the senders(which is online) is op

    Just tough of something, tell me if i'm wrong.
    In this code (the first one),
    Code:
    for(Player p : Bukkit.getOnlinePlayers()){
    p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0);
    }
    is 'getOnlinePlayers()' returning a list? if so, do 'p' equals the whole list? Then is is trying to shoot the whole list before '.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0);' or what I say is totally wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  12. Thej0y
    It returns an array of Player object (marked as Player[] ), it's like a list but has fixed size and more primitive.
    And 'Player p' is the current element in the iteration, you're looping through the values of the array.

    I don't know why you're stressing over that... it sends a sound for each online player, it's as simple as that.

    Your offline player code is weird though, it makes no sense...
    Your remote would send console commands which has operator access regardless, so getting the player named like the server would be totally pointless... Just use sender.isOp() and be done with it.

    Also I recommend you use command.getName() instead of label because the label is the command typed which can be an alias and if it is it won't match your equals.

    Code:
    if(cmd.getName().equalsIgnoreCase("sendall")) {
        if(sender.isOp()) {
            for(Player p : Bukkit.getOnlinePlayers()) {
                p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0);
            }
        }
    }
    And by the way, you should make your code more readable by indenting it, like I did above, spacing to level the code.
     
  13. Offline

    Thej0y

    Digi
    Thank a lot for all those infos :)
    I was stressing with this code because it wasnt working, but after re-writing everything it works... so I guess I worked around the (surley little) mistake all day long without never seeing it -.-

    as you guys told me earlyer, my code was actually working :p
    Thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page