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. 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. this.getServer().broadcastMessage("message");
  3. :O it worked Thanks!!!

    haha new problem xD on my getDisplayName it says "null Throws an egg!"
  4. Player p = event.getPlayer();

    this.getServer().broadcastMessage(p + " threw an egg!');
  5. 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. 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. then I think you imported the wrong Player :)
  8. hmm ok...

    Code (Text):
    1.         public void onPlayerEggThrow(PlayerEggThrowEvent event) {
    2.             Player p = event.getPlayer();
    3.             getServer().broadcastMessage(p + " Throws an egg!");
    4.             }
  9. make sure you imported:
    import org.bukkit.entity.Player;
  10. That is imported yes..
  11. ohh derp I am dumb.
    Code (Text):
    2. public void onPlayerEggThrow(PlayerEggThrowEvent event) {
    3.             String p = event.getPlayer().toString();
    4.             this.getServer().broadcastMessage(p + " Throws an egg!");
    5.         }
  12. when i'm adding "this." infront of getServer it gies me an error....

    Code (Text):
    1. The method getServer() is undefined for the type Main.AutoMSGPlayerListener
  13. just get rid of the this I just wrote it in a hurry :p
  14. hmm ok. for some reason it still gives me "CraftPlayer{name=GudfareN} Throws an egg!"

    i feel so noob at this lol xD
  15. 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. hehe ok. Thanks anyway. u got me a huge step forwards!
  17. 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 (Text):
    1. public void onPlayerEggThrow(PlayerEggThrowEvent event) {
    2.      String name = event.getPlayer().getName();
    3.      plugin.getServer().broadcastMessage(name + " threw an egg!");
    4. }
  18. ohh getName, herp-a-derp XD
    Have not had to use that before, it is now noted
  19. 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?

    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. 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. Code:java
    2. yay it works like a dream. but if i want to do it from an event..
    5. [CODE]public void onPlayerEggThrow(PlayerEggThrowEvent event)[/CODE]
    7. just copied the top part of the event.
  22. then:
    1. Player player = event.getPlayer();
    2. player.sendMessage("Awesome Message! :D");
  23. Code:java
    2. Thanks! it worked
    nunber1_Master likes this.
Thread Status:
Not open for further replies.

Share This Page