How can I save a location?

Discussion in 'Plugin Development' started by Java4Monkeys, Apr 18, 2014.

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

    Java4Monkeys

    Code:
    package me.BuissnessMonkey.McPlaza.commands;
     
    import me.BuisssnessMonkey.McPlaza.Main;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class DropDown implements CommandExecutor{
        public Main plugin;
     
        public DropDown(Main Instance) {
            plugin = Instance;
     
            }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String msg,
                String[] args) {
                Player p = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("setdrop")) {
                Location loc = p.getLocation();
                p.getWorld().setSpawnLocation((int)loc.getBlockX(), (int)loc.getBlockY(), (int)loc.getBlockZ());
                String prefix = ChatColor.WHITE + "[" + ChatColor.AQUA + "" + ChatColor.BOLD + "McPlaza" + ChatColor.WHITE + "]";
                p.sendMessage(prefix + ChatColor.GOLD +"Drop down location set");
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("drop")) {
                Location loc = p.getWorld().getSpawnLocation();
                p.teleport(loc);
             
                String prefix = ChatColor.WHITE + "[" + ChatColor.AQUA + "" + ChatColor.BOLD + "McPlaza" + ChatColor.WHITE + "]";
                p.sendMessage(prefix + ChatColor.DARK_AQUA + "Teleported to ffa, start fighting!");
        }
     
     
    return false;}
    }
     
    
    Hey Bukkit,I have a problem with my plugin (not all of it since this is my whole server project) Anyways I've made it so you can /setdrop To set a drop and /drop to teleport to it And it works
    But when I reload a server it doesn't save, It teleports me to a different location.

    I have looked up how to do but I have only found "saving hashmaps" And "saving in a configuration" And something to do with ObjectOutputStream (I think) but I didn't understand it and it would be much easier and appreciated if you would add to my code.
     
  2. Offline

    Jozeth

    Java4Monkeys
    - http://forums.bukkit.org/threads/better-saving-and-retrieving-location-from-config.157254/
     
  3. Offline

    TheMcScavenger

    Save location:
    Code:
    Location location = player.getLocation();
    int x = location.getBlockX();
    int y = location.getBlockY();
    int z = location.getBlockZ();
    String worldName = location.getWorld().getName();
     
    getConfig().set("location.x", x);
    getConfig().set("location.y", y);
    getConfig().set("location.z", z);
    getConfig().set("location.world", worldName);
     
    saveConfig();
    Load location & Teleport:
    Code:
    int x = getConfig().getInt("location.x");
    int y = getConfig().getInt("location.y");
    int z = getConfig().getInt("location.z");
    World world = Bukkit.getWorld(getConfig().getInt("location.world"));
     
    player.teleport(new Location(world, x, y, z));
     
    Java4Monkeys likes this.
  4. Offline

    Java4Monkeys

    TheMcScavenger & Jozeth Where would I put it inside my code though? It would help a lot to add it on the existing code.

    I think I know you from somewhere scavenger, you work in hostforward right?
    If so your host is amazing <3
     
  5. Offline

    TheMcScavenger

    The part for save location is simply what you'd put on the /setdrop command, and the part for the load location is what you'd put on the /drop command. And yes, I do ;)
     
  6. Offline

    Java4Monkeys

    Thank you so much, it worked :D
     
Thread Status:
Not open for further replies.

Share This Page