[RESOURCE] Put location to config file and back

Discussion in 'Resources' started by Plo124, Apr 7, 2014.

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

    Plo124

    Hi guys, my 2nd resource/tutorial on the Bukkit Forums.

    This time I will give you a class file to put a location to a config file and then retrieve it. You may have already done this before, but this resource makes it really easy to do.

    Using this resource will put a .yml file in bukkit/locations, so not in the plugins folder, making it easy for other plugins to retrieve the location.

    The title of the thread explains what the resource is enough so here is the code:
    Code:java
    1. package put.your.package.here;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11.  
    12. public class ConfigLocation {
    13. public static Location getFromConfig(String loctype){
    14. Location l = new Location(Bukkit.getWorld("world"),0,0,0);
    15. World w = Bukkit.getWorld((String) getPlayerObject("locations",loctype+".w"));
    16. float x = (float) getPlayerObject("locations",loctype+".x");
    17. float y = (float) getPlayerObject("locations",loctype+".y");
    18. float z = (float) getPlayerObject("locations",loctype+".z");
    19. float p = (float) getPlayerObject("locations",loctype+".p");
    20. float ya = (float) getPlayerObject("locations",loctype+".ya");
    21. l.setWorld(w);
    22. l.setPitch(p);
    23. l.setX(x);
    24. l.setY(y);
    25. l.setZ(z);
    26. l.setYaw(ya);
    27. return l;
    28. }
    29. public static void setToConfig(String loctype,Location l){
    30. setObject("locations",loctype+".x",l.getX());
    31. setObject("locations",loctype+".y",l.getY());
    32. setObject("locations",loctype+".z",l.getZ());
    33. setObject("locations",loctype+".p",l.getPitch());
    34. setObject("locations",loctype+".ya",l.getYaw());
    35. setObject("locations",loctype+".w",l.getWorld().getName());
    36. }
    37. public static String getPath(String par1Name)
    38. {
    39. return "locations/"+par1Name+".yml";
    40. }
    41. private static void createNewData(String par1Name)
    42. {
    43. File f = new File(getPath(par1Name));
    44. if(!f.exists()){
    45. try {
    46. YamlConfiguration.loadConfiguration(new File(Bukkit.getPluginManager().getPlugin("Amalthea").getDataFolder(), "playerConfigTemplate.yml")).save(f);
    47. } catch (IOException e) {
    48. e.printStackTrace();
    49. }
    50. }
    51. }
    52. private static void setObject(String par1Name, String par2Path, Object par3Value)
    53. {
    54. try
    55. {
    56. File f = new File(getPath(par1Name));
    57. createNewData(par1Name);
    58. FileConfiguration pDat = YamlConfiguration.loadConfiguration(f);
    59. pDat.set(par2Path, par3Value);
    60. pDat.save(f);
    61. } catch (IOException e) {
    62. e.printStackTrace();
    63. }
    64. }
    65. private static Object getPlayerObject(String par1Name, String par2Path)
    66. {
    67. File f = new File(getPath(par1Name));
    68. createNewData(par1Name);
    69. FileConfiguration pDat = YamlConfiguration.loadConfiguration(f);
    70. return pDat.get(par2Path);
    71. }
    72. }
    73.  
     
  2. Offline

    L33m4n123

    1.) x,y,z are double not floats.

    2) Why is noone using just simply one String and splits it? You have there 6 I/O processes wich can be lowered to 1 and due to the fact that I/O processes are not the fastest your probably only want one

    Only 1

    Code:
     //In config
    location: "world|0|0|0|0|0"
     
    //to retrieve
    String [] loc = config.getString("location").split("|");
    Location location = new Location(Bukkit.getWorld(loc[0]),Double.valueOf(loc[1]),Double.valueOf(loc[2]),Double.valueOf(loc[3]),Float.valueOf(loc[4]),Float.valueOf(oc[5]);
    
     
    bigteddy98 likes this.
  3. Offline

    Plo124

    L33m4n123
    A double and float both supports decimals, and you are unlikely to need to make a decimal in minecraft in a location more accurate than to 3 decimal places.

    Your code does look easy to use, and I respect your simplicity.
     
  4. Offline

    bigteddy98

    Why using such complicated methods? You could just use this serializer I made:
    Code:java
    1. public static Location convertStringToLocation(String s) {
    2. String[] split = s.split("_");
    3. return new Location(Bukkit.getWorld(split[0]), Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]), Float.parseFloat(split[4]), Float.parseFloat(split[5]));
    4. }
    5. public static String convertLocationToString(Location loc) {
    6. return loc.getWorld().getName() + "_" + loc.getX() + "_" + loc.getY() + "_" + loc.getZ() + "_" + loc.getPitch() + "_" + loc.getYaw();
    7. }

    Saving it to a yaml can also be done in a few lines of code. I think your way of doing this is way to complicated.
     
    KingFaris11 and darkness1999 like this.
  5. Offline

    Plo124

    bigteddy98
    Haha nice, but my way does the job too :p

    This thread is turning into a 'Share the simplest way to serialize a location to a string and back' thread.
     
  6. Offline

    L33m4n123

    It's not only simplier but also faster and thus making it better than yours thats why I mentioned it. Because 1 I/O over 6 I/O
     
  7. Exactly what I thought, this had a lot of lines of codes that can be shortened into just one.
     
Thread Status:
Not open for further replies.

Share This Page