Teleportation Wait, Dont Move!

Discussion in 'Plugin Development' started by iAmGuus, Jul 2, 2014.

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

    iAmGuus

    Hey Guys,
    another question. i was making a command for my server, and i want to make it so when you type /spawn you will teleport after 3 secs, but if you move. then it will stop.
    Everything worked fine, except for one thing. U can just move? Any solution. Here is my code:

    Code:java
    1. if (label.equalsIgnoreCase("spawn")) {
    2. if(sender instanceof Player) {
    3. final Player p = (Player) sender;
    4. if(p.hasPermission("xwarps.spawn")) {
    5.  
    6. final Location spawn = new Location((World) p.getWorld(), 1000.5, 156, 1000.5);
    7.  
    8. Location loc = p.getLocation();
    9. final int x = loc.getBlockX();
    10. final int y = loc.getBlockY();
    11. final int z = loc.getBlockZ();
    12. final int delay = 3;
    13. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "You will be teleported in " + delay + " seconds");
    14. new BukkitRunnable() {
    15. int t = delay;
    16. boolean haveTeleported = false;
    17. @SuppressWarnings("deprecation")
    18. public void run() {
    19. if (t <= 0) {
    20. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Teleported!");
    21.  
    22. p.getInventory().clear();
    23. p.getInventory().setArmorContents(null);
    24.  
    25. p.getInventory().setHeldItemSlot(4);
    26.  
    27. ItemStack kitselector = new ItemStack(Material.NETHER_STAR);
    28. ItemMeta kitselectorMeta = kitselector.getItemMeta();
    29. kitselectorMeta.setDisplayName(ChatColor.BOLD + "Kit Selector");
    30. kitselector.setItemMeta(kitselectorMeta);
    31. p.getInventory().setItem(4, kitselector);
    32.  
    33. ItemStack ranks = new ItemStack (Material.INK_SACK);
    34. ItemMeta ranksMeta = ranks.getItemMeta();
    35. ranksMeta.setDisplayName("" + ChatColor.DARK_RED + ChatColor.BOLD + "Ranks");
    36. ranks.setDurability((short) 12);
    37. ranks.setItemMeta(ranksMeta);
    38. p.getInventory().setItem(3, ranks);
    39.  
    40. ItemStack kits = new ItemStack (Material.INK_SACK);
    41. ItemMeta kitsMeta = kits.getItemMeta();
    42. kitsMeta.setDisplayName("" + ChatColor.BLUE + ChatColor.BOLD + "Kits");
    43. kits.setDurability((short) 11);
    44. kits.setItemMeta(kitsMeta);
    45. p.getInventory().setItem(5, kits);
    46.  
    47. ItemStack parkour = new ItemStack (Material.INK_SACK);
    48. ItemMeta parkourMeta = kits.getItemMeta();
    49. parkourMeta.setDisplayName("" + ChatColor.BLUE + ChatColor.BOLD + "Parkour");
    50. parkour.setDurability((short) 10);
    51. parkour.setItemMeta(kitsMeta);
    52. p.getInventory().setItem(0, parkour);
    53.  
    54. ItemStack eenveen = new ItemStack (Material.INK_SACK);
    55. ItemMeta eenveenMeta = kits.getItemMeta();
    56. eenveenMeta.setDisplayName("" + ChatColor.BLUE + ChatColor.BOLD + "1v1");
    57. eenveen.setDurability((short) 10);
    58. eenveen.setItemMeta(kitsMeta);
    59. p.getInventory().setItem(8, eenveen);
    60.  
    61. p.updateInventory();
    62. p.teleport(spawn);
    63. haveTeleported = true;
    64. cancel();
    65. }
    66.  
    67. Location loc2 = p.getLocation();
    68. int xx = loc2.getBlockX();
    69. int yy = loc2.getBlockY();
    70. int zz = loc2.getBlockZ();
    71.  
    72. if(haveTeleported = false) {
    73. if (x != xx || y != yy || z != zz) {
    74. p.sendMessage("You moved, so teleportation was cancelled!");
    75. cancel();
    76. }
    77. }
    78. t--;
    79. }
    80. }.runTaskTimer(this, 20L, 20L);
    81. }
    82. }
    83. }


    Btw, there is no error in eclipse or something.


    Grtz, iAmGuus
     
  2. Offline

    fireblast709

    iAmGuus
    • Why a timer task, a delayed task should be plenty right?
    • Just schedule a delayed task, store the BukkitTask/int (depending on your implementation) it returns
    • Listen to the PlayerMoveEvent. If they have a BukkitTask/int in the Map and they moved (I would suggest to allow rotation at least), cancel that task and send a message that they moved.
    • If they don't move, the delayed task will execute in the end, teleporting them
     
  3. Offline

    iAmGuus

  4. iAmGuus Also, if(haveTeleported = false) is very wrong from a Java point of view
     
  5. Offline

    iAmGuus

    Ok, i did it fine now, but if i move my cursor only it will say teleportation stopped?

    heres code:

    Code:
            if (label.equalsIgnoreCase("spawn")) {
                if(sender instanceof Player) {
                    final Player p = (Player) sender;
                    final Location spawn = new Location((World) p.getWorld(), 1000.5, 156, 1000.5);
                    teleport.add(p.getName());
                    p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "You will be teleported in 5 seconds, dont move!");
                   
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                       
                        public void run() {
                            if(teleport.contains(p.getName())) {
                                p.teleport(spawn);
                                p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Teleported!");
                                teleport.remove(p.getName());
                            }
                        }
                       
                    }, 60L);
            }
        }
        return true;
    }
       
            @EventHandler
            public void onMove(PlayerMoveEvent e) {
                Player p = e.getPlayer();
                if(teleport.contains(p.getName())) {
                    if(e.getFrom() != e.getTo()) {
                        teleport.remove(p.getName()) ;
                        p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "You moved, so you wont get teleported!");
                    }
                }
            }
     
  6. Offline

    DoctorDark

  7. Offline

    Necrodoom

    iAmGuus yes, because its still moving. Check the locations if you want differently.
     
  8. Offline

    tommyhoogstra

  9. Offline

    DoctorDark

  10. Offline

    tommyhoogstra

    Depending on what you search, google alters the top results.
    So me and you would get different things, regardless of the link.
     
Thread Status:
Not open for further replies.

Share This Page