Problem with /tpa

Discussion in 'Plugin Development' started by gamemster2468, Sep 25, 2014.

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

    gamemster2468

    Hello,

    I'm having some problems with a tpa command. There's no error in console -

    Here's my code:
    Code:java
    1.  
    2. HashMap<Player, Player> tpa = new HashMap<Player, Player>();
    3.  
    4. @Override
    5. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args[]) {
    6. Player p = (Player) sender;
    7. Player target = Bukkit.getPlayer(args[0]);
    8. if (command.getName().equalsIgnoreCase("tpa")) {
    9. if (args.length == 1) {
    10. if (target != null) {
    11. tpa.put(target, p);
    12. p.sendMessage(Msg.cPrefix + "Teleport request sent!");
    13. } else {
    14. p.sendMessage(Msg.cPrefix + RED + "User not found.");
    15. }
    16. } else {
    17. p.sendMessage(Msg.cPrefix + "Usage: /tpa [user]");
    18. }
    19. } else if (command.getName().equalsIgnoreCase("tpaccept")) {
    20. if (tpa.get(p) != null) {
    21. tpa.get(p).teleport(p);
    22. p.sendMessage(Msg.cPrefix + "You successfully accepted the teleportation request.");
    23. tpa.get(p).sendMessage(Msg.cPrefix + "Your request has been accepted.");
    24. tpa.put(p, null);
    25. } else {
    26. p.sendMessage(Msg.cPrefix + RED + "Error: No requests found.");
    27. }
    28. } else if (command.getName().equalsIgnoreCase("tpdeny")) {
    29. if(tpa.get(p) != null) {
    30. p.sendMessage(Msg.cPrefix + "You successfully declined the teleportation request.");
    31. tpa.get(p).sendMessage(Msg.cPrefix + "Your request has been declined.");
    32. tpa.put(p, null);
    33. }else {
    34. p.sendMessage(Msg.cPrefix + RED + "Error: No requests found.");
    35. }
    36. }
    37. return false;
    38. }[/user]
     
  2. Offline

    fireblast709

    gamemster2468 then what exactly is the issue? Knowing that will help us to find the issue :p.
     
  3. Offline

    gamemster2468


    It doesn't work, it just doesn't do / say anything fireblast709

    Anyone?

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

    nite

    Just switch these around (The one on the bottom is correct)
    Code:
            Player target = Bukkit.getPlayer(args[0]);
            if (command.getName().equalsIgnoreCase("tpa")) {
                if (args.length == 1) {
    It should be
    Code:
            if (command.getName().equalsIgnoreCase("tpa")) {
     
                if (args.length == 1) {
                    Player target = Bukkit.getPlayer(args[0]);
    I would also add a message to the target of the tpa request telling them that you want to tp :D
    "target.sendMessage(p.getDisplayName() + " Would Like To Teleport To You!");"

    Should fix your problem
     
  5. Offline

    coasterman10

    Add some debug messages to figure out what parts of your code are or aren't working. Also check your cast to Player before I suspect BCBroz copy+pasting
     
  6. Offline

    fireblast709

    Must be a different channel, he used command.getName() (whereas BCBroz uses label)

    gamemster2468 like nite sneakily mentioned but forgot to explain, you should always check the argument length before using arguments. For example: I want to use the 1st argument. Then I first check if args.length >= 1, then I can use args[0].

    Lastly, you are currently causing a memory leak because you don't seem to remove Players from the Map when they quit (note that this doesn't only count for the Map keys, but also for the Map values!). A quick solution for the Map values is to use weak referencing, but standard Java objects don't offer that. Luckily enough, Bukkit included Google Guava in their jar, so we can use MapMaker. The following block creates a HashMap with weak values, which means that if a Player disconnects, the Map won't stop the GC (Garbage Collector) to free the memory that the Player was holding (and it will remove the entry from the Map - neat, isn't it?)
    Code:java
    1. Map<Player, Player> tpa = new MapMaker().weakValues().build();
    This will stop memory from leaking.

    [note] You should still listen to the PlayerQuitEvent and invoke tpa.remove(event.getPlayer())!
     
Thread Status:
Not open for further replies.

Share This Page