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

    TheMcPlayer1997

    Hey! I'm making my very first own plugin and i'm wondering how I can add color support to it, so people can use color codes in the config.yml file.
    Thank you.
     
  2. Offline

    ShredNyx

    Do u know the methods for generating a config.yml file and color codes for what..
     
  3. Offline

    L33m4n123

  4. Offline

    TheMcPlayer1997

  5. Offline

    L33m4n123

    re-read the api. Thats not how you use it^^
     
  6. Offline

    TheMcPlayer1997

    I'm stuck, can you help me?
     
  7. Offline

    L33m4n123

    Code:java
    1. String message = "&3Message to colorize";
    2. String coloredMessage = ChatColor.translateAlternateColorCodes('&', message);
     
  8. Offline

    TheMcPlayer1997

    I have a command named "test":
    but i'm getting a "Warning" in eclipse:
    The value of the local variable coloredMessage is not used.
     
  9. Offline

    L33m4n123

    Well. Now you only saved it to a variable. You have to USE it actually.
     
  10. Offline

    TheMcPlayer1997

    This may sound stupid but how can I use it?..
    I'm new to java started today.. just keep that in mind
     
  11. Offline

    jimbo8

    You should watch a few Java tutorials first/read some books :)

    You don't need much knowledge, just the basics ;)
     
  12. Offline

    L33m4n123

    Well. Don't want to be an arse. However I think you should look more into java before starting working with the Bukkit API

    I mean you have to use the variable for anything. Like sending it in a text to the player or what ever
     
  13. Offline

    TheMcPlayer1997

    Doing java for straight 4 hours now, i have a plugin with 120 lines and a fully working config.yml and language.yml file.. I think i'm doing good for a newbie
     
  14. Offline

    L33m4n123

    Well. If you would have been knowing a bit more, you would have known what the error was you had ;)

    However I wish you best of luck. And just going to tell you. It's not going to be easier^^
     
  15. Offline

    TheMcPlayer1997

    I'm pretty much stuck at this stage, i'm trying to add color support to the language.yml file but i'm unsure how.
    I don't know where to put the code you gav me so yeah..
     
  16. Offline

    ZeusAllMighty11

    TheMcPlayer1997

    4 hours isn't going in depth.. that would be learning some basic things. It took me a while more than 4 hours to grasp core concepts.


    To use the string, you would just use the coloredMessage variable instead of message
     
  17. Offline

    TheMcPlayer1997

    Didn't say i'm pro ;P, anyway should I add this:
    String coloredMessage = ChatColor.translateAlternateColorCodes('&', message);
    Under the command i'm creating or where? I'm keep getting the "Warning"
     
  18. Offline

    ArthurMaker

    TheMcPlayer1997
    Or we can just do it:

    String example = "&cText in red!";
    player.sendMessage(example.replace("&", "§"));

    Done :v
     
  19. Offline

    ShredNyx

  20. If you want to let players use colors in chat, I think you can invoke a method upon the chat event to modify its message. You could then change the message from, say, "&3I like colors!" to the output of the translateAlternateColorCodes() method. e.g.:

    @EventHandler
    public void onPlayerChat(PlayerChatEvent e){
    String msg = ChatColor.translateAlternateColorCodes('&',e.getMessage());
    e.setMessage(msg);
    }
     
  21. Offline

    TheMcPlayer1997

    I want have command "Test" inside of my config file;
    if(commandLabel.equalsIgnoreCase("test")){
    player.sendMessage(getConfig().getString("Test"));
    What should I do to get & support for this message?
     
  22. Offline

    ArthurMaker

    Easy:


    if(commandLabel.equalsIgnoreCase("test")){
    player.sendMessage(getConfig().getString("Test").replace("&", "§"));

    This should work. =P
     
  23. Offline

    1Rogue


    Don't use .replace(), use the bukkit API for this:
    Code:java
    1. player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("example")));
     
    TheMcPlayer1997 likes this.
  24. Offline

    Jake6177

    My Gosh......once you you've used:

    Code:java
    1. String coloredMessage = ChatColor.translateAlternateColorCodes('&', message);

    then you can do something like so, for example:
    Code:
    (in your command, check for the sender being an instance of player and if so, cast a new variable converting the sender into a player:)
    player.sendMessage(coloredMessage);
    So for your test command, if you just want it to repeat back to the player:

    Code:java
    1. if (commandLabel.equalsIgnoreCase("test")) {
    2. if (sender instanceof Player) {
    3. Player player = (Player) sender;
    4. if (args.length == 1) {
    5. player.sendMessage(ChatColor.translateAlternateColorCodes('&', args[0]);
    6. }
    7. }
    8. }


    Should work enough so..

    Please learn more Java :3
     
  25. Offline

    1Rogue

    Code:java
    1. public String getColoredString(String path) {
    2. return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
    3. }
     
    TheMcPlayer1997 and Jake6177 like this.
  26. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Removed some advertising from the thread.
     
  27. Offline

    TheMcPlayer1997

    this is a string and therefore it should be added before all the commands?
    Code:java
    1.  
    2. public String getColoredString(String path) {
    3. return ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString(path));
    4. }
    5.  
    6. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    7. Player player = (Player) sender;
    8. etc..
    9.  
     

  28. If I understand what you mean (didn't read all posts on this thread), then it doesn't matter.
    Java is an OOP language, Object Oriented Programming.
    This means you can actually throw methods all around as you wish, as long as the code within those make sense.
    In the command, or wherever you'd need that string you can simply call that method.
     
  29. Offline

    1Rogue


    The whole point of OOP is to not have to copy/paste things everywhere (well okay, there are many, but for this example...).

    Code:java
    1. YourClass example = /*class instance containing the method getColoredString(String path)*/;
    2. String colored = example.getColoredString("path.to.string"); // is colored version of .getString(path)
     
  30. Offline

    TheMcPlayer1997

    Well, the plugin doesn't react on the & inside of the config.yml file, any suggestions?
     
Thread Status:
Not open for further replies.

Share This Page