[Tutorial][Java 7 Only] Storing locations/information in a csv like database style

Discussion in 'Resources' started by waicool20, Oct 20, 2013.

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

    waicool20

    I don't know why but whenever I get on the forums I always see people asking how to save locations(or other information) to a file, so I just decided to make a small tutorial for those who want to learn a bit of the new Files and Path package in Java 7 and MORE importantly how to save data using those packages.

    So for this example I'll just use a simple collection of locations, you will want to store the world,x,y,z into a file format with a delimiter of your choice, in my case a semi-colon like this :

    world1;x1;y1;z1
    world2;x2;y2;z2
    ......
    .....

    Create a new class and name LocationManager(or however you like),and assume you have a collection of locations like this:
    Code:java
    1. public static ArrayList<Location> locations = new ArrayList<Location>();

    Define a Path object to your desired config file name:
    I have also made a charset object reference because we will be using that a lot later on.
    Code:java
    1. private static final Path configPath = Paths.get(<PLUGININSTANCEHERE>.getDataFolder().toPath() + "/locations.yml");
    2. private static final Charset charset = Charset.forName("UTF-8");

    NOTE: Creating a Path object does not create the file, it only creates a reference to the file you want to process whether it is existent or non-existent!

    Now to make a custom saveDefaults() method for your locations.yml:
    Code:java
    1. public static void saveDefaults() {
    2. if (Files.notExists(configPath)) {
    3. try {
    4. Files.createFile(configPath);
    5. } catch (IOException e) {
    6. //Handle the exception yourself
    7. }
    8. }
    9. }

    The code is pretty much self explanatory, but all this does is that it checks if the locations.yml exists, if not it will create a new empty file.
    Ok so now we want to convert our location the format above:
    Code:java
    1. private static String locationToStorage(Location location){
    2. ArrayList<String> locationInfo = new ArrayList<String>();
    3. StringBuilder stringBuilder = new StringBuilder();
    4. for(String string : locationInfo){
    5. stringBuilder.append(string);
    6. stringBuilder.append(";");
    7. }
    8. return stringBuilder.toString();
    9. }

    Now we want to save the arraylist of locations to the file:
    Code:java
    1. private static void save() {
    2. try {
    3. BufferedWriter bufferedWriter = Files.newBufferedWriter(configPath, charset, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
    4. for (Location location : locations) {
    5. String locationInfo = locationToStorage(location);
    6. bufferedWriter.append(locationInfo);
    7. bufferedWriter.newLine();
    8. }
    9. bufferedWriter.close();
    10. } catch (IOException e) {
    11. //Handle the exception yourself
    12. }
    13. }


    Good so now you have a file with locations in it so how do we load it back? Easy
    Code:java
    1. public static void loadLocations() {
    2. try {
    3. saveDefaults();
    4. BufferedReader bufferedReader = Files.newBufferedReader(configPath, charset);
    5. String line;
    6. while ((line = bufferedReader.readLine()) != null) {
    7. String[] locationInfo = line.split(";");
    8. if(locationInfo.length < 4) continue; //If the string isn't longer than 4 skip it
    9. World world = Bukkit.getServer().getWorld(locationInfo[0]);
    10. if (world == null) {
    11. Path worldFolder = Paths.get(Bukkit.getServer().getWorldContainer().toPath() + "/" + locationInfo[1]);
    12. if (Files.exists(worldFolder)) {
    13. Bukkit.getServer().createWorld(new WorldCreator(locationInfo[0]));
    14. world = Bukkit.getServer().getWorld(locationInfo[0]);
    15. } else {
    16. // Do something if server could not load world
    17. }
    18. }
    19. int x = Integer.parseInt(locationInfo[1]);
    20. int y = Integer.parseInt(locationInfo[2]);
    21. int z = Integer.parseInt(locationInfo[3]);
    22.  
    23. Location location = new Location(world,x,y,z);
    24.  
    25. locations.add(location);
    26. }
    27. } catch (IOException e) {
    28. //Handle exception yourself
    29. }
    30. // Do something if loading was a success
    31. }


    And so all you need to do add LocationManager.saveDefaults() and LocationManager.loadLocations to your onEnable() and LocationManager.save() to your onDisable(), though I prefer to save every time I add a new object to the arraylist to make sure its saved.

    If you really don't know you save locations like this now:
    Code:java
    1. LocationManager.locations.add(location);
    2. LocationManager.save();

    Well that's it! Remember this only works if you compile on Java 7, that's goes for the server also!Hope you learned some java lol, waicool20 signing out.
     
Thread Status:
Not open for further replies.

Share This Page