Help With Saving PlayerData

Discussion in 'Plugin Development' started by CodeAX2, Jul 18, 2017.

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

    CodeAX2

    Hi there! I am trying to create a minigame plugin, and I need to save basically everything about the player (inventory, armor, effects, health, etc.) so that once they are done playing they are reverted back to their original state. Is there an easy way to do this without creating hashmaps for every single atribute like inventory or health? I don't need to save anything to a file unless it would be simpler to do that. Any ideas on how I could do this?
     
  2. Offline

    xXkguyXx

    Is the server going to restart during the minigame? If not then you could do hashmaps, there really is not point of saving it.
     
  3. Offline

    CodeAX2

    The server won't restart during the minigame. I am wondering if there is a way of condensing all the player data to one hashmap rather than around 10 seperate ones for health, exp, inventory, etc.
     
  4. Offline

    Machine Maker

    @CodeAX2 Create a class that stores all the data for one player. Then create a new instance of the class object every time a player joins the mini game. You can make a constructor for the class that takes the players inventory, name, location, team, etc. Then make methods in the class to get all the info.
     
  5. Offline

    xXkguyXx

    Create an object and store it in the hashmap,

    Code:
    
    HashMap<String, PlayerData> userdata = new HashMap<>(); // string is the players name, PlayerData is the object
    
    
    Then your custom PlayerData Object to store all of playerdata

    Code:
    
    public class PlayerData {
    
    
    private ItemStack[] armor;
    private ItemStack[] inventory;
    private int health;
    private int exp;
    // other stuff here
    
    public PlayerData(ItemStack[] armor, ItemStack[] inventory, int health, int exp) {
    
    this.armor = armor;
    // etc.
    }
    
    }
    
    Then you would have getters for the methods, and then when your ready get the Object from the HashMap for the player and set it, then remove it from the hashmap.

    ALSO: Sorry for bad formatting, No IDE installed on this laptop :p
     
Thread Status:
Not open for further replies.

Share This Page