Creating a /back Plugin

Discussion in 'Plugin Development' started by Sequax, Dec 19, 2013.

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

    Sequax

    Hello Bukkit community, I have little to no knowledge of Java, but I learn easily. I decided that the best way would be to dig deep and then learn from there. Please do not judge me for my knowledge of Java. If you're here to help, great!

    I want to figure out how to find the players location, store it, then wait until they teleport to a new location, where I could teleport them back to their previous location. I have the command basically set up, I just need to know how to actually make the command teleport. I appreciate any and all help, but it would be best if you could complete the code and tell me how it was done. I won't be publishing this plugin anywhere, it is purely for experimental purposes.

    Anyways, so far, this is the code I have:

    Code:java
    1. package GoBack;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.World;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class GoBack extends JavaPlugin {
    12.  
    13.  
    14. @Override
    15. public void onEnable(){
    16. getLogger().info("[GoBack] GoBack has been enabled.");
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. getLogger().info("[GoBack] GoBack has been disabled.");
    22. }
    23.  
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    26.  
    27. if (sender instanceof Player){
    28.  
    29. Player player = (Player) sender;
    30.  
    31. if(cmd.getName().equalsIgnoreCase("back")) {
    32. player.sendMessage(ChatColor.GOLD + "Teleportation Will Commence in 5 Seconds.");
    33. return true;
    34.  
    35.  
    36.  
    37.  
    38. Player.teleport(loc);
    39.  
    40. }else {
    41. sender.sendMessage("You Must Be a Player in Order To Perform This Command.");
    42. return false;
    43. //Done in order to prevent console from using /back
    44. }
    45.  
    46.  
    47.  
    48. }
    49. return false;
    50.  
    51. }
    52.  
    53. }
     
  2. Offline

    The_Doctor_123

    I highly recommend you learn Java first before trying to learn a 3rd party API, like Bukkit. See this thread for some learning options.
     
    Garris0n likes this.
  3. Offline

    Sequax


    I think you might be right.
     
    Garris0n likes this.
  4. Offline

    The_Doctor_123

    Sequax
    Yeah, without the proper knowledge of Java, you're just going to be getting nowhere. Once you learn the basics, knowledge becomes easily expandable.

    If you're in school, winter break should be here/coming up soon. Nice opportunity to learn some stuff. :)
     
    lol768 and Garris0n like this.
  5. Offline

    Sequax

    Yeah, got the Java for Dummies book. It's helping to clarify things a lot!
     
    Garris0n likes this.
  6. Offline

    The_Doctor_123

    Great! That will get you off on a nice start. ;)
     
    Garris0n likes this.
  7. Offline

    reider45

    You can create a location by using something like
    Code:java
    1. Location loc = new Location(player.getWorld(), player.getX(), player.getY(), player.getZ());
    2.  
    3. // Or something like that
     
  8. Offline

    Sequax


    So, I've learned quite a bit... And, I was wondering how I'd use the locations I stored in the teleport method?

    This is what I have so far:
    Code:java
    1. package goBack.Plugin;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.player.PlayerTeleportEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin {
    15.  
    16.  
    17. @Override
    18. public void onEnable(){
    19. getLogger().info("[GoBack] GoBack has been enabled.");
    20. }
    21.  
    22. @Override
    23. public void onDisable(){
    24. getLogger().info("[GoBack] GoBack has been disabled.");
    25. }
    26.  
    27. //enable and disable messages
    28.  
    29. public static HashMap<String, Location> last = new HashMap<String, Location>();
    30.  
    31. public void onTeleport(PlayerTeleportEvent event) {
    32. String playerName = event.getPlayer().getName();
    33. last.remove(playerName);
    34. last.add(playerName, event.getPlayer().getLocation());
    35. //Apparantly there's an error here
    36. }
    37.  
    38. //Logs player locations
    39.  
    40. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    41.  
    42. if (sender instanceof Player){
    43.  
    44. Player senderPlayer = (Player) sender;
    45.  
    46. if(cmd.getName().equalsIgnoreCase("back")) {
    47. senderPlayer.sendMessage(ChatColor.GOLD + "Teleportation Will Commence in 5 Seconds.");
    48. //Sends player confirmation message
    49.  
    50. Player player = (Bukkit.getServer().getPlayer(args[1]));
    51. Location previousLocation = player.getLocation();
    52.  
    53. player.teleport(previousLocation);
    54.  
    55. return true;
    56.  
    57. }else {
    58. sender.sendMessage("You Must Be a Player in Order To Perform This Command.");
    59. //Prevents console from using command
    60. }
    61. }
    62. return false;
    63. }
    64. }
     
  9. Offline

    xTigerRebornx

    Instead of using player.getLocation() when storing their location in PlayerTeleportEvent, use event.getFrom()
    Then, when teleporting in /back, get the location from your HashMap
     
  10. Offline

    The_Doctor_123

    Sequax
    Erm.. But.. I said..
     
Thread Status:
Not open for further replies.

Share This Page