Solved /home | List path from configuration file TO message ? [thanks for helping]

Discussion in 'Spigot Plugin Development' started by SlydEkinox, Mar 26, 2020.

  1. Hi, for start : Im sorry for my bad English..

    I have a problem with my Home plugin, he work perfectly but i don't know how send ALL homes of the player from the configuration file, if someone can help me..

    Code (Java):
    Player p = (Player) sender;
               
                if(args.length == 0)
                {
                   
                    if(Essentials.getInstance().getConfig().contains("homes." + p.getName()))
                    {
                       
                        Essentials.getInstance().getConfig().getConfigurationSection("homes." + p.getName()).getKeys(false).forEach(key -> {
                           
                            if(Essentials.getInstance().getConfig().isList("homes." + p.getName() + key))
                            {
                               
                                List<String> list = Essentials.getInstance().getConfig().getStringList("homes." + p.getName() + key);
                               
                                p.sendMessage(list.toString());
                               
                            }
                           
                        });
                       
                    }
                    else
                    {
                       
                        p.sendMessage("§a[§6§lOrikone§a] §cAucun home trouvé dans nos fichiers.");
                       
                    }
                   
                    return true;
                   
                }
     
  2. You are sending the list itself (the reference), not what's in the list.
    There are a few ways to solve this.
    Simple loop:
    Code (Java):
    for(int i=0; i<list.size(); i++) {
    p.sendMessage(list.get(i));
    }
    For-Each loop
    Code (Text):
    for(String home : list) {
    p.sendMessage(home);
    }
    Stream (Java 8+)
    Code (Java):
    list.forEach(h -> p.sendMessage(h));
     
  3. I try to put your "simple loop" but doesn't work :/

    Code:
    Code (Java):

                   if(Essentials.getInstance().getConfig().contains("homes." + p.getName()))
                   {
                     
                       Essentials.getInstance().getConfig().getConfigurationSection("homes." + p.getName()).getKeys(false).forEach(key -> {
                         
                           if(Essentials.getInstance().getConfig().isList("homes." + p.getName() + key))
                           {
                             
                               List<String> list = Essentials.getInstance().getConfig().getStringList("homes." + p.getName() + key);
                             
                               for(int i = 0; i<list.size(); i++)
                               {
                                 
                                   p.sendMessage(list.get(i));
                                 
                               }
                             
                           }
                         
                       });
                     
                   }
                   else
                   {
                     
                       p.sendMessage("§a[§6§lOrikone§a] §cAucun home trouvé dans nos fichiers.");
                     
                   }
     
    No error in console :/
     
  4. Maybe you forgot a . (dot) between p.getName() & key?
     
  5. You're right, for some reason I thought so because I thought it was based off list.stream.forEach()
    @SlydEkinox Then your list might be empty. Try printing the size of the list into the console first.
     
  6. The code is checking the configurationSection so don't need a dot, but i tried with and has the same result :/

    (No console in error and do anything in the chat)

    My configuration:

    Code (Text):

    homes:
      ImNotJoking_:
        nsm:
          world: monde
          x: -1.0015900281109733
          y: 65.0
          z: 328.47211026411736
          pitch: 4.9342566
          yaw: 16.935791
        test:
          world: monde
          x: -5.715829959223781
          y: 64.0
          z: 356.26049406768726
          pitch: 4.9342566
          yaw: 16.935791
        test123:
          world: monde
          x: -7.82726220090097
          y: 64.0
          z: 359.52119305341677
          pitch: 4.9342566
          yaw: 16.935791
        test1234:
          world: monde
          x: -7.870231869019727
          y: 64.0
          z: 364.8198934612711
          pitch: 4.9342566
          yaw: 16.935791

     
     
  7. I believe locations are serializable, so you can do homeLoc.serialize() and Location.deserialize(pathToLoc).
    You can still print out the list to the console so you know what's not working.
     
  8. i tried a sysout contains "list.size()" but anything in the console :/
     
  9. Are you sure the code is being run at all?
    Go to the top of your method and test with System.out.println("debug message") which messages are being send and which not.
     
  10. Yes for sur ! When a do /home <name> im teleported at the home, the cmd work perfectly :/

    (for sure, i tried the sysout at the top and it work)
     
  11. Okay.. i just send "key" to the player and it work but i don't know why the player receive all messages line by line ??
     

    Attached Files:

  12. Well, that's what your code does. You send them their homes line by line.
    I think your code doesn't pass this statement, because looking at your config you have
    Code (Text):
    homes:
      player1:
        loc1
        loc2
    That requires
    Code (Text):
    #getStringList("homes." + playerName + "." + homeName)
    EDIT: Guess I'm just confused...
    Just in general it's not ideal to store playerNames because a player can change their name. Instead use
    Code (Java):
    player.getUniqueID().toString()
    to store a player and do
    Code (Java):
    UUID uuid = UID.fromString(#getString("whatever path").trim());
    Player player = Bukkit.getPlayer(uuid);
     
    to get the player back.