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. 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. 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 (Text):
    1.     public Map<String, Location> locations = new HashMap<String, Location>();//hashmap saving the players name and their set location
    2.  
    3.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    4.  
    5.         if (cmd.getName().equalsIgnoreCase("savepos")){//on command savepos
    6.             if (sender instanceof Player == true){//checking if the sender is a player
    7.                 Player player = (Player) sender;//casting the sender to a player
    8.  
    9.                 locations.put(player.getDisplayName().toLowerCase(), player.getLocation());//saves the players location to the hashmap
    10.                 player.sendMessage(ChatColor.DARK_RED +"location saved!");//send message to player in dark red
    11.                 return true;
    12.             }
    13.             return false;
    14.         }
    15.  
    16.         if (cmd.getName().equalsIgnoreCase("warppos")){//on command warppos
    17.             if (sender instanceof Player == true){//checking if the sender is a player
    18.                 Player player = (Player) sender;//casting the sender to a player
    19.                 if(args.length == 1){//checking if there is only 1 argument
    20.                     if (locations.containsValue(args[0].toLowerCase())){// if the hashmap contains the the players location
    21.                         player.teleport(locations.get(args[0].toLowerCase()));//get the location fomr the hashmap
    22.                         player.sendMessage(ChatColor.DARK_RED +"Teleporting...");//send message to player in dark red
    23.                         return true;
    24.                     }else{
    25.                         player.sendMessage(ChatColor.DARK_RED +"That player does not have a saved location!");//send message to player in dark red
    26.                         return true;
    27.                     }
    28.                 }else{
    29.                     player.sendMessage(ChatColor.DARK_RED +"Incorrect ammount of argumants!");//send message to player in dark red
    30.                     player.sendMessage(ChatColor.DARK_RED +"/warppos <player name>");//send message to player in dark red
    31.                     return true;
    32.                 }
    33.             }
    34.             return false;
    35.         }
    36.      }
    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. ok many thanks
    I have slightly rewritten the code:

    Code (Text):
    1. package me.dragon252525.testPlugin1;
    2.  
    3.  
    4.  
    5. import java.util.HashMap;
    6.  
    7. import java.util.Map;
    8.  
    9.  
    10.  
    11. import org.bukkit.ChatColor;
    12.  
    13. import org.bukkit.Location;
    14.  
    15. import org.bukkit.command.Command;
    16.  
    17. import org.bukkit.command.CommandSender;
    18.  
    19. import org.bukkit.entity.Player;
    20.  
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23.  
    24.  
    25. public class TestPlugin1 extends JavaPlugin{
    26.  
    27.    
    28.  
    29.     @Override
    30.  
    31.     public void onDisable() {
    32.  
    33.        
    34.  
    35.         System.out.println("TestPlugin1 disabled.");
    36.  
    37.  
    38.  
    39.     }
    40.  
    41.    
    42.  
    43.    
    44.  
    45.     @Override
    46.  
    47.     public void onEnable() {
    48.  
    49.        
    50.  
    51.         System.out.println("TestPlugin1 enabled.");
    52.  
    53.  
    54.  
    55.     }
    56.  
    57.  
    58.  
    59.     public Map<String, Location> locations = new HashMap<String, Location>();//hashmap saving the players name and their set location
    60.  
    61.  
    62.  
    63.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    64.  
    65.  
    66.  
    67.         if (cmd.getName().equalsIgnoreCase("settestwarp")){//on command savepos
    68.  
    69.             if (sender instanceof Player == true){//checking if the sender is a player
    70.  
    71.                 Player p = (Player) sender;//casting the sender to a player
    72.  
    73.                
    74.  
    75.                 if (args.length == 0) {
    76.  
    77.                 locations.put("Test1", p.getLocation());//saves the players location to the hashmap
    78.  
    79.                 p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "The testwarp has been set!");
    80.  
    81.                 return true;
    82.  
    83.                 } else {
    84.  
    85.                     p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "Argument Error! This command requires no argument!");
    86.  
    87.                     return false;
    88.  
    89.                 }
    90.  
    91.             }
    92.  
    93.             return false;
    94.  
    95.         }
    96.  
    97.  
    98.  
    99.         if (cmd.getName().equalsIgnoreCase("tptestwarp")){//on command warppos
    100.  
    101.             if (sender instanceof Player == true){//checking if the sender is a player
    102.  
    103.                 Player p = (Player) sender;//casting the sender to a player
    104.  
    105.                
    106.  
    107.                 if(args.length > 1){
    108.  
    109.                     p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "Argument Error! This command requires only ONE argument!");
    110.  
    111.                     p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "/tptestwarp <player name>");
    112.  
    113.                     return false;
    114.  
    115.                 }
    116.  
    117.                
    118.  
    119.                 if(args.length == 0){
    120.  
    121.                     p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "Argument Error! This command requires an argument!");
    122.  
    123.                     p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "/tptestwarp <player name>");
    124.  
    125.                     return false;
    126.  
    127.                 }
    128.  
    129.                
    130.  
    131.                 if(args.length == 1){//checking if there is only 1 argument
    132.  
    133.                     if (locations.containsValue("testwarp")){// if the hashmap contains the the players location
    134.  
    135.                         Player p2 = (Player) this.getServer().getPlayer(args[0]);
    136.  
    137.                         if (!(p == p2)) {
    138.  
    139.                             p2.teleport(locations.get("testwarp"));//get the location fomr the hashmap
    140.  
    141.                             p2.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "You've been teleported!");
    142.  
    143.                             p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + args[0] + " was teleported!");
    144.  
    145.                             return true;
    146.  
    147.                         } else {
    148.  
    149.                             p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "You can't teleport yourself!");
    150.  
    151.                             return false;
    152.  
    153.                         }
    154.  
    155.                     }else{
    156.  
    157.                         p.sendMessage(ChatColor.DARK_GRAY + "[TestPlugin1] " + ChatColor.GRAY + "There is no testwarp right now!");
    158.  
    159.                         return true;
    160.  
    161.                     }
    162.  
    163.                 }
    164.  
    165.             }
    166.  
    167.             return false;
    168.  
    169.         }
    170.  
    171.         return false;
    172.  
    173.     }
    174.  
    175.     }
    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 (Text):
    1. locations.put("Test1", p.getLocation());
    should be this
    Code (Text):
    1. 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 (Text):
    1. if (!(p. == p2)) {
    do it like this
    Code (Text):
    1. if (!(p.equals(p2))) {
     
  6. ok I used:
    Code (Text):
    1.  
    2. locations.put(player.getDisplayName().toLowerCase(), player.getLocation());
    3.  
    and
    Code (Text):
    1.  
    2. if (!(p.equals(p2))) {
    3.  
    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. 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