Broadcast with color

Discussion in 'Plugin Development' started by jkctech, Oct 16, 2014.

Thread Status:
Not open for further replies.
  1. Hello,
    so im working on a broadcast plugin, but ive encountered some problems.
    i don't know how to set the message as one variable, since when you use spaces, it counts as another argument.
    also, i don't quite know how to add colors so that they can type "&ahello" as a broadcast, it changes to green.
    can you guys also add the possibility to get something like this from the config: "hello %player%" and change it to the player.
    i know this is a big thing, but i really need help with this.

    this is my code so far:
    Code:java
    1. package me.jkctech.BroodKast;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin implements Listener {
    13.  
    14. public void loadConfiguration(){
    15. saveDefaultConfig();
    16. }
    17.  
    18. @Override
    19. public void onEnable(){
    20. PluginDescriptionFile ymlFile = this.getDescription();
    21. this.getLogger().info(ymlFile.getName() + " Versie " + ymlFile.getVersion() + " is geactiveerd!");
    22. this.getLogger().info("Deze plugin is gemaakt door JKCTech!");
    23. this.getServer().getPluginManager().registerEvents(this, this);
    24. loadConfiguration();
    25. }
    26. @Override
    27. public void onDisable(){
    28. PluginDescriptionFile ymlFile = this.getDescription();
    29. this.getLogger().info(ymlFile.getName() + ymlFile.getVersion() + " is nu uitgeschakeld!");
    30. }
    31.  
    32. @EventHandler
    33. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    34.  
    35. if(!(sender instanceof Player)){
    36. sender.sendMessage(ChatColor.GOLD+"[JKC-BroodKast] "+ChatColor.RED+"Je moet in-game zijn om dit te doen!");
    37. return false;
    38. }
    39. final Player p = (Player) sender;
    40. if(label.equalsIgnoreCase("kast")){
    41. if(p.hasPermission("broodkast.send") || p.isOp()){
    42. if(args.length == 0){
    43.  
    44. String toTranslate = "thing from config";
    45. String translated = ChatColor.translateAlternateColorCodes('§', toTranslate);
    46. String totaal = translated.replaceAll("Å", "");
    47.  
    48. } else {
    49.  
    50. }
    51. } else {
    52. p.sendMessage(ChatColor.GOLD+"[JKC-BroodKast] "+ChatColor.RED+"Je hebt geen toegang tot die command!");
    53. }
    54. }
    55. return true;
    56. }
    57. }
     
  2. Offline

    FerusGrim

    Code:
    ChatColor.translateAlternateColorCodes('&', message);
    Code:
    for (Player player : getServer().getOnlinePlayers()) {
        message.replace("%player%", player.getDisplayName());
        getServer().broadcast(player, message);
    }
     
  3. Thanks!
    i will try that.
     
  4. Offline

    fireblast709

    FerusGrim that does broadcast the message for every player. You might want to use sendMessage(String) instead ;3.
     
    ChipDev likes this.
  5. Offline

    FerusGrim


    Thanks. :3 Didn't feel like looking at the Docs~
     
  6. Offline

    ChipDev

    Err. no :p
    Code:java
    1. getServer().broadcastMessage(String s);
    2. // not
    3. getServer().broadcast(Player p, String s);

    Or use per player:
    Code:java
    1. player.sendMessage(String s);

    like fireblast709 said!
     
  7. Offline

    FerusGrim

    You're correct. :) As has already been pointed out. :p
     
  8. But still, what do with the multiple arguments problem?
    how do i get it as 1 argument and place it in the broadcast?

    Never mind, i used this method:
    Code:java
    1. String berichtInput = "";
    2.  
    3. for(int i = 0; i < args.length; i++){
    4. String arg = args[i] + " ";
    5. berichtInput = berichtInput + arg;
    6. }
    7. String bericht = ChatColor.translateAlternateColorCodes('&', berichtInput);[/i]


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  9. Offline

    Panjab

    jkctech

    Use the given StringBuilder

    Code:java
    1.  
    2. StringBuilder berichtInput = new StringBuilder();
    3.  
    4. for (int i = 1; i < args.length; i++) {
    5. if (berichtInput.length() > 0)
    6. berichtInput.append(" ");
    7.  
    8. berichtInput.append(args[i]);
    9. }
    10.  
    11. String bericht = ChatColor.translateAlternateColorCodes('&', berichtInput.toString());[/i]
     
Thread Status:
Not open for further replies.

Share This Page