Setting a players Chat colour

Discussion in 'Plugin Development' started by MetaSurvival, Sep 21, 2013.

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

    MetaSurvival

    With a command, how would I be able to set a players chatcolour
    For example: /chat red, would set the senders chat colour to Red. (&c)

    I tried using sender.setmessage, but I can't, I think I need to use an event, but Idk what event and how I would do it. Thanks if you could help! :D
     
  2. Offline

    L33m4n123

    If you want that from that moment on he types only in red. my Idea would be setting a prefix to the chatcolor code of red with that what he types will be red. Or saving him in a hashmap and then PlayerChatEvent you basicly get the Message he wanted to sent with getMessage() and then make the newMessage to ChatColor.RED + hisMessage
     
    MetaSurvival likes this.
  3. Offline

    MetaSurvival

    L33m4n123
    Thank you for the reply;
    This is what I've done:
    Code:java
    1. if(command.getName().equalsIgnoreCase("chat")){
    2. if(sender.hasPermission("object.chat")){
    3. if(args.length == 0){
    4. sender.sendMessage(ChatColor.RED + "Usage: /chat <black;darkblue;darkgreen;darkaqua;darkred;darkpurple;gold;gray;darkgray;blue;green;aqua;red;purple;yellow;white");
    5. }
    6. else if(args.length == 1){
    7. if(args[0].equalsIgnoreCase("black")){
    8. if(black.contains(sender.getName())){
    9. black.remove(sender.getName());
    10. sender.sendMessage("Changed Chat Colour to WHITE!");
    11. return true;
    12. }
    13. if(!(black.contains(sender.getName()))){
    14. black.add(sender.getName());
    15. sender.sendMessage(ChatColor.BLACK + "You will now speak in BLACK!");
    16. }
    17. }
    18. }
    19. }
    20. }
    21. return true;
    22. }
    23.  

    Code:
    @EventHandler
    public void onChat(AsyncPlayerChatEvent event){
    String message = event.getMessage();
    if(black.contains(event.getPlayer())){
    event.getMessage();
    event.setMessage(ChatColor.BLACK + message);
    }
    }
    }
    
    When I do /chat black, it puts me in the arraylist, but I still talk in White.. ?

    I have registered the Events.
     
  4. Offline

    L33m4n123

    Let me test a bit arround

    Edit: Did you registered your event? ;)
     
    MetaSurvival likes this.
  5. Offline

    MetaSurvival

    lol
    Code:
    this.getServer().getPluginManager().registerEvents((this), this);
    
    I know they are registered because I have a different event also, and it works.
     
  6. Offline

    L33m4n123

    MetaSurvival likes this.
  7. Offline

    MetaSurvival

    L33m4n123
    Do you know why it is not working? :p
     
  8. Offline

    L33m4n123

    MetaSurvival
    I somehow guess it is something with your hashset that you do not get added correctly because
    Code:java
    1. @EventHandler
    2. public void onChat (AsyncPlayerChatEvent evt) {
    3. String message = evt.getMessage();
    4. evt.setMessage(ChatColor.BLACK + message);
    5. }


    works perfectly fine. Try to add a debug message after your if-clause where you check if the player is in the hashset. so something like
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent event){
    3. String message = event.getMessage();
    4. if(black.contains(event.getPlayer())){
    5. event.setMessage(ChatColor.BLACK + message);
    6. } else {
    7. event.setMessage("Something went wrong with the hashset");
    8. }
    9. }
    10. }


    if you get that message you know something is messed up with the hashset ;)

    *redid the code.. With the else.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    MetaSurvival likes this.
  9. Offline

    MetaSurvival

    L33m4n123
    Does it matter that I used a Arraylist instead of a hashmap?
    And yes, it is something wrong with the arraylist...
    If I have to use a hashmap could you tell me how please? :3
    This is what I used for the ArrayList: public List<String> black = new ArrayList<String>();
     
  10. Offline

    L33m4n123

    A hashset would be basicly

    public HashSet<String> black = new HashSet<String>();

    and then the same as you do with black.add(player.getName()); and black.contains(player.getName());

    But it should work with the ArrayList the same as with a HashSet unless I missunderstand how Lists work. AFAIK its just personal preference what to choose
     
    MetaSurvival likes this.
  11. Offline

    MetaSurvival

    L33m4n123
    What is wrong with mine then?

    L33m4n123
    YEEES Found out the problem -_-...
    Changed
    Code:
    if(black.contains(event.getPlayer()));
    to:
    Code:
    if(black.contains(event.getPlayer().getName()));
    .. Such a small error xD

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

    L33m4n123


    D'oh just wanted to say.. the code works perfectly fine for me.. lol
     
    MetaSurvival likes this.
  13. Offline

    MetaSurvival

    L33m4n123
    Do you know how I would make it so a player would stay in a array list after the server is restarted/reloaded?
     
  14. Offline

    L33m4n123


    You would need to write that Arraylist to a file when the onDisable() is called and need to load the file in the onEnable()

    Never did it before though but I guess google will find you something for that
     
    MetaSurvival likes this.
  15. Offline

    MetaSurvival

    L33m4n123
    Code:java
    1. if(command.getName().equalsIgnoreCase("chat")){
    2. if(args.length == 0){
    3. sender.sendMessage(ChatColor.RED + "Usage: /chat <black;darkblue;darkgreen;darkaqua;darkred;darkpurple;gold;gray;darkgray;blue;green;aqua;red;purple;yellow;white");
    4. }
    5. else if(args.length == 1){
    6. if(args[0].equalsIgnoreCase("black") || args[0].equalsIgnoreCase("&0")){
    7. if(sender.hasPermission("chat.black")){
    8. if(black.contains(sender.getName())){
    9. black.remove(sender.getName());
    10. sender.sendMessage("Changed Chat Colour to WHITE!");
    11. return true;
    12. }else{
    13. black.add(sender.getName());
    14. sender.sendMessage(ChatColor.BLACK + "You will now speak in BLACK!");
    15. return true;
    16. }
    17. }else{
    18. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    19. }
    20. }
    21. if(args[0].equalsIgnoreCase("darkblue")){
    22. if(sender.hasPermission("chat.darkblue")){
    23. if(darkblue.contains(sender.getName())){
    24. darkblue.remove(sender.getName());
    25. sender.sendMessage("Changed Chat Colour to WHITE!");
    26. return true;
    27. }else{
    28. darkblue.add(sender.getName());
    29. sender.sendMessage(ChatColor.DARK_BLUE + "You will now speak in DARK BLUE!");
    30. return true;
    31. }
    32. }else{
    33. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    34. }
    35. }
    36. if(args[0].equalsIgnoreCase("darkgreen")){
    37. if(sender.hasPermission("chat.darkgreen")){
    38. if(darkgreen.contains(sender.getName())){
    39. darkgreen.remove(sender.getName());
    40. sender.sendMessage("Changed Chat Colour to WHITE!");
    41. return true;
    42. }else{
    43. black.add(sender.getName());
    44. sender.sendMessage(ChatColor.DARK_GREEN + "You will now speak in DARK GREEN!");
    45. return true;
    46. }
    47. }else{
    48. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    49. }
    50. }
    51. if(args[0].equalsIgnoreCase("darkaqua")){
    52. if(sender.hasPermission("chat.darkaqua")){
    53. if(darkaqua.contains(sender.getName())){
    54. darkaqua.remove(sender.getName());
    55. sender.sendMessage("Changed Chat Colour to WHITE!");
    56. return true;
    57. }else{
    58. darkaqua.add(sender.getName());
    59. sender.sendMessage(ChatColor.DARK_AQUA + "You will now speak in DARK AQUA!");
    60. return true;
    61. }
    62. }else{
    63. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    64. }
    65. }
    66. if(args[0].equalsIgnoreCase("darkred")){
    67. if(sender.hasPermission("chat.darkred")){
    68. if(darkred.contains(sender.getName())){
    69. darkred.remove(sender.getName());
    70. sender.sendMessage("Changed Chat Colour to WHITE!");
    71. return true;
    72. }else{
    73. darkred.add(sender.getName());
    74. sender.sendMessage(ChatColor.DARK_RED + "You will now speak in DARK RED!");
    75. return true;
    76. }
    77. }else{
    78. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    79. }
    80. }
    81. if(args[0].equalsIgnoreCase("darkpurple")){
    82. if(sender.hasPermission("chat.darkpurple")){
    83. if(darkpurple.contains(sender.getName())){
    84. darkpurple.remove(sender.getName());
    85. sender.sendMessage("Changed Chat Colour to WHITE!");
    86. return true;
    87. }else{
    88. darkpurple.add(sender.getName());
    89. sender.sendMessage(ChatColor.DARK_PURPLE + "You will now speak in DARK PURPLE!");
    90. return true;
    91. }
    92. }else{
    93. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    94. }
    95. }
    96. if(args[0].equalsIgnoreCase("gold")){
    97. if(sender.hasPermission("chat.gold")){
    98. if(gold.contains(sender.getName())){
    99. gold.remove(sender.getName());
    100. sender.sendMessage("Changed Chat Colour to WHITE!");
    101. return true;
    102. }else{
    103. gold.add(sender.getName());
    104. sender.sendMessage(ChatColor.GOLD + "You will now speak in GOLD!");
    105. return true;
    106. }
    107. }else{
    108. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    109. }
    110. }
    111. if(args[0].equalsIgnoreCase("gray")){
    112. if(sender.hasPermission("chat.gray")){
    113. if(gray.contains(sender.getName())){
    114. gray.remove(sender.getName());
    115. sender.sendMessage("Changed Chat Colour to WHITE!");
    116. return true;
    117. }else{
    118. gray.add(sender.getName());
    119. sender.sendMessage(ChatColor.GRAY + "You will now speak in GRAY!");
    120. return true;
    121. }
    122. }else{
    123. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    124. }
    125. }
    126. if(args[0].equalsIgnoreCase("darkgray")){
    127. if(sender.hasPermission("chat.darkgray")){
    128. if(darkgray.contains(sender.getName())){
    129. darkgray.remove(sender.getName());
    130. sender.sendMessage("Changed Chat Colour to WHITE!");
    131. return true;
    132. }else{
    133. darkgray.add(sender.getName());
    134. sender.sendMessage(ChatColor.DARK_GRAY + "You will now speak in DARK GRAY!");
    135. return true;
    136. }
    137. }else{
    138. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    139. }
    140. }
    141. if(args[0].equalsIgnoreCase("blue")){
    142. if(sender.hasPermission("chat.blue")){
    143. if(blue.contains(sender.getName())){
    144. blue.remove(sender.getName());
    145. sender.sendMessage("Changed Chat Colour to WHITE!");
    146. return true;
    147. }else{
    148. blue.add(sender.getName());
    149. sender.sendMessage(ChatColor.BLUE + "You will now speak in BLUE!");
    150. return true;
    151. }
    152. }else{
    153. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    154. }
    155. }
    156. if(args[0].equalsIgnoreCase("green")){
    157. if(sender.hasPermission("chat.green")){
    158. if(green.contains(sender.getName())){
    159. green.remove(sender.getName());
    160. sender.sendMessage("Changed Chat Colour to WHITE!");
    161. return true;
    162. }else{
    163. green.add(sender.getName());
    164. sender.sendMessage(ChatColor.GREEN + "You will now speak in GREEN!");
    165. return true;
    166. }
    167. }else{
    168. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    169. }
    170. }
    171. if(args[0].equalsIgnoreCase("aqua")){
    172. if(sender.hasPermission("chat.aqua")){
    173. if(aqua.contains(sender.getName())){
    174. aqua.remove(sender.getName());
    175. sender.sendMessage("Changed Chat Colour to WHITE!");
    176. return true;
    177. }else{
    178. aqua.add(sender.getName());
    179. sender.sendMessage(ChatColor.AQUA + "You will now speak in AQUA!");
    180. return true;
    181. }
    182. }else{
    183. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    184. }
    185. }
    186. if(args[0].equalsIgnoreCase("red")){
    187. if(sender.hasPermission("chat.red")){
    188. if(red.contains(sender.getName())){
    189. red.remove(sender.getName());
    190. sender.sendMessage("Changed Chat Colour to WHITE!");
    191. return true;
    192. }else{
    193. red.add(sender.getName());
    194. sender.sendMessage(ChatColor.RED + "You will now speak in RED!");
    195. return true;
    196. }
    197. }else{
    198. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    199. }
    200. }
    201. if(args[0].equalsIgnoreCase("purple")){
    202. if(sender.hasPermission("chat.purple")){
    203. if(purple.contains(sender.getName())){
    204. purple.remove(sender.getName());
    205. sender.sendMessage("Changed Chat Colour to WHITE!");
    206. return true;
    207. }else{
    208. purple.add(sender.getName());
    209. sender.sendMessage(ChatColor.LIGHT_PURPLE + "You will now speak in PURPLE!");
    210. return true;
    211. }
    212. }else{
    213. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    214. }
    215. }
    216. if(args[0].equalsIgnoreCase("yellow")){
    217. if(sender.hasPermission("chat.yellow")){
    218. if(yellow.contains(sender.getName())){
    219. yellow.remove(sender.getName());
    220. sender.sendMessage("Changed Chat Colour to WHITE!");
    221. return true;
    222. }else{
    223. yellow.add(sender.getName());
    224. sender.sendMessage(ChatColor.YELLOW + "You will now speak in YELLOW!");
    225. return true;
    226. }
    227. }else{
    228. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    229. }
    230. }
    231. }
    232. }
    233. return false;
    234. }
    235.  

    It works well.. Except when I do this:
    /chat black - It turns my chat to black, then I do:
    /chat red - It turns my chat to red, the I do:
    /chat blue - And my chat stays as red

    This happens with any colours, I can do it 2 times, but then it just stays the same colour...
    I am pretty sure it's something to do with the "return true;" on the code.. Could you fix/help me please? :3
     
  16. Offline

    L33m4n123

    *cough*
    as one example
    Code:java
    1. if (args[0].equalsIgnoreCase("darkgreen")) {
    2. if (sender.hasPermission("chat.darkgreen")) {
    3. if (darkgreen.contains(sender.getName())) {
    4. darkgreen.remove(sender.getName());
    5. sender.sendMessage("Changed Chat Colour to WHITE!");
    6. return true;
    7. } else {
    8. black.add(sender.getName());
    9. sender.sendMessage(ChatColor.DARK_GREEN
    10. + "You will now speak in DARK GREEN!");
    11. return true;
    12. }
    13. } else {
    14. sender.sendMessage(ChatColor.RED
    15. + "You do not have permission to execute this command!");
    16. }
    17. }


    IF darkgreen contains the player you remove him. ok works.. else
    you ADD him to BLACK instead of darkgreen. Copy & Paste much, eh? :D

    Edit: Also. What I personally would do at the beginning

    Code:java
    1. if(black.contains(sender.getName()) {
    2. black.remove(sender.getName());
    3. }


    Amd that for every color BEFORE you check WHAT argument they entered.. that way in the end they do not end up in 3 different Color Lists and the Chat event gets confused^^
     
    MetaSurvival likes this.
  17. Offline

    MetaSurvival

    L33m4n123
    Oh geeze.. -__- That took me ages..
    How would I do it instead?
     
  18. Offline

    L33m4n123


    It's just that one line where you forgot to replace black.add with darkgreen.add

    Edit: but read my edit from the post above
     
    MetaSurvival likes this.
  19. Offline

    MetaSurvival

    L33m4n123
    Mhm I don't understand? -_- Can you show me an example?
     
  20. Offline

    L33m4n123

    Need to do it a bit different then I thought first;

    Code:java
    1. if(command.getName().equalsIgnoreCase("chat")){
    2. if(args.length == 0){
    3. sender.sendMessage(ChatColor.RED + "Usage: /chat <black;darkblue;darkgreen;darkaqua;darkred;darkpurple;gold;gray;darkgray;blue;green;aqua;red;purple;yellow;white");
    4. }
    5. else if(args.length == 1){
    6. if(args[0].equalsIgnoreCase("black") || args[0].equalsIgnoreCase("&0")){
    7. if(sender.hasPermission("chat.black")){
    8. if (allTheOtherLists.contains(sender.getName())) {
    9. allTheOtherLists.remove(sender.getName()));
    10. }
    11. if(black.contains(sender.getName())){
    12. black.remove(sender.getName());
    13. sender.sendMessage("Changed Chat Colour to WHITE!");
    14. return true;
    15. }else{
    16. black.add(sender.getName());
    17. sender.sendMessage(ChatColor.BLACK + "You will now speak in BLACK!");
    18. return true;
    19. }a
    20. }else{
    21. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    22. }
    23. }
    24. [ rest of your code adjusted]
    25.  



    ofc instead of allTheOtherLists you do if(aqua.contains .... after that if(darkgreen) contains.. just to make sure the player is not in more than one List.
     
  21. Offline

    MetaSurvival

    L33m4n123
    Wouldn't it be
    Code:java
    1. else if(args.length == 1){
    2. if(black.contains(sender.getName())){
    3. black.remove(sender.getName()));
    4. }
    5. if(darkblue.contains(sender.getName())){
    6. darkblue.remove(sender.getName()));
    7. }
    8. ETC, ETC, ETC
    9. if(args[0].equalsIgnoreCase("black") || args[0].equalsIgnoreCase("&0")){
    10. if(sender.hasPermission("chat.black")){
    11. if(black.contains(sender.getName())){
    12. black.remove(sender.getName());
    13. sender.sendMessage("Changed Chat Colour to WHITE!");
    14. return true;
    15. }else{
    16. black.add(sender.getName());
    17. sender.sendMessage(ChatColor.BLACK + "You will now speak in BLACK!");
    18. return true;
    19. }
    20. }else{
    21. sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
    22. }
    23. }
    24.  
     
  22. Offline

    L33m4n123

    MetaSurvival Thats what I thought first too. However your message Changed Chat Color to White now does not work anymore if you remove them before that ^^
     
    MetaSurvival likes this.
  23. Offline

    MetaSurvival

    L33m4n123
    I don't understand how I am ment to do it now -__-
     
  24. Offline

    L33m4n123

    Code:java
    1.  
    2. if arg 0 is black {
    3. remove him from all the list except black!
    4. if he is on black {
    5. remove him from black
    6. send him a message that he writes now in white
    7. } else {
    8. add him to black
    9. }
    10. }
    11. if arg 0 is blue {
    12. remove him from all the list except blue!
    13. if he is on blue{
    14. remove him from blue
    15. send him a message that he writes now in white
    16. } else {
    17. add him to blue
    18. }
    19. }
    20. if arg 0 is green {
    21. remove him from all the list except green!
    22. if he is on green{
    23. remove him from green
    24. send him a message that he writes now in white
    25. } else {
    26. add him to green
    27. }
    28. }


    like that
     
    MetaSurvival likes this.
  25. Offline

    LeoFSU

    You might wanna check permission first, then use a switch construct to switch between colors.

    Take a look at the javadoc.
     
  26. Offline

    MetaSurvival

    L33m4n123
    Looks like it's going to take years to do that lol
     
  27. Offline

    LeoFSU

    Using switch is cleaner and looks better.
     
  28. Offline

    MetaSurvival

    LeoFSU
    What does "Switch" mean?
     
  29. Offline

    L33m4n123

    Well what you can also do is taking a HashMap<String, String> colors = new HashMap<String, String>();
    instead all of those lists you got for now.

    To add the player to that you do

    colors.put(playerName, "color");

    To check which color he has you can do

    colors.get(playerName).equals("color"); so as the example I did already

    Code:java
    1.  
    2. if arg 0 is black {
    3. if (colors.get(sender.getName()).equals("black")) {
    4. colors.remove(sender.getName());
    5. send him a message that he writes now in white
    6. } else {
    7. colors.put(sender.getName(), "black");
    8. }
    9. }
    10. if arg 0 is blue {
    11. if (colors.get(sender.getName()).equals("blue")) {
    12. colors.remove(sender.getName());
    13. send him a message that he writes now in white
    14. } else {
    15. colors.put(sender.getName(), "blue");
    16. }
    17. }
    18. if arg 0 is green {
    19. if (colors.get(sender.getName()).equals("green")) {
    20. colors.remove(sender.getName());
    21. send him a message that he writes now in white
    22. } else {
    23. colors.put(sender.getName(), "green");
    24. }
    25. }



    switch case constructs do not support strings below Java 1.7 and right now servers out there still use Java 1.6 so you should to compile your plugin in Java 1.6 thus switch-case constructs with strings do not work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    MetaSurvival likes this.
  30. Offline

    MetaSurvival

    L33m4n123
    Would I still have to remove them from a each list?
     
Thread Status:
Not open for further replies.

Share This Page