How to make a delay for players to use certain items?

Discussion in 'Plugin Development' started by Shzylo, Aug 3, 2013.

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

    Shzylo

    I want to hook up making a delay for players to use this mining tool I made with my explosive server (to delay or keep down griefing) and I just cannot figure it out. There is no code left of the delay but I know I need a hashmap to store the players and that is about it. here is the code important code for my sulphur that you need to see:
    Code:java
    1. @EventHandler
    2. public void onBlockInteract(PlayerInteractEvent e) {
    3. Block b = e.getClickedBlock();
    4. Action act = e.getAction();
    5. Player p = e.getPlayer();
    6. Material hand = p.getItemInHand().getType();
    7. World w = p.getWorld();
    8.  
    9. if (enabled) {
    10. if (hand.equals(Material.SULPHUR)) {
    11. if (act.equals(Action.RIGHT_CLICK_BLOCK)) {
    12. if (b != null) {
    13. Block bTarget = p.getTargetBlock(null, 5);
    14. Block bAbove = bTarget.getRelative(BlockFace.UP);
    15. Block bNorth = bTarget.getRelative(BlockFace.NORTH);
    16. Block bSouth = bTarget.getRelative(BlockFace.SOUTH);
    17. Block bEast = bTarget.getRelative(BlockFace.EAST);
    18. Block bWest = bTarget.getRelative(BlockFace.WEST);
    19. Block bDown = bTarget.getRelative(BlockFace.DOWN);
    20.  
    21. Block bSEast = bTarget.getRelative(BlockFace.SOUTH_EAST);
    22. Block bSWest = bTarget.getRelative(BlockFace.SOUTH_WEST);
    23. Block bNEast = bTarget.getRelative(BlockFace.NORTH_EAST);
    24. Block bNWest = bTarget.getRelative(BlockFace.NORTH_WEST);
    25.  
    26. Block bUpNorth = bTarget.getRelative(0, 1, 1);
    27. Block bUpSouth = bTarget.getRelative(0, 1, -1);
    28. Block bUpEast = bTarget.getRelative(-1, 1, 0);
    29. Block bUpWest = bTarget.getRelative(1, 1, 0);
    30. Block bUpNEast = bTarget.getRelative(1, 1, 1);
    31. Block bUpNWest = bTarget.getRelative(-1, 1, 1);
    32. Block bUpSEast = bTarget.getRelative(1, 1, -1);
    33. Block bUpSWest = bTarget.getRelative(-1, 1, -1);
    34. Block bDownNorth = bTarget.getRelative(0, -1, 1);
    35. Block bDownSouth = bTarget.getRelative(0, -1, -1);
    36. Block bDownEast = bTarget.getRelative(-1, -1, 0);
    37. Block bDownWest = bTarget.getRelative(1, -1, 0);
    38. Block bDownNEast = bTarget.getRelative(1, -1, 1);
    39. Block bDownNWest = bTarget.getRelative(-1, -1, 1);
    40. Block bDownSEast = bTarget.getRelative(1, -1, -1);
    41. Block bDownSWest = bTarget.getRelative(-1, -1, -1);
    42.  
    43. if (p.hasPermission("mobmeat.sulphur")) {
    44. if (!p.getGameMode().equals(GameMode.CREATIVE)) {
    45. if (p.getItemInHand().getAmount() >= miningAmount) {
    46. if (p.getItemInHand().getAmount() > miningAmount) {
    47. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - miningAmount);
    48. } else if (p.getItemInHand().getAmount() == miningAmount) {
    49. p.setItemInHand(null);
    50. }
    51. usable = true;
    52. }
    53. } else
    54. usable = true;
    55.  
    56. if (usable) {
    57. if (explosionSize == 0) {
    58. getBlock(e, bTarget, p, w);
    59. } else if (explosionSize > 0) {
    60. getBlock(e, bTarget, p, w);
    61. getBlock(e, bAbove, p, w);
    62. getBlock(e, bNorth, p, w);
    63. getBlock(e, bSouth, p, w);
    64. getBlock(e, bEast, p, w);
    65. getBlock(e, bWest, p, w);
    66. getBlock(e, bDown, p, w);
    67.  
    68. if (explosionSize > 1) {
    69. getBlock(e, bSEast, p, w);
    70. getBlock(e, bSWest, p, w);
    71. getBlock(e, bNEast, p, w);
    72. getBlock(e, bNWest, p, w);
    73.  
    74. if (explosionSize > 2) {
    75. getBlock(e, bUpNorth, p, w);
    76. getBlock(e, bUpSouth, p, w);
    77. getBlock(e, bUpEast, p, w);
    78. getBlock(e, bUpWest, p, w);
    79. getBlock(e, bUpNEast, p, w);
    80. getBlock(e, bUpNWest, p, w);
    81. getBlock(e, bUpSEast, p, w);
    82. getBlock(e, bUpSWest, p, w);
    83. getBlock(e, bDownNorth, p, w);
    84. getBlock(e, bDownSouth, p, w);
    85. getBlock(e, bDownEast, p, w);
    86. getBlock(e, bDownWest, p, w);
    87. getBlock(e, bDownNEast, p, w);
    88. getBlock(e, bDownNWest, p, w);
    89. getBlock(e, bDownSEast, p, w);
    90. getBlock(e, bDownSWest, p, w);
    91. }
    92. }
    93. } else {
    94. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "--- ERROR ---");
    95. p.sendMessage(ChatColor.BLUE
    96. + "There is an error with SULPHUR_EXLOSIVE. It appears that the explosion size "
    97. + "is not a valid size. Please contact an administrator to fix this error.");
    98. }
    99. } else {
    100. p.sendMessage(ChatColor.RED + "You need " + ChatColor.GOLD + miningAmount + ChatColor.RED
    101. + " sulphur to use the mining tool!");
    102. }
    103. }
    104. }
    105. }
    106. }
    107. }
    108. usable = false;
    109. }
     
  2. Offline

    Milkywayz

  3. Offline

    Shzylo

    I've been using the same thing over and over and it is really making me fed up, as I have just tried two methods, and one didn't work at all (using scheduleSyncDelayedTask) and the other was in which I used a video tutorial and it used the Thread.sleep method which I don't find comfortable using.
     
  4. Offline

    MCForger

    Shzylo
    Here is a good class you could use in the resource section for starting to use cooldowns.
     
  5. Offline

    Shzylo

    I've ran into doing this type of thing a lot of times and I keep getting errors or problems. Here is my current code:
    Code:java
    1. ..
    2. @EventHandler
    3. public void onBlockInteract(PlayerInteractEvent e) {
    4. Block b = e.getClickedBlock();
    5. Action act = e.getAction();
    6. Player p = e.getPlayer();
    7. Material hand = p.getItemInHand().getType();
    8. World w = p.getWorld();
    9.  
    10. if (enabled) {
    11. if (hand.equals(Material.SULPHUR)) {
    12. if (act.equals(Action.RIGHT_CLICK_BLOCK)) {
    13. if (b != null) {
    14. ..
    15.  
    16. if (p.hasPermission("mobmeat.sulphur")) {
    17. if (!map.containsKey(p.getName())) {
    18. if (!p.getGameMode().equals(GameMode.CREATIVE)) {
    19. if (p.getItemInHand().getAmount() >= miningAmount) {
    20. if (p.getItemInHand().getAmount() > miningAmount) {
    21. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - miningAmount);
    22. } else if (p.getItemInHand().getAmount() == miningAmount) {
    23. p.setItemInHand(null);
    24. }
    25. usable = true;
    26. }
    27. } else
    28. usable = true;
    29.  
    30. if (usable) {
    31. if ..
    32. } else {
    33. ..
    34. }
    35. } else {
    36. ..
    37. }
    38.  
    39. if(p.getGameMode() != GameMode.CREATIVE) {
    40. d.setCooldownLength(p, miningDelay, map);
    41. d.startCooldown(p, map);
    42. }
    43. } else
    44. p.sendMessage(ChatColor.RED + "You are still on cooldown for another " + d.getTimeLeft(p, map)
    45. + " seconds!");
    46. }
    47. }
    48. }
    49. }
    50. }
    51. usable = false;
    52. }
    53.  
    54. public class Cooldown {
    55.  
    56. // change Main to your plugin's main class
    57. public MobMeat p;
    58.  
    59. public Cooldown(MobMeat instance) {
    60. this.p = instance;
    61. }
    62.  
    63. int task;
    64.  
    65. public void setCooldownLength(Player player, int time, HashMap<String, Integer> hashmap) {
    66. hashmap.put(player.getName(), time);
    67. }
    68.  
    69. public int getTimeLeft(Player player, HashMap<String, Integer> hashmap) {
    70. int time = hashmap.get(player.getName());
    71. return time;
    72. }
    73.  
    74. public void startCooldown(final Player player, final HashMap<String, Integer> hashmap) {
    75. task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(p, new BukkitRunnable() {
    76. public void run() {
    77. int time = hashmap.get(player.getName());
    78. if (time != 0) {
    79. hashmap.put(player.getName(), time - 1);
    80. } else {
    81. hashmap.remove(player.getName());
    82. Bukkit.getServer().getScheduler().cancelTask(task);
    83. }
    84. }
    85. }, 0L, 20L);
    86. }
    87. }
    88. }


    My stack trace for "Plugin cannot be null" when it is not, and it says I get two errors, one at line 434:
    Code:
    task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(p, new BukkitRunnable() {
    and I get another error at 226:
    Code:
    d.startCooldown(p, map);
     
  6. Offline

    Shzylo

    My attempts are still futile.
     
Thread Status:
Not open for further replies.

Share This Page