/back Cooldown on Death

Discussion in 'Plugin Development' started by Godbrandont, Jun 14, 2014.

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

    Godbrandont

    Hello, I have recently been developing a /back cooldown plugin for the essentials plugin for my server. However I have been having a little trouble with implementing it to activate the cooldown on death.

    I have tried using the onPlayerDeath() function, however I'm not sure how to go about doing it. So far I have made it so when the player types /back, it executes the command and activates the cooldown, then the player cannot type the command for however long designated in the config.

    I would appreciate it greatly if someone could possibly provide me with some code that could activate the cooldown whenever the player dies without it conflicting with anything or anything of the sort.

    Here's the code for the main class that the main cooldown function is a part of:

    Code:java
    1. package me.thecraftmine.tcmcooldown;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7. import java.io.InputStreamReader;
    8. import java.util.ArrayList;
    9. import java.util.Properties;
    10. import java.util.logging.Level;
    11. import java.util.logging.Logger;
    12.  
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.ChatColor;
    15. import org.bukkit.Effect;
    16. import org.bukkit.OfflinePlayer;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.EventPriority;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.entity.EntityDeathEvent;
    22. import org.bukkit.event.entity.PlayerDeathEvent;
    23. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    24. import org.bukkit.event.player.PlayerQuitEvent;
    25. import org.bukkit.plugin.PluginManager;
    26. import org.bukkit.plugin.java.JavaPlugin;
    27.  
    28. public class tcmcooldown extends JavaPlugin implements Listener {
    29. private Properties lastUsedBack = new Properties();
    30. private int defaultCooldown = 10000;
    31. private tcmcooldowngroup[] groups = null;
    32. public void onEnable() {
    33. lastUsedBack.clear();
    34. PluginManager pm = getServer().getPluginManager();
    35. pm.registerEvents(this, this);
    36. getCommand("tcmcooldownreload").setExecutor(new tcmcooldownreloadcommand(this));
    37. loadConfig();
    38. Logger logger = Bukkit.getServer().getLogger();
    39. logger.log(Level.INFO, "TCM Back Cooldown has been enabled!");
    40. return;
    41. }
    42. public void onDisable() {
    43. lastUsedBack.clear();
    44. Logger logger = Bukkit.getServer().getLogger();
    45. logger.log(Level.INFO, "TCM Back Cooldown has been disabled!");
    46. return;
    47. }
    48. void loadConfig() {
    49. ArrayList<tcmcooldowngroup> groupList = new ArrayList<tcmcooldowngroup>();
    50. try {
    51. File dir = getDataFolder();
    52. if (!dir.exists()) dir.mkdirs();
    53. File configFile = new File(dir, "config.txt");
    54. if (!configFile.exists()) {
    55. FileOutputStream fos = new FileOutputStream(configFile);
    56. fos.write(("# The Craft Mine Back Cooldown Config\n").getBytes());
    57. fos.write(("\n").getBytes());
    58. fos.write(("# Define the cooldown below in milliseconds (1000 milliseconds = 1 second)\n").getBytes());
    59. fos.write(("cooldown = 10000\n").getBytes());
    60. fos.write(("\n").getBytes());
    61. fos.close();
    62. }
    63. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile)));
    64. String line = null;
    65. while ((line = reader.readLine()) != null) {
    66. if (line.contains("#")) line = line.substring(0, line.indexOf("#"));
    67. line = line.trim();
    68. if (line.contains("=")) {
    69. String key = line.substring(0, line.indexOf("=")).trim();
    70. String val = line.substring(line.indexOf("=") + 1).trim();
    71. if (key.equals("cooldown")) {
    72. defaultCooldown = Integer.parseInt(val);
    73. } else {
    74. groupList.add(new tcmcooldowngroup(key, Integer.parseInt(val)));
    75. }
    76. }
    77. }
    78. } catch (Exception e) {
    79. e.printStackTrace();
    80. }
    81. groups = groupList.toArray(new tcmcooldowngroup[groupList.size()]);
    82. }
    83. @EventHandler(priority = EventPriority.NORMAL)
    84. public void playerQuit(PlayerQuitEvent event) {
    85. // Not removing the player will result in a memory leak.
    86. lastUsedBack.remove(event.getPlayer().getName());
    87. }
    88. @EventHandler(priority = EventPriority.HIGHEST)
    89. public void playerTypedCommand(PlayerCommandPreprocessEvent event) {
    90. if (event.isCancelled()) return;
    91. String msg = event.getMessage();
    92. if (msg.startsWith("/back") || msg.startsWith("back")) {
    93. long now = System.currentTimeMillis();
    94. Player p = event.getPlayer();
    95. long lastUsed = Long.parseLong(lastUsedBack.getProperty(p.getName(), "0"));
    96. if (lastUsed != 0L) {
    97. int cooldown = -1;
    98. for (int i = 0; i < groups.length; i++) {
    99. if (p.hasPermission("tcmcooldown." + groups[i].group)) {
    100. cooldown = cooldown == -1 ? groups[i].cooldown : Math.min(groups[i].cooldown, cooldown);
    101. }
    102. }
    103. if (cooldown == -1) cooldown = defaultCooldown;
    104. if (now - lastUsed < cooldown) {
    105. event.setCancelled(true);
    106. long timeleft = cooldown - (now - lastUsed);
    107. int timeleftInSeconds = (int) (timeleft / 1000L);
    108. p.sendMessage(ChatColor.RED + "Please wait " + timeleftInSeconds + " second" + (timeleftInSeconds == 1 ? "" : "s") + " before using /back again.");
    109. return;
    110. }
    111. }
    112. lastUsedBack.setProperty(p.getName(), Long.toString(now));
    113. }
    114. }
    115.  
    116. }
    117. [/i][/i][/i]



    Thanks.


    bump

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

    Garris0n

    1. Bump only once per 24 hours.
    2. Read this and fix your class name(s).
    3. What are you even asking for? You have the code right there, you wrote it.
     
  3. Offline

    Godbrandont

    If you actually read my post/code, you would know quite well what I'm asking for. I want to have the function I have which is a cooldown/warmup for the /back cooldown that activates the cooldown when the player dies.
     
  4. Offline

    Garris0n

    Yes, but the code is already there. It's in the command handler, as you said:
     
  5. Offline

    nateracecar5

    Are you saying that the cooldown isn't working? Because you have the code already typed in your class as shown here:
    Code:java
    1. if (lastUsed != 0L) {
    2. int cooldown = -1;
    3. for (int i = 0; i < groups.length; i++) {
    4. if (p.hasPermission("tcmcooldown." + groups[i].group)) {
    5. cooldown = cooldown == -1 ? groups[i].cooldown : Math.min(groups[i].cooldown, cooldown);
    6. }
    7. }
    8. if (cooldown == -1) cooldown = defaultCooldown;
    9. if (now - lastUsed < cooldown) {
    10. event.setCancelled(true);
    11. long timeleft = cooldown - (now - lastUsed);
    12. int timeleftInSeconds = (int) (timeleft / 1000L);
    13. p.sendMessage(ChatColor.RED + "Please wait " + timeleftInSeconds + " second" + (timeleftInSeconds == 1 ? "" : "s") + " before using /back again.");
    14. return;
    15. }
    16. }[/i][/i][/i]


    If it's not working, then say so. Tell us your problem in detail. Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page