Getting player location and saving

Discussion in 'Plugin Development' started by hi_guy_5, Aug 24, 2014.

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

    hi_guy_5

    hi i've never made a plugin that really needed to store information for a long period of time, but im trying to make a command that will save the players current location and be able to use that location later.

    should i have the plugin write the cords to a config? I guess i'm just trying to ask what is the best way to do this
     
  2. Offline

    ItsMattHogan

    Yes, you should write the player's coordinates to a configuration file or you could use a hashmap but the hashmap will be cleared if the server is reloaded.

    Config method;
    Code:java
    1. // Save to config
    2. plugin.getConfig.set(player.getUniqueId().toString() + ".lastLocation.x", player.getLocation.getX());
    3. plugin.getConfig.set(player.getUniqueId().toString() + ".lastLocation.y", player.getLocation.getY());
    4. plugin.getConfig.set(player.getUniqueId().toString() + ".lastLocation.z", player.getLocation.getZ());
    5. plugin.getConfig.set(player.getUniqueId().toString() + ".lastLocation.world", player.getLocation.getWorld().getName());
    6. plugin.saveConfig();
    7.  
    8. // Query from config
    9. Location location = new Location(
    10. Bukkit.getWorld(player.getUniqueId().toString() + ".lastLocation.world"),
    11. plugin.getConfig().getDouble(player.getUniqueId().toString() + ".lastLocation.y", player.getLocation.getY()),
    12. plugin.getConfig().getDouble(player.getUniqueId().toString() + ".lastLocation.z", player.getLocation.getZ()),
    13. plugin.getConfig().getDouble(player.getUniqueId().toString() + ".lastLocation.z", player.getLocation.getZ())
    14. );
    15.  


    Hashmap method;
    Code:java
    1. private HashMap<Player, Location> playerLocations = new HashMap<Player, Location>();
    2.  
    3. // Add the player's location
    4. playerLocations.put(player, player.getLocation());
    5.  
    6. // Get the player's location
    7. playerLocations.get(player) // This will return the player's location
     
  3. Offline

    nuno1212sss

    Write to a file ( or config) and load it up when you need it :)
     
  4. Offline

    Freelix2000

    I've had to do this in a lot of plugins, I usually just make two methods that are static and I just copy/paste them into each new project.
    Code:
    public static String locToString(Location loc){
            return loc.getWorld().getName() + "!" + loc.getX() + "!" + loc.getY() + "!" + loc.getZ() + "!" + loc.getYaw() + "!" + loc.getPitch();
        }
        public static Location stringToLoc(String str){
            return new Location(Bukkit.getWorld(str.split("!")[0]), Double.parseDouble(str.split("!")[1]), Double.parseDouble(str.split("!")[2]), Double.parseDouble(str.split("!")[3]), Float.parseFloat(str.split("!")[4]), Float.parseFloat(str.split("!")[5]));
        }
    That will allow you to easily parse a precise location into a string and back into a location. Just use getConfig().set("something", locToString(location)); and stringToLoc(getConfig().get("something"));
     
  5. Offline

    hi_guy_5

    thanks for all the help guys
     
Thread Status:
Not open for further replies.

Share This Page