See what i did wrong?

Discussion in 'Plugin Development' started by MineDoubleSpace, Nov 17, 2013.

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

    MineDoubleSpace

    This is my code for tpa/tpaccept and when i send the request it sends properly, but when i try to to /tpaccept it says no requests.

    Code:java
    1.  
    2. HashMap<Player, Player> req = new HashMap<Player, Player>();
    3.  
    4. @Override
    5. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    6. if (!(sender instanceof Player)) {
    7. return true;
    8. }
    9. if (cmd.getName().equalsIgnoreCase("tpa")) {
    10. Player player = (Player) sender;
    11. if (args.length == 0) {
    12. player.sendMessage(ChatColor.RED + "/tpa <player>");
    13. return true;
    14. }
    15. Player target = Bukkit.getServer().getPlayer(args[0]);
    16. if (target == null) {
    17. player.sendMessage(ChatColor.RED + "Player not found");
    18. return true;
    19. }
    20. req.put(target, player);
    21. player.sendMessage(ChatColor.GOLD + "Teleport request sent to " + ChatColor.AQUA + target.getName());
    22. target.sendMessage(ChatColor.GOLD + "You recived a teleport request from " + ChatColor.AQUA + player.getName());
    23. target.sendMessage(ChatColor.GOLD + "Use " + ChatColor.AQUA + "/tpaccept" + ChatColor.AQUA + " to accept the teleport request!");
    24. return true;
    25. }
    26. if (cmd.getName().equalsIgnoreCase("tpaccept")) {
    27. Player target = (Player) sender;
    28. if (!req.containsKey(target)) {
    29. target.sendMessage(ChatColor.RED + "You do not have any teleport requests");
    30. return true;
    31. }
    32. Player player = Bukkit.getServer().getPlayerExact(req.get(target).getName());
    33. player.teleport(target);
    34. return true;
    35. }
    36. return true;
    37. }


    Thanks for the help!
     
  2. Offline

    maxben34

    MineDoubleSpace
    What you are doing here is teleporting the sender to himself. As you can see, Player target = (Player) sender, creates a variable (target) and you make a variable player, which gets the name of the target from your HashMap. You teleport the target from the hashmap to himself in the next thing. I think this is why it seems like nothing is happening.

    I know you pretty well, and I know that you are able to fix this yourself :).
    BTW. You might want to check if the sender is an instanceof player under your if statement for the tpaccept command.
     
  3. Offline

    xTrollxDudex

  4. Offline

    MineDoubleSpace

  5. Offline

    drtshock

    Not bad if garbage is collected.
     
  6. Offline

    xTrollxDudex

    Question is... Is it being GC'd?

    Back to the topic:
     
Thread Status:
Not open for further replies.

Share This Page