config

Discussion in 'Plugin Development' started by TerroDoor, Apr 2, 2020.

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

    TerroDoor

    So nothing is coming up in my config, I think im talking to it wrong. I want all players to get put into the "player-data" Section if they aren't already on join.

    Code:
    public class Join implements Listener {
     public Main pl;
     public Join(Main ins) {
     pl = ins;
     }
     @EventHandler
     public void onJoin(PlayerJoinEvent e) {
     e.setJoinMessage(null);
    
     Player p = (Player)e.getPlayer();
     String uuid = p.getUniqueId().toString();
     String name = p.getName().toLowerCase();
     int kills = 0;
     int deaths = 0;
     int streak = 0;
     int bal = 0;
    
     if (pl.getConfig().getString("player-data").contains(".uuid" + uuid)) {
     p.sendMessage("welcome back!");
     
    } else {
    
     pl.getConfig().set("player-data", ".name" + name);
     pl.getConfig().set("player-data", ".uuid" + uuid);
     pl.getConfig().set("player-data", ".kills" + kills);
     pl.getConfig().set("player-data", ".deaths" + deaths);
     pl.getConfig().set("player-data", ".streak" + streak);
     pl.getConfig().set("player-data", ".balance" + bal);
     pl.saveConfig();
    
     return;
     }
     return;
     }
     
  2. Offline

    KarimAKL

    @TerroDoor You're using the ConfigurationSection#set() & Configuration#get() methods wrong.
    Code:Java
    1. // It should be
    2. #set("player-data.name", name)
    3.  
    4. // Instead of
    5. #set("player-data", ".name" + name)
    6.  
    7. // Also, the get method should be
    8. #getString("player-data.uuid." + uuid) != null
    9.  
    10. // Instead of
    11. #getString("player-data").contains(".uuid" + uuid)

    Please do try to understand how they work, instead of just copying what i wrote here.
     
    Last edited by a moderator: Apr 2, 2020
    TerroDoor likes this.
  3. Offline

    TerroDoor

    @KarimAKL what is the null check doing? if I have another player join the server with a different ID will it check if it's their's that's null?
     
    KarimAKL likes this.
  4. Offline

    KarimAKL

    @TerroDoor ConfigurationSection#get() will return null if nothing was found at the path so, yes, it checks if their UUID is at that path.

    Also, i just noticed i made a mistake on that part, i've edited it now.
     
  5. Offline

    TerroDoor

    @KarimAKL Thanks for the help, im understanding more. Why wouldn't the .contains method work in this case?

    Also, here's the updated code, It won't send the player his welcome message..

    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    e.setJoinMessage(null);
    Player p = (Player)e.getPlayer();
    String uuid = p.getUniqueId().toString();
    String name = p.getName().toLowerCase();
    int kills = 0;
    int deaths = 0;
    int streak = 0;
    int bal = 0;
    if (pl.getConfig().getString("player-data.uuid" + uuid) != null) {
    p.sendMessage("welcome back!");
    } else {
    pl.getConfig().set("player-data" + ".uuid", uuid);
    pl.saveConfig();
    return;
    }
    
    It saves correctly into the config tho

    EDIT; I just switched my code around and implemented the .contains method, the welcome message is firing. Here's the update:

    Code:
     if (pl.getConfig().getString("player-data.uuid").contains(uuid)) {
     p.sendMessage("welcome back!");
     } else {
     
  6. @TerroDoor It appears as if you are trying to get the string from the config and add the player's UUID on the end of it. I think what you would have to do is:
    Code:
    if (pl.getConfig().getString("player-data." + uuid != null) {
        p.sendMessage("welcome back!")
    }
    
     
  7. Offline

    TerroDoor

    @FreakyPear5 It's not saving every player its overriding them in the existing section, "player-data". I want to put every player that joins into this config section
     
  8. Ah right, ok.
     
  9. Offline

    KarimAKL

    Because what you were doing was getting the string from the path "player-data", without even checking if it exists, then checking if the string (".uuid" + uuid) is inside of the string gotten.
    Example:
    Code:Java
    1. // If the path "player-data" doesn't exist
    2. #getString("player-data").contains(".uuid" + uuid) // Would throw a NullPointerException
    3.  
    4. // If the path "player-data" does exist
    5. String uuid = "SomeUUID"; // Just an example in this case
    6. #getString("player-data").contains(".uuid" + uuid) // Would return true if the string at the path was ".uuidSomeUUID", not "SomeUUID"


    Give me an example of how you want the file to look and how it does look, then i can understand it better.
     
  10. Offline

    TerroDoor

    Thanks for this I’m starting to understand. Basically every player that joins I want to be put into a “player-data” list with their name uuid etc.

    Example config :

    player-data:
    name: name
    UUID: uuid

    And so on for every player that joins. If the config is empty when I first start up the server and the first player joins how can I create the “player-data” string and also add the player into it. When the second player joins they will also be checked if they are in. That list and if not be placed in there too

    Example

    Player-data:
    Name: p1
    UUID: uuid

    Name: p2
    UUID: uuid



    Sent from my iPhone using Tapatalk
     
  11. Offline

    KarimAKL

    @TerroDoor The way you're doing it is wrong, you can't have multiple keys with the same name in the same section.

    You should change it from:
    Code:YAML
    1. player-data:
    2. name: playerName1
    3. uuid: uuid1
    4. name: playerName2
    5. uuid: uuid2

    To:
    Code:YAML
    1. player-data:
    2. uuid1:
    3. name: playerName1
    4. uuid2:
    5. name: playerName2


    If you're still having trouble, let me know.
     
  12. Offline

    TerroDoor

    I wrote this off my phone how it looks on my computer is:

    Player-data:

    Uuid
    Name


    Sent from my iPhone using Tapatalk
     
  13. Offline

    KarimAKL

    @TerroDoor Use code blocks to keep the indention.

    Also, please do show how it looks with 2 players.
     
  14. Offline

    TerroDoor

    Well basically it puts player1 into the config like shown above BUT when player 2 joins, player 1 gets overwritten instead of being placed underneath player1s data


    Sent from my iPhone using Tapatalk
     
  15. Offline

    KarimAKL

    @TerroDoor I stated the reason before:
    If you use the same key, it'll just overwrite it.
     
  16. Offline

    TerroDoor

    @KarimAKL so they cant all be stored inside of "player-data"?
     
  17. Offline

    KarimAKL

    @TerroDoor No, you need to do something like what i posted earlier.
     
  18. Offline

    timtower Administrator Administrator Moderator

    KarimAKL likes this.
  19. Offline

    TerroDoor

    @KarimAKL

    it still overrides and only puts one player in at a time, tips?
    Sorry im really bad and I always get myself confused but im ambitious to learn this.

    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    e.setJoinMessage(null);
    Player p = (Player)e.getPlayer();
    String uuid = p.getUniqueId().toString();
    
    if (pl.getConfig().getString("player-data.uuid" + uuid) != null) {
    
    p.sendMessage("welcome back");
    
    } else {
    
    pl.getConfig().set("player-data.name", p.getName().toLowerCase());
    pl.getConfig().set("player-data.uuid", uuid);
    pl.saveConfig();
    
    return;
    }
    
    return;
    }
    }
    
    Config.yml

    Code:
    player-data:
      name: soaker
      uuid: 98b3475b-4e76-3008-b046-d448eaf4
     
  20. Offline

    KarimAKL

    @TerroDoor When you use this:
    Code:Java
    1. #set("player-data.name", p.getName().toLowerCase())

    You're setting the key as "player-data.name", then it overwrites it the second time you use it, you need to include the player's UUID in that key. This also means that you won't have to set the UUID as a value.
     
  21. Offline

    TerroDoor

    So Config.set(“player-data.name” + p.getNane().toLowerCase(), p.getName.Tolowercase();

    Wait so what will the value be?


    Sent from my iPhone using Tapatalk
     
  22. Offline

    KarimAKL

    @TerroDoor
    1. Use the player's UUID as part of the key, not their name.
    2. You need a section separator between "name" and the UUID.
    3. I'd make the "name" key a sub-section of the UUID section.
     
    TerroDoor likes this.
  23. Offline

    TerroDoor

    Thankyou so much I got it working wen you mentioned sub-key, I realised I couldn’t have every path inside player-data starting with “name” as that pathwould already be existing with another player. Instead the key the player name and the uuid is now a sub key using “player-data.” + p.getname() + “.uuid”, uuid;

    I’ll post my code when I’m home, Thankyou again for your help and patience as I’m a slow learner..



    Sent from my iPhone using Tapatalk
     
  24. Offline

    KarimAKL

    As i said before:
    So that would be:
    Code:Java
    1. ("player-data." + uuid + ".name", p.getName())


    No problem, i especially enjoy helping people that ask questions like you did:
     
    TerroDoor likes this.
  25. Offline

    TerroDoor

    Oh how it all makes sense now, finally :p

    Thankyou @KarimAKL you’ve been a great help


    Sent from my iPhone using Tapatalk
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page