Warp delay?

Discussion in 'Plugin Development' started by iAmGuus, Aug 11, 2014.

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

    iAmGuus

    Hey there,
    this might be a much asked question, but i need it for my new plugin which im coding,
    So, anyways. im coding a warp plugin with a delay of 2 seconds.
    I coded everything with the commands, everything works except for the delay,
    here is my code:

    http://pastebin.com/9zE1mdaJ

    btw: events are registered in Main class, if u need just say :)

    Anyways, thanks for viewing,
    Regards,
    iAmGuus
     
  2. Code:java
    1. examplearraylist.add(p.getName());
    2. Bukkit.getScheduler().scheduleSyncDelayedTask(this , new Runnable() {
    3. public void run() {
    4. examplearraylist.remove(p.getName());
    5. }
    6. }, 40);
     
  3. Code:java
    1. Main.getServer().getScheduler().scheduleSyncDelayedTask(Main, new Runnable(){
    2. public void run() {
    3. //Everything here is delayed
    4. }
    5. },(gethowevermanaysecondsyouwanttodelayby)*20);
     

  4. Good code son :)
     
  5. Offline

    iAmGuus

    Sorry, but wanted it so if he moves a block, not with his head, then the teleportation will be cancelled?
    I already tried it but doesnt work?
     
  6. iAmGuus
    You will probably want to use what DarkRangerMC posted... The scheduleAsyncDelayedTask(), not Sync. Just considering what you will be doing.

    Here's an idea....
    When the player sends the command to teleport, put him in an arraylist, and using event listeners... Listen for the playermove event. On that event, check and see if the player is in that arraylist. If he is, remove him. Then, in the delay, check and see if the player is in the arraylist before doing anything. If he moved, then he wont be anymore, and therefor wont be teleported.

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

  7. I removed the A...
     
  8. DarkRangerMC xP
    Well it doesn't really matter either way.
    Not in his case.
    Sorry if I'm overcomplicating things. :p
     
  9. Offline

    iAmGuus

    xYourFreindx That is exactly what i did in my class? isnt it, or am i wrong?

    What i did here:
    Code:java
    1. ArrayList<String> delay = new ArrayList<String>();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if(sender instanceof Player) {
    5. final Player p = (Player) sender;
    6. if(label.equalsIgnoreCase("setwarp")) {
    7. if(p.hasPermission("experiencepvp.warps.setwarp")) {
    8. if(args.length == 0) {
    9. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.RED + "You must specify a warp name!");
    10. return true;
    11. }
    12. Main.warpsConfig.set("warps." + args[0] + ".world", p.getLocation().getWorld().getName());
    13. Main.warpsConfig.set("warps." + args[0] + ".x", p.getLocation().getX());
    14. Main.warpsConfig.set("warps." + args[0] + ".y", p.getLocation().getY());
    15. Main.warpsConfig.set("warps." + args[0] + ".z", p.getLocation().getZ());
    16. Main.warpsConfig.set("warps." + args[0] + ".yaw", p.getLocation().getYaw());
    17. Main.warpsConfig.set("warps." + args[0] + ".pitch", p.getLocation().getPitch());
    18. Main.saveNewConfig();
    19. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Warp " + args[0] + " has been set at your location!");
    20. } else {
    21. p.sendMessage("You may not run this command!");
    22. }
    23. }
    24.  
    25. if(label.equalsIgnoreCase("warp")) {
    26. if(p.hasPermission("experiencepvp.warps.warp")) {
    27. if(args.length == 0) {
    28. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Warps: " + Main.warpsConfig.getConfigurationSection("warps").getKeys(false));
    29. return true;
    30. }
    31. if(Main.warpsConfig.getConfigurationSection("warps." + args[0] + "") == null) {
    32. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.RED + "Warp " + args[0] + " does not exist!");
    33. return true;
    34. }
    35. final World w = Bukkit.getServer().getWorld(Main.warpsConfig.getString("warps." + args[0] + ".world"));
    36. final double x = Main.warpsConfig.getDouble("warps." + args[0] + ".x");
    37. final double z = Main.warpsConfig.getDouble("warps." + args[0] + ".z");
    38. final double y = Main.warpsConfig.getDouble("warps." + args[0] + ".y");
    39. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Stop moving, you will be teleported in 3 seconds!");
    40. delay.add(p.getName());
    41. new BukkitRunnable() {
    42.  
    43. public void run() {
    44. if(delay.contains(p.getName())) {
    45. p.teleport(new Location(w, x, y, z));
    46. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Teleported successfully!");
    47. delay.remove(p.getName());
    48. }
    49. }
    50. }.runTaskLater(Main.getPlugin(), 2 * 20);
    51. return true;
    52. } else {
    53. p.sendMessage(ChatColor.RED + "You may not warp!");
    54. return true;
    55. }
    56. }
    57. if(label.equalsIgnoreCase("delwarp")) {
    58. if(p.hasPermission("experiencepvp.warp.delwarp")) {
    59. if(args.length == 0) {
    60. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.RED + "You must specify a warp name!");
    61. return true;
    62. }
    63. if(Main.warpsConfig.getConfigurationSection("warps." + args[0]) == null) {
    64. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Warp " + args[0] + " does not exist!");
    65. return true;
    66. }
    67. Main.warpsConfig.set("warps." + args[0], null);
    68. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "Warp " + args[0] + " has been successfully deleted!");
    69. Main.saveNewConfig();
    70. }
    71. } else {
    72. p.sendMessage(ChatColor.RED + "You may not warp!");
    73. }
    74. }
    75. return true;
    76. }
    77. @EventHandler
    78. public void onMove(PlayerMoveEvent e) {
    79. Player p = e.getPlayer();
    80. if(delay.contains(p.getName())) {
    81. if(e.getFrom().getX() != e.getTo().getX() || e.getFrom().getY() != e.getTo().getY() || e.getFrom().getZ() != e.getTo().getZ()) {
    82. delay.remove(p.getName()) ;
    83. p.sendMessage(ChatColor.BLUE + "ExperiencePvP> " + ChatColor.WHITE + "You moved, so you wont get teleported!");
    84. }
    85. }
    86. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. iAmGuus That is exactly what you wrote. :p
    You know what they say about great minds...

    Anyways, you said it didn't work.... Is there a stacktrace I could look at?
     
  11. Offline

    iAmGuus

    Nop, there is no stacktrace, if i do /warp lol (for example) everything just works fine, except for that i can move while im in the arraylist, no message says that the teleport has been cancelled...
     
  12. What's all this for?

    if(e.getFrom().getX()!= e.getTo().getX()|| e.getFrom().getY()!= e.getTo().getY()|| e.getFrom().getZ()!= e.getTo().getZ()){
     
  13. Offline

    iAmGuus

    That is to check if the player has moved a block, in the player move event? atleast, i thought. If i do remove it, then it will do nothing, tho
     
  14. xYourFreindx Still recommending Async for non-thread safe methods, I see.
     
  15. AdamQpzm I've got the definition of Async and Sync down.... But due to your condescending tone, I suppose I probably got the definition of what a "thread safe meathod" is.... Wrong.
     
  16. xYourFreindx I don't mean to sound condescending :) But the vast majority of the Bukkit API isn't thread safe - your best bet is to never use any of it async (aside from the scheduler), especially when it's something like this, there's not really a justification for why it should be async. :)
     
  17. iAmGuus It's 6 AM, I haven't slept... and I don't trust anything I say, but I don't want to leave you hanging here...
    If the player triggered the playermove event then he has obviously moved... So the only purpose I can see in thaat beautiful code there is to see if he moved a whole block. (to lazy to check and see if it returns an int or a double).
    Ummm... Make sure your events are registered correctly?

    AdamQpzm , you're right! What was I thinking.... iAmGuus , I'm sorry for my ignorance.
    *cough* thanks Adam, I need to redo my entire 73kb, 4 class plugin....
    P.s I love your condescending tone. It makes me warm and fuzzy on the inside.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page