Teleportation wait, dont move problems!

Discussion in 'Plugin Development' started by SenseTheGod, Jan 28, 2014.

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

    SenseTheGod

    Okay, basically im trying to have it when you type /warp 1v1 you warp to that location, and if there is a player nearby you have to wait 6seconds but if you move for what ever reason you cant teleport, you get a message saying the teleportation has been cancelled.
    sCommands.java
    Code:java
    1.  
    2. package me.sensethegod;
    3.  
    4. import me.sensethegod.commands.Commands;
    5. import me.sensethegod.listeners.PlayerListener;
    6.  
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class sCommands extends JavaPlugin {
    13. private Commands executor;
    14.  
    15. @Override
    16. public void onEnable() {
    17. this.executor = new Commands(this);
    18. getServer().getPluginManager().registerEvents(new PlayerListener(this.executor), this);
    19. getLogger().info("SenseTheGod's plugin enabled!");
    20. }
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
    24. if (sender instanceof Player) {
    25. return this.executor.execute(sender, cmd, args); //returns true/false
    26. } else {
    27. getLogger().warning("Console isn't supported!");
    28. }
    29. return false; //fall back to return false
    30. }
    31. }
    32.  

    Commands.java
    Code:java
    1.  
    2. package me.sensethegod.commands;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.Iterator;
    6. import java.util.List;
    7.  
    8. import me.sensethegod.sCommands;
    9.  
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Location;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Entity;
    15. import org.bukkit.entity.Player;
    16.  
    17. public class Commands {
    18. private final sCommands plugin;
    19.  
    20. public Commands(sCommands instance) {
    21. this.plugin = instance;
    22. }
    23.  
    24. public int WarpTask;
    25. public List<String> playersWaitingToWarp = new ArrayList<String>();
    26.  
    27. public boolean execute(CommandSender s, Command c, String[] a) {
    28. if (s instanceof Player) {
    29. final Player player = (Player) s;
    30.  
    31. if (c.getName().equalsIgnoreCase("help"))
    32. {
    33. player.sendMessage(ChatColor.DARK_GRAY + "--=======ProphecyKits Help=======--");
    34. player.sendMessage(ChatColor.BOLD + "IP: Prophecykits.us");
    35. player.sendMessage(ChatColor.GRAY + "Type /kits to view all of our kits");
    36. player.sendMessage(ChatColor.GRAY + "Type /bal to view your balance");
    37. player.sendMessage(ChatColor.GRAY + "Type /staff to view our staff");
    38. player.sendMessage(ChatColor.GRAY + "Type /warps to view all the warps");
    39. player.sendMessage(ChatColor.GRAY + "Type /donate to purchase a rank");
    40. player.sendMessage(ChatColor.GRAY + "Type /lag to view the amount of lag(0% Lag)");
    41. player.sendMessage(ChatColor.GRAY + "Type /helpop <message> to recieve help from staff");
    42.  
    43. return true;
    44. }
    45.  
    46. else if (c.getName().equalsIgnoreCase("warps"))
    47. {
    48. player.sendMessage(ChatColor.DARK_GRAY + "ProphecyWarps: " + ChatColor.GRAY + "1v1, 1v1FPS, Brackets, RaceOfDeath, Shop, HG");
    49. return true;
    50. }
    51.  
    52. else if (c.getName().equalsIgnoreCase("warp")) {
    53. if (a.length == 1) {
    54. if (a[0].equalsIgnoreCase("1v1")) {
    55. List<Entity> nearbyEntities = player.getNearbyEntities(16.0, 16.0, 16.0);
    56. if (nearbyEntities.isEmpty()) {
    57. Location location = new Location(player.getWorld(), 10835, 212, 10208);
    58. player.teleport(location);
    59. player.sendMessage(ChatColor.AQUA + "You have been teleported to 1v1 arena.");
    60. } else {
    61. Iterator<Entity> nearbyEntityIterator = nearbyEntities.iterator();
    62. while(nearbyEntityIterator.hasNext()) {
    63. if (nearbyEntityIterator.hasNext() && (nearbyEntityIterator.next() instanceof Player)) {
    64. player.sendMessage(ChatColor.GRAY + "There is a player nearby, you must wait 6 seconds to teleport, don't move!");
    65. this.playersWaitingToWarp.add(player.getName());
    66. WarpTask = this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
    67. @Override
    68. public void run() {
    69. Location location = new Location(player.getWorld(), 10835, 212, 10208);
    70. player.teleport(location);
    71. player.sendMessage(ChatColor.AQUA + "You have been teleported to 1v1 arena.");
    72. }
    73. }, 120L);
    74. this.playersWaitingToWarp.remove(player.getName());
    75. return true;
    76. }
    77. }
    78. }
    79. }
    80.  
    81. if (a[0].equalsIgnoreCase("1v1fps")) {
    82. Location location = new Location(player.getWorld(), 10679, 137, 10065);
    83. player.teleport(location);
    84. player.sendMessage(ChatColor.AQUA + "You have been teleported to FPS arena.");
    85. }
    86.  
    87. else if (a[0].equalsIgnoreCase("shop")) {
    88. Location location = new Location(player.getWorld(), 10671, 13, 10089);
    89. player.teleport(location);
    90. player.sendMessage(ChatColor.AQUA + "You have been teleported to the shop.");
    91. }
    92.  
    93. else if (a[0].equalsIgnoreCase("brackets")) {
    94. Location location = new Location(player.getWorld(), -609, 6, -527);
    95. player.teleport(location);
    96. player.sendMessage(ChatColor.AQUA + "You have been teleported to the brackets.");
    97. }
    98.  
    99. else if (a[0].equalsIgnoreCase("raceofdeath")) {
    100. Location location = new Location(player.getWorld(), 10789, 8, 10003);
    101. player.teleport(location);
    102. player.sendMessage(ChatColor.AQUA + "You have been teleported to the race of death.");
    103. }
    104.  
    105. else if (a[0].equalsIgnoreCase("hg")) {
    106. Location location = new Location(player.getWorld(), 2946, 11, 5234);
    107. player.teleport(location);
    108. player.sendMessage(ChatColor.AQUA + "You have been teleported to hg.");
    109. }
    110. } else {
    111. player.sendMessage(ChatColor.DARK_GRAY + "ProphecyWarps: " + ChatColor.GRAY + "1v1, 1v1FPS, Brackets, RaceOfDeath, Shop, HG");
    112. return true;
    113. }
    114.  
    115. if (c.getName().equalsIgnoreCase("plugins"))
    116. {
    117. player.sendMessage(ChatColor.WHITE + "Plugins (3): " + ChatColor.GREEN + "MuchProphecy, SuchKits, Wow");
    118. return true;
    119. }
    120.  
    121. if (c.getName().equalsIgnoreCase("pl"))
    122. {
    123. player.sendMessage(ChatColor.WHITE + "Plugins (3): " + ChatColor.GREEN + "MuchProphecy, SuchKits, Wow");
    124. return true;
    125. }
    126. if (c.getName().equalsIgnoreCase("faggot"))
    127. {
    128. player.sendMessage(ChatColor.GRAY + "Why did you type /faggot? I think your a faggot.");
    129. return true;
    130. }
    131. if (c.getName().equalsIgnoreCase("prophecykits"))
    132. {
    133. player.sendMessage(ChatColor.GREEN + "The best server ever kid, btw your eZ.");
    134. return true;
    135. }
    136. if (c.getName().equalsIgnoreCase("staff"))
    137. {
    138. player.sendMessage(ChatColor.DARK_GRAY + "-=====-Staff List-=====-");
    139. player.sendMessage(ChatColor.GRAY + "Owner: " + ChatColor.DARK_RED + "Negations, Zepthic.");
    140. player.sendMessage(ChatColor.GRAY + "Coder: " + ChatColor.AQUA + "DatSword, MooshViolet.");
    141. player.sendMessage(ChatColor.GRAY + "Admin: " + ChatColor.RED + "Industrialize.");
    142. player.sendMessage(ChatColor.GRAY + "Mod: " + ChatColor.DARK_PURPLE + "RedEyedBadger, InsideInside.");
    143. player.sendMessage(ChatColor.GRAY + "T-Mod: " + ChatColor.LIGHT_PURPLE + "Rabiesking, vaporRaids. ");
    144.  
    145. return true;
    146. }
    147. }
    148. }
    149. return false;
    150. }
    151. }
    152.  

    PlayerListener.java
    Code:java
    1.  
    2. package me.sensethegod.listeners;
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerMoveEvent;
    9. import me.sensethegod.commands.Commands;
    10. public class PlayerListener implements Listener {
    11. private Commands commands;
    12.  
    13. public PlayerListener(Commands cmdInstance) {
    14. this.commands = cmdInstance;
    15. }
    16.  
    17.  
    18. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
    19. public void pME(PlayerMoveEvent event) {
    20. if (this.commands.playersWaitingToWarp.contains(event.getPlayer().getName())) {
    21. this.commands.playersWaitingToWarp.remove(event.getPlayer().getName());
    22. event.getPlayer().sendMessage(ChatColor.DARK_GRAY + "You moved so you were unable to warp!");
    23. Bukkit.getScheduler().cancelTask(this.commands.WarpTask);
    24. }
    25. }
    26. }
    27.  

    Please help me fix this, all help is appreciated it! Please give me good answers too!
     
  2. Offline

    viper_monster

    SenseTheGod your current code will cancel teleportation even when you just moved the cursor, you should check if e.getFrom() != e.getTo() before canceling it.
     
  3. Offline

    CoderMusgrove

    Hello! I attempted this myself, and I seem that I succeeded. If you move just one block, it will cancel the teleportation. You can still move your mouse; you can even jump. I used a BukkitRunnable and it checked the conditions while the time was greater than or equal to 0. It was fairly simple, and I'll give you the code, but you'll have to modify it to your style. ;)

    Code:java
    1. public final class WarpDelay extends JavaPlugin implements Listener {
    2.  
    3. public void onEnable() {
    4. getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
    8. String cmd = c.getName();
    9. if (!(s instanceof Player)) return false;
    10. final Player p = (Player) s;
    11.  
    12. if (cmd.equalsIgnoreCase("warpme")) {
    13. Location loc = p.getLocation();
    14. final int x = loc.getBlockX();
    15. final int y = loc.getBlockY();
    16. final int z = loc.getBlockZ();
    17. final int delay = 6;
    18. p.sendMessage("You will teleport in " + delay + " seconds");
    19. new BukkitRunnable() {
    20. int t = delay;
    21.  
    22. @Override
    23. public void run() {
    24. if (t <= 0) {
    25. cancel();
    26. p.sendMessage("Teleporting!");
    27. // p.teleport(location);
    28. return;
    29. }
    30.  
    31. Location loc2 = p.getLocation();
    32. int xx = loc2.getBlockX();
    33. int yy = loc2.getBlockY();
    34. int zz = loc2.getBlockZ();
    35.  
    36. if (xx != x || yy != y || zz != z) {
    37. p.sendMessage("You moved, so teleportation was cancelled!");
    38. cancel();
    39. }
    40.  
    41. t--;
    42. }
    43. }.runTaskTimer(this, 20L, 20L);
    44. return true;
    45. }
    46. return false;
    47. }
    48. }
    49.  
     
  4. Offline

    SomewhatEvil

    CoderMusgrove , I tested it out on my own teleporting plugin and it works except for one problem, if you teleport successfully, it plays the movement message. It might be just me, but if it isn't I would put the movement message in a for loop and add a boolean to check if it was the teleporting itself that triggered the movement message.
     
  5. Offline

    SenseTheGod

    Okay thanks man! I really appreciate it, Do you mind putting in the fixed code in please?
     
  6. Offline

    SomewhatEvil

    This should work in theory but this is CoderMusgrove's example edited by text so it might not. Let me do a bit more testing before using my code over Musgrove's.

    Code:
    public final class WarpDelay extends JavaPlugin implements Listener {
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
            String cmd = c.getName();
            if (!(s instanceof Player)) return false;
            final Player p = (Player) s;
     
            if (cmd.equalsIgnoreCase("warpme")) {
                Location loc = p.getLocation();
                final int x = loc.getBlockX();
                final int y = loc.getBlockY();
                final int z = loc.getBlockZ();
                final int delay = 6;
                p.sendMessage("You will teleport in " + delay + " seconds");
                new BukkitRunnable() {
                    int t = delay;
     
                    @Override
                    public void run() {
                    boolean haveTeleported = false;
                        if (t <= 0) {
                            cancel();
                            p.sendMessage("Teleporting!");
                            // p.teleport(location);
                            haveTeleported = true;
                        }
     
                        Location loc2 = p.getLocation();
                        int xx = loc2.getBlockX();
                        int yy = loc2.getBlockY();
                        int zz = loc2.getBlockZ();
     
                        if(haveTeleported = false) {
                        if (xx != x || yy != y || zz != z) {
                            p.sendMessage("You moved, so teleportation was cancelled!");
                            cancel();
                        }
                        }
     
                        t--;
                    }
                }.runTaskTimer(this, 20L, 20L);
                return true;
            }
            return false;
        }
    }
     
  7. Offline

    SenseTheGod

    Do you think you could just make one like mine? Sorry, im new to all of this.
     
  8. Offline

    CoderMusgrove

    That's very strange, I'm not getting that at all.

    Edit: I think it might be that I forgot to return; at the end!
    Original Post Edited.
     
  9. Offline

    SomewhatEvil

    This is the code from my edited version, make sure to change the variables to your own. Its a bit slow but I think that it was already slow so im not sure if changing it is the cause.
    Code:java
    1.  
    2. new BukkitRunnable() {
    3. int t = delay;
    4. @SuppressWarnings("unused")
    5. boolean haveTeleported = false;
    6. @Override
    7. public void run() {
    8. if (t <= 0) {
    9. cancel();
    10. player.sendMessage("Teleporting!");
    11. //player.teleport(location);
    12. haveTeleported = true;
    13. }
    14.  
    15. Location loc2 = player.getLocation();
    16. int xx = loc2.getBlockX();
    17. int yy = loc2.getBlockY();
    18. int zz = loc2.getBlockZ();
    19.  
    20. if(haveTeleported = false) {
    21. if (xx != currX || yy != currY || zz != currZ) {
    22. player.sendMessage("You moved, so teleportation was cancelled!");
    23. cancel();
    24. }
    25. }
    26. t--;
    27. }
    28. }.runTaskTimer(this, 20L, 20L);


    CoderMusgrove , it could be because I'm using my own plugin for this but I do agree with you that the cancel not working for me is strange. Ill try using your code and seeing if it works for me.

    The reason I think your not getting it CoderMusgrove , is because your plugin doesn't actually teleport you and that is what's triggering the message. I guessing another solution would to be just to flip the teleporting and the movement check around. SenseTheGod , what you have to to is take CodesMusgrove's code and put it in a BukkitRunnable(), making your event part useless. If you need me to explain in more detail, feel free to ask since I also just started :).

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

    SenseTheGod

    Thanks man i appreciate it, do you have skype? add my new one just start a conversation with me please.
     
Thread Status:
Not open for further replies.

Share This Page