Can't write in config files and checks are getting skipped! [QUICK HELP NEEDED]

Discussion in 'Plugin Development' started by Ahmet094, Dec 9, 2012.

Thread Status:
Not open for further replies.
  1. Hey guys,

    it shouldn't be that hard to figure out what's wrong with my code, but atm I just can't get it working..

    It's a part of a friends/peace plugin with which you can send a "peace request" to a player and so on. While sending the request of course it should be checked if they aren't already friends..
    This part is somehow getting skipped and the name of the player doesn't get added to the config.
    So it's possible to send requests over and over to the same player..
    I'm sorry if it's hard to get into the code, but I translated the main parts.

    And the config should look like this:

    Code:
    Naps:
      Ahmet094:
          - TestPlayer
          - Player
          - Player 2
    Requests:
      Ahmet094:
          - TestPlayer2     [ <-- this one should get deleted after the request is getting accepted ]
    
    Means that Ahmet094 has 3 friends and a pending request for TestPlayer2



    Code:
      @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if ((sender instanceof Player)) {
                if ((label.equalsIgnoreCase("frieden"))) {
                    Player p = (Player) sender;
                    String pName = p.getName();
                    if (args.length == 0) {
                        p.sendMessage("mehr infos!");
                        return true;
                        ///  PLUGIN INFO
                    }
     
                    if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("start")) {
                            Player s = Bukkit.getPlayer(args[1]);
     
     
     
     
                            List<Player> matches = getServer().matchPlayer(args[1]);
     
                            if (matches.isEmpty()) {
                                p.sendMessage(prefix + "Player isn't online!");
                            } else {
                                Player f = matches.get(0);
                                String fName = f.getName();
     
                                if (!conf.isSet("Naps." + pName)) {
                                    conf.set("Naps." + pName, null);
                                    saveConfig();
                                }
     
                                if (!conf.isSet("Requests." + pName)) {
                                    conf.set("Requests." + pName, null);
                                    saveConfig();
                                }
     
                                if (!conf.isSet("Naps." + fName)) {
                                    conf.set("Naps." + fName, null);
                                    saveConfig();
                                }
     
                                if (!conf.isSet("Requests." + fName)) {
                                    conf.set("Requests." + fName, null);
                                    saveConfig();
                                }
     
                                if (!conf.getStringList("Naps." + pName).contains(fName)) {
                                    if (!conf.getStringList("Naps." + fName).contains(pName)) {
                                        if (!conf.getStringList("Requests." + pName).contains(fName)) {
                                            if (!conf.getStringList("Requests." + fName).contains(pName)) {
                                                conf.getStringList("Requests." + pName).add(fName);
                                                saveConfig();
                                                p.sendMessage(prefix + "Request was sent to " + fName);
                                                f.sendMessage(prefix + "You got a request from " + pName);
                                                saveConfig();
                                                return true;
                                            } else {
                                                f.sendMessage(prefix + pName + " wants to remind you bla bla..");
                                                p.sendMessage(prefix + "There's already a pending request for bla bla " + ChatColor.GOLD + fName);
                                                return true;
                                            }
                                        } else {
                                            f.sendMessage(prefix + pName + " wants to remind you bla bla..");
                                            p.sendMessage(prefix + "There's already a pending request for bla bla " + ChatColor.GOLD + fName);
                                            return true;
                                        }
                                    } else {
                                        p.sendMessage(prefix + "You're already in peace bla bla !");
                                        return true;
                                    }
                                } else {
                                    p.sendMessage(prefix + "You're already in peace bla bla !");
                                    return true;
                                }
                            }
                        }
                        if (args[0].equalsIgnoreCase("annehmen")) {
                        }
                    }
                }
            }
            return false;
        }

    Thanks in advance!
     
  2. try adding debug handlers to the
    Code:java
    1. if (!conf.isSet("Naps." + pName)) {
    2. conf.set("Naps." + pName, null);
    3. saveConfig();
    4. }
    parts, I think something messes up there
     
  3. Do you have example code pls?

    With createSection it creates this areas in the config, which's just right

    Code:
    Naps:
      Ahmet094: {}
      Ahmet: {}
    Requests:
      Ahmet094: {}
      Ahmet: {}
    
    Code:
    if (!conf.contains("Naps." + pName)) {
                                    conf.createSection("Naps." + pName);
                                    saveConfig();
                                }
                             
                                if (!conf.contains("Anfragen." + pName)) {
                                    conf.createSection("Anfragen." + pName);
                                    saveConfig();
                                }
                             
                                if (!conf.contains("Naps." + fName)) {
                                    conf.createSection("Naps." + fName);
                                    saveConfig();
                                }
                             
                                if (!conf.contains("Anfragen." + fName)) {
                                    conf.createSection("Anfragen." + fName);
                                    saveConfig();
                                }


    But it doesn't add the "friends" to the list..

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

    Lolmewn

    should be
    Code:
    node:
      anotherNode:
      - ListItem
      - ListItem2
     
  5. This doesn't help me, as I know that it should look like this..
    Any ideas? :/

    This part doesn't seem to work although everything is written correctly..
    Code:
    conf.getStringList("Requests." + pName).add(fName);
    saveConfig();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  6. Offline

    Lolmewn

    That's because you get a List<String>, and add the value to it. You should use conf.set(yourPath, yourList);
     
    Ahmet094 likes this.
  7. Shiiit, it worked!

    Code:
    List liste = conf.getStringList("Requests." + pName);
    liste.add(fName);
    conf.set("Requests." + pName, liste);
    Thanks so much! :)
     
  8. Offline

    Lolmewn

Thread Status:
Not open for further replies.

Share This Page