How to check if a String already exists in config?

Discussion in 'Plugin Development' started by PerezHD, Feb 22, 2015.

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

    PerezHD

    Hey guys, I am having some issues with my configuration file and code. I am attempting to create a titles plugin and I need some help on strings for configurations. I am trying to make it so if the title of args[0] already exists in the config, it will send a player a message saying it already exists.

    CODE:

    Code:
                      if (args.length == 2){
                          String title = args[0];
                          String prefix = args[1];
                          if (getConfig().getString("Titles." + title + ".Title", String.valueOf(prefix)) == null){
                              p.sendMessage(ChatColor.RED + "That title already exist!");
                              return true;
                          } else {
                              if(title.length() <= 16 && title.length() >= 4){
                                  if(prefix.length() <= 16 && prefix.length() >= 4){
                                      p.sendMessage(ChatColor.GREEN + "You have created a title called " + ChatColor.YELLOW + args[0] + ChatColor.GREEN + " with the prefix " + ChatColor.GRAY + "'" + ChatColor.RESET + args[1]
                                              .replace("_", " ")
                                              .replace("&", "ยง") + ChatColor.GRAY + "'" + ChatColor.GREEN + ".");
                                      getConfig().set("Titles." + args[0] + ".Title", String.valueOf(args[1]));
                                      saveConfig();
                                      return true;
                                  } else {
                                      p.sendMessage(ChatColor.RED + "Your title prefix needs to be 4 chars minimum and 16 chars max.");
                                      return true;
                                  }
                              } else {
                                  p.sendMessage(ChatColor.RED + "Your title needs to be 4 chars minimum and 16 chars max.");
                                  return true;
                              }
                          }
                      }

    Here is what is happening in-game:
    [​IMG]

    I can basically spam it. Please help me.
     
  2. Offline

    Epicballzy

    Check if the section is null?
    Code:java
    1. if(myFile.getString("title." + args[0[) == null) {
    2. //stuff
    3. }
     
  3. Offline

    PerezHD

    That's what I did in the code above...
     
  4. Offline

    1Rogue Retired Staff

    Code:java
    1. if (getConfig().getString("Titles." + title + ".Title", String.valueOf(prefix)) == null){


    This will never evaluate to true, because you supplied a default value if nothing existed there.
     
    PerezHD likes this.
  5. Offline

    TheEntropy

    edit: ninja'd :/
     
  6. Offline

    PerezHD

    I see what you are saying. So then how would I go into mind with this? Show me what to do?
     
  7. Offline

    1Rogue Retired Staff

    Stop supplying a default value?
     
  8. Offline

    PerezHD

    @1Rogue I fixed it. What I did was if (getConfig().contains...
     
  9. @PerezHD Glad that your problem is solved. Please set this thread to Solved. Thread Tools > Edit Title > Prefix > Solved
     
Thread Status:
Not open for further replies.

Share This Page