TPA system - not working properly

Discussion in 'Plugin Development' started by MRGhorm1379, Mar 1, 2014.

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

    MRGhorm1379

    I'm trying to develop a TPA system similar to the essentials /tpa for a plugin that I'm building. The only problem is (and I tested this because I thought this would happen); Lets say I /tpa to person A, and person B /tpa's to me, when person A does /tpaccept, person B teleports to person A, instead of me being teleported to person A.
    Is there a fix for this kind of thing?

    Code for /tpa:
    Code:java
    1. public class CommandTpa implements CommandExecutor{
    2.  
    3. public static boolean enabled = false;
    4. public static Player tpaSender;
    5.  
    6.  
    7. private BDMain plugin;
    8.  
    9. public CommandTpa(BDMain plugin) {
    10. this.plugin = plugin;
    11. }
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    15. // TODO Auto-generated method stub
    16.  
    17. if (sender instanceof Player){
    18.  
    19. Player player = (Player) sender;
    20. Player target = (Bukkit.getServer().getPlayer(args[0]));
    21. tpaSender = player;
    22. if(cmd.getName().equalsIgnoreCase("tpa")){
    23. player.sendMessage(ChatColor.GREEN + "Your request has been sent!");
    24. target.sendMessage(ChatColor.YELLOW + "A player has requested to teleport to you: " + player);
    25. target.sendMessage(ChatColor.YELLOW + "Type /tpaccept to accept teleport request");
    26. }
    27. }
    28.  
    29. return false;
    30. }
    31. }


    Code for /tpaccept:
    Code:java
    1. public class CommandTpaccept implements CommandExecutor {
    2.  
    3. public static boolean enabled = false;
    4.  
    5.  
    6. private BDMain plugin;
    7.  
    8. public CommandTpaccept(BDMain plugin) {
    9. this.plugin = plugin;
    10. }
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    14. // TODO Auto-generated method stub
    15.  
    16. if (sender instanceof Player){
    17.  
    18. Player player = (Player) sender;
    19. Player requester = CommandTpa.tpaSender;
    20. if(cmd.getName().equalsIgnoreCase("tpaccept")){
    21. requester.teleport(player);
    22. player.sendMessage(ChatColor.GREEN + "Request accepted!");
    23. requester.sendMessage(ChatColor.GREEN + "Teleport request accepted!");
    24. Bukkit.broadcastMessage(ChatColor.BLUE + "Player " + requester + " has teleported to " + player + "!");
    25. }
    26. }
    27. return false;
    28. }
    29. }
    30.  


    I actually just figured it out. For the reference of others, you'd need to use a Java Hashmap. Here's what I did and how I used it:

    CommandTpa:

    Code:java
    1. package me.BattleDome.main;
    2.  
    3. public class CommandTpa implements CommandExecutor{
    4.  
    5. public static boolean enabled = false;
    6. public static Player tpaSender;
    7. public static Map<Player, Player> tpamap = new HashMap<Player, Player>();
    8.  
    9.  
    10. private BDMain plugin;
    11.  
    12. public CommandTpa(BDMain plugin) {
    13. this.plugin = plugin;
    14. }
    15.  
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    18. // TODO Auto-generated method stub
    19.  
    20. if (sender instanceof Player){
    21.  
    22. Player player = (Player) sender;
    23. Player target = (Bukkit.getServer().getPlayer(args[0]));
    24. tpaSender = player;
    25. if(cmd.getName().equalsIgnoreCase("tpa")){
    26. tpamap.put(target, player);
    27. String playername = player.getName();
    28. String targetname = target.getName();
    29. player.sendMessage(ChatColor.GOLD + "Your request has been sent!");
    30. target.sendMessage(ChatColor.GOLD + "A player has requested to teleport to you: " + ChatColor.RED + playername);
    31. target.sendMessage(ChatColor.GOLD + "Type /tpaccept to accept teleport request");
    32. }
    33. }
    34.  
    35. return false;
    36. }
    37. }
    38.  



    CommandTpaccept:


    Code:java
    1. public class CommandTpaccept implements CommandExecutor {
    2.  
    3. public static boolean enabled = false;
    4.  
    5.  
    6. private BDMain plugin;
    7.  
    8. public CommandTpaccept(BDMain plugin) {
    9. this.plugin = plugin;
    10. }
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    14. // TODO Auto-generated method stub
    15.  
    16. if (sender instanceof Player){
    17.  
    18. Player player = (Player) sender;
    19. Player requester = CommandTpa.tpamap.get(player);
    20. if(cmd.getName().equalsIgnoreCase("tpaccept")){
    21. requester.teleport(player);
    22. CommandTpa.tpamap.remove(player);
    23. String playername = player.getName();
    24. String requestername = requester.getName();
    25. player.sendMessage(ChatColor.GREEN + "Request accepted!");
    26. requester.sendMessage(ChatColor.GREEN + "Teleport request accepted!");
    27. Bukkit.broadcastMessage(ChatColor.BLUE + "Player " + requestername + " has teleported to " + playername + "!");
    28. }
    29. }
    30. return false;
    31. }
    32. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    tommyhoogstra

    @MRGhorm1379
    I know this is a tad old, and its probably wont be answered.
    But what if multiple people send the same player a request, wouldnt this code teleport you to all of them.
    Or do hashmaps only store one of each key?
    so lets say you have
    Code:java
    1. hashmap.put(1,"hello");
    2. hashmap.put(1,"bye");
    3. hashmap.put(1,"test");


    Would "1" be overwrited every time and the final value be test?
     
  3. Offline

    FerusGrim

    In short, yes.
    Code:java
    1. Map<int, String> hashmap = new HashMap<Key, Value>;


    In hashmaps, the 'Key' is always unique. So when you say:
    Code:java
    1. hashmap.put(1, "foo");
    2. hashmap.put(1, "bar");

    You're writing 'foo' to key '1'.
    Then, you're overwriting the key '1', afterwords, with 'bar'.
     
  4. Offline

    tommyhoogstra

    FerusGrim thanks for that, just clarifying.
     
Thread Status:
Not open for further replies.

Share This Page