Saving home coordinates and loading them?

Discussion in 'Plugin Development' started by rockyandirou, Mar 25, 2012.

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

    rockyandirou

    I just started building my first bukkit plugin, and my brother suggested that I put a home and warp command to it. I've tried, and then I've noticed the only way without saving the coords into a file and not saving them in the java file, is making when you do /home set it saves the coords and when you do /home warp it goes to that coords. When you restart the server, the coords are no longer the same. So, I tried to save a HashMap, and so far I got this:

    Code:
    public class HomeCommand implements CommandExecutor
    {
        private final CmdRock plugin;
        public Map<String, Location> homes = new HashMap<String, Location>();
        int ty;
        int tx;
        int tz;
        
        public HomeCommand(CmdRock plugin)
        {
            this.plugin = plugin;
        }
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("home"))
            {
                        Player target = Bukkit.getPlayerExact(sender.getName());
                        int hx = target.getLocation().getBlockX();
                        int hy = target.getLocation().getBlockY();
                        int hz = target.getLocation().getBlockZ();
                        float pitch = target.getLocation().getPitch();
                        float yaw = target.getLocation().getYaw();
                
                if (args[0].contains("set"))
                {
                        
                    
                    if (sender.hasPermission("cmdrock.*") || sender.hasPermission("cmdrock.home.set"))
                    {
                        
                        target.sendMessage(ChatColor.YELLOW + "Home set!");
                        Location home = new Location(target.getWorld(), hx, hy, hz, pitch, yaw);
                        homes.put(null, home);
                    }
                } else if (args[0].contains("warp"))
                {
                    //Location home = new Location(target.getWorld(), SLAPI.load("/CmdRock/homes/home-" + sender.getName() + ".txt"));    
                    //target.teleport();
                        target.sendMessage(ChatColor.YELLOW + "Warped to home!");
                }
            }
            
            return false;
        }
    }
    
    And I want to save the coords like, when you do /home set it saves to plugins/CmdRock/homes/home-sendername.txt
     
  2. Offline

    dadaemon

  3. Offline

    rockyandirou

  4. Offline

    Digi

    It tells you how to load and read values... I quote:
    And depends what you mean by "make the player teleport".. do you want to teleport the player at home when he joins or what ?
    To teleport a player, just player.teleport(location), simple.

    You could also use a different method of storage like the default bukkit home plugin does, see it's source: https://github.com/Bukkit/HomeBukkit
     
  5. Offline

    rockyandirou

    No no no. I meant, save the ints and floats, then load it and make a new variable.
    Like (I think it does) hMod did. You used the home command, then it saved on a txt file called home-yourname.txt with the coordinates.
     
  6. Offline

    Digi

    Code:
    String playerName = player.getName();
    World world = plugin.getServer().getWorld( plugin.getConfig().getString(playerName + ".world") );
    
    if(world == null) // world is invalid or doesn't exist anymore
    {
        player.sendMessage("World '" + world + "' does not exist anymore.");
        return;
    }
    
    int x = plugin.getConfig().getInteger(playerName + ".x");
    int y = plugin.getConfig().getInteger(playerName + ".y");
    int z = plugin.getConfig().getInteger(playerName + ".z");
    
    player.teleport(new Location(world, x, y, z));
    That is loading from config.yml from the player's name node, use it inside the home command.
    In the config it should be like, example:
    Code:
    Digi:
        world: world
        x: 31
        y: 68
        z: -25
    But you don't need to write them manually, you just use the setting methods, see that link.

    Still... if you want to use a home plugin, I recommend you use the official home plugin since it's faster and better made.
    But if you just want to learn, I incurage you to make the plugin :p
     
  7. Offline

    CorrieKay

    If you want, i have created my own warping plugin. (handles back/home/global warps/private warps)

    i have a few class files that handle getting and setting warps, along with teleporting players around.

    https://github.com/CorrieKay/PonyWarp3/tree/master/src/me/Corrie/PonyWarp3/Utils
    if you wanted to take a look :3

    (note: these are not meant to be copypasted into your own code, they wont work because theyre highly propitiatory to my own plugins :p BUT you can take a look if you wish, and figure out how they work!)
     
  8. Offline

    rockyandirou

    How would I set it to save it?
     
  9. Offline

    CorrieKay

    to save it? use saveConfig(); (method in Plugin, your main inherits it, so if youre using it outside of the main, use yourpluginvariable.saveConfig(); )
     
  10. Offline

    rockyandirou

    ... I meant save it like, save the coordinate ints. Use the same method? coordinateX.saveConfig();
     
  11. Offline

    CorrieKay

    to save it as an integer? just use config.set(path, int);
     
  12. Offline

    rockyandirou

    So, if path is /home/ then it would save to serverfolder/plugins/CmdRock/home/afile.txt containg the int?
     
  13. Offline

    CorrieKay

    Ah! i see the problem.

    You need to create a new configuration handler, because Plugin.getConfig() returns the yaml configuration at the plugins folder only.

    if you'd like to see an example of my configuration getter, check out mine :3

    https://github.com/CorrieKay/PonySe...rrie/PonySentials/Handlers/ConfigHandler.java

    And again remember this code is propritary to my server, you'd need to fiddle with the code yourself, HOWEVER!

    This is how it works:

    the two getConfig() functions return configurations of players. one takes a player object, one takes a string (which is the players real name. Not nickname) Both functions work the same way, and i will point out, agian, its all proprietary to my server, a lot of values are hard-coded, such as the file directory.

    the get/saveElement functions are for various other configs that i have in the root configuration directory

    the saveConfig function takes a configuration as a parameter, and saves it. The parameter is a configuration file, but it only works for player configs, so it saves it to /players/playername.yml. To save anything else, remember you need to specify the directory.

    lines 73-77 create a file if one doesn't exist.
     
  14. Offline

    Digi

    path is the node path, not file path, something like "players.PlayerName.location" is:
    Code:
    players:
      PlayerName:
        location: here
    "here" beeing the value of that path.

    For file path, use getDataFolder() to get the File() object that points to "<server>/plugins/YourPluginName/" and from there you can add subfolders and whatever you need.
     
  15. Offline

    rockyandirou

    EDIT: figured it out, thanks Digi and Corrie!
     
  16. Offline

    Digi

    getConfig() checks, creates and loads the config... it only does it once so if you use it 10 times it won't reload the config 10 times =)

    So just do plugin.getConfig().set("players." + playername + ".x"), and the same for y, z and world... or you could use a string and split it into values but I guess this is simplier for you.
     
  17. Offline

    s1mpl3x

    i did something pretty similar a while ago for the under 50 challenge thread here, works fine so far, may be helpful;)

    Code:
    package me.simplex.homecmd;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Homecmd extends JavaPlugin {
     
        private static final String PREFIX = ChatColor.GREEN + "[HOMECMD] " + ChatColor.WHITE;
        private FileConfiguration cfg;
     
        public void onDisable() {
        }
     
        public void onEnable() {
            cfg = getConfig();
        }
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(PREFIX + "Player only command");
                return true;
            }
            Player p = (Player) sender;
            if (command.getName().equalsIgnoreCase("home") && args.length == 0 && p.hasPermission("homecmd.go")) {
                int x = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getWorld().getSpawnLocation().getBlockX());
                int y = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getWorld().getSpawnLocation().getBlockY());
                int z = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getWorld().getSpawnLocation().getBlockZ());
                p.teleport(new Location(p.getWorld(), x, y, z));
                p.sendMessage(PREFIX + "teleported to home");
                return true;
            }
            if (command.getName().equalsIgnoreCase("sethome") && args.length == 0 && p.hasPermission("homecmd.set")) {
                cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getLocation().getBlockX());
                cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getLocation().getBlockY());
                cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getLocation().getBlockZ());
                saveConfig();
     
                p.sendMessage(PREFIX + "Set your home in this world to [" + p.getLocation().getBlockX() + "|" + p.getLocation().getBlockY() + "|" + p.getLocation().getBlockZ() + "]");
                return true;
            }
            return false;
        }
    }
     
  18. Offline

    Digi

    s1mpl3x
    You forgot to implement world parameter, it will act ugly if you use /home in another world... most likely end up in a wall and die.

    EDIT: oh, you're using a home for each world, that's confusing :p
     
  19. Offline

    s1mpl3x

    nope look at it again, works fine for multiworld
     
  20. Offline

    plarsootje

    hello the code you placed here won't work when i try to do /sethome it does nothing
    can you help me i want to have it in my plugin thanks
     
  21. Offline

    ferrybig

    its /home set
     
  22. Offline

    plarsootje

    why it's commandLabel.equalsIgnoreCase("sethome") ????????????
     
  23. Offline

    ferrybig

    wait, I see something else at the last code snipped, you have the permission?
     
  24. Offline

    plarsootje

    How do you mean permission?
     
  25. Offline

    ferrybig

    you never seen the last code snippet?
     
  26. Offline

    plarsootje

    i had it over the last code where the code gives how to do /home and /sethome
     
  27. Offline

    ferrybig

    so you know you need permssions to make the commands work?
     
  28. Offline

    Drakonix

    Hmm, I've got a different question. How to use the method that is in the HomeBukkit plugin? I have never seen something like this before and I'm really curious .
     
  29. Offline

    plarsootje

    yeah i knew permissions but there is an error in my console please help i really want to have the command home and sethome
     
  30. Offline

    ferrybig

    give error
     
Thread Status:
Not open for further replies.

Share This Page