Solved Value Saving Help

Discussion in 'Plugin Development' started by gabessdsp, Dec 6, 2013.

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

    gabessdsp

    So here is my line of code, onPlayerJoin this line is executed:


    plugin.getConfig().set(player.getName() + ".FirstJoinTime", Long.valueOf(System.currentTimeMillis()));

    and after that I save and reload the config, but if I look at the config file the information I expect to be under the players name and FirstJoinTime is not there at all
     
  2. Offline

    PogoStick29

    The config file is empty? Maybe you aren't saving it correctly. Any exceptions?
     
  3. Offline

    gabessdsp

    PogoStick29

    I run this right after that command is issued:

    plugin.saveConfig();
    plugin.reloadConfig();

    but no, no exceptions are thrown at all. Just for some mysterious reason it's not there.

    Here's my whole block of code just in case it helps:

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. player = event.getPlayer();
    4. unique = Bukkit.getOfflinePlayers().length;
    5.  
    6. if (player.hasPlayedBefore() == false && plugin.getConfig().getString(player.getName()) == null) {
    7. event.setJoinMessage(stringCleaner(plugin.getConfig().getString("messages.NEWPLAYERJOIN")));
    8.  
    9. plugin.getConfig().set(player.getName(), Long.valueOf(System.currentTimeMillis()));
    10. plugin.getConfig().set(player.getName() + ".FirstJoinTime", Long.valueOf(System.currentTimeMillis()));
    11. plugin.saveConfig();
    12. plugin.reloadConfig();
    13.  
    14. } else if (!player.hasPlayedBefore() || plugin.getConfig().getString(player.getName()) == null) {
    15. event.setJoinMessage(stringCleaner(plugin.getConfig().getString("messages.NODATAJOIN")));
    16.  
    17. plugin.getConfig().set(player.getName(), Long.valueOf(System.currentTimeMillis()));
    18. plugin.getConfig().set(player.getName() + ".FirstJoinTime", Long.valueOf(System.currentTimeMillis()));
    19. plugin.saveConfig();
    20. plugin.reloadConfig();
    21.  
    22. } else {
    23.  
    24. //more stuff
    25.  
    26. }
    27. }
     
  4. Offline

    PogoStick29

    That is very strange. Just as a side note, you are setting both the name and name.FirstJoinTime to values, which isn't what you want to do. Also, System.currentTimeMillis() should be a long, so you don't need Long.valueOf().
     
  5. Offline

    gabessdsp

    PogoStick29



    Well I actually do want to set the name to a value, and then set name.FirstJoinTime to a value, but it's not working and I don't know why.

    Also thank you for telling me about the Long.valueOf()

    EDIT: The name itself does get the value assigned, but I need the name.FirstJoinPlus to get the value as well which is what isn't happening. That's how I know it is saving and reloading correctly.
     
  6. Offline

    PogoStick29

    First off, you need a PlayerLoginEvent, not a PlayerJoinEvent, to check if a player has never played before.

    Second, in the first statement, you are checking if the player has not played before and they are not in the config. In the second statement, you are check if the player has not played before or they are not in the config. Delete the second statement or change it.
     
  7. Offline

    gabessdsp



    Umm using onPlayerJoin works for the hasplayedbefore check as well. But also those lines will both be staying because they do separate things. If it's "and" then it means the player has never played before. If it's "or" then that means they've played before, but not since my plugin was installed on the server. I do realize I can change them a little bit to do the same thing but I still need to fix the problem of the FirstJoinTime.


    I got it to mark the FirstJoinTime.

    But now when the user quits the game I have it save the time they left to their name, and that deletes FirstJoinTime.

    How do I change the long after the name instead of making a new one?
     
  8. Offline

    PogoStick29

    1. I am 99% sure you need a PlayerLoginEvent, but if it works for you, great.
    2. Remove the !player.hasPlayedBefore() part from your second if statement, because you don't need it and there could be some cases of players' names being in the config without them playing.
    3. Are you saving it to FirstJoinTime or another variable?
     
  9. Offline

    gabessdsp



    I'm saving it to FirstJoinTime in the config.

    So right now I have it so that when the player joins it goes:

    playername:
    FirstJoineTime: 209486902486


    but when the player logs out I run this bit of code:

    plugin.getConfig().set(player.getName(), Long.valueOf(System.currentTimeMillis()));

    to save their logout time.


    After that the config name gets changed to this:

    playername: 989028590234643

    so the FirstJoinTime value is deleted.
     
  10. Offline

    PogoStick29

    I believe that is because by setting a variable, you are deleting anything under it. The easiest way to solve this is to store the logout time to another variable under playername.

    playername:
    FirstJoinTime: 209486902486
    LastLogoutTime: 696969696969
     
  11. Offline

    gabessdsp


    Alright, I'll just do that then. Thank you.
     
  12. Offline

    PogoStick29

    No problem.
     
  13. Offline

    gabessdsp


    Okay so again it's the same problem.

    On the player join it sets it to

    playername:
    FirstJoinTime: 98402u6946

    but then on logout it deletes that and does


    playername:
    LastJoinTime: 9842609
     
  14. Offline

    PogoStick29

    Can you paste the modified code?
     
  15. Offline

    gabessdsp

    For some reason it appears that the jar file didn't update in my plugins folder correctly, so it actually does work. Nevermind.
     
Thread Status:
Not open for further replies.

Share This Page