Solved Read and Store List of Locations from Config

Discussion in 'Plugin Help/Development/Requests' started by BigAl512, May 22, 2015.

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

    BigAl512

    PHP:
    arenas:
      
    1:
        
    red:
           
    x1
           y
    1
           z
    1
           world
    world
        blue
    :
           
    x1
           y
    2
           z
    1
           world
    world
    How would I go about storing and reading this.
    I need it to be converted to a Location object, so then a player can do type a command and be tp'd to the location belonging to the team that they are on.
     
  2. Offline

    Lilret123

    @BigAl512
    get the section you want, red team would be "arenas.1.red", then create a location from that
    then get the x, y, and z
    then the world
    use the data to create a location
    Code:
     FileConfiguration config = (gettheconfighere); //get the config
    
            String arena = "arenas.1.red"; //store this to prevent typing it over and over again
    
            double x = config.getDouble(arena+".x"), y = config.getDouble(arena+".y"), z = config.getDouble(arena+".z");// get the x,y,z
    
            World world = Bukkit.getWorld(config.getString(arena + ".world"));//get the world
    
            Location location = new Location(world, x, y, z);//use data from config to create a location
     
  3. Offline

    mythbusterma

    @BigAl512

    @1Rogue I think this is your department...

    Well, since the wrong answer has already been posted, I guess I have to do it.

    Create a ConfigurationSerializable and make it have the fields of a location, then save it to and from config as per the API specification (read the documentation).


    EDIT: Wrong link.
     
    Last edited: May 22, 2015
    scrollbar likes this.
  4. Offline

    BigAl512

    @mythbusterma
    I am having a hard time understand what you are saying, very new to bukkit. Also, what is so wrong with @Lilret123's response, will it not work at all, or is it just not the right way to do it.
     
  5. Offline

    Lilret123

    @BigAl512
    There's nothing wrong with my method, everyone does everything differently
     
  6. Offline

    mythbusterma

    @BigAl512

    It's error-prone and not very portable (sorry I had the wrong documentation link above).

    It would be something like this:

    Code:
    public class LocationSerializable extends ConfigurationSerializable {
    
         double x;
         double y;
         double z;
         double pitch;
         double yaw;
         String world;
    
         public Map<String, Object> serialize() {
              Map<String, Object> temp = new HashMap<>();
              temp.put ("x", x);
              // etc. etc.
              return temp;
         }
        public LocationSerializable(Map<String, Object> map) {
             x = map.get("x");
             // etc.
        }
    
        public LocationSerializable(Location location) {
             x = location.getX();
             //etc.
        }
    
         public Location asLocation() {
              return new Location (Bukkit.getServer().getWorld(world), x, y, z, yaw, pitch);
         }
    }
    Also, you will have to put this snippet somewhere (usually in onEnable()):

    ConfigurationSerialization.registerClass(LocationSerializable.class)

    @Lilret123

    Just because there is more than one way to do something, doesn't mean that every method is equal.
     
    Last edited: May 22, 2015
  7. Offline

    Lilret123

    @mythbusterma
    I didnt say if either method was better, i simply said people do things differently.
    You on the other hand, marked my response off as "wrong", when it does exactly what he wanted.. In less code...
     
  8. Offline

    mythbusterma

    @Lilret123
    Your code is brittle and not repeatable, it's also far less straight-forward to see what is going on.

    When you use ConfigurationSerializable, the reading and writing is very simple to understand:

    To save:
    Code:
    Location location; // arbitrary location
    ConfigurationSection section; // any section or file
    section.set("loc1", new LocationSerializable(location));
    
    To read:
    Code:
    ConfigurationSection section; // again, any section or file
    Location loc = section.get("loc1").asLocation();
     
    BigAl512 likes this.
  9. Offline

    Lilret123

    @mythbusterma
    I see what you mean with the reading and writing part, but what do you mean "brittle and not repeatable"?
     
  10. Offline

    mythbusterma

    @Lilret123

    That a simple typo in the copy-pasting of your code would cause it to break, as would changing a name. Also, it's not immediately clear when reading the config what it may represent.

    When saving it as a String, you have write unique code each time, or have a function that takes in the ConfigurationSection, making it either not repeatable, or ugly.
     
  11. Offline

    BigAl512

    @mythbusterma
    Using Bukkit 1.8, here is what I have
    Code (open)

    Code:
    package org.alexwebber.paintball;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.configuration.serialization.ConfigurationSerialization;
    
    public class LocationSerializable extends ConfigurationSerialization {
    
        double x;
        double y;
        double z;
        double pitch;
        double yaw;
        String world;
    
        public Map<String, Object> serialize() {
            Map<String, Object> temp = new HashMap<String, Object>();
            temp.put("x", x);
            temp.put("y", y);
            temp.put("z", z);
            temp.put("pitch", pitch);
            temp.put("yaw", yaw);
            temp.put("world", world);
            return temp;
        }
    
        public LocationSerializable(Map<String, Object> map) {
            x = (Double) map.get("x");
            y = (Double) map.get("y");
            z = (Double) map.get("z");
            pitch = (Double) map.get("pitch");
            yaw = (Double) map.get("yaw");
            world = (String) map.get("world");
        }
    
        public LocationSerializable(Location location) {
            x = location.getX();
            y = location.getY();
            z = location.getZ();
            pitch = location.getPitch();
            yaw = location.getYaw();
            world = location.getWorld().toString();
        }
    
        public Location asLocation() {
            return new Location(Bukkit.getServer().getWorld(world), x, y, z);
        }
    }
    

    On line 30 and 39 I get this error in Eclipse: "Implicit super constructor ConfigurationSerialization() is undefined. Must explicitly invoke another constructor"

    Thanks
     
  12. Offline

    mythbusterma

    @BigAl512

    You're extending the wrong class.
     
  13. Offline

    BigAl512

    I tried extending the other one and it would not work. (Didn't exist). I am using Craftbukkit-1.8R03. What import are you using?

    Edit: Tried typing the class to extend backwards. Will try when I get home.
     
    Last edited by a moderator: May 22, 2015
  14. Moved to Bukkit Alternates.
     
  15. Offline

    BigAl512

    @mythbusterma -
    When I use this code, when the player types /pb set red
    Code:
                    Location location = p.getLocation(); // arbitrary location
                    ConfigurationSection section;
                    section.set("loc1", new LocationSerializable(location));
    The local variable section may not have been initalized.
     
  16. Offline

    mythbusterma

    @BigAl512

    That's because section was an example....you're going to have to use your own ConfigurationSection.....some random one.
     
  17. Offline

    BigAl512

    @mythbusterma Thanks for your assistance, I have figured it out! :)
    Changing this to Solved.

    Thanks again!
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page