Solved Config Names

Discussion in 'Plugin Development' started by Coopah, Jul 30, 2014.

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

    Coopah

    Does anyone know a way where I can create something in a config file which would store a bunch of names? For example, I would like a heading saying "Members:" and then under that it would list all the players names that are in that group. And then after the players name it would have a suffix. Example: cooper: leader. Also how would I check would suffix they have?
     
  2. Coopah You mean like this?
    Code:
    members:
      cooper: leader
      adamqpzm: knows everything apparently
    You can do that by using the Configurtation API and the above examples would be a string with the path of "members.cooper" or "members.adamqpzm" and you'd get/set the String with such a path
     
  3. Offline

    Coopah

    AdamQpzm
    Yes, however, I don't want it to be a constant thing. When they create a clan using /c create <name> I want it to add the name of the clan to the config file and then under that all the members so if it only had the creator it would have, cooper: leader. Also how would I check for something after the name, in this case, the leader part.
     
  4. Coopah I'm not sure what you mean, can you show me an example config?
     
  5. Offline

    Coopah

    AdamQpzm
    Okay, so when a player types /c create <name> we will use the name as test in this example, it will create in the config file and section called test. It will also create a section called "Members" which would list all the members in it. Under members it will have the person that typed /c create test name and then what role he is, in this case, he's the leader.
    So the config file would look like:
    Test:
    Members:
    cooper1289: leader
     
  6. Coopah In that case it would be the same principle except that the path would be clanname.members.username
     
  7. Offline

    Coopah

    AdamQpzm
    I have this, however when someone else types /c create test2 it changes all the data to test2. How do I make a uniqueone each time?
    Code:
    Code:java
    1. String value = clanname;
    2. String owner = p.getName();
    3. getConfig().set("path.clans.clanname", value);
    4. getConfig().set("path.clans.creator", owner);
    5. getConfig().set("path.clans.members", "");
    6. saveConfig();
     
  8. Coopah You should replace the hardcoded String with the name of the clan, so that it'll be unique
     
  9. Offline

    Coopah

    AdamQpzm
    I can't change the "String" or by changing the path.clans.clanname wont make it unique? So under path: I want it to be the clanname
     
  10. Coopah I mean do a path like this:

    Code:
    getConfig().set("clans." + clanname + ".creator", owner);
     
  11. Offline

    Coopah

    AdamQpzm
    Awhh thank you. Might have a few more questions to come :3

    AdamQpzm
    So how would I check the name of the clan the player is in?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  12. Coopah You'd have to iterate over every clan's members until you find the one you're looking for
     
  13. Offline

    Coopah

    AdamQpzm
    I want something when a player types /clan it gets the clan name that their in. How would I do this?
     
  14. Offline

    Coopah

    AdamQpzm
    This is giving me a null pointer error on this line:
    Code:java
    1. p.sendMessage(Strings.prefix() + "Clan: " + ChatColor.YELLOW + getConfig().getConfigurationSection("path.clans." + p.getName() + ".clanname").getKeys(false));
    2.  

    I'm creating it here:
    Code:java
    1. String owner = p.getName();
    2. getConfig().set("path.clans." + p.getName() + ".clanname", clanname);
    3. getConfig().set("path.clans." + p.getName() + ".creator", owner);
    4. getConfig().set("path.clans." + p.getName() + ".mods", "");
    5. getConfig().set("path.clans." + p.getName() + ".members", "");
    6. saveConfig()
     
  15. Coopah Output each of those values to see which is null
     
  16. Offline

    Coopah

    AdamQpzm
    I believe the .clanname is null, when I removed it, it worked fine.
     
  17. Coopah You'd have to check your config file to make sure it contains the path you're accessing.
     
  18. Offline

    Coopah

    AdamQpzm
    Config File:
    HTML:
    path:
      clans:
        cooper1289:
          clanname: test
          creator: cooper1289
          mods: ''
          members: ''
    Seems fine right?
     
  19. Coopah As far as I can tell, yes.
     
  20. Offline

    Coopah

  21. Offline

    Necrodoom

    Coopah paste full class and explain which line produces NPE.
     
  22. Offline

    Coopah

    Necrodoom
    Code:
    Code:java
    1. public void onEnable() {
    2. getServer().getPluginManager().registerEvents(this, this);
    3. FileConfiguration config = getConfig();
    4. config.addDefault("Clans Plugin", "Made by Cooper1289");
    5. config.addDefault("Enabled", true);
    6. getConfig().options().copyDefaults(true);
    7. saveConfig();
    8. }
    9.  
    10. public static List<String> joinreq = new ArrayList<String>();
    11.  
    12. public boolean onCommand(CommandSender cs, Command cmd, String s, String[] args) {
    13. Player p = (Player) cs;
    14. if (cmd.getName().equalsIgnoreCase("clans")) {
    15. if (args.length < 1) {
    16. p.sendMessage(Strings.prefix() + "Clan: " + ChatColor.YELLOW + getConfig().getConfigurationSection("path.clans." + p.getName() + ".clanname").getKeys(false));
    17. } else if (args[0].equalsIgnoreCase("help")) {
    18. p.sendMessage("for forums");
    19. } else if (args[0].equalsIgnoreCase("create")) {
    20. if (args.length == 1) {
    21. p.sendMessage(Strings.prefix() + "Please enter a Clan Name.");
    22. return true;
    23. } else if (args.length == 2) {
    24. String clanname = args[1];
    25. if (getConfig().getBoolean("Enabled")) {
    26. if (getConfig().contains(clanname)) {
    27. p.sendMessage(Strings.prefix() + "This Clan Name already exists.");
    28. return true;
    29. } else {
    30. if (getConfig().contains(p.getName())) {
    31. p.sendMessage(Strings.prefix() + "You are already in a Clan.");
    32. return true;
    33. } else {
    34. Bukkit.getServer().broadcastMessage(Strings.prefix() + ChatColor.GREEN + p.getName() + ChatColor.GRAY + " has formed Clan: " + ChatColor.YELLOW + clanname + ChatColor.GRAY + ".");
    35. String owner = p.getName();
    36. getConfig().set("path.clans." + p.getName() + ".clanname", clanname);
    37. getConfig().set("path.clans." + p.getName() + ".creator", owner);
    38. getConfig().set("path.clans." + p.getName() + ".mods", "");
    39. getConfig().set("path.clans." + p.getName() + ".members", "");
    40. saveConfig();
    41. p.playSound(p.getLocation(), Sound.NOTE_BASS, 1.0F, 1.0F);
    42. return true;
    43. }
    44. }
    45. } else {
    46. p.sendMessage(Strings.prefix() + "Plugin has been disabled! Please contact a Server Admin right away.");
    47. return true;
    48. }

    Line 16 is the NPE.
     
  23. Offline

    Coopah

    Bumpio
     
  24. Offline

    xTigerRebornx

    Coopah The path you are getting isn't a configuration section, and none in your config exists at that path, therefor its null.
     
  25. Offline

    Coopah

    xTigerRebornx
    It does exist in my config, it shouldn't be returning null.
     
  26. Offline

    xTigerRebornx

    Coopah
    Code:
    getConfig().getConfigurationSection("path.clans." + p.getName() + ".clanname")
    That path is clearly a String in your config, not a ConfigurationSection
     
  27. Offline

    Coopah

  28. Offline

    xTigerRebornx

    Coopah Make the path point the ConfigurationSection that you want, not the String.
     
  29. Offline

    Coopah

    xTigerRebornx
    How would I check for something that's not a string in a configurationsection?
     
Thread Status:
Not open for further replies.

Share This Page