saving a location in a config

Discussion in 'Plugin Development' started by nitrousspark, Jun 15, 2012.

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

    nitrousspark

    how would i save a location in the config. like if somebody types /setlocation it will save that location in a .yml
     
  2. When you would use the search you probably will already find about twenty threads about the exact same question.
     
  3. Save it using this:

    Code:
    config.set("something.location.X" + player.getLocation.getX());
    config.set("location.World" + player.getLocation.getWorld());
    config.set("location.Y" + player.getLocation.getY());
    config.set("location.Z" + player.getLocation.getZ());
    config.set("location.Yaw" + player.getLocation.getYaw());
    config.set("location.Pitch" + player.getLocation.getPitch());
    and then to get it and store it as a location use this:

    Code:
    Location newloc = new Location(config.get("location.World"), config.getDouble("location.X"), config.getDouble("location.Y"), config.getDouble("location.Z"), config.getFloat("location.Yaw"), config.getPitch("location.Pitch"));
    I am doing that from memory so it may not be 100% correct.
     
    vtg_the_kid and hawkfalcon like this.
  4. Wow never realised how badly I messed up there, should be

    Code:
    config.set("location.World" + player.getLocation.getWorld());
    config.set("location.X" + player.getLocation.getX());
    config.set("location.Y" + player.getLocation.getY());
    config.set("location.Z" + player.getLocation.getZ());
    config.set("location.Yaw" + player.getLocation.getYaw());
    config.set("location.Pitch" + player.getLocation.getPitch());
    
     
  5. Offline

    Gawdzahh

    Close but you did something wrong, player.getLocation needs to be a Location. So instead do this
    Code:
    Location loc = player.getLocation();
    config.set("location.World" + loc.getWorld());
    config.set("location.X" + loc.getX());
    config.set("location.Y" + loc.getY());
    config.set("location.Z" + loc.getZ());
    config.set("location.Yaw" + loc.getYaw());
    config.set("location.Pitch" + .loc.getPitch());
    :) Hope it helps
     
  6. Offline

    WarmakerT

    Nope!
    Code:
    Location loc = player.getLocation();
    config.set("location.World" , loc.getWorld());
    config.set("location.X" , loc.getX());
    config.set("location.Y" , loc.getY());
    config.set("location.Z" , loc.getZ());
    config.set("location.Yaw" , loc.getYaw());
    config.set("location.Pitch" , loc.getPitch());
     
    RemotelyOperated likes this.
  7. Offline

    Gawdzahh

    True, haha
     
  8. Shouldn't that be
    Code:java
    1. Location loc = player.getLocation();
    2. config.set("location.World" , loc.getWorld().getName());
    3. config.set("location.X" , loc.getX());
    4. config.set("location.Y" , loc.getY());
    5. config.set("location.Z" , loc.getZ());
    6. config.set("location.Yaw" , loc.getYaw());
    7. config.set("location.Pitch" , loc.getPitch());
     
  9. Offline

    StaticJava

  10. Offline

    HeavyMine13

    Code:java
    1. public void onEnable() {
    2. getConfig().options().copyDefaults(true);
    3. saveConfig();
    4. }
    5.  
    6.  
    7. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    8.  
    9.  
    10. Player p = (Player) sender;
    11.  
    12. if (cmd.getName().equalsIgnoreCase("setloc")){
    13.  
    14. if (sender.hasPermission("loc.set"))
    15. {
    16. getConfig().set("loc.world", p.getLocation().getWorld().getName());
    17. getConfig().set("loc.x", p.getLocation().getX());
    18. getConfig().set("loc.y", p.getLocation().getY());
    19. getConfig().set("loc.z", p.getLocation().getZ());
    20. saveConfig();
    21. p.sendMessage(ChatColor.GREEN + "Location set, sir!");
    22. return true;
    23. }
    24. }
    25. if (cmd.getName().equalsIgnoreCase("loctp")) {
    26. (getConfig().getConfigurationSection("hd") == null) {
    27. p.sendMessage(ChatColor.RED + "The loc has not yet been set!");
    28. }
    29. World w = Bukkit.getServer().getWorld(getConfig().getString("loc.world"));
    30. double x = getConfig().getDouble("loc.x");
    31. double y = getConfig().getDouble("loc.y");
    32. double z = getConfig().getDouble(loc.z");
    33. p.teleport(new Location(w, x, y, z));
    34. }
    35. }
    36. return true;
    37. }




    Config should be:
    Code:
    loc:
      world:
      x:
      y:
      z:
    Have fun!
     
    1 person likes this.
  11. Offline

    Maurdekye

    nitrousspark Jeez guys, just save it as a string. I made a post earlier discussing this very predicament;
     
Thread Status:
Not open for further replies.

Share This Page