Solved JSON help (system)

Discussion in 'Plugin Development' started by DoggyCode™, Nov 22, 2016.

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

    DoggyCode™

    Hey!

    I want to use JSON in order to save my user/player data, I find it more efficient in the long run. However, I have one problem. My JSON file is looking like this:
    PHP:
    {
      
    "working":"yup",
      
    "players": [
        {
          
    "uuid":"uuid1",
          
    "lives":0
        
    },
        {
          
    "uuid":"uuid2",
          
    "lives":0
        
    }
      ]
    }
    Please ignore the "yup", it's just to check if the data is retriveble, which it is. If you look closely, you can see the "players" array, which is fed with so called "player data". I have access to this array, and I know how to get the data.

    Each object within that array (you can currently only see 2), are called PlayerData.java, and the entire document is called "user_data.json", hence the object with the array in it is called UserData.java.

    I have a class called DataManage.java, when there is made a new instance of this class, it grabs the "user_data.json" document and converts it to the UserData.java object. Inside this object, you can find the PlayerData[].java array. I iretate through this array, and add each PlayerData object to a HashMap, the playerData.getUuid() as the key, and the object itself as the value.

    Now comes my problem
    PHP:
    public void getPlayerDataByUuid(String uuid) {
      return 
    hashmap.get(uuid);
    }
    That is my method for getting one PlayerData chunk from the array
    PHP:
    Player p = (Player)sender;
    PlayerData pD getPlayerDataByUuid(p.getUniqueId().toString());
    Now, I can access for example this:
    PHP:
    {
        
    "uuid":"uuid2",
        
    "lives":0
    }
    if p's uuid is "uuid2".

    Now, I use methods to change/modify values within pD. Ex:
    PHP:
    pD.setLives(2);
    There we go, now we have changed it! Now I need a way of being able to store that object again inside the UserData.java object's PlayerData[] array, as an updated version. And later, at intervals or when the plugin shuts down, store the entire UserData.java object inside the "user_data.json" document (this i know how to do).

    Any help is appreciated!
     
  2. Offline

    Zombie_Striker

    @DoggyCode™
    Can't you just update the "hashmap" wit the player's current lives, and then store this value when you store the entire UserData.java object?
     
  3. Offline

    DoggyCode™

    well, i will have to be able to store more than one value (the player's lives). So I can't store the lives in the hashmap
     
  4. Offline

    Zombie_Striker

    @DoggyCode™
    What if you create another object to store all the data you need (and store that in the hashmap)?
     
  5. Offline

    DoggyCode™

    @Zombie_Striker
    Do you know why I can't cast the array to PlayerData?
    ArrayList<PlayerData> list = new ArrayList<PlayerData>();
    Iterator it = getHashMap().entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    list.add((PlayerData) pair.getValue());
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove(); // avoids a ConcurrentModificationException}
    PlayerData[] playerDatas = (PlayerData[]) list.toArray();

    I'm getting the UnhandledException at PlayerData[] playerDatas = (PlayerData[]) list.toArray();


    EDIT:
    Well, right now I'm trying to get all the values in the HashMap (which are PlayerData objects) and storing them in an ArrayList, then setting the PlayerData[] array in UserData to that ArrayList using toArray, but im getting exception

    @Zombie_Striker
    How come the hashmap size be 0 and the list size be 2?

    PHP:
            ArrayList<PlayerData> list = new ArrayList<>();
            
    Iterator it getHashMap().entrySet().iterator();
            while (
    it.hasNext()) {
                
    Map.Entry pair = (Map.Entry)it.next();
                list.
    add((PlayerDatapair.getValue());
                
    it.remove(); // avoids a ConcurrentModificationException
            
    }
            
    System.out.println("Array size: " + list.size());
            
    System.out.println("Hashmap size: " getHashMap().size());
            
    PlayerData[] array = list.toArray(new PlayerData[list.size()]);
            
    userData.setPlayers(array);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.


    EDIT:
    Apparantly it.remove(); removes it entirely from the hashmap
     
    Last edited: Nov 22, 2016
Thread Status:
Not open for further replies.

Share This Page