Solved List and Colors In config

Discussion in 'Plugin Development' started by random_username, Sep 14, 2013.

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

    random_username

    Hello, first, I want to tell you that I'm new to coding bukkit plugins, so lets say I'm a newbie :) .
    I want my plugin to show in chat:
    Staff:
    Member 1 - Owner
    Member 2 - Admin

    On my onCommand, I wrote:
    Code:java
    1. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    2. Player player = (Player) Sender;
    3. if(commandLabel.equalsIgnoreCase("staff")){
    4. if(Sender.hasPermission("ecore.staff")){
    5. player.sendMessage(ChatColor.AQUA + "Staff:" + getConfig().getList("Staff"));
    6.  
    7. }else{
    8. player.sendMessage(getConfig().getString("noperm"));
    9. }
    10. }
    11. }


    My Config says:
    Code:
    noperm: '&4You Do not have permission to do this.'
     
    Staff:
      - 'Member 1- Owner'
      - 'Member 2- Owner'
    When i use /staff all I get is:
    Staff: Null

    How can I make it appear like in the beginning of the post? Also, how can I make my plugin support color codes like &a from the Config? Thanks for the help :)
     
  2. random_username
    Your config stringlist should be formatted like this
    Code:
    Staff:
    - 'Member 1- Owner'
    - 'Member 2- Owner'
    Also, if you try to send them a List, it's going to return the List object, not a list of strings. Use this instead.
    Code:
    player.sendMessage(ChatColor.AQUA + "Staff:" + Arrays.asList(getConfig().getStringList("Staff")));
     
  3. Offline

    random_username

    The code you gave me for the list is giving me a warning, "Severity and DescriptionPathResourceLocationCreation TimeId
    Type safety : A generic array of List<String> is created for a varargs parameter"
    Is it supposed to be like that?

    Hello, Can someone help please? Is there a way for my plugin to support color codes from the config (i.e. &a)?
    Also, when i tried
    Code:java
    1. player.sendMessage(ChatColor.AQUA + "Staff:" + Arrays.asList(getConfig().getStringList("Staff")));

    It still didn't appear like the format I wanted. I want it to appear in different lines, like when you use /rules in essentials. I'm a new to this, so I don't understand what I can do to fix this :)

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

    random_username

    Bump, still need help please :)
     
  5. Offline

    amhokies

    Code:java
    1. for(Object o : getConfig().getList("Staff"))
    2. {
    3. if(o instanceof String)
    4. {
    5. player.sendMessage((String)o);
    6. }
    7. }

    This bit of code will loop through the list of Strings you have in your config, printing them each on a new line.
     
  6. Offline

    random_username

    that worked, thanks! Also, do you know how to make color codes like &5 from the config show up correctly in game? When I type the command, the chat says &asomething instead of changing the message color. :)
     
  7. Offline

    amhokies

    Yeah, you just need to replace the & with a §

    Code:java
    1. for(Object o : getConfig().getList("Staff"))
    2. {
    3. if(o instanceof String)
    4. {
    5. String s = (String)o;
    6. player.sendMessage(s.replace("&","§"));
    7. }
    8. }
     
  8. Offline

    Gater12

    random_username I believe it's ChatColor.translateAlternateColorCodes('&', "string");
     
    amhokies likes this.
  9. Offline

    amhokies

    I didn't even know that existed. Thanks for sharing :)
     
    Gater12 likes this.
  10. Offline

    random_username

    I have tried applying the code you told me in some other commands, but now I'm getting an error, can you please tell me what have I done wrong? sorry, but i'm new at this. :)
    Code:java
    1. package me.Kait18.EnstopiaCore;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.potion.PotionEffect;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14. public class EnstopiaCore extends JavaPlugin{
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16. public static EnstopiaCore plugin;
    17.  
    18. @Override
    19. public void onDisable() {
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Disabled.");
    22. }
    23.  
    24. @Override
    25. public void onEnable() {
    26. PluginDescriptionFile pdfFile = this.getDescription();
    27. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Enabled.");
    28. getConfig().options().copyDefaults(true);
    29. this.saveDefaultConfig();
    30. }
    31.  
    32. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    33. Player player = (Player) Sender;
    34. if(commandLabel.equalsIgnoreCase("pl")){
    35. if(Sender.hasPermission("ecore.pl")){
    36. player.sendMessage("Plugins " + getConfig().getString("plnum") + ": " + getConfig().getString("pllist"));
    37. ChatColor.translateAlternateColorCodes('&', "string");
    38.  
    39. }else{
    40. player.sendMessage(getConfig().getString("noperm"));
    41. }
    42. }else if(commandLabel.equalsIgnoreCase("help")){
    43. if(Sender.hasPermission("ecore.help")){
    44. for(Object o : getConfig().getList("Help1"))
    45. {
    46. if(o instanceof String)
    47. {
    48. String s = (String)o;
    49. player.sendMessage(s.replace("&","§"));
    50. }
    51. }
    52. }else{
    53. player.sendMessage(getConfig().getString("noperm"));
    54. }
    55. }else if(commandLabel.equalsIgnoreCase("help2")){
    56. if(Sender.hasPermission("ecore.help")){
    57. for(Object o : getConfig().getList("Help2"))
    58. {
    59. if(o instanceof String)
    60. {
    61. String s = (String)o;
    62. player.sendMessage(s.replace("&","§"));
    63. }
    64. }
    65. }else{
    66. player.sendMessage(getConfig().getString("noperm"));
    67. }
    68. }else if(commandLabel.equalsIgnoreCase("Features")){
    69. if(Sender.hasPermission("ecore.features")){
    70. for(Object o : getConfig().getList("Features1"))
    71. {
    72. if(o instanceof String)
    73. {
    74. String s = (String)o;
    75. player.sendMessage(s.replace("&","§"));
    76. }
    77. }
    78. }else{
    79. player.sendMessage(getConfig().getString("noperm"));
    80. }
    81. }else if(commandLabel.equalsIgnoreCase("Features2")){
    82. if(Sender.hasPermission("ecore.features")){
    83. for(Object o : getConfig().getList("Features2"))
    84. {
    85. if(o instanceof String)
    86. {
    87. String s = (String)o;
    88. player.sendMessage(s.replace("&","§"));
    89. }
    90. }
    91. }else{
    92. player.sendMessage(getConfig().getString("noperm"));
    93. }
    94. }else if(commandLabel.equalsIgnoreCase("Features3")){
    95. if(Sender.hasPermission("ecore.features")){
    96. for(Object o : getConfig().getList("Features3"))
    97. {
    98. if(o instanceof String)
    99. {
    100. String s = (String)o;
    101. player.sendMessage(s.replace("&","§"));
    102. }
    103. }
    104. }else{
    105. player.sendMessage(getConfig().getString("noperm"));
    106. }
    107.  
    108. }
    109. return false;
    110. }
    111. }
    112.  
     
  11. Please provide the full error message and the line where the error appears.
     
  12. Offline

    random_username

    Nvm, sorry, Yaml error in config.
     
Thread Status:
Not open for further replies.

Share This Page