[WIP] Logging Player Lists in Config.

Discussion in 'Plugin Development' started by bendbassett, Jul 22, 2014.

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

    bendbassett

    Hi im trying to make it so that players can make and access lists they make in game. But i'm having trouble with the variables, but can have unlimited lists, and i would like the config to look like this.​
    Players:​
    VOLCOMXD22:​
    Fruits:​
    - Apples​
    - Oranges​
    - Bananas​
    TylerCvb12:​
    Vegetables:​
    - Lettuce​
    - Broccoli​
    - Carrots​
    Meats:​
    - Chicken​
    - Beef​
    - Fish​

    Here is what i have so far, but i'm having trouble with my variables

    Code:java
    1. package me.Ben.Lister;
    2.  
    3. import java.util.List;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Lister extends JavaPlugin{
    13.  
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. private String ConfigLoaded = "[Lister] *Beta*: config file loaded";
    16. private String ConfigSaved = "[Lister] *Beta*: config file saved";
    17. FileConfiguration config = getConfig();
    18.  
    19. @Override
    20. public void onEnable() {
    21. getConfig().options().copyDefaults(true);
    22. saveConfig();
    23. logger.info(ConfigLoaded);}
    24.  
    25. public void onDisable() {
    26. saveConfig();
    27. logger.info(ConfigSaved);}
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    30. if(cmd.getName().equalsIgnoreCase("lister")) {
    31. Player p = (Player) sender;
    32. String pname = p.getName();
    33.  
    34. if(args.length < 1) {
    35. return false;}
    36. if(args.length > 3) {
    37. return false;}
    38.  
    39. if(args.length == 2) {
    40. if(args[1] == "new"){
    41. String list = args[2];
    42. List<String> names = this.getConfig().getStringList("players");
    43. List<String> player = this.getConfig().getStringList(p);
    44. this.getConfig().set("players."+p, args[2]);
    45. }
    46.  
    47. if(args[1] != "new"){
    48. return false;}
    49. }
    50.  
    51.  
    52. }
    53. }
    54. }
    55.  
     
  2. Offline

    CorrieKay

    If your configuration file looks exactly like what you posted here, VOLCOMXD22 is not a sub-node of Players. Also, you're getting "players" not "Players" in your "new" subcommand.

    You're getting two lists, for whatever reason, and then not using them. When you call set(), you're setting "players.<playername>" to a string, which is the third argument of the args array.

    Lastly, array indices start at 0, not one. So you're likely to get an array index out of bounds exception with this code. Change line 40 to "args[0]" and line 41 to "args[1]"
     
  3. Offline

    bendbassett

    i understand what i did wrong with the args problem, and have fixed what i needed to do but still don't understand how to set the players name a subcategory under the players category and wonder what i did solved it?

    Code:java
    1. package me.Ben.Lister;
    2.  
    3. import java.util.List;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Lister extends JavaPlugin{
    13.  
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. private String ConfigLoaded = "[Lister] *Beta*: config file loaded";
    16. private String ConfigSaved = "[Lister] *Beta*: config file saved";
    17. FileConfiguration config = getConfig();
    18.  
    19. @Override
    20. public void onEnable() {
    21. getConfig().options().copyDefaults(true);
    22. saveConfig();
    23. logger.info(ConfigLoaded);}
    24.  
    25. public void onDisable() {
    26. saveConfig();
    27. logger.info(ConfigSaved);}
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    30. if(cmd.getName().equalsIgnoreCase("lister")) {
    31. Player p = (Player)sender;
    32.  
    33. if(args.length < 1) {
    34. return false;}
    35. if(args.length > 3) {
    36. return false;}
    37.  
    38. if(args.length == 2) {
    39. if(args[0] == "new"){
    40. String list = args[1];
    41. List<String> newlist = this.getConfig().getStringList(args[0]);
    42. this.getConfig().set("players."+p, newlist);
    43. saveConfig();
    44. }
    45.  
    46. if(args[1] != "new"){
    47. return false;}
    48. }
    49.  
    50.  
    51. }
    52. return false;
    53. }
    54. }
     
  4. Offline

    CorrieKay

    Alright, i think there may be a little misunderstanding on how exactly configurations work here.

    Basically, you're trying to allow a player to create text lists. You store them like this:

    Code:
    players:
      <playername>:
        <listName>:
        - <list entry 1>
        - <list entry 2>
    If you're trying to create a new list, you need to expect that there will be no list there anyways. Therefore, you want to set an empty list to the node. DO so like this:

    getConfig().set("players." + p.getName() + "." + args[1], new ArrayList<String>());

    To modify a list, adding something, you'll want to grab a list (making sure to account for players trying to add something to a list that hasnt been created yet), add to it, then set it back to the node, and then save the config. Something like this:

    Code:Java
    1. List<String> list = getConfig().getStringList("players." + p.getName() + "." + args[1]);
    2. if(list == null){
    3. p.sendMessage("Null list!");
    4. return true;
    5. }
    6. list.add(args[2]);
    7. getConfig().set("players." + p.getName() + "." + args[1], list);


    Does this make sense?
     
  5. Offline

    bendbassett

    yes, thank you ill try this out, and get back as soon as i can

    i also am trying to make it so that if the list doesn't exist, then return it false, i have a simple idea of making it out of a if statement.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page