Can't send colored(or any) messages to console in onEnable() with ConsoleSender?

Discussion in 'Plugin Development' started by Brian_Entei, May 11, 2013.

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

    Brian_Entei

    Title is self-explanatory. I can't seem to get my plugin to send the console a message at all, colored or not. Here is my code, snipped a bit to hide the goods(the package and imports are in working order):



    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2. public static Main plugin;
    3. public final static Logger logger = Logger.getLogger("Minecraft");
    4.  
    5. public void LoginListener(Main JavaPlugin) {
    6. getServer().getPluginManager().registerEvents(this, plugin);
    7. }
    8. @Override
    9. public void onDisable() {PluginDescriptionFile pdffile = this.getDescription();
    10. logger.info(pdffile.getName() + " v" + pdffile.getVersion() + " is now disabled.");
    11. }
    12. @Override
    13. public void onEnable() {PluginDescriptionFile pdffile = this.getDescription();
    14. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    15. boolean sentAMsg = sendConsoleAMsg(ChatColor.DARK_RED + "Test with color");
    16. logger.info("--------------------------------------------------------------------");
    17. logger.info("Message function returned: " + sentAMsg);
    18. logger.info("--------------------------------------------------------------------");
    19. boolean sentAMsg = sendConsoleAMsg("Test without color");
    20. logger.info("--------------------------------------------------------------------");
    21. logger.info("Second message returned: " + sentAMsg);
    22. logger.info("--------------------------------------------------------------------");
    23. }
    24. public static boolean sendConsoleAMsg(String msg) {
    25. try {
    26. Bukkit.getServer().getConsoleSender().sendMessage(msg);
    27. return true;
    28. } catch (NullPointerException e) {
    29. FileMgmt.LogCrash(e, e.getMessage(), false);
    30. }
    31. return false;
    32. }
    33. }


    I CAN, however, use
    Code:java
    1. sendConsoleAMsg(ChatColor.DARK_RED + "Hello, world!");

    in any other event or function/method. So why can't I use this in onEnable()?
     
  2. Offline

    chasechocolate

    Try just:
    Code:java
    1. this.getLogger().info(ChatColor.RED + "Color test.");
     
  3. Offline

    Brian_Entei

    That just prints the message in white with funny characters, and I can't install anything that changes the way cmd.exe runs. Any other ideas?
     
  4. Offline

    RROD

    The Console Logger is not able to accept ChatColor input. You can only add ChatColor to CommandSender.sendMessage(), which is accessible via onCommand() and through getServer().

    So in your Plugin class you can call:
    Code:java
    1.  
    2. this.getServer().getConsoleSender().sendMessage(message)
    3.  


    The only downside to this is (as far as I'm aware), it will not write a message in the log file. It will only print it to the Console.

    EDIT: I didn't read that part x3 Seems you figured that out on your own :p
     
  5. Offline

    Brian_Entei

    That code raises a null pointer exception:

    Code:
    13:33:15 [SEVERE] Error occurred while enabling EnteisAutoBroadcast v4.5 (Is it up to date?)
    java.lang.NullPointerException
            at com.gmail.br45entei.enteisautobroadcast.Main.onEnable(Main.java:122)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:383)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugin(CraftServer.java:306)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.enablePlugins(CraftServer.java:288)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.<init>(CraftServer.java:242)
            at net.minecraft.server.v1_5_R3.PlayerList.<init>(PlayerList.java:55)
            at net.minecraft.server.v1_5_R3.DedicatedPlayerList.<init>(SourceFile:11)
            at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.java:95)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:388)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    That's why I made the sendConsoleAMsg function to handle null pointer exceptions. I have no idea why it throws a nullPointerException, it just does. The line you provided is on line 122.
     
  6. maybe the console isn't set up yet when the server is starting
     
  7. Brian_Entei
    And which is line 122 ? Because your posted code only has 33 lines.
     
  8. Offline

    Brian_Entei

    ferrybig, That's probably why, but then how the heck do other plugins log colored messages while my server starts? BattleArena does this for sure, and I just would like to know how to too.

    Digi, I said before that I cut all the goods out of the code I posted, I was saying that

    Code:java
    1. this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Color test");
    was on line 122.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  9. Brian_Entei
    Well... that posted code has alot of other issues and syntax errors... so this is why I ask: is that code used in your main class which is linked in the plugin.yml ?

    Because if you extend JavaPlugin in another class that is NOT the main class then your getServer() method for example will return null, you have to pass the plugin instance over to use stuff from the main class.

    And this code:
    Code:
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Color test");
    ...placed in onEnable() from a main class works properly, I've just tested it.
     
  10. Offline

    Brian_Entei

    Well, the code I use getConsoleSender() in is linked in the plugin.yml, BUT, do you have
    Code:
    load: STARTUP
    in your plugin.yml? I think that's why I get the exception. And what syntax errors? The code runs smoothly.

    Also, if a class isn't the one specified in the plugin.yml, then DON'T make it extend JavaPlugin? Is that what you meant? If so, thanks, I didn't know that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  11. Brian_Entei
    I've also tested by using it in onLoad() (which is triggered before onEnable) along with load: STARTUP, it works just fine!
    And yes, that's what I meant, only the main class (that is linked in plugin.yml) should extend JavaPlugin, any other class extending that would be broken.
     
  12. Offline

    Brian_Entei

    Well, no matter what I try, I always get the nullpointerexception that I posted above, so I'll copy my onEnable() function and the other functions related to it that are in the error(which I'm posting a fresh copy of here too).

    First part of Main class:
    Show Spoiler
    Code:java
    1. package com.gmail.br45entei.enteiscommands;
    2.  
    3. import com.gmail.br45entei.enteiscommands.Broadcast;
    4. import com.gmail.br45entei.enteiscommands.ChatMgmt;
    5. import com.gmail.br45entei.enteiscommands.FileMgmt;
    6. import com.gmail.br45entei.enteiscommands.CommandMgmt;
    7.  
    8. import java.io.File;
    9. import java.io.IOException;
    10. import java.util.Iterator;
    11. import java.util.List;
    12. import java.util.logging.Logger;
    13.  
    14. import net.milkbowl.vault.chat.Chat;
    15.  
    16. import org.bukkit.Bukkit;
    17. import org.bukkit.ChatColor;
    18. import org.bukkit.Chunk;
    19. import org.bukkit.Location;
    20. import org.bukkit.Material;
    21. import org.bukkit.World;
    22. import org.bukkit.block.Biome;
    23. import org.bukkit.block.Block;
    24. import org.bukkit.command.Command;
    25. import org.bukkit.command.CommandSender;
    26. import org.bukkit.command.ConsoleCommandSender;
    27. import org.bukkit.configuration.file.FileConfiguration;
    28. import org.bukkit.configuration.file.YamlConfiguration;
    29. import org.bukkit.enchantments.Enchantment;
    30. import org.bukkit.entity.Item;
    31. import org.bukkit.entity.Player;
    32. import org.bukkit.event.EventHandler;
    33. import org.bukkit.event.EventPriority;
    34. import org.bukkit.event.Listener;
    35. import org.bukkit.event.block.Action;
    36. import org.bukkit.event.player.AsyncPlayerChatEvent;
    37. import org.bukkit.event.player.PlayerChangedWorldEvent;
    38. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    39. import org.bukkit.event.player.PlayerDropItemEvent;
    40. import org.bukkit.event.player.PlayerInteractEvent;
    41. import org.bukkit.event.player.PlayerJoinEvent;
    42. import org.bukkit.event.player.PlayerQuitEvent;
    43. import org.bukkit.event.server.ServerCommandEvent;
    44. import org.bukkit.inventory.ItemStack;
    45. import org.bukkit.plugin.PluginDescriptionFile;
    46. import org.bukkit.plugin.RegisteredServiceProvider;
    47. import org.bukkit.plugin.java.JavaPlugin;
    48.  
    49. public class Main extends JavaPlugin implements Listener {
    50. private static Main plugin;
    51. private static final Logger logger = Logger.getLogger("Minecraft");
    52. public static final String rwhite = ChatMgmt.rwhite;public static final ChatColor aqua = ChatMgmt.aqua;public static final ChatColor black = ChatMgmt.black;public static final ChatColor blue = ChatMgmt.blue;public static final ChatColor bold = ChatMgmt.bold;public static final ChatColor daqua = ChatMgmt.daqua;public static final ChatColor dblue = ChatMgmt.dblue;public static final ChatColor dgray = ChatMgmt.dgray;public static final ChatColor dgreen = ChatMgmt.dgreen;public static final ChatColor dpurple = ChatMgmt.dpurple;public static final ChatColor dred = ChatMgmt.dred;public static final ChatColor gold = ChatMgmt.gold;public static final ChatColor gray = ChatMgmt.gray;public static final ChatColor green = ChatMgmt.green;public static final ChatColor italic = ChatMgmt.italic;public static final ChatColor lpurple = ChatMgmt.lpurple;public static final ChatColor magic = ChatMgmt.magic;public static final ChatColor red = ChatMgmt.red;public static final ChatColor reset = ChatMgmt.reset;public static final ChatColor striken = ChatMgmt.striken;public static final ChatColor underline = ChatMgmt.underline;public static final ChatColor white = ChatMgmt.white;public static final ChatColor yellow = ChatMgmt.yellow;
    53. public static String pluginName = rwhite + "[" + dred + "Entei's Commands" + rwhite + "] ";
    54. public static String enchants = "&2&zListing applicable enchantments:&z&3Armour:0-7&z&3Swords:16-21 & 34&z&3Tools:32-35&z&3Bows:48-51 & 34";
    55. public static boolean startUpBroadcasts = false;
    56.  
    57. public static File noDropListFile = null;
    58. public static String NoDropFileName = "UnDroppableItems.yml";
    59. public static FileConfiguration noDropList;
    60. public static boolean gotDisallowedItemList = false;
    61. public static String disAllowedItems = "";
    62.  
    63. //To be loaded from config.yml
    64. public static String noRank = "";
    65. public static String noAccess = "";
    66. public static String noPerm = "";
    67. public static String broadcastDisplay = "";
    68. public static String announcementDisplay = "";
    69. public static String playerOnly = "";
    70. public static String dataFolderName = "";
    71.  
    72. public static boolean showDebugMsgs = false;
    73.  
    74. public static boolean logChats = false;
    75. public static boolean showChatDebugMessages = false;
    76.  
    77. public static boolean enableChatManagement = false;
    78. public static String chatSayFormat = "";
    79. public static boolean enableChatFormat = false;
    80.  
    81. public static boolean enableCustomNicks = false;
    82.  
    83. public static String consoleSayFormat = "";
    84. public static boolean useConsoleSayFormat = false;
    85.  
    86. public static String chatMsgTooSimilar = "";
    87. public static boolean useTooSimilarMsg = false;
    88. public static boolean enableTooSimilarFilter = false;
    89.  
    90. public static String chatterCussed = "";
    91. public static boolean useChatterCussedMsg = false;
    92.  
    93. public static String chatterCussedConsoleMsg = "";
    94. public static boolean useCussConsoleMsg = false;
    95.  
    96. public static String blockedLinkMsg = "";
    97. public static boolean showBlockedLinkMsg = false;
    98. public static boolean blockLinkMessages = false;
    99.  
    100. public static boolean enableDropList = false;
    101. public static boolean enableDropDebugMsgs = false;
    102. public static boolean enableUserDropMsgs = false;
    103. public static String noDropMessage = "";
    104. public static boolean enableDropConsoleMsgs = false;
    105. public static String noDropConsoleMsg = "";
    106. public static String allowDropConsoleMsg = "";
    107.  
    108. public static String noSpamMessage = "";
    109. public static boolean enableSpamFilter = false;
    110. public static int charPercentageLimit = 0;
    111. //End of To be loaded from config.yml
    112. public static ConsoleCommandSender console = null;
    113.  
    114. public void LoginListener(Main JavaPlugin) {
    115. getServer().getPluginManager().registerEvents(this, plugin);
    116. }
    117. @Override
    118. public void onDisable() {PluginDescriptionFile pdffile = this.getDescription();
    119. logger.info(pdffile.getName() + " v" + pdffile.getVersion() + " is now disabled.");
    120. }
    121. @Override
    122. public void onEnable() {PluginDescriptionFile pdffile = this.getDescription();
    123. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    124. console = getServer().getConsoleSender();
    125. File dataFolder = getDataFolder();
    126. if(!(dataFolder.exists())) {
    127. dataFolder.mkdir();
    128. }
    129. try{dataFolderName = getDataFolder().getAbsolutePath();} catch (SecurityException e) {FileMgmt.LogCrash(e, null,true);}
    130. if(showDebugMsgs) {ChatMgmt.sendConsoleMessage(pluginName + "The dataFolderName variable is: \"" + dataFolderName + "\"!");}
    131. noDropListFile = new File(dataFolderName, NoDropFileName);
    132. noDropList = new YamlConfiguration();
    133. try {
    134. loadYamlFiles();
    135. } catch (Exception e) {
    136. e.printStackTrace();
    137. }
    138. loadYamls();
    139. gotDisallowedItemList = loadNonDropList();
    140. /*if(gotDisallowedItemList == true) {
    141. ChatMgmt.sendConsoleMessage(pluginName + "Using Un-droppable items list from file: " + disAllowedItems);
    142. }*/
    143. /*try{
    144. newline = System.getProperty("line.separator");
    145. } catch (NullPointerException e) {
    146. FileMgmt.LogCrash(e, e.getMessage(), true);
    147. }*/
    148. this.saveDefaultConfig();
    149. loadConfig();
    150. if(true) {
    151. boolean vaultAvailable = false;
    152. RegisteredServiceProvider<Chat> chatProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
    153. if (chatProvider != null) {
    154. ChatMgmt.chat = chatProvider.getProvider();
    155. } else if(showDebugMsgs == true) {
    156. ChatMgmt.sendConsoleMessage(pluginName + dred + "Could not load chat service... Chat formatting will not be available.(No Vault Plugin, or coding issue?)");
    157. }
    158. if (Bukkit.getServer().getPluginManager().getPlugin("Vault") != null) {
    159. vaultAvailable = true;
    160. }
    161. if(vaultAvailable == true) {
    162. if(ChatMgmt.chat != null) {
    163. if(showDebugMsgs == true) {ChatMgmt.sendConsoleMessage(pluginName + green + "Vault detected! Chat formatting will be available.");}
    164. ChatMgmt.useVaultChat = true;
    165. } else {
    166. if(showDebugMsgs == true) {ChatMgmt.sendConsoleMessage(pluginName + yellow + "Internal variable \"chat\" returned false. Chat formatting will not be available.(No Vault Plugin, or coding issue?)");}
    167. ChatMgmt.useVaultChat = false;
    168. }
    169. } else {
    170. if(showDebugMsgs == true) {ChatMgmt.sendConsoleMessage(pluginName + dred + "Vault not detected; Chat formatting will be disabled.");}
    171. ChatMgmt.useVaultChat = false;
    172. }
    173. }
    174. ChatMgmt.sendConsoleMessage(pluginName + gold + "version " + gold + pdffile.getVersion() + gold + " is now enabled!");
    175. startBroadcast();
    176. startUpBroadcasts = true;
    177. }


    First part of ChatMgmt class(has the function that sends the message to the console)
    Show Spoiler
    Code:java
    1. package com.gmail.br45entei.enteiscommands;
    2.  
    3. import com.gmail.br45entei.enteiscommands.Main;
    4. import com.gmail.br45entei.enteiscommands.FileMgmt;
    5.  
    6. import java.io.File;
    7. import java.io.IOException;
    8. import java.util.ArrayList;
    9. import java.util.Iterator;
    10. import java.util.List;
    11. import java.util.regex.Matcher;
    12. import java.util.regex.Pattern;
    13. import java.util.regex.PatternSyntaxException;
    14.  
    15. import net.milkbowl.vault.chat.Chat;
    16.  
    17. import org.bukkit.Bukkit;
    18. import org.bukkit.ChatColor;
    19. import org.bukkit.entity.Player;
    20.  
    21. public class ChatMgmt {
    22. public static ChatMgmt plugin;
    23. public static Chat chat = null;//Leave this here!
    24. public static boolean useVaultChat = false;
    25. public static boolean madeChatMsg = false;
    26. public static List<String> lastPlayerMessage = new ArrayList<String>();
    27. public static int lastPlayerInt = 0;
    28. public static final int chatCache = 200;
    29. public static List<String> lastPlayerMsgPlayer = new ArrayList<String>();
    30. public static final String rwhite = ChatColor.RESET+""+ChatColor.WHITE;public static final ChatColor aqua = ChatColor.AQUA;public static final ChatColor black = ChatColor.BLACK;public static final ChatColor blue = ChatColor.BLUE;public static final ChatColor bold = ChatColor.BOLD;public static final ChatColor daqua = ChatColor.DARK_AQUA;public static final ChatColor dblue = ChatColor.DARK_BLUE;public static final ChatColor dgray = ChatColor.DARK_GRAY;public static final ChatColor dgreen = ChatColor.DARK_GREEN;public static final ChatColor dpurple = ChatColor.DARK_PURPLE;public static final ChatColor dred = ChatColor.DARK_RED;public static final ChatColor gold = ChatColor.GOLD;public static final ChatColor gray = ChatColor.GRAY;public static final ChatColor green = ChatColor.GREEN;public static final ChatColor italic = ChatColor.ITALIC;public static final ChatColor lpurple = ChatColor.LIGHT_PURPLE;public static final ChatColor magic = ChatColor.MAGIC;public static final ChatColor red = ChatColor.RED;public static final ChatColor reset = ChatColor.RESET;public static final ChatColor striken = ChatColor.STRIKETHROUGH;public static final ChatColor underline = ChatColor.UNDERLINE;public static final ChatColor white = ChatColor.WHITE;public static final ChatColor yellow = ChatColor.YELLOW;
    31. public static final String getPunctuationChars = "\\p{Punct}+";
    32. public static final String getWhiteSpaceChars = "\\s+"/*"\\p{Space}+"*/;
    33. public static final String getAlphaNumericChars = "\\p{Alnum}+";
    34. public static final String getAlphabetChars = "\\p{Alpha}+";
    35. public static final String getNumberChars = "\\p{Digit}+";
    36. public static final String getUpperCaseChars = "\\p{Lower}+";
    37. public static final String getLowerCaseChars = "\\p{Upper}+";
    38. public static String formatColorCodes(String msg) {msg = msg.replaceAll("&w", white + "");msg = msg.replaceAll("&_", rwhite);msg = msg.replaceAll("&b", aqua + "");msg = msg.replaceAll("&0", black + "");msg = msg.replaceAll("&9", blue + "");msg = msg.replaceAll("&l", bold + "");msg = msg.replaceAll("&3", daqua + "");msg = msg.replaceAll("&1", dblue + "");msg = msg.replaceAll("&8", dgray + "");msg = msg.replaceAll("&2", dgreen + "");msg = msg.replaceAll("&5", dpurple + "");msg = msg.replaceAll("&4", dred + "");msg = msg.replaceAll("&6", gold + "");msg = msg.replaceAll("&7", gray + "");msg = msg.replaceAll("&a", green + "");msg = msg.replaceAll("&o", italic + "");msg = msg.replaceAll("&d", lpurple + "");msg = msg.replaceAll("&k", magic + "");msg = msg.replaceAll("&c", red + "");msg = msg.replaceAll("&m", striken + "");msg = msg.replaceAll("&n", underline + "");msg = msg.replaceAll("&f", white + "");msg = msg.replaceAll("&e", yellow + "");msg = msg.replaceAll("&r", reset + "");return msg;}
    39. public String ignoreCase(String str) {return "(?i)" + Pattern.quote(str);}
    40.  
    41. public static boolean sendConsoleMessage(String message){
    42. if (message == null || message.isEmpty()) return false;
    43. message = formatColorCodes(message);
    44. if (message.contains("&z")) {
    45. String[] msgs = message.split("&z");
    46. for (String msg: msgs){
    47. Main.console.sendMessage(msg.replaceAll("&z", ""));
    48. }
    49. return true;
    50. } else {
    51. Main.console.sendMessage(message);
    52. return true;
    53. }
    54. }
    55. public static boolean sendMessage(Player target, String message){
    56. if (message == null || message.isEmpty() || target == null || !(target instanceof Player)) return false;
    57. message = formatColorCodes(message);
    58. if (message.contains("&z")) {
    59. String[] msgs = message.split("&z");
    60. for (String msg: msgs){
    61. target.sendMessage(msg.replaceAll("&z", ""));
    62. }
    63. return true;
    64. } else {
    65. target.sendMessage(message);
    66. return true;
    67. }
    68. }


    And here is the error I get when running the plugin as I posted it here(with the rest of it, of course, but I made sure the line numbers are correct):
    Show Spoiler
    16:11:36 [INFO] [EnteisCommands] Enabling EnteisCommands v4.6
    16:11:36 [SEVERE] Error occurred while enabling EnteisCommands v4.6 (Is it up to date?)
    java.lang.NullPointerException
    at com.gmail.br45entei.enteiscommands.ChatMgmt.sendConsoleMessage(ChatMgmt.java:51)
    at com.gmail.br45entei.enteiscommands.Main.onEnable(Main.java:174)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:383)
    at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugin(CraftServer.java:306)
    at org.bukkit.craftbukkit.v1_5_R3.CraftServer.enablePlugins(CraftServer.java:288)
    at org.bukkit.craftbukkit.v1_5_R3.CraftServer.<init>(CraftServer.java:242)
    at net.minecraft.server.v1_5_R3.PlayerList.<init>(PlayerList.java:55)
    at net.minecraft.server.v1_5_R3.DedicatedPlayerList.<init>(SourceFile:11)
    at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.java:95)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:388)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)


    Oh, and if you see a way I can do something in the code I posted simpler or better, feel free to comment about it, I will gladly accept help!

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

    socram8888

    Personally, I would create a function like "debug(String message)" function that will check if showDebugMessages is enabled, rather than using a if for each debug message.
     
  14. Brian_Entei
    Your 'console' field is not asigned, it's still null.

    Just use Bukkit.getConsoleSender() directly instead of storing so many things.
     
  15. Offline

    Brian_Entei

    socram8888 Do you mean like this?
    Code:java
    1. sendDebugMsg(String message);


    That would tale a lot of ifs out of the equation. Thanks! Oh, and Digi If I do that, then it throws the error I posted for that line, even if I don't have load: STARTUP set in the plugin.yml, but maybe I've fixed it, so when I get home, I'll try it anyway, so thanks.
     
  16. Brian_Entei
    You need to asign it in onEnable() if you really want to use a field... but like I said, it would be better to use it directly where you need it.
     
  17. Offline

    Intravenous

    I know this thread is a little old but looking for a similar answer I was hoping to find it here quickly through google links but did not. So I believe this following is a quick solution to having color in your console.
    Server.log will log following but the ansi color codes appear not the color.

    Enter the following shortcut vars to your main class,
    Code:java
    1. // console coloring made easy
    2. public static final String ANSI_RESET = "\u001B[0m";
    3. public static final String ANSI_BLACK = "\u001B[30m";
    4. public static final String ANSI_RED = "\u001B[31m";
    5. public static final String ANSI_GREEN = "\u001B[32m";
    6. public static final String ANSI_YELLOW = "\u001B[33m";
    7. public static final String ANSI_BLUE = "\u001B[34m";
    8. public static final String ANSI_PURPLE = "\u001B[35m";
    9. public static final String ANSI_CYAN = "\u001B[36m";
    10. public static final String ANSI_WHITE = "\u001B[37m";
    11. public static final String ANSI_BOLD = "\u001B[1m";


    Now when you want to send color to console with lets say your onEnable...
    use something like this,
    Code:java
    1. getLogger().info("["+ ANSI_CYAN + "MyPluginName" + ANSI_RESET +"]" + ANSI_GREEN +" has been Enabled." + ANSI_RESET);


    *edit: removed this.myLogger, replaced with getLogger()

    (ref) ANSI color codes
     
    TECGaming360 likes this.
  18. Offline

    legoman519

    RROD

    THANK YOU!
     
  19. Offline

    Brian_Entei

    Intravenous wow, I was also looking for that! I'll try it out as soon as possible!

    EDIT: Doesn't seem to work on Windows 7 32 bit or 64 bit... maybe the ANSI codes are for other platforms?
     
  20. Offline

    Fabricio20

    @Intravenous
    Tanks Man :D
     
Thread Status:
Not open for further replies.

Share This Page