save location and teleport a player there [question]

Discussion in 'Plugin Development' started by Dragon252525, Apr 6, 2012.

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

    Dragon252525

    How can I save the postion of the player in a config file when he types "/savepos" or something like that and later teleport another player to this location (with "/warppos <name>")? is's like a simple warp or home plugin.
     
  2. Offline

    IcyRelic

    Well you could write the X,Y,Z,Yaw,Pitch locations to a config but it wont be the exact postitioning it will be looking the right way and everything but not standing on the EXACT spot on the block but you will be on the block
     
  3. Offline

    Tzeentchful

    here is how i would do it.

    Code:
        public Map<String, Location> locations = new HashMap<String, Location>();//hashmap saving the players name and their set location
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
     
            if (cmd.getName().equalsIgnoreCase("savepos")){//on command savepos
                if (sender instanceof Player == true){//checking if the sender is a player
                    Player player = (Player) sender;//casting the sender to a player
     
                    locations.put(player.getDisplayName().toLowerCase(), player.getLocation());//saves the players location to the hashmap
                    player.sendMessage(ChatColor.DARK_RED +"location saved!");//send message to player in dark red
                    return true;
                }
                return false;
            }
     
            if (cmd.getName().equalsIgnoreCase("warppos")){//on command warppos
                if (sender instanceof Player == true){//checking if the sender is a player
                    Player player = (Player) sender;//casting the sender to a player
                    if(args.length == 1){//checking if there is only 1 argument
                        if (locations.containsValue(args[0].toLowerCase())){// if the hashmap contains the the players location
                            player.teleport(locations.get(args[0].toLowerCase()));//get the location fomr the hashmap
                            player.sendMessage(ChatColor.DARK_RED +"Teleporting...");//send message to player in dark red
                            return true;
                        }else{
                            player.sendMessage(ChatColor.DARK_RED +"That player does not have a saved location!");//send message to player in dark red
                            return true;
                        }
                    }else{
                        player.sendMessage(ChatColor.DARK_RED +"Incorrect ammount of argumants!");//send message to player in dark red
                        player.sendMessage(ChatColor.DARK_RED +"/warppos <player name>");//send message to player in dark red
                        return true;
                    }
                }
                return false;
            }
         }
    im saving the player name as a string into a hash map along with their current position. the on the warp pos command it looks for the save location from the hash map and teleports the sender if the location is contained within the hash map.
     
  4. Offline

    Dragon252525

    ok many thanks
    I have slightly rewritten the code:

    Code:
    package me.dragon252525.testPlugin1;
     
     
     
    import java.util.HashMap;
     
    import java.util.Map;
     
     
     
    import org.bukkit.ChatColor;
     
    import org.bukkit.Location;
     
    import org.bukkit.command.Command;
     
    import org.bukkit.command.CommandSender;
     
    import org.bukkit.entity.Player;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class TestPlugin1 extends JavaPlugin{
     
       
     
        @Override
     
        public void onDisable() {
     
           
     
            System.out.println("TestPlugin1 disabled.");
     
     
     
        }
     
       
     
       
     
        @Override
     
        public void onEnable() {
     
           
     
            System.out.println("TestPlugin1 enabled.");
     
     
     
        }
     
     
     
        public Map<String, Location> locations = new HashMap<String, Location>();//hashmap saving the players name and their set location
     
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
     
     
     
            if (cmd.getName().equalsIgnoreCase("settestwarp")){//on command savepos
     
                if (sender instanceof Player == true){//checking if the sender is a player
     
                    Player p = (Player) sender;//casting the sender to a player
     
                   
     
                    if (args.length == 0) {
     
                    locations.put("Test1", p.getLocation());//saves the players location to the hashmap
     
                    p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "The testwarp has been set!");
     
                    return true;
     
                    } else {
     
                        p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "Argument Error! This command requires no argument!");
     
                        return false;
     
                    }
     
                }
     
                return false;
     
            }
     
     
     
            if (cmd.getName().equalsIgnoreCase("tptestwarp")){//on command warppos
     
                if (sender instanceof Player == true){//checking if the sender is a player
     
                    Player p = (Player) sender;//casting the sender to a player
     
                   
     
                    if(args.length > 1){
     
                        p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "Argument Error! This command requires only ONE argument!");
     
                        p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "/tptestwarp <player name>");
     
                        return false;
     
                    }
     
                   
     
                    if(args.length == 0){
     
                        p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "Argument Error! This command requires an argument!");
     
                        p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "/tptestwarp <player name>");
     
                        return false;
     
                    }
     
                   
     
                    if(args.length == 1){//checking if there is only 1 argument
     
                        if (locations.containsValue("testwarp")){// if the hashmap contains the the players location
     
                            Player p2 = (Player) this.getServer().getPlayer(args[0]);
     
                            if (!(p == p2)) {
     
                                p2.teleport(locations.get("testwarp"));//get the location fomr the hashmap
     
                                p2.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "You've been teleported!");
     
                                p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + args[0] + " was teleported!");
     
                                return true;
     
                            } else {
     
                                p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "You can't teleport yourself!");
     
                                return false;
     
                            }
     
                        }else{
     
                            p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "There is no testwarp right now!");
     
                            return true;
     
                        }
     
                    }
     
                }
     
                return false;
     
            }
     
            return false;
     
        }
     
        }
    But there is a problem: when I say "/tptestwarp Fragorioxx" the plugin replies "There is no testwarp right now!"
    Is there any thing wrong with the hashmap?

    sorry I'm an absolute noob...
     
  5. Offline

    Tzeentchful

    you have screw the whole thing up
    the first value in hash map is the player who set the location name in lower case.

    you did this
    Code:
    locations.put("Test1", p.getLocation());
    should be this
    Code:
    locations.put(player.getDisplayName().toLowerCase(), player.getLocation());
    take my code again and try not to change stuff like that.

    also you should not do this.

    Code:
    if (!(p. == p2)) {
    do it like this
    Code:
    if (!(p.equals(p2))) {
     
  6. Offline

    Dragon252525

    ok I used:
    Code:
    locations.put(player.getDisplayName().toLowerCase(), player.getLocation());
    
    and
    Code:
    if (!(p.equals(p2))) {
    
    But it's always the same thing. It tells me "There is no testwarp right now!"
    I don't correctly know what a hashmap is; shouldn't that be saved anywhere?

    @this:

    I wanted to make it so that it creates only one warppoint for the whole players.
     
  7. Offline

    hatstand

    No.

    The display name can change, using it as a key is a bad idea. Use the player's normal name.
     
Thread Status:
Not open for further replies.

Share This Page