Solved [1.13] Setting standard money doesn't work

Discussion in 'Spigot Plugin Development' started by TheGameKing77, Jul 25, 2018.

  1. Hi,

    For some reason, im having trouble with something, the outcome is probably easier than i think but i just cant get it worked out.

    So in my config i have a path: "options.defaultAmount" set to 25. I want that when a player joins for the first time, it sets that amount as defaultAmount in the userData.yml

    This is the part in my onJoin where the new player is registered:
    Code (Java):
    try {
                   
                    String pid = p.getUniqueId().toString();
                   
                    int defStart = 0;
                    int defaultTokens = userData.getInt("options.defaultAmount");
                   
                    userData.load(userDataFile);
                    userData.set("user." + pid + ".name", p.getName());
                    userData.set("user." + pid + ".uuid", pid);
                    userData.set("user." + pid + ".tokenAmount", defStart + defaultTokens); //this is what is going wrong
                    userData.save(userDataFile);
                   
                } catch(Exception ex) {
                    if(config.getBoolean("options.useConsoleColors")) {
                        ConsoleLogs.Error(ChatColor.RED + "Something went wrong while creating new userRec in onJoin event");
                    } else {
                        ConsoleLogs.Error("Something went wrong while creating new userRec in onJoin event");
                    }
                    ex.printStackTrace();
                   
                }
    I do not get any console errors, it just sets the "user.uuid.tokenAmount" to 0
    What is going wrong? (code in front of "//this is what is going wrong" is the troublemaker.)
     
  2. Is userData actually your default config? Seems like an odd name ... guessing it's not loaded correctly but hard to tell without seeing that part of the code.
     
  3. Default config is just config.yml, but all users are saved in userData.yml. And its loaded correctly, no console errors, sets all the other paths perfectly fine. It just sets tokenAmount to 0 instead of 25

    EDIT: Wow.. sorry, didnt read.. My fault, see now that instead of config there is userData. Sorry!!
     
  4. Oke, was happy too quickly.. Right now this is my new code: (Full onJoin)
    Code (Java):
    @EventHandler
        public void onJoin(PlayerJoinEvent event) {
         
            Player p = event.getPlayer();
         
            //When a player is not registered in the userData.yml, their needs to be created a new userRec.
            //First there need to be checked if the configVersion is 3, then set all the values in the config using UUID's
         
            //Check if configVersion is right
            if(!(userData.contains("user." + p.getUniqueId().toString()))) {
             
                //Player isnt in the userData, make a new record for him
                try {
                 
                    String pid = p.getUniqueId().toString();
                 
                    int defStart = 0;
                    int defaultTokens = config.getInt("options.defaultAmount");
                 
                    userData.load(userDataFile);
                    userData.set("user." + pid + ".name", p.getName());
                    userData.set("user." + pid + ".uuid", pid);
                    userData.set("user." + pid + ".tokenAmount", config.getInt("options.defaultAmount")); //this is what is going wrong
                    userData.save(userDataFile);
                 
                } catch(Exception ex) {
                    if(config.getBoolean("options.useConsoleColors")) {
                        ConsoleLogs.Error(ChatColor.RED + "Something went wrong while creating new userRec in onJoin event");
                    } else {
                        ConsoleLogs.Error("Something went wrong while creating new userRec in onJoin event");
                    }
                    ex.printStackTrace();
                 
                }
             
                if(config.getBoolean("options.useConsoleColors")) {
                    //found in the records, cc
                    ConsoleLogs.Info(ChatColor.DARK_AQUA + "Player " + ChatColor.GREEN + p.getName() + ChatColor.DARK_AQUA + " has joined for the first time using this plugin! Registering him in userData.yml...");
                } else {
                    //found in the records, no cc
                    ConsoleLogs.Info("Player " + p.getName() + " has joined for the first time using this plugin! Registering him in userData.yml...");
                }
             
            } else {
             
                //player is fouund in the record
                if(config.getBoolean("options.useConsoleColors")) {
                 
                    //use consolecolor
                    ConsoleLogs.Info(ChatColor.DARK_AQUA + "Player " + ChatColor.GREEN + p.getName() + ChatColor.DARK_AQUA + " is found in the records!");
                 
                } else {
                 
                    //do not use consolecolor
                    ConsoleLogs.Info("Player " + p.getName() + " is found in the records!");
                 
                }
            }
         
        }
    And it still sets tokenAmount in userData to 0.. I just can't find the troublemaker thing here..

    EDIT: Turned out that i forgot to load the default config, sorry for this post. Still thanks for your help!
     
    #4 TheGameKing77, Jul 25, 2018
    Last edited: Jul 25, 2018