Broadcast Plugin | Need Help!

Discussion in 'Plugin Development' started by filurp, Sep 28, 2012.

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

    filurp

    At the moment, i have got the general layout of my plugin but i need to know how i can make the 'announce' command broadcast a message. (Example: /announce <message>)
    All help is much appreciated!



    Code:
    package com.filurp.fAnnouncer;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class fAnnouncer extends JavaPlugin {
        @Override
        public void onEnable() {
           
        }
       
        @Override
        public void onDisable() {
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("announce")) {
               
            }
            return false;
        }
    }
     
  2. Offline

    brord

  3. Offline

    filurp

  4. Offline

    Muddr

    getServer().broadcastMessage("Hello World");
     
  5. Offline

    jojohnson1

    Code:java
    1.  
    2.  
    3. //in your onCommand():
    4.  
    5. {
    6. broadcast("Hello");
    7. }
    8.  
    9. //Then create a method in your class:
    10.  
    11. public void broadcast(String msg) {
    12.  
    13. if (msg != null) {
    14. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "[Plugin] " + ChatColor.RED + "-Announcement- " + ChatColor.YELLOW + msg);
    15. }
    16.  
    17. }
    18.  
    19. [/syntax]
    20.  
    21. EDIT: Just a little Mistake in Code :P[/Plugin]
     
  6. Offline

    filurp

     
  7. Offline

    jojohnson1

    Yes, you could, but if you want to do other commands that want to broadcast things, you just have to type broadcast("test"); and it will always be the same format, but yes, you could
     
  8. Offline

    filurp

    So will this work?


    Code:
    package com.filurp.fAnnouncer;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class fAnnouncer extends JavaPlugin {
        @Override
        public void onEnable() {
       
        }
     
        @Override
        public void onDisable() {
       
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("announce")) {
                Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "[Plugin] " + ChatColor.RED + "-Announcement- " + ChatColor.YELLOW + msg);
           
            }
            return false;
        }
     
  9. Offline

    jojohnson1

    yes, it should, except that you have to replace msg with the arguments given after the Command. i hope you know how to make a String Builder, if not, tell me, i'll send you one. Then you would have to insert a String msg = //words after commands...
    So you can't just say broadcast(msg); because the Server doesn't know what th hell "msg" shall be.

    But have you ever heard of a SenderNotPlayerException? if the Command is sent by console there might be an Error if you change something on a player like his inventory. I'm not a Pro, but it is always better if you insert a player check, so only players can use the command ingame:

    Code:java
    1.  
    2. if (sender instanceof Player) {
    3. //continue with your code
    4. }else {
    5. System.out.println("[Plugin] This command must be run as a player!");
    6. }
    7. [/syntax]
    8.  
    9. Or it might be useful to see how long the announcement is, like if you just type /announce everybody would get a Message like:
    10. [COLOR=#99cc00][Plugin] [COLOR=#ff0000]-Announcement- [/COLOR][/COLOR]
    11. but nothing afterwards.
    12.  
    13. So you could insert the following code:
    14.  
    15. [syntax=java]
    16. //checks whether the player has typed 1 or more words (words after
    17. if (args.length >= 1) { // args should be in your thread, like onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) ...
    18. //send announcemnet
    19. }else {
    20. sender.sendMessage("An Announcemt must contain at least 1 word!");
    21. }
    22. [/syntax]
    23.  
    24. So if you haven't heard of any of this stuff before, you should maybe have a look at this: [url]http://wiki.bukkit.org/Plugin_Tutorial[/url]
    25.  
    26. I Hope i could help you, if not, just reply on this.[/Plugin]
     
  10. Offline

    jojohnson1

    Hey Dude,

    I worked out a Code which really works, and it looks like this:

    Code:java
    1.  
    2. if (sender instanceof Player) {
    3.  
    4. if (c.equalsIgnoreCase("bc")) {
    5.  
    6. if (p.isOp() || p.hasPermission(new Permission("lexi.chat.bc"))) {
    7.  
    8. if (args.length >= 1) {
    9.  
    10. String msg = "";
    11. for (int i = 0; i < args.length; i++) {
    12. msg = msg + args[i] + " ";
    13. }
    14.  
    15. Bukkit.broadcastMessage(ChatColor.RED + "[Broadcast] " + ChatColor.WHITE + msg); return true;
    16.  
    17. }else {
    18. p.sendMessage(ChatColor.RED + "Wrong Syntax: A message must contain at least 1 word!");
    19. return false;
    20. }
    21.  
    22. }else {
    23. p.sendMessage(ChatColor.RED + "You do not Permissions to perform this Command."); return true;
    24. }
    25.  
    26. }
    27. }else {
    28. System.out.println("[Plugin] This Command must be executed by an ingame Player");
    29. }
    30. [/syntax][/i][/Plugin][/i]
     
Thread Status:
Not open for further replies.

Share This Page