Solved Trouble generating player data files

Discussion in 'Plugin Development' started by vertigolf, Sep 16, 2014.

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


    Dear bukkit developers,

    It has come to my attention that YAML playerdata files will mix up if 2 players join at the same time or join next to eachother. This means that the name of the file will get the UUID of the first player and the defaults of the second player will be written. There isn't a file created for the second player, nor are the defaults stored of the first player.

    I have spend quite some time in solving this issue but i haven't found a solution yet.

    I have tried: Hashmaps, code without maps, final-izing alot of identifiers, but nothing seems to work.

    The code is attached. Could you guys help me or give me suggestion on what to do?

    My joinevent:

    1. package;
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import java.util.UUID;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    13. import;
    14. import;
    16. public class hjacraftjoinevent implements Listener
    17. {
    18. //constructor
    19. playerdatafile data;
    20. hjacraftmain plugin;
    21. public hjacraftjoinevent(hjacraftmain instance, playerdatafile instance2)
    22. {
    23. plugin = instance;
    24. data = instance2;
    25. }
    27. @EventHandler
    28. public void onJoin(PlayerJoinEvent PJE)
    29. {
    30. //getting the player, UUID, playername and IPadress
    31. final Player joiningPlayer = PJE.getPlayer();
    32. final UUID playerID = joiningPlayer.getUniqueId();
    34. Map <UUID, String> PlayerName = new HashMap<>();
    35. PlayerName.put(playerID, joiningPlayer.getName());
    37. Map <UUID, String> IPadress = new HashMap<>();
    38. IPadress.put(playerID, joiningPlayer.getAddress().getAddress().toString());
    40. //checking if player has playedbefore and create config and add the 'defaults'
    41. if(!(joiningPlayer.hasPlayedBefore()))
    42. {
    43. data.createplayerconfigFile(playerID);
    44. data.getplayerconfigFile(playerID).set("Currentname", PlayerName.get(playerID));
    45. data.getplayerconfigFile(playerID).set("Ipadress",IPadress.get(playerID));
    46. data.saveplayerconfigFile(playerID);
    48. PJE.setJoinMessage(ChatColor.DARK_AQUA + "Welcome player " + ChatColor.GRAY + joiningPlayer.getName() + ChatColor.DARK_AQUA + " to |HJA|craft");
    49. }
    50. //if not first join update playername and IP
    51. else
    52. {
    53. data.getplayerconfigFile(playerID).set("Currentname", PlayerName.get(playerID));
    54. data.getplayerconfigFile(playerID).set("Ipadress",IPadress.get(playerID));
    55. data.saveplayerconfigFile(playerID);
    57. //if someone was flying when disconnected, enable fly
    58. if(data.getplayerconfigFile(playerID).getBoolean("fly"))
    59. {
    60. joiningPlayer.setAllowFlight(true);
    61. joiningPlayer.setFlying(true);
    62. joiningPlayer.sendMessage(ChatColor.DARK_RED + "Saved your ass! Flymode turned ON");
    63. }
    64. }
    65. }
    66. }

    My get, save and create methods:

    1. package;
    3. import;
    4. import;
    5. import java.util.UUID;
    6. import java.util.logging.Level;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    11. public class playerdatafile
    12. {
    13. //constructor
    14. hjacraftmain plugin;
    15. public playerdatafile(hjacraftmain instance)
    16. {
    17. plugin = instance;
    18. }
    20. //basicly the code from the bukkit tutorial with other names and usage of the UUID
    21. //thnx tutorial maker ;)
    22. private FileConfiguration playerconfig = null;
    23. private File playerconfigFile = null;
    25. public void createplayerconfigFile(final UUID playerID)
    26. {
    27. if(playerconfig == null)
    28. {
    29. playerconfigFile = new File(plugin.getDataFolder() + "/Data", playerID + ".yml");
    30. }
    31. playerconfig = YamlConfiguration.loadConfiguration(playerconfigFile);
    32. }
    34. public FileConfiguration getplayerconfigFile(final UUID playerID)
    35. {
    36. if(playerconfig == null)
    37. {
    38. createplayerconfigFile(playerID);
    39. }
    40. return playerconfig;
    41. }
    43. public void saveplayerconfigFile(final UUID playerID)
    44. {
    45. if(playerconfig == null || playerconfigFile == null)
    46. {
    47. return;
    48. }
    49. try
    50. {
    51. getplayerconfigFile(playerID).save(playerconfigFile);
    52. }
    53. catch(IOException ex)
    54. {
    55. plugin.getLogger().log(Level.SEVERE, "Could not save to data file:" + playerconfigFile, ex);
    56. }
    57. }
    59. }
  2. Offline


    Debug your program, Call something like System.out.println("DEBUG: X has been done"); after doing stuff, Also, print out the players name onJoin, check if it prints both the names, if not then that's the events fault.

    EDIT: If the program doesn't print out for example, "DEBUG: Player saved to file" then that most likely failed.
  3. Offline


    Thnx for the hint i did this with the UUID but nothing seemed wrong with it as it went through it printing always the same UUID, i will now test it with the other variables and let you know

    nope it didnt work all things are nicely proccessed, everything that belongs to 1 is proccessed first and stays with 1
    eveything with 2 is with 2. Any other suggestion? A join from LAN and WAN couldnt be the issue right?

    Edit: photo with debug info

    Attached Files:

    Last edited by a moderator: Jun 30, 2016
  4. Offline


    These players don't seem to have the same UUID?
    Try debugging the other methods (the one for saving the file, etc).

    May want to remove that image since it contains your ip.
  5. Offline


    They dont i bought an alt and i multilog at the same time using the WAN and LAN ip adresses, u will start debugging save get and create thnx for the help

    Nope didnt work either i debugged it and the UUID of vertigolf is completely seperated from the UUID of vertiminigolf on all three of them, i debugged after each line. The UUID is the only thing that is called by those methods

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 14, 2016
  6. Offline


    Okay, can you give me the code for the method where you save the files?
  7. Offline


    COuld it be that generating the file takes too long so it just processes the other lines and its done when the code arrives at the second player?

    McMhz its in the post look at the second code tab, its the last method

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 14, 2016
  8. Offline


    1) Change from playeridID to playerID.toString() in all save,create,get methods
    2) Using the same YamlConfiguration for every single player is more than 65% likely to cause something like this
    3) ^^ I'd try using HashMap<UUID, YamlConfiguration>
  9. Offline


    1 didnt work,
    3 what would i have to fill in the YamlConfiguration part?
    this is what i have now:

    1. Map<String, YamlConfiguration> playeryaml = new HashMap<>();
    2. playeryaml.put(playerID,)

    i could save the configuration to another string and make a hasmap with 2 strings?
  10. Offline


    HashMap<UUID, YamlConfiguration> players = new HashMap<UUID, YamlConfiguration>();
    When you save & load, instead of what you were doing before (playeryaml = YamlConfiguration.load)
    Do this:
    player.put(player.getUniqueID(), YamlConfiguration.load(lalalla));
    Then, to get a players YamlConfig do this:
    //player = HashMap, p = Player Object.
        return player.get(p.getUniqueID());
      //Player yaml hasn't been loaded yet
  11. Offline


    McMhz thanks alot for all your help, i didnt use your solution as it brought its own errors with it. I however fixed it by entirely rewriting the create , get and save commands. I now seperated creating from getting /saving which fixed the bugsies. I would like to suggest to the bukkit staffs that they update the tutorial. I can provide the code i use if needed
Thread Status:
Not open for further replies.

Share This Page