How Can I Add Color (&) Support For My Plugin?

Discussion in 'Plugin Development' started by TheMcPlayer1997, Nov 17, 2013.

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

    1Rogue


    show your current code.
     
  2. Offline

    The_Doctor_123

    1Rogue
    Some method like that would be better served as static.
     
  3. Offline

    1Rogue


    True, I used to have it as '_' and used static imports to obtain the value inside other classes, however '_' is becoming standardized in releases past Java 8 and I don't think going into static is really necessary to this particular case, it's more about the application of the code than the accessing.
     
  4. Offline

    TheMcPlayer1997

    Just a part of it:
    Code:java
    1.  
    2.  
    3. public void onEnable() {
    4. PluginDescriptionFile pdfFile = this.getDescription();
    5. this.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " Has Been Enabled!");
    6. getConfig().options().copyDefaults(true);
    7. saveConfig();
    8. }
    9.  
    10. public void sendMultipleMessages(String messages, Player player)
    11. {
    12. player.sendMessage(messages.split("\n"));
    13. }
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    16. Player player = (Player) sender;
    17. if(commandLabel.equalsIgnoreCase("Test")){
    18. player.sendMessage(getConfig().getString("Test").replace("/n", );
    19. player.sendMessage(ChatColor.GOLD + "Testing, &1:");
    20.  

    I have defined the Config file and enabled saving thing, i tried to enable \n to make new line in the "config.yml" and color codes with & but it's not working, I just bought a book about Java btw..
     
  5. Offline

    1Rogue


    I don't see any of the code I mentioned, and your .replace() method does not have a second argument.
     
  6. Offline

    The_Doctor_123

    1Rogue
    Methods never need to be static, but when the method doesn't require any resources from the class it's in(except other static stuff) it should be static. It's a good identifier of how it should be used.
     
  7. Offline

    1Rogue


    That's not 100% true, but it's not bad to use it that way (which I was agreeing with you on...).
     
  8. Offline

    TheMcPlayer1997

    Constantly removing and re-adding code,
    Code:java
    1.  
    2. public void onEnable() {
    3. PluginDescriptionFile pdfFile = this.getDescription();
    4. this.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " Has Been Enabled!");
    5. getConfig().options().copyDefaults(true);
    6. saveConfig();
    7. }
    8.  
    9. public void sendMultipleMessages(String messages, Player player)
    10. {
    11. player.sendMessage(messages.split("\n"));
    12. }
    13.  
    14. public String getColoredString(String path) {
    15. return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
    16. }
    17.  
    18. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    19. Player player = (Player) sender;
    20. if(commandLabel.equalsIgnoreCase("Test")){
    21. player.sendMessage(getConfig().getString("Testing, &1; "));
    22.  

    Very confused how I can get \n and (&) working inside of the config file..
     
  9. Offline

    1Rogue


    They aren't working because you aren't calling them...

    You need to call the methods, not .getConfig();
     
  10. The thing is, you have code in it that could work, but you're not calling them like 1Rogue mentioned.
    To make it a little more clear, it's like trying to get a nail in a piece of wood, you lay the hammer next to you but proceed to bump it in with your fists. (without any success of course)

    I respect the fact that you're still learning, but I highly recommend you to look into how Java and its syntax work, you don't have to go into too much detail, as the most experience comes from working with it on things such as plugins, but a basic understanding is required.
     
  11. Offline

    TheMcPlayer1997

    Oh my gosh, how do I call a method?
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("example")));
    this will get the string called "example" right?

    Bought a book, I just learned some basics, the book have around 610 sides and i already read 20..

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

  12. There's a method called getColoredString with the exact code you used there.
    It's better to call that, so this:

    Code:
    player.sendMessage(getColoredString("Testing"));
    
    would essentially be the same thing, but make it far more clear and easy if you're working on a larger scale.




    Great! I suggest reading through that, you don't really have to read all of that though.
    If you start reading against your will, the chance that you'll learn much will be pretty slim.
    If there are parts that explain the syntax, OOP, and basics, I suggest reading that though.
     
  13. Offline

    TheMcPlayer1997

    Well, just very confused;
    player.sendMessage(getColoredString("Testing"));
    will this get the "Testing" string or?
    Code:java
    1.  
    Code:java
    1.  
    2. public String getColoredString(String path) {
    3. return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
    4. }
    5.  

    I have this code but I don't know how to call it
     
  14. Offline

    1Rogue


    It will get whatever path you pass to it.
     
  15. Offline

    TheMcPlayer1997

    ('&', plugin.getConfig().getString(path));
    is "path" the name of the string?
     

  16. Yes, the path is the path of the string in the config.
    if your config would be:

    Code:
    messages:
     colored:
      message1: &1Hello there!
    
    or something alike, then the path would be messages.colored.message1
     
  17. Offline

    TheMcPlayer1997

    Ah I forgot about that.
    So I have this:
    Code:java
    1.  
    2. public void sendMultipleMessages(String messages, Player player)
    3. {
    4. player.sendMessage(messages.split("\n"));
    5. }
    6.  
    7. public String getColoredString(String path) {
    8. return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
    9. }
    10.  

    The first one should create a new line in the config.yml when someone type \n
    and the second one should allow me to use & inside of the config.yml.
    Now i have to call those methods to get they working for every single command but i don't know how to do that or are i'm tottaly doing wrong?
     

  18. The first one wouldn't work.

    Code:java
    1.  
    2. public void sendMultipleMessages(String messages, Player player) {
    3. String[] messageList = messages.split("\n");
    4. for(int i = 0; i < messageList.length; i++) {
    5. player.sendMessage(messageList[ i ]);//without spaces around i, editor saw it as bbcode
    6. }
    7. }
    8.  


    that would send it on a separate line each time a \n is there.
     
    TheMcPlayer1997 and 1Rogue like this.
  19. Offline

    TheMcPlayer1997

    Is that code supposed to work with the config or in game chat?
    And btw, which editor do you use?
     
  20. Offline

    1Rogue

    it takes a string, splits it into an array, and then sends a message for each new string in the array.

    He probably just freehanded it on the forum.
     

  21. 1Rogue pretty much answered all of that.
    Thanks Rogue
     
  22. Offline

    TheMcPlayer1997

    Cupcakes69
    Just a question, why can't anyone use \n in the config but I can use it in the source code?
     
  23. Offline

    chrisman0091

    Here is how I did it for my plugin.

    In a Utils class:
    Code:java
    1. public class Utils {
    2. public static String colorize(String s) {
    3. return ChatColor.translateAlternateColorCodes('&', s);
    4. }


    Then I created an event for when the player sent a chat message, created a check to see if it contained any of the color codes WITH the symbol before it(&1-&f, then format codes too).
    After that I use my method from my utils like such:
    Code:java
    1. event.setMessage(Utils.colorize(event.getMessage()));


    Works fine for me. Also worthy to note that when people used format codes(like &k) I added a ChatColor.RESET afterwards, otherwise it formatted the whole line of chat which obviously is not what I wanted.
     
  24. Offline

    Squawkers13

    Just try this:
    Code:java
    1. player.sendMessage(ChatColor.GOLD + "Message"); //Example implementation

    Use the bukkit ChatColor enum like that and it will be replaced with colors in console and chat.
     

  25. TheMcPlayer1997
    Because the source code is Java and the config is yaml
     
  26. Offline

    Fabricio20

    Tanks Man! This Is Really Simple, But Helped Me A Lot!! :D
     
  27. Offline

    ImDeJay

    I havnt read all the posts in here, just thought i'd throw in this little method i use.

    Code:
    static String replaceColors(String message) {
     
    return ChatColor.translateAlternateColorCodes('&', message);
     
    }
    usage:

    Code:
    String theMSG = "&4This is a red message...&bNow its aqua!";
    sender.sendmessage(replaceColors(theMSG));
    
     
    TheMcPlayer1997 likes this.
Thread Status:
Not open for further replies.

Share This Page