Solved Adding a score to a scoreboard from a (username).yml file

Discussion in 'Plugin Development' started by McKiller5252, Mar 25, 2014.

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

    McKiller5252

    Got a friend to help, No need :)

    nvm, Got a friend to help me with it :)

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

    DrEinsteinium

    McKiller5252 Okay, so what you're trying to do is create a statistics system using flatfiles such as YML? This is a lot more simple than it sounds, but I don't suggest using YML for it. Using normal files such as a simple .txt file would be the easiest way, however, I have found that the code used to work with files in a filesystem is a bit ugly, but whatever.

    My solution(although fairly long) is to create some methods such as load() and save() inside of a class file that will load and save data from your file in your onEnable() and onDisable() methods. This will make your system a lot more efficient, because reading and writing to files on your server can get very resource intensive and it's best to only read/write when required.

    You're going to want a systematic way of storing the data in this file such as this:
    Code:
    Steve:9001,
    DrEinsteinium:50,
    McKiller5252:100
    
    In your load() method, you're going to want to use a BufferedReader to read the file and pull in all the data into one large String object. This means you're just going to have a giant String that looks like this:
    Code:
    Steve:9001,DrEinsteinium:50,McKiller5252:100
    Using a built in method called .split() in the String class you can split up all of your data into sections. Using this code will split up all of your data into easy to parse data that can be put into a HashMap quite easily.
    Code:
     String[] array = str.split(","); 
    Using the above code will make an array of strings that looks like this:
    Code:
     String[] array = {"Steve:9001","DrEinsteinium:50","McKiller5252:100"};
    Now that you have an easy to work with array you can use a for loop to go through every entry in your array and add them to a HashMap. This HashMap should be a constant, meaning you should put it inside of a class file that never gets re-instantiated during runtime (never using the new keyword to make a new instance of the class).

    Code:
      HashMap<String, Integer> map = new HashMap<String,Integer>();
      for(String str : array)
      {
        String[] temp = str.split(":");
        map.put(temp[0], Integer.valueOf(temp[1]));
      } 
    
    Now you have a HashMap filled with player usernames and their point values. Whenever you want to change the amount of points a player has you're going to want to use something like this:
    Code:
    map.get("DrEinsteinium") = map.get("DrEinsteinium") + 5;
    // I don't know if this works actually... Might wanna test it. 
    
    Okay, so that covers the load() method, but we still have to save our data. Luckily, this is easier and less complicated. Using a BufferedWriter you can write all of your data to a new text file. I suggest either deleting the "old" data file that we used earlier or -backing it up- to a different location, because we are going to want a clean file.

    So here is something like the code you are going to need. I am using a loop to go through the hashmap directly using Map.Entry.
    Code:
    BufferedWriter writer = new BufferedWriter(File);
    for(Map.Entry<String, Integer> entry : map)
    {
      writer.write(entry.getKey() + ":" + entry.getValue() + ",\n");
    }
    writer.close();
    
    I hope this clears up some things for you an at least explains kind of what the process is if you want to write all your code from scratch for reading data. Flatfiles are not the most efficient kind of data storage, and I definitely suggest something more like MySQL. There are some good Bukkit tutorials and API's available for it and it might save you a lot of time if you use it.

    If you have any questions, just ask. Here are some links to class files that might help you solve your problem:

    File
    BufferedReader
    BufferedWriter
    InputStream
    OutputStream
    HashMap
    Map.Entry
     
    McKiller5252 likes this.
Thread Status:
Not open for further replies.

Share This Page