staticCoffee
staticCoffee

Reputation: 127

Bukkit: How to permentately store player and class information?

Currently, I'm writing a RPG plugin. I've created a PlayerInfo class that stores the player's UUID, character class like knight and archer, and skills that modify a player's attributes, e.g. vitality would increase a players health, strength would increase a player's physical damage, and so on.

Here's the class so far:

public class PlayerInfo {   
    public String suuid;
    public String charClass;
    public int vitality;
    public int strength;
    public int defense;
    public int dexterity;
    public int intelligence;
    public int faith;

    public PlayerInfo() {

    }

    public PlayerInfo(String UUID, String characterClass, int VIT, int STR, int DEF, int DEX, int INT, int FAI) {
        suuid = UUID;
        charClass = characterClass;
        vitality = VIT;
        strength = STR;
        defense = DEF;
        dexterity = DEX;
        intelligence = INT;
        faith = FAI;
    }

I'd like to say as well that I'm primarily a Python and Javascript programmer. I picked up Java about 2 days ago, so I apologize if my code is unhygienic. I'm still trying to understand the languages and its practices. If you would like, I'd appreciate any advice on that as well. Though, feel free to just answer my base question, as I will be posting to Code Review, too, at some point in the future.

Moving forward...

While working on the basic framework of this plugin, I've realized that the information within the PlayerInfo class is most likely not saved when the server is stopped. I thought that possibly I could write the information, using FileOutputStream and ObjectOutputStream, to a file, and store that in a config folder, and later retrieve it with the Input versions of those modules. However, I ran into an issue when trying to dynamically pick up the path of the jar file, as my server told me that my Access was denied when trying to create the folder and file.

My last issue comes when trying to use the loaded information. My plugin's commands start with /static. If I had a command named /static stats that displayed the users stats, would it be as simple as comparing the user's UUID to the one stored in the save file? For example:

Player player = (Player) sender;
String suuid = player.getUniqueId().toString();
if (character.suuid == suuid) {
// Load stats here...
}

Or is there a different way to go about doing it?

To condense my post down a bit:
How can I store Player and class information effectively, and how can I later retrieve that data to use for information and see if the current player matches a saved object? Is this what stuff like SQL is used for?

Upvotes: 2

Views: 2477

Answers (2)

elving
elving

Reputation: 90

Load each player when they connect to the server into a memory (make a new PlayerInfo Object for each of them, using for example a HashMap or a List).

On player disconnect you have to save all the information into a file/database (sql included) which is being restored and loaded again on next connect of the given player.

The information is being lost when server stops, therefore you need to store it somewhere and retrieve it when you need it.

You can create a simple function to retrieve a PlayerInfo based on a HashMap

HashMap<String, PlayerInfo> allplayers = new HashMap<String, PlayerInfo>();


public PlayerInfo getPlayer(Player player){
  if(allplayers.containsKey(player.getName())){
     return allplayers.get(player.getName());
  }else{
     return null;
  }
}

And to put players into a HashMap

public void addPlayer(Player player){
  if(!allplayers.containsKey(player.getName())){
     //Load playerInfo
     PlayerInfo p = new PlayerInfo(....);
     //put into hashmap
     allplayers.put(player.getName(), p);
  }
}

//saving PlayerInfo can be called on PlayerDisonnect/Server shutdown
 public void savePlayer(Player player){ 
    if(allplayers.containsKey(player.getName())){
      PlayerInfo p = allplayers.get(player.getName()); 
     //Save information 
      .....
     //remove player from HashMap
     allplayers.remove(player.getName());   
    }  
 } `

Upvotes: 2

Meysam Ghorbani
Meysam Ghorbani

Reputation: 74

Okay , u can use serialization on your class , and i guess its the way cause u have just the primitives , and then u can byte write ur created serialization file to a sql database , and when u need it , u can again read it back and type cast it to ur class object . Then u can invoke an iterator to loop through the propertises and find matches for ur player with .equals method .

Upvotes: -1

Related Questions