[easy] Saving an array of locations to the config.yml

Discussion in 'Plugin Development' started by griffy100, Sep 29, 2013.

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

    griffy100

    How would I save an array of Bukkit locations to the config?
    Are locations even serializeable?
     
  2. Offline

    Gopaintman

    I just save the World, X,Y and Z. Then when I wish to create it in my code I do Location loc = new Location(World,x,y,z);
     
  3. Offline

    griffy100


    Gopaintman Can i have some code please?
     
  4. Offline

    Gopaintman

    Yea sure.

    Code:java
    1. public void saveLocation(Location loc){
    2. this.getConfig().set("x", loc.getX());
    3. this.getConfig().set("x", loc.getY());
    4. this.getConfig().set("z", loc.getZ());
    5. this.getConfig().set("World", loc.getWorld().getName());
    6. try
    7. {
    8. this.getConfig().save
    9. }catch(Exception x){
    10. x.printStackTrace();
    11. }
    12. }
    13. public Location getLocation(){
    14. return new Location(this.getConfig().getString("World"),
    15. this.getConfig().getDouble("x"),
    16. this.getConfig().getDouble("y"),
    17. this.getConfig().getDouble("z"));
    18. }
     
  5. Offline

    griffy100

    @Gopaintman
    I need to save a whole array of locations
     
  6. Offline

    Gopaintman

  7. Offline

    codename_B

    griffy100 you just need to apply the principle that he's shown you here, to creating a list of things.
    Iterate through your Location objects and convert them to String objects, and vice versa.
     
  8. Offline

    Tehmaker



    WHATS YOUR PROBLEM??? Do you really think 99% of people in this section know Java? Dude, you and I are probably that 1% that actually knows Java, not just using methods from Bukkit
     
  9. Offline

    griffy100

    I know java. If you didn't know java how would you make plugins?
     
  10. Offline

    Hutchmaster99

    I honestly do not know Java. Well, I know the simple stuff, but I don't entirely know Java.
     
  11. Offline

    waicool20

    Go read a book about Java honestly its easy if you take the time to learn it srsly
     
  12. Offline

    Hutchmaster99

    I'm just learning it as I go. I used to code mods back in 1.4.7, which then I was new to Java and Eclipse. Now, a year later, I have gotten used to it. I know the basics of Java, and I can pretty much make my own code for a plugin, if I know it. I'll re-learn Java at some point maybe :p Bye. [bookshelf][meat][creeper][creeper][log][cobble][diamondblock][brickblock][redmushroom][wheatgrown][cake]
     
  13. Offline

    Tehmaker

    Alright kid.... You "know" java right... Then just do what codename said.
     
  14. Offline

    Hutchmaster99

    Are you talking to me? It's much better to tag people, or click the reply button :p
     
  15. Offline

    griffy100

    You don't have to be abruptive and sarcastic!
     
  16. Offline

    Garris0n

    Okay I just spent like thirty seconds slamming my face into my desk, so now it's time to respond...
    Seriously? 99% don't know java? This is a forum for helping people learn to use an API that is written in Java and requires Java to use. If you don't know java, how on earth do you expect to program in java? Bukkit isn't just some separate language that doesn't use Java, it's an extension of Java, and if you don't actually know any Java(or at least a programming language that uses OOP if you're just doing basic stuff) then you'll have absolutely no idea what you're doing. There are some people that don't know Java in this section, but they are not the majority, and we tell them to go learn java.

    griffy100

    The best way to go about this is probably to serialize the entire location into a string in the format world;x;y;z, or, if you need it, world;x;y;z;yaw;pitch.

    Something like this:

    Code:java
    1. public static Location stringToLocation(String locationString){
    2.  
    3. String[] split = locationString.split(";");
    4. if(split.length < 4) //or 6 with the yaw and pitch, this is just a quick check to prevent out of bounds issues
    5. return null;
    6.  
    7. World world = Bukkit.getServer().getWorld(split[0]);
    8.  
    9. if(world == null) //make sure the world exists
    10. return null;
    11.  
    12. double x;
    13. double y;
    14. double z;
    15. //pitch and yaw could go here (as floats) if you wanted
    16.  
    17. try{
    18.  
    19. x = Double.parseDouble(split[1]);
    20. y = Double.parseDouble(split[2]);
    21. z = Double.parseDouble(split[3]);
    22.  
    23. }
    24. catch(NumberFormatException e){ //ensures that they parse correctly
    25.  
    26. return null;
    27.  
    28. }
    29.  
    30. return new Location(world, x, y, z); //again, could add pitch and yaw here
    31.  
    32. }


    Code:java
    1. public static String locationToString(Location location){
    2.  
    3. return location.getWorld().getName() + ";" +
    4. location.getX() + ";" + location.getY() + ";" + location.getZ(); //again, pitch and yaw
    5.  
    6. }


    Now you can store these in a list of Locations and serialize them into a list of strings in your config if you want, and then retrieve them in the same manner.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    jimuskin, Axe2760 and Chinwe like this.
  17. Offline

    Hutchmaster99

    Although it would be better for me to learn Java; I haven't learned it. I know Javascript, and stuff, and I used to code mods. However, I do not know a lot about Java. Me learning Bukkit has taught me how to code in that language, I'm obviously not the best--and I'm learning, but I don't think it's absolutely necessary to learn Java before hand. Then again, I have learned other languages, such as; JavaScript, Batch, HTML, CSS, and maybe a couple more.
     
  18. Offline

    The_Doctor_123

    You people, if you do not have the basics of Java down, how do you expect to learn something like the Bukkit API? Copy and paste code and call it yours?
     
  19. Offline

    Garris0n

    Sadly, yes...
     
  20. Offline

    Hutchmaster99

    Well I guess you could say that. Then again I could say you're copying code, because you obviously had to learn it some time didn't you?
     
  21. Offline

    The_Doctor_123

    Hutchmaster99
    Yes, however, people typically copy too much code at one time, resulting in them being overwhelmed.
     
  22. Offline

    Axe2760

    This was not the way I learned at all. Some things in java were very foreign to my prior small bit of python experience. I made several attempts to learn java, all failing. Then I started messing with bukkit.

    ...I learn better by doing things rather than reading tutorial after tutorial. I mean, what can you do in a system console game? What can you do with bukkit? The possibilities are just so much more fun to do and there are so much more... I would go as far as to say that learning bukkit has taught me java.

    So, just know, even if it wasnt your personal experience or that of some of those around you, it isn't necessarily applicable to just anyone? People learn differently.
     
    Hutchmaster99 likes this.
  23. Offline

    Hutchmaster99

    That was so beautiful! :'(
     
    Axe2760 likes this.
  24. Offline

    Tehmaker


    I highly doubt that you will learn switch statements, arrays, etc. Just by learning the Bukkit API. You may have learned properly, and then again you will only know the basics of Java. Until you can make a fully standalone program in Java that serves a purpose, with a decent UI, you don't know Java.
     
  25. Offline

    Axe2760

    The problem with that statement Is that I DO know those. After learning the basics with bukkit I started to learn Swing, applets, and jMonkeyEngine... But I learned those with Bukkit?

    Either way, let us not get off-topic. PM, perhaps?
     
  26. Offline

    d33k40

    Just make 2 methods, one for save and other for load:
    Code:java
    1. public void saveLoc(Location loc, String id)
    2. {
    3. double x = loc.getX();
    4. double y = loc.getY();
    5. double z = loc.getZ();
    6. World w = loc.getWorld();
    7. plugin.getConfig().set(id, x+";"+y+";"+z+";"+w.getName());
    8. }
    9. public Location loadLoc(String id)
    10. {
    11. String[] data = plugin.getConfig().getString(id).split(";");
    12. Location loc = new Location(plugin.getServer().getWorld(data[3]), Double.parseDouble(data[0]), Double.parseDouble(data[1]), Double.parseDouble(data[2]));
    13. return loc;
    14. }


    Resulting:
    Code:java
    1. test:
    2. save:
    3. loc: 102.0;135.0;44.0;world
     
Thread Status:
Not open for further replies.

Share This Page