Adding Color Support

Discussion in 'Plugin Development' started by dingus007, Sep 10, 2013.

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

    dingus007

    So I found this from another thread on Bukkit.org but it doesn't work and I don't know how to fix it.
    I am calling this function when appending multiple arguments to a string, it doesn't replace or do anything.

    Code:java
    1. public static String colorize(String s){
    2. if(s == null) return null;
    3. return s.replaceAll("&([0-9a-f])", "\u00A7$1");
    4. }
     
  2. Offline

    Tirelessly

    ChatColor.translateAlternateColorCodes('&', string);
     
  3. Offline

    iiHeroo


    This helped me xD.
     
  4. Offline

    dingus007

    The player says it anywhere in the sentence and there's no way to get it.
     
  5. Offline

    Tirelessly

    What are you saying?
     
  6. Offline

    iiHeroo


    okay question, how am I suppose to make it look ?
    This is how I have it

    Code:java
    1. public static String colorize(String s){
    2. if(s == null) return null;
    3. return s.replaceAll("&([0-9a-f])", "\u00A7$1");
    4. ChatColor.translateAlternateColorCodes('&', string);
    5. }
    6.  
     
  7. Offline

    uyuyuy99

    It's much simpler than that:
    Code:java
    1. public static String colorize(String str) {
    2. return ChatColor.translateAlternateColorCodes('&', str);
    3. }
     
  8. Offline

    iiHeroo


    Would if you want color codes to work in the config.yml that are send player messages. For example the config says

    Birth: &4Day

    It'd send the player the message "Day" in Dark_Red.
     
  9. Offline

    dingus007

    So I'm making a command that can say something to everyone, but have color support. I want people to be able to put color codes anywhere inside of the string and still have them affected. So you say

    /tellus everyone &lThis is a test &3Hi

    and it broadcasts

    This is a test Hi
     
  10. Offline

    uyuyuy99

    iiHeroo That's exactly what the function is for.
    dingus007 Just combine all the arguments of the command into a single string, and use the function I made (seen above).
     
  11. Offline

    iiHeroo


    Didn't work, will need to look into this :3 Does it matter where I type this tbh ? Because this is how I have it
    Code:java
    1. package server.stuff;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class info extends JavaPlugin{
    12.  
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14. public static info plugin;
    15. public static String colorize(String str) {
    16. return ChatColor.translateAlternateColorCodes('&', str);
    17. }
    18.  
    19.  
    20. @Override
    21. public void onDisable(){
    22. this.logger.info("[ServerInfoPlus] ServerInfoPlus v1.4.3 by iiHeroo is being disabled !");
    23. this.logger.warning("[ServerInfoPlus] ServerInfoPlus v1.4.3 by iiHeroo is being disabled !");
    24.  
    25. }
    26.  
    27. @Override
    28. public void onEnable(){
    29. this.logger.info("[ServerInfoPlus] ServerInfoPlus v1.4.3 by iiHeroo is being enabled !");
    30. this.logger.warning("[SeverInfoPlus] ServerInfoPlus v1.4.3 by iiHeroo is being enabled !");
    31. getConfig().options().copyDefaults(true);
    32. saveConfig();
    33.  
    34. }
    35. String var1 = "Skype: ";
    36. String var2 = "Facebook: ";
    37. String var3 = "Twitter: ";
    38. String var4 = "Youtube: ";
    39. String var5 = "Website: ";
    40. String var6 = "IP: ";
    41. String var7 = "Open: ";
    42. String var8 = "by iiHeroo ";
    43. String var9 = "v1.4.5 ";
    44. String var10 = "Server Info Plus ";
    45. String var11 = "/ip";
    46. String var12 = "/website";
    47. String var13 = "/facebook";
    48. String var14 = "/twitter";
    49. String var15 = "/youtube";
    50. String var16 = "/skype";
    51. String var17 = "Tell's you the server IP. ";
    52. String var18 = "Tell's you the server website. ";
    53. String var19 = "Tell's you the Facebook Page of the server. ";
    54. String var20 = "Tell's you the Twitter account of the owner. ";
    55. String var21 = "Tell's you the Youtube account of the owner. ";
    56. String var22 = "Tells's you the Owner's Skype account. ";
    57. String var23 = " - ";
    58. String var24 = " /ip";
    59. String var25 = "/open";
    60. String var26 = "Tell's you when the server opened ";
    61. String var27 = " /trialmods";
    62. String var28 = "/mods";
    63. String var29 = "/admins";
    64. String var30 = "/superadmins";
    65. String var31 = "/headmod";
    66. String var32 = "/headadmin";
    67. String var33 = "/coowner";
    68. String var34 = "/owner";
    69. String var35 = " - ";
    70. String var36 = "Tell's you the TrialMods of the server. ";
    71. String var37 = "Tell's you the Mods of the server.. ";
    72. String var38 = "Tell's you the Admins of the server. ";
    73. String var39 = "Tell's you the Super-Admins of the server. ";
    74. String var40 = "Tell's you the Head-Mod of the server. ";
    75. String var41 = "Tell's you the Head-Admin of the server. ";
    76. String var42 = "Tell's you the Co-Owner of the server ";
    77. String var43 = "Tell's you the Owner of the server. ";
    78. String var44 = "/donate";
    79. String var45 = "/email";
    80. String var46 = "Tell's you how to donate ";
    81. String var47 = "Tell's you the email of the server or owner ";
    82. String var48 = "/twitch";
    83. String var49 = "Tell's you the Owner's twitch channel. ";
    84.  
    85.  
    86. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    87. Player player = (Player) sender;
    88. if(commandLabel.equalsIgnoreCase("Skype"))
    89. if(player.hasPermission("serverinfoplus.skype"))
    90. player.sendMessage(getConfig().getString("Skype"));
    91. if(commandLabel.equalsIgnoreCase("Facebook"))
    92. if(player.hasPermission("serverinfoplus.facebook"))
    93. player.sendMessage(getConfig().getString("Facebook"));
    94. if(commandLabel.equalsIgnoreCase("Twitter"))
    95. if(player.hasPermission("serverinfoplus.twitter"))
    96. player.sendMessage(getConfig().getString("Twitter"));
    97. if(commandLabel.equalsIgnoreCase("YouTube"))
    98. if(player.hasPermission("serverinfoplus.youtube"))
    99. player.sendMessage(getConfig().getString("Youtube"));
    100. if(commandLabel.equalsIgnoreCase("Website"))
    101. if(player.hasPermission("serverinfoplus.website"))
    102. player.sendMessage(getConfig().getString("Website"));
    103. if(commandLabel.equalsIgnoreCase("IP"))
    104. if(player.hasPermission("serverinfoplus.ip"))
    105. player.sendMessage(getConfig().getString("IP"));
    106. if(commandLabel.equalsIgnoreCase("Open"))
    107. if(player.hasPermission("serverinfoplus.open"))
    108. player.sendMessage(getConfig().getString("Open"));
    109. if(commandLabel.equalsIgnoreCase("sip"))
    110. if(args.length == 1) {
    111. if(args[0].equalsIgnoreCase("1"))
    112. if(player.hasPermission("serverinfoplus.help"))
    113. player.sendMessage(ChatColor.BLUE + var10 + ChatColor.GREEN + var9 + ChatColor.GOLD + var8 + ChatColor.AQUA + var16 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var22 + ChatColor.AQUA + ChatColor.AQUA + var24 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var17 + ChatColor.AQUA + var13 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var19 + ChatColor.AQUA + var14 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var20 + ChatColor.AQUA + var15 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var21 + ChatColor.AQUA + var25 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var26 + ChatColor.AQUA + var12 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var18 );
    114. if(args[0].equalsIgnoreCase("2"))
    115. if(player.hasPermission("serverinfoplus.help"))
    116. player.sendMessage(ChatColor.BLUE + var10 + ChatColor.GREEN + var9 + ChatColor.GOLD + var8 + ChatColor.AQUA + var27 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var36 + ChatColor.AQUA + var28 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var37 + ChatColor.AQUA + var29 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var38 + ChatColor.AQUA + var30 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var39 + ChatColor.AQUA + var31 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var40 + ChatColor.AQUA + var32 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var41 + ChatColor.AQUA + var33 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var42 + ChatColor.AQUA + var34 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var43);
    117. if(args[0].equalsIgnoreCase("3"))
    118. if(player.hasPermission("serverinfoplus.help"))
    119. player.sendMessage(ChatColor.BLUE + var10 + ChatColor.GREEN + var9 + ChatColor.GOLD + var8 + ChatColor.AQUA + var44 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var46 + ChatColor.AQUA + var45 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var47 + ChatColor.AQUA + var48 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var49 );
    120. }
    121. if(commandLabel.equalsIgnoreCase("mods"))
    122. if(player.hasPermission("serverinfoplus.mods"))
    123. player.sendMessage(getConfig().getString("Mods"));
    124. if(commandLabel.equalsIgnoreCase("admins"))
    125. if(player.hasPermission("serverinfoplus.admins"))
    126. player.sendMessage(getConfig().getString("Admins"));
    127. if(commandLabel.equalsIgnoreCase("superadmins"))
    128. if(player.hasPermission("serverinfoplus.superadmins"))
    129. player.sendMessage(getConfig().getString("Superadmins"));
    130. if(commandLabel.equalsIgnoreCase("headmmod"))
    131. if(player.hasPermission("serverinfoplus.headmod"))
    132. player.sendMessage(getConfig().getString("Headmod"));
    133. if(commandLabel.equalsIgnoreCase("headadmin"))
    134. if(player.hasPermission("serverinfoplus.headadmin"))
    135. player.sendMessage(getConfig().getString("Headadmin"));
    136. if(commandLabel.equalsIgnoreCase("coowner"))
    137. if(player.hasPermission("serverinfoplus.coowner"))
    138. player.sendMessage(getConfig().getString("Coowner"));
    139. if(commandLabel.equalsIgnoreCase("owner"))
    140. if(player.hasPermission("serverinfoplus.owner"))
    141. player.sendMessage(getConfig().getString("Owner"));
    142. if(commandLabel.equalsIgnoreCase("trialmods"))
    143. if(player.hasPermission("serverinfoplus.trialmods"))
    144. player.sendMessage(getConfig().getString("Trialmods"));
    145. if(commandLabel.equalsIgnoreCase("email"))
    146. if(player.hasPermission("serverinfoplus.email"))
    147. player.sendMessage(getConfig().getString("Email"));
    148. if(commandLabel.equalsIgnoreCase("donate"))
    149. if(player.hasPermission("serverinfoplus.donate"))
    150. player.sendMessage(getConfig().getString("Donate"));
    151. if(commandLabel.equalsIgnoreCase("twitch"))
    152. if(player.hasPermission("serverinfoplus.twitch"))
    153. player.sendMessage(getConfig().getString("Twitch"));
    154.  
    155.  
    156. return false;
    157. }
    158.  
    159.  
    160. }
     
  12. Offline

    uyuyuy99

    iiHeroo What isn't working, exactly? Any errors? Can you show me the code?

    Anyways, the placement of the function is fine where you put it. And not to change the subject, but you don't need either of those logger messages in the onDisable function - Bukkit logs the enabling/disabling of plugins on its own.
     
  13. Offline

    dingus007

    That's what I do, but your method won't work since I'm allowing the players to change the color anywhere in the string.
     
  14. Offline

    uyuyuy99

    What do you think this method is for? :p
     
Thread Status:
Not open for further replies.

Share This Page