Solved Plugin Help (When a player does /advert 'MSG' it broadcast it)

Discussion in 'Plugin Development' started by Thunderbolt_316, Jul 20, 2016.

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

    Thunderbolt_316

    I want to make it so When a player does /advert 'MSG' it broadcast it like for example.
    '[Advert] NAME: My Message'

    Here is the format I'm using. I'm a bit new so please take it easy on me.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player p = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("advert")) {
                if (!p.hasPermission("crp.advert")) {
                    p.sendMessage(ChatColor.RED + "You don't have permission for this command.");
                    return true;
                }
              //THIS IS WHERE THE BROADCAST STUFF WOULD GO. Just confused on how to do it.
            }
    }
     
  2. Offline

    Thunderbolt_316

    Ok, so I got the broadcast thing done but, I gotta do it so it grabs when they do /advert Hello, world!
    Here's what i got so far
    Code:
    getServer().broadcastMessage(" " + ChatColor.translateAlternateColorCodes ('&', this.getConfig().getString("Prefix") + " ") + p.getDisplayName() + ChatColor.GRAY + ":" + ChatColor.WHITE + MESSAGE);
     
  3. Offline

    Tecno_Wizard

    @Thunderbolt_316 wow that's a mess. Have you ever used either StringBuilder or String#format? Both of them wll make your code a whole lot cleaner. Builder individually adds each piece of the string with append calls while format replaces all the variables with symbols in a template string and the variable arguments replace the symbols at runtime.

    ie.

    Code:
    // this is what you are doing
    Player.sendMessage(ChatColor.AQUA  + plugin.getPrefix() + ":"  + player.getName()  + " " + "someMessage");
    
    // and it could be this!
    Player.sendMessage(String.format("%s%s:%s %s", ChatColor.AQUA, plugin.getPrefix(), player.getName(), "someMessage"));
     
  4. Offline

    Thunderbolt_316

    No, I have not I'm new to JAVA and this is my first time.

    What I want as the message at the end is when they do '/advert Test' it'll be The Message that have like I put Test I want it to sya whatever they said.

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

    Dave Nathanael

    I would do something like this:

    Code:
    String message;
    for(String arg : args){
        message.concat(" ");
        message.concat(arg);
    }
    
    //loop through all players online and p.sendMessage(message);
    EDIT: just broadcast the message instead of looping
     
  6. Offline

    Thunderbolt_316

    [​IMG]
     
  7. Offline

    Dave Nathanael

    Try this instead sorry
    message = message.concat (" ");
    message = message.concat (arg);

    Sent from my GT-I9060 using Tapatalk
     
  8. Offline

    Thunderbolt_316

    Problem Solved! Thank you for everyone who helped!
     
Thread Status:
Not open for further replies.

Share This Page