Plugin Help - Broadcast a message to all players

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

  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
     
  7. hmm ok. now it says "CraftPlayer{name=GudfareN} Throws an egg!"
     
  8. then I think you imported the wrong Player :)
     
  9. hmm ok...

    Code (Text):
    1.         public void onPlayerEggThrow(PlayerEggThrowEvent event) {
    2.             Player p = event.getPlayer();
    3.             getServer().broadcastMessage(p + " Throws an egg!");
    4.             }
     
  10. make sure you imported:
    import org.bukkit.entity.Player;
     
  11. That is imported yes..
     
  12. ohh derp I am dumb.
    Code (Text):
    1.  
    2. public void onPlayerEggThrow(PlayerEggThrowEvent event) {
    3.             String p = event.getPlayer().toString();
    4.             this.getServer().broadcastMessage(p + " Throws an egg!");
    5.         }
    6.  
     
  13. 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
     
  14. just get rid of the this I just wrote it in a hurry :p
     
  15. hmm ok. for some reason it still gives me "CraftPlayer{name=GudfareN} Throws an egg!"

    i feel so noob at this lol xD
     
  16. 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
     
  17. hehe ok. Thanks anyway. u got me a huge step forwards!
     
  18. 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. }
     
  19. ohh getName, herp-a-derp XD
    Have not had to use that before, it is now noted
     
  20. Hehe that fixed it. Thanks for all the help.
     
  21. 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;
     
  22. 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!");
     
  23. 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.
     
  24. then:
    Code:java
    1. Player player = event.getPlayer();
    2. player.sendMessage("Awesome Message! :D");
     
  25. Code:java
    1.  
    2. Thanks! it worked
     
    nunber1_Master likes this.

Share This Page