Adding Color Code Support To A Broadcast Message.

Discussion in 'Plugin Development' started by SleepyDog, Nov 4, 2014.

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

    SleepyDog

    Okay, i am new to java. This is what i need fixing. I am creating a personal /broadcast command to a little admin plugin i am making for my practice Dev server.

    This what i have so far (Just started)

    The color support i added is not working. Do you know how i can make it work? Thanks!
    Code:java
    1. package me.sleepydog935;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Color;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Admin extends JavaPlugin {
    10.  
    11. public void onEnable(){
    12. //loadConfig(); // Just here for later use //
    13.  
    14. }
    15.  
    16. public void onDisable() {
    17.  
    18.  
    19. }
    20.  
    21. public void loadConfig() {
    22.  
    23. getConfig().options().copyDefaults(true);
    24. saveConfig();
    25. return;
    26. }
    27.  
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    30.  
    31. if (cmd.getName().equalsIgnoreCase("broadcast")){
    32. String broadcastMessage = "";
    33. if (args.length > 0) broadcastMessage = args[0];
    34. if (!broadcastMessage.equals("")) {
    35. String broadcastMessageFinal = "&3 Broadcast &8» &7" + broadcastMessage;
    36. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', broadcastMessageFinal));
    37.  
    38. }
    39. else {
    40. sender.sendMessage("§c Error §8» §7" + " You must enter a message to broadcast!");
    41.  
    42. }
    43.  
    44. }
    45. return false;
    46.  
    47.  
    48. }
    49. }
     
  2. Offline

    TheCodingCat

    any errors in console or just no chat color. And btw SleepyDog I think you need to learn a little more java because the plugin you are working on seems a little over complex
     
  3. Offline

    SleepyDog

    [​IMG]
     
  4. Offline

    leon3001

    TheCodingCat A broadcast plugin? :eek: Fairly easy; I would say. Yeah, perfect for beginners in my opinion. :p
     
    SleepyDog likes this.
  5. Offline

    SleepyDog

    This plugin is for learning, i use the forums and pick other plugins apart. Learn how they work and how i can use them. Most people learn form reading the 1000000 page java books you can buy. I cant get/use one of those. I resulted to just learn as i develop.

    Any idea how to fix my problem?

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

    teej107

    import ChatColor
     
  7. Offline

    SleepyDog

    Do you know the import name, it's not showing.
     
  8. Offline

    teej107

    You're importing Bukkit's Color and you don't need that. You need to import ChatColor.
     
    leon3001 likes this.
  9. Offline

    SleepyDog

    I don't know the import name! When i hover over 'ChatColor' it is not showing
     
  10. Offline

    teej107

    Remove the Color import, look it up in the JavaDocs or Google because it's slipping my mind atm.
     
  11. Offline

    leon3001

    SleepyDog import org.bukkit.ChatColor; I believe.
     
  12. Offline

    SleepyDog

    Import not found
     
  13. Offline

    mythbusterma

    SleepyDog

    Are you certain Bukkit is on your build path?
     
  14. Offline

    SleepyDog

    yes
     
  15. Offline

    Barinade

    Add "" + before it
    ("" + ChatColor.transla... etc
     
  16. Offline

    Cheesepro

    SleepyDog
    You can take my code as a example :D hope this helps you...
    Every thing was very straight forward except the "for" loop for a beginner.
    *This class is a sub class so I don't have the onEnable and onDisable etc. contained in the Main
    Code:java
    1. package me.cheesepro.minefuturepl.mainpack;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class commandbroadcast implements CommandExecutor{
    11.  
    12. public boolean onCommand(CommandSender sender, Command cmd, String label,
    13. String[] args) {
    14. Player player = (Player) sender;
    15. if(sender instanceof Player){
    16. if(label.equalsIgnoreCase("broadcast")){
    17. String usrcmdinput="";
    18. for(int i=0;i<args.length;i++){
    19. usrcmdinput = usrcmdinput + args[i] + " ";
    20. }
    21. if(args.length>0){
    22. player.sendMessage(ChatColor.GRAY + "Message [ " +usrcmdinput +"] sent!");
    23. Bukkit.broadcastMessage(ChatColor.YELLOW + "[!]" + ChatColor.RESET + ChatColor.RED + "" + ChatColor.BOLD + "Broadcast" + ChatColor.RESET + ChatColor.YELLOW + "[!] " + usrcmdinput);
    24. }else if(args.length==0){
    25. player.sendMessage(ChatColor.YELLOW + "type /broadcast [Message]");
    26. }
    27. }
    28. }else{
    29. Bukkit.getLogger().info("Command executor must be a in-game player!");
    30. }
    31. return false;
    32. }
    33.  
    34. }
    35. [/i]
     
  17. Offline

    mythbusterma

    Barinade

    Why would you do that? There is not a single reason why you would do that, nor is it at all related to his error.
     
  18. Offline

    Barinade

    At a glance it seemed like the function calls for a String, which ChatColor isn't
     
  19. Offline

    Dudemister1999

    Barinade But with that, you would still be able to add the import.
     
  20. Offline

    mythbusterma

    Barinade

    So what do we do to fix that? You call toString() on it, not implicitly invoke it with empty quotes, which clutters up code.
     
  21. Offline

    Barinade

    I was thinking he might have already had it and the error was different. I literally just glanced at it and recalled past issues.


    Personal preference
     
  22. Offline

    SleepyDog



    Thanks for the help, it still doesn't fix my problem.
     
  23. Offline

    Barinade

    Show what you have now, make sure you're importing
    org.bukkit.ChatColor

    You have
    org.bukkit.Color
    imported, but that's different
     
  24. Offline

    SleepyDog

    i know
     
  25. Offline

    Barinade

     
  26. Offline

    SleepyDog

    I decided i dont want my staff using color boradcasts because it is often abused. I picked a nice light grey and used that. I am about to post another thread. Keep a eye out please:p
     
Thread Status:
Not open for further replies.

Share This Page