Shorter location getMethod

Discussion in 'Plugin Development' started by TomTheDeveloper, Mar 18, 2013.

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

    TomTheDeveloper

    Code:java
    1. public void getLocation(String xpath, String ypath, String zpath){
    2. double x = config.getDouble(xpath);
    3. double y = config.getDouble(ypath);
    4. double z = config.getDouble(zpath);
    5. org.bukkit.Location name =new org.bukkit.Location(null, x,y,z);
    6. return name;


    I am stuck now, i want that this method will return the location in one variable called name, how can i do this?
     
  2. Offline

    Scizzr

    public Location getLocation(String xpath, String ypath, String zpath) {
    double x = config.getDouble(xpath),
    y = config.getDouble(ypath),
    z = config.getDouble(zpath);
    return new Location(null, x, y, z);
    }

    Changing null to Location designates that the method should return a Location.
     
  3. TomTheDeveloper
    'void' is the return type, you change that and then you return the object you want, something like Scizzr posted but you should not use 'null' in the new Location(), that's where you need to supply a world, it throw an exception instantly or it will bite you back when you're trying to use it on something that's world related.
     
  4. Offline

    Scizzr

    Good point; oversight.

    TomTheDeveloper
    You should store the world's name in the config, since you've got everything else there.
    Code:
    public Location getLocation(String worldpath, String xpath, String ypath, String zpath) {
        String w = config.getString(worldpath);
        double x = config.getDouble(xpath),
               y = config.getDouble(ypath),
               z = config.getDouble(zpath);
        return new Location(w, x, y, z);
    }
    
     
  5. Scizzr
    The world is not a string, it needs a World object :p
    Either parse the World object directly as argument or use Bukkit.getWorld() to get the World object from a world name.
     
  6. Offline

    Scizzr

    Sigh. #SleepDeprivation

    Code:
    public Location getLocation(String worldpath, String xpath, String ypath, String zpath) {
        String w = config.getString(worldpath);
        double x = config.getDouble(xpath),
               y = config.getDouble(ypath),
               z = config.getDouble(zpath);
        return new Location(Bukkit.getWorld(w), x, y, z);
    }
    
     
    TomTheDeveloper likes this.
  7. Offline

    TomTheDeveloper

    Okey thx

    Thx man,

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page