Solved Help with a color chat plugin?

Discussion in 'Plugin Development' started by GlacialCreeper, May 7, 2014.

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

    GlacialCreeper

    Helloooo my fellow bukkitsters :) I've started learning bukkit, and with what I've learned so far, I'm hoping to make a very small color plugin to see if it works. Please dont tell me that someone's already done it, without at least helping me a bit. I just wanna test myself and I'm kinda stuck. So here's my code:
    Code:java
    1. package me.glacialcreeper.colortalk;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class ColorTalk extends JavaPlugin {
    10.  
    11. public void onEnable(){
    12.  
    13. }
    14.  
    15. public void onDisable(){
    16.  
    17. }
    18.  
    19. String message0 = null;
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    22.  
    23. for (String m: args){
    24. message0 += m;
    25. }
    26.  
    27.  
    28.  
    29. Player player = (Player) sender;
    30.  
    31. //check command specs
    32. if (cmd.getName().equalsIgnoreCase("c")){
    33. if (!(sender instanceof Player)) {
    34. sender.sendMessage(ChatColor.RED + "This command can only be used in-game!");
    35. } else {
    36. if (args.length == 0){
    37. player.sendMessage(ChatColor.RED + "Please specify the color!");
    38. } else if (args.length == 1) {
    39. player.sendMessage(ChatColor.BLUE + "You forgot to add your message!");
    40. } else {
    41.  
    42. }
    43. }
    44. }
    45.  
    46. return false;
    47. }
    48.  
    49. public String getMessage(){
    50. return message0;
    51. }
    52.  
    53. }


    I have tried this before and it worked, but it would only print out one word. I just deleted it to start from scratch.

    But anyway, I'm stuck and as you can see, my command is "c" and takes 2 parameters, the color and the message. But you'll probably also see that each part of a message someone says in chat is an argument. I'm not sure how to fix that, and I tried the enhanced for loop but realized that you cant delete the first argument (correct me if I'm wrong). Also, I'm not done with the code, I know I still have to test for each of the colors later.
     
  2. Offline

    iBecameALoaf

    You can use a stringbuilder, it will combine all arguments after the i variable in the loop:

    Code:
    StringBuilder sb = new StringBuilder();
    for(int i = 1; i < args.length; i++) {
    sb.append(args[i]).append(" ");
    }
    String s = sb.toString().trim();
    s is the rest of the args after the first one.

    For the colors, you could just use s.replace("&e", ChatColor.YELLOW).
     
    GlacialCreeper likes this.
  3. Offline

    Drkmaster83

    Though I'd personally recommend using Regex or ChatColor:
    Code:
    s.replaceAll("(&([a-fk-or0-9A-FK-OR]))", "\u00A7$2"); //regex
    ChatColor.translateAlternateColorCodes('&', s); //ChatColor
    
     
    GlacialCreeper and AdamQpzm like this.
  4. Offline

    GlacialCreeper

    Ok, thanks guys, I'm not sure I got an alert about your replies, so sorry for not paying attention! Plus, nobody had replied for a day, so...yeah embarrassing sorry.

    I actually watched a video (pogostick29dev youtube) and on his config video, it included a stringbuilder. Then I made a stupid mistake of deleting my project to start from scratch. Got stuck, and now I see your reply! This should help, thanks!

    Ah, wait hold on. I'm using eclipse, and I can't use the replace() method you mentioned on it because replace() takes 2 char parameters. Is there another way to replace the first argument with the actual message?

    Also, I'm making it so they can do this:
    /c blue <message>
    /c red <message>
    ... and so forth. I'm not planning on making this using color code args.

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

    tryy3

    You could most likely need to do a switch statement that adds the color
    so something like
    Code:
    ChatColor color;
     
    switch(args[0])
    {
        blue:
            color = ChatColor.BLUE;
            break;
    }
     
    player.sendMessage(color + "Hi");
     
    GlacialCreeper likes this.
  6. Offline

    Garris0n

    Just ChatColor. Why rewrite the API in a way that's more susceptible to future modifications to the game?

    iBecameALoaf

    Yes, a StringBuilder is better than String concatenation. Use the ChatColor method for translating color codes, though, for reasons mentioned above.

    tryy3

    Uh...what?
     
  7. Offline

    tryy3

    He wants to use color names not color codes

    i.e
    /c red hello

    not
    /c &4 hello

    so you would need to get the name, and in the code convert it to a chatcolor, assuming there is no inbuilt method to do it, as far as I know all ChatColor methods requires the Color Code not Color Name
     
    GlacialCreeper likes this.
  8. Offline

    GlacialCreeper

    Ok, good to know there are people still awake (1:08 here in est). I personally dont see anything wrong with this code, but obviously there is, do any of you see it?
    Code:java
    1. package me.glacialcreeper.colortalk;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class ColorTalk extends JavaPlugin {
    11.  
    12. public void onEnable(){
    13. Bukkit.getServer().getLogger().info(ChatColor.GREEN+"[ColorTalk] ColorTalk has been"
    14. + " enabled.");
    15. }
    16.  
    17. public void onDisable(){
    18. Bukkit.getServer().getLogger().info(ChatColor.RED+"[ColorTalk] ColorTalk has been"
    19. + " disabled.");
    20. }
    21.  
    22. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    23.  
    24. Player player = (Player) sender;
    25.  
    26. if (cmd.getName().equalsIgnoreCase("c")){
    27. if (!(sender instanceof Player)){
    28. sender.sendMessage("This command can only be used in-game!");
    29. }
    30.  
    31. if (args.length == 0){
    32. return false;
    33. } else if (args.length == 1){
    34. player.sendMessage("Please say your message!");
    35. } else if (args.length == 2){
    36.  
    37. StringBuilder builder = new StringBuilder();
    38.  
    39. for (int i = 1; i < args.length; i++){
    40. builder.append(args[i] + " ");
    41. }
    42.  
    43. String message = builder.toString();
    44.  
    45. switch (args[0]){
    46. case "green":
    47. player.chat(ChatColor.GREEN+message);
    48. break;
    49. case "blue":
    50. player.chat(ChatColor.BLUE+message);
    51. break;
    52. case "red":
    53. player.chat(ChatColor.RED+message);
    54. break;
    55. case "yellow":
    56. player.chat(ChatColor.YELLOW+message);
    57. break;
    58. case "purple":
    59. player.chat(ChatColor.DARK_PURPLE+message);
    60. break;
    61. case "aqua":
    62. player.chat(ChatColor.AQUA+message);
    63. break;
    64. default:
    65. player.sendMessage(ChatColor.RED+"Oops! That color isn't available!");
    66. }
    67.  
    68.  
    69. }
    70. }
    71.  
    72. return true;
    73. }
    74.  
    75. }
    76. [/i]
     
  9. Offline

    justcool393

    GlacialCreeper The only thing I can think of is if you registered your command in the plugin.yml file.

    Also, the logging messages on enable and disable aren't really needed, as Bukkit already handles this.
     
  10. Offline

    GlacialCreeper


    Well, here's my plugin.yml
    Code:
    author: GlacialCreeper
    name: ColorTalk
    version: 1.0
    main: me.glacialcreeper.colortalk.ColorTalk
    description: Allows you to talk with colors
    commands:
      c:
        usage: /c <color> <message>
        description: Lets you pick a color to color your chat
        aliases: [color]
    I know already that yml doesnt let you use tabs, I used spaces; also the terminal didnt complain about an invalid plugin.yml .
     
  11. Offline

    justcool393

    GlacialCreeper Okay. That should be good. Is there any errors while running it?
     
  12. Offline

    GlacialCreeper

    Oh by the way, I got the color working, but what happens is:
    1. If you give it just one message argument (e.g. /c blue hi instead of /c blue hi there), it'll work
    the same goes for if you use underscores/dashes for spaces (since strings can use them)
    2. However, if you give it 2 or more, it wont print anything in the chat at all.

    Idk what the problem is, the for loop has the counter to argument 1, so it should've put all the other message args into builder. My problem is that it wont print anything in the chat from above 1 arg.

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

    tryy3

    Line 35, it checks if you got 2 arguments (not more
    change it to >= 2 instead of == 2
     
    justcool393 and GlacialCreeper like this.
  14. Offline

    GlacialCreeper

    WOOOWWW That's GOTTA be it! Nice catch...

    tryy3 That fixed it! Didn't see that at all, lol. I'll try not to do something like that again! :)

    Thanks guys!

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

    justcool393

    Edit: Missed the responses before me. Sorry

    GlacialCreeper I found it. It looks like you are checking only if the argument length is 2.

    Your code is
    Code:java
    1. } else if (args.length == 2) {


    but should be

    Code:java
    1. } else if (args.length >= 2) {
     
  16. Offline

    GlacialCreeper

    Yeah lol, that was a bad mistake. Thanks!
     
    justcool393 likes this.
Thread Status:
Not open for further replies.

Share This Page