Plugin Help - Broadcast a message to all players

Discussion in 'Plugin Development' started by vYN, Jan 9, 2012.

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

    vYN

    Hi. I'm trying to make my plugin broadcast a message to every player online... but i can't figure out how to do that.. for example when a player throws an egg. The plugin will broadcast Who throw an egg like this: "Name throw an egg"

    Thanks in advance.
     
  2. Offline

    nala3

    this.getServer().broadcastMessage("message");
     
  3. Offline

    vYN

    :O it worked Thanks!!!

    haha new problem xD on my getDisplayName it says "null Throws an egg!"
     
  4. Offline

    nala3

    Player p = event.getPlayer();

    this.getServer().broadcastMessage(p + " threw an egg!');
     
  5. Offline

    T145

    Making a plugin that broadcasts a message for everything a player does would get kind of annoying, don't you think? I know some of my admin friends who got trolled hard in their chat from using plugins that relate to the idea your planning. Spamming eggs everywhere to generate mobs of chickens is really obnoxious, but having each egg spammed through chat may only worsen the situation.
     
  6. Offline

    vYN

    hehe i know it will. i'm just doing this to learn some new stuff. like a learning curve that goes upwards xD

    hmm ok. now it says "CraftPlayer{name=GudfareN} Throws an egg!"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  7. Offline

    nala3

    then I think you imported the wrong Player :)
     
  8. Offline

    vYN

    hmm ok...

    Code:
            public void onPlayerEggThrow(PlayerEggThrowEvent event) {
                Player p = event.getPlayer();
                getServer().broadcastMessage(p + " Throws an egg!");
                }
     
  9. Offline

    nala3

    make sure you imported:
    import org.bukkit.entity.Player;
     
  10. Offline

    vYN

    That is imported yes..
     
  11. Offline

    nala3

    ohh derp I am dumb.
    Code:
    public void onPlayerEggThrow(PlayerEggThrowEvent event) {
                String p = event.getPlayer().toString();
                this.getServer().broadcastMessage(p + " Throws an egg!");
            }
    
     
  12. Offline

    vYN

    when i'm adding "this." infront of getServer it gies me an error....

    Code:
    The method getServer() is undefined for the type Main.AutoMSGPlayerListener
     
  13. Offline

    nala3

    just get rid of the this I just wrote it in a hurry :p
     
  14. Offline

    vYN

    hmm ok. for some reason it still gives me "CraftPlayer{name=GudfareN} Throws an egg!"

    i feel so noob at this lol xD
     
  15. Offline

    nala3

    hmm. well I'm not very good at helping, I know what I am doing (most of the time) but I can't explain for my life :p
     
  16. Offline

    vYN

    hehe ok. Thanks anyway. u got me a huge step forwards!
     
  17. Offline

    user_43347

    If you don't know how to get the plugin instance, you should read up on Java and Bukkit before continuing.
    Anyways, this would do just that.
    Code:
    public void onPlayerEggThrow(PlayerEggThrowEvent event) {
         String name = event.getPlayer().getName();
         plugin.getServer().broadcastMessage(name + " threw an egg!");
    }
     
  18. Offline

    nala3

    ohh getName, herp-a-derp XD
    Have not had to use that before, it is now noted
     
  19. Offline

    vYN

    Hehe that fixed it. Thanks for all the help.

    Hi. One more question... sorry..
    How do i send a message from the plugin to the player that issues the command for the plugin?

    Code:java
    1.  
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("egghatcher")){
    3. getServer().broadcastMessage(ChatColor.BLUE + "From Plugin" + ChatColor.GREEN +" Test");
    4. return true;
    5. }
    6. return false;


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  20. Offline

    number1_Master

    Code:java
    1. Player player = (Player) sender; //this sets player as the sender
    2. //now instead of getServer().broadcast (...)
    3. player.sendMessage("This is my message!");
     
  21. Offline

    vYN

    Code:java
    1.  
    2. yay it works like a dream. but if i want to do it from an event..
    3.  
    4.  
    5. [CODE]public void onPlayerEggThrow(PlayerEggThrowEvent event)[/CODE]
    6.  
    7. just copied the top part of the event.
     
  22. Offline

    number1_Master

    then:
    Code:java
    1. Player player = event.getPlayer();
    2. player.sendMessage("Awesome Message! :D");
     
  23. Offline

    vYN

    Code:java
    1.  
    2. Thanks! it worked
     
    nunber1_Master likes this.
Thread Status:
Not open for further replies.

Share This Page