Trying to create my own /back command (not from death, but from a teleport)

Discussion in 'Plugin Development' started by redcanoe, Aug 8, 2014.

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

    redcanoe

    Hey folks, I am trying to create a plugin that returns you back to your last known position after you /tp to a player.

    I have an SMP server where my mods play legitimately, and they need to return to where they were before checking up on people with /tp.

    Currently, it sometimes works, but mostly it just teleports you to the person you just ported to again.

    Help is appreciated, thanks!

    Code:java
    1. package me.agent.BukkitArc;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import java.util.UUID;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.PluginManager;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.Location;
    16.  
    17. public class BukkitArc extends JavaPlugin {
    18.  
    19. Logger l = Bukkit.getLogger();
    20. public tpListener entityListener = new tpListener(this);
    21. public static Map<UUID, Location> DL = new HashMap<UUID, Location>();
    22.  
    23. HashMap<String, Location> playerHomes = new HashMap<String, Location>();
    24.  
    25. @Override
    26. public void onEnable()
    27.  
    28. {
    29. l.info("ArcaneSurvival has been enabled!");
    30. new ListenerClass(this);
    31. PluginManager pluginManager = getServer().getPluginManager();
    32. pluginManager.registerEvents(this.entityListener, this);
    33.  
    34. }
    35.  
    36. @Override
    37. public void onDisable()
    38.  
    39. {
    40. l.info("Disabling ArcaneSurvival!");
    41. Bukkit.broadcastMessage(ChatColor.GREEN + "Reload complete.");
    42. }
    43.  
    44. // tpback
    45. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    46. {
    47. if ((sender instanceof Player))
    48. {
    49. Player player = (Player)sender;
    50. UUID uuid = player.getUniqueId();
    51. if ((cmd.getName().equalsIgnoreCase("tpback")) && (sender.hasPermission("arcane.mod")))
    52. {
    53. player.teleport((Location)DL.get(uuid));
    54.  
    55.  
    56. return true;
    57. }
    58. sender.sendMessage("You cannot do that.");
    59. return true;
    60. }
    61. return true;
    62. }
    63.  
    64.  
    65.  
    66.  
    67.  


    And here is the listener:

    Code:java
    1. package me.agent.BukkitArc;
    2.  
    3. import java.util.UUID;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerTeleportEvent;
    11.  
    12. // tpback listener
    13.  
    14. public class tpListener
    15. implements Listener
    16. {
    17. public tpListener(BukkitArc instance) {}
    18.  
    19. @EventHandler(priority=EventPriority.NORMAL)
    20. public void onTeleport(PlayerTeleportEvent event)
    21. {
    22. UUID uuid = event.getPlayer().getUniqueId();
    23. Player player = event.getPlayer();
    24. Location loc = player.getLocation();
    25. BukkitArc.DL.put(uuid, loc);
    26. }
    27. }
    28.  
     
  2. Offline

    JaguarJo

    Moved to a more appropriate forum.
     
  3. Offline

    _LB

    What happens when you debug the calls to your onTeleport method?
     
  4. Offline

    Forseth11

    It is teleporting you to the spot you just teleported to because you have teleported to that spot multiple times. To see what I send a message to a player whenever they teleport. Idk why bukkit does this but the teleport event seems to be fired more than once. To counter act this simple check if the location they teleported too is either in another world or further away then 3 blocks before you set this location as their last location.
     
  5. Offline

    Necrodoom

    Forseth11 I don't have that problem. You probably have something else going on, or you registered the event twice.
     
  6. Offline

    Tss1410

    On playerTeleportEvent, wich of the locations does getLocation returns? Have you tried to replace it with e.getFrom()?
     
  7. If you have a command to teleport, and to teleport back you should...
    Save the location in an arraylist when the player types the teleportcommand.
    And if they do /back they will be teleported to the location in the arraylist. Simple as it is.
     
  8. Offline

    jimbo8

    Then it wouldn't be compatible with other plugins. DarkRangerMC
     

  9. Arg, then you just save it in the arraylist at the Teleport event...
     
  10. Offline

    Forseth11

    Well it has seemed to happen to me in like 30 different plugins.
     
  11. Offline

    Necrodoom

    Forseth11 make a thread describing your problem, because I've never seen it happen.
     
  12. Offline

    Forseth11

    Necrodoom It's not really a problem I always have just worked around it and btw it only seemed to happen when something was lagging and when you teleport you bounce a bit then you can move.
     
  13. Offline

    redcanoe

    Well I'm frustrated at this point, either nothing you guys suggested worked or I just didn't understand it.

    I've been googling and troubleshooting this thing for a few hours now, it's such a simple command but I can't seem to figure it out... Can somebody please walk me through this like I'm an idiot?
     
  14. Erm.... Sorry... Could you replace your line registering your events with this one...

    getServer().getPluginManager().registerEvents(new tpListener(this), this);
    And see if that works? :D
     
Thread Status:
Not open for further replies.

Share This Page