Solved Cooldown Problems... Pls Help

Discussion in 'Plugin Development' started by MrRedstone3000, Oct 19, 2014.

?

Fix the problem

  1. Solving

    0 vote(s)
    0.0%
  2. Solution

    0 vote(s)
    0.0%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    MrRedstone3000

    I tried to make cooldown but it dont realy works...
    if i go over "cooldown" (my HashMap) it sas "cooldown cannot be resolved or is not a field"

    here my import into the mainClass:

    Code:java
    1. getServer().getPluginManager().registerEvents(new Flash(this), this);



    and here my Class:

    Code:java
    1. package EventHandler;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.Sound;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.Plugin;
    13.  
    14.  
    15.  
    16. public class Flash implements Listener {
    17.  
    18.  
    19.  
    20. private Plugin plugin;
    21. public Flash(Plugin pl){
    22. this.plugin = pl;
    23. }
    24.  
    25.  
    26. @EventHandler
    27.  
    28. public void onFlash(PlayerInteractEvent e) {
    29. Player p = e.getPlayer();
    30.  
    31. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR){
    32. if(e.getPlayer().getItemInHand().getType() == Material.NETHER_STAR){
    33. long time = System.currentTimeMillis();
    34.  
    35. if(plugin.cooldown.containsKey(p.getName())) {
    36. long lastUsage = this.cooldown.get(p.getName());
    37.  
    38. if(lastUsage + 1000*15 > time) {
    39. p.sendMessage("§8You are still on Cooldown for " + lastUsage + " Sekunds");
    40. return;
    41. }
    42. }
    43.  
    44. p.getPlayer().setVelocity(p.getPlayer().getLocation().getDirection().multiply(4));
    45. p.sendMessage("§8You are on Cooldown for 15 Secunds");
    46. p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 10.0F, 2);
    47. this.cooldown.put(p.getName(), time);
    48. }
    49. }
    50. }
    51. }


    Any help is great!
     
  2. Offline

    Dudemister1999

    MrRedstone3000 Would you mind posting your plugin class, I need to see if you have the cooldown variable available.
     
  3. Offline

    MrRedstone3000

    Ok

    Code:java
    1. package main.pack;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.meta.ItemMeta;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. import EventHandler.Fireman;
    15. import EventHandler.Flash;
    16. import EventHandler.Nuker;
    17. import EventHandler.Phantom;
    18. import EventHandler.Pyro;
    19. import EventHandler.SignClick;
    20. import Kits.Kits;
    21.  
    22.  
    23.  
    24. public class main extends JavaPlugin {
    25.  
    26. private Inventory inv=null;
    27.  
    28.  
    29. public void onDisable() {
    30.  
    31. }
    32.  
    33. public void onEnable() {
    34.  
    35. getServer().getPluginManager().registerEvents(new Kits(), this);
    36. getServer().getPluginManager().registerEvents(new Fireman(this), this);
    37. getServer().getPluginManager().registerEvents(new Pyro(), this);
    38. getServer().getPluginManager().registerEvents(new Nuker(), this);
    39. getServer().getPluginManager().registerEvents(new Phantom(this), this);
    40. getServer().getPluginManager().registerEvents(new Flash(this), this);
    41. getServer().getPluginManager().registerEvents(new SignClick(), this);
    42.  
    43. getConfig().options().copyDefaults(true);
    44. saveConfig();
    45.  
    46.  
    47. }
    48.  
    49. HashMap<String, Long> cooldown = new HashMap<String, Long>();
    50.  
    51.  
    52.  
    53. public boolean onCommand(CommandSender sender, Command cmd, String Lable, String[] args) {
    54.  
    55. Player p = (Player) sender;
    56.  
    57. if(cmd.getName().equalsIgnoreCase("kit")) {
    58. if(p.hasPermission("/kit"))
    59. inv = p.getPlayer().getServer().createInventory(null, 45, "§0§lYour Kits");
    60.  
    61.  
    62.  
    63. //gui 8
    64. ItemStack istack08 = new ItemStack(Material.IRON_FENCE);
    65. ItemMeta istackMeta08 = istack08.getItemMeta();
    66. istackMeta08.setDisplayName(" ");
    67. istack08.setItemMeta(istackMeta08);
    68.  
    69. //gui 7
    70. ItemStack istack07 = new ItemStack(Material.IRON_FENCE);
    71. ItemMeta istackMeta07 = istack07.getItemMeta();
    72. istackMeta07.setDisplayName(" ");
    73. istack07.setItemMeta(istackMeta07);
    74.  
    75. //gui 6
    76. ItemStack istack06 = new ItemStack(Material.IRON_FENCE);
    77. ItemMeta istackMeta06 = istack06.getItemMeta();
    78. istackMeta06.setDisplayName(" ");
    79. istack06.setItemMeta(istackMeta06);
    80.  
    81. //gui 5
    82. ItemStack istack05 = new ItemStack(Material.IRON_FENCE);
    83. ItemMeta istackMeta05 = istack05.getItemMeta();
    84. istackMeta05.setDisplayName(" ");
    85. istack05.setItemMeta(istackMeta05);
    86.  
    87. //gui 4
    88. ItemStack istack04 = new ItemStack(Material.BOOK);
    89. ItemMeta istackMeta04 = istack04.getItemMeta();
    90. istackMeta04.setDisplayName("§aAll Kits");
    91. istack04.setItemMeta(istackMeta04);
    92.  
    93. //gui 3
    94. ItemStack istack03 = new ItemStack(Material.IRON_FENCE);
    95. ItemMeta istackMeta03 = istack03.getItemMeta();
    96. istackMeta03.setDisplayName(" ");
    97. istack03.setItemMeta(istackMeta03);
    98.  
    99. //gui 2
    100. ItemStack istack02 = new ItemStack(Material.IRON_FENCE);
    101. ItemMeta istackMeta02 = istack02.getItemMeta();
    102. istackMeta02.setDisplayName(" ");
    103. istack02.setItemMeta(istackMeta02);
    104.  
    105. //gui 1
    106. ItemStack istack01 = new ItemStack(Material.IRON_FENCE);
    107. ItemMeta istackMeta01 = istack01.getItemMeta();
    108. istackMeta01.setDisplayName(" ");
    109. istack01.setItemMeta(istackMeta01);
    110.  
    111. //gui 0
    112. ItemStack istack0 = new ItemStack(Material.IRON_FENCE);
    113. ItemMeta istackMeta0 = istack0.getItemMeta();
    114. istackMeta0.setDisplayName(" ");
    115. istack0.setItemMeta(istackMeta0);
    116.  
    117. //Kit 1
    118. ItemStack istack = new ItemStack(Material.STONE_SWORD);
    119. ItemMeta istackMeta = istack.getItemMeta();
    120. istackMeta.setDisplayName("§rPvP");
    121. istack.setItemMeta(istackMeta);
    122.  
    123. //kit 2
    124. ItemStack istack2 = new ItemStack(Material.BOW);
    125. ItemMeta istackMeta2 = istack2.getItemMeta();
    126. istackMeta2.setDisplayName("§rArcher");
    127. istack2.setItemMeta(istackMeta2);
    128.  
    129.  
    130. //kit 3
    131. ItemStack istack3 = new ItemStack(Material.GOLD_SWORD);
    132. ItemMeta istackMeta3 = istack3.getItemMeta();
    133. istackMeta3.setDisplayName("§rAssassine");
    134. istack3.setItemMeta(istackMeta3);
    135.  
    136.  
    137. //kit 4
    138. ItemStack istack4 = new ItemStack(Material.FLINT_AND_STEEL);
    139. ItemMeta istackMeta4 = istack4.getItemMeta();
    140. istackMeta4.setDisplayName("§rFireman");
    141. istack4.setItemMeta(istackMeta4);
    142.  
    143. //kit 5
    144. ItemStack istack5 = new ItemStack(Material.TNT);
    145. ItemMeta istackMeta5 = istack5.getItemMeta();
    146. istackMeta5.setDisplayName("§rNuker");
    147. istack5.setItemMeta(istackMeta5);
    148.  
    149.  
    150. //kit 6
    151. ItemStack istack6 = new ItemStack(Material.FEATHER);
    152. ItemMeta istackMeta6 = istack6.getItemMeta();
    153. istackMeta6.setDisplayName("§rPhantom");
    154. istack6.setItemMeta(istackMeta6);
    155.  
    156.  
    157. //kit 7
    158. ItemStack istack7 = new ItemStack(Material.NETHER_STAR);
    159. ItemMeta istackMeta7 = istack7.getItemMeta();
    160. istackMeta7.setDisplayName("§rFlash");
    161. istack7.setItemMeta(istackMeta7);
    162.  
    163.  
    164.  
    165.  
    166.  
    167.  
    168.  
    169. inv.setItem(0, istack0);
    170. inv.setItem(1, istack01);
    171. inv.setItem(2, istack02);
    172. inv.setItem(3, istack03);
    173. inv.setItem(4, istack04);
    174. inv.setItem(5, istack05);
    175. inv.setItem(6, istack06);
    176. inv.setItem(7, istack07);
    177. inv.setItem(8, istack08);
    178.  
    179. inv.setItem(9, istack); //kit 1
    180. inv.setItem(10, istack2); //kit 2
    181. inv.setItem(11, istack3); //kit 3
    182. inv.setItem(12, istack4); //kit 4
    183. inv.setItem(13, istack5); //kit 5
    184. inv.setItem(14, istack6); //kit 6
    185. inv.setItem(15, istack7); //kit 7
    186.  
    187.  
    188.  
    189.  
    190.  
    191.  
    192. p.getPlayer().openInventory(inv);
    193.  
    194.  
    195.  
    196. }
    197.  
    198.  
    199.  
    200.  
    201. return false;
    202.  
    203. }
    204.  
    205.  
    206.  
    207.  
    208. }
    209.  
    210.  
    211.  
    212.  
    213.  
     
  4. Offline

    FerusGrim

    You're using 'this' to refer to something in your Main class, from outside of your Main class.

    'this' points to the class that the code is inside of.

    Change:
    Code:java
    1. private Plugin plugin;
    2. public Flash(Plugin pl) {
    3. this.plugin = pl;
    4. }
    To:
    Code:java
    1. private main plugin;
    2. public Flash(main plugin) {
    3. this.plugin = plugin;
    4. }


    Then, refer to cooldown by:
    Code:java
    1. plugin.cooldown


    Also, I chose to ignore the fact that your Main class should either be capitalized, or be the name of your plugin, but chose not to...






    But, really, name your Main class either Main or... whatever the name of your plugin is.

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

    MrRedstone3000

    Tthanks a lot,
    My Main class is called Main but i totaly forgot to Type that in there XD
     
  6. Offline

    FerusGrim

    No problem. If this is resolved, could you mark it as solved? That way, if anyone is looking for an issue like this in the future, they'll see that a solution was found. :)
     
  7. Offline

    MrRedstone3000

    Ok i do it tomorow and if it works i male it solved

    I am at my Handy rigth now...

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

    The Fancy Whale

    Sorry what?
     
  9. Offline

    MrRedstone3000

    I was typing with my handy XD

    So i made it like you sad it now there are no errors at my "cooldown" but it still :/ wont work
    my main class is "main"
    and the package is called main.pack
    and here is the code:

    Code:java
    1. package EventHandler;
    2.  
    3. import main.pack.main;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.Sound;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12.  
    13.  
    14.  
    15. public class Flash implements Listener {
    16.  
    17.  
    18.  
    19. private main plugin;
    20. public Flash(main plugin) {
    21. this.plugin = plugin;
    22. }
    23.  
    24.  
    25. @EventHandler
    26.  
    27. public void onFlash(PlayerInteractEvent e) {
    28. Player p = e.getPlayer();
    29.  
    30. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR){
    31. if(e.getPlayer().getItemInHand().getType() == Material.NETHER_STAR){
    32. long time = System.currentTimeMillis();
    33.  
    34. if(plugin.cooldown.containsKey(p.getName())) {
    35. long lastUsage = plugin.cooldown.get(p.getName());
    36.  
    37. if(lastUsage + 1000*15 > time) {
    38. p.sendMessage("§8You are still on Cooldown for " + lastUsage + " Sekunds");
    39. return;
    40. }
    41. }
    42.  
    43. p.getPlayer().setVelocity(p.getPlayer().getLocation().getDirection().multiply(4));
    44. p.sendMessage("§8You are on Cooldown for 15 Secunds");
    45. p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 10.0F, 2);
    46. plugin.cooldown.put(p.getName(), time);
    47. }
    48. }
    49. }
    50. }



    and my main :



    Code:java
    1. package main.pack;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.meta.ItemMeta;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. import EventHandler.Fireman;
    15. import EventHandler.Flash;
    16. import EventHandler.Nuker;
    17. import EventHandler.Phantom;
    18. import EventHandler.Pyro;
    19. import EventHandler.SignClick;
    20. import Kits.Kits;
    21.  
    22.  
    23.  
    24. public class main extends JavaPlugin {
    25.  
    26. private Inventory inv=null;
    27.  
    28.  
    29. public void onDisable() {
    30.  
    31. }
    32.  
    33. public void onEnable() {
    34.  
    35. getServer().getPluginManager().registerEvents(new Kits(), this);
    36. getServer().getPluginManager().registerEvents(new Fireman(this), this);
    37. getServer().getPluginManager().registerEvents(new Pyro(), this);
    38. getServer().getPluginManager().registerEvents(new Nuker(), this);
    39. getServer().getPluginManager().registerEvents(new Phantom(this), this);
    40. getServer().getPluginManager().registerEvents(new Flash(this), this);
    41. getServer().getPluginManager().registerEvents(new SignClick(), this);
    42.  
    43. getConfig().options().copyDefaults(true);
    44. saveConfig();
    45.  
    46.  
    47. }
    48.  
    49. public HashMap<String, Long> cooldown = new HashMap<String, Long>();
    50.  
    51.  
    52.  
    53. public boolean onCommand(CommandSender sender, Command cmd, String Lable, String[] args) {
    54.  
    55. Player p = (Player) sender;
    56.  
    57. if(cmd.getName().equalsIgnoreCase("kit")) {
    58. if(p.hasPermission("/kit"))
    59. inv = p.getPlayer().getServer().createInventory(null, 45, "§0§lYour Kits");
    60.  
    61.  
    62.  
    63. //gui 8
    64. ItemStack istack08 = new ItemStack(Material.IRON_FENCE);
    65. ItemMeta istackMeta08 = istack08.getItemMeta();
    66. istackMeta08.setDisplayName(" ");
    67. istack08.setItemMeta(istackMeta08);
    68.  
    69. //gui 7
    70. ItemStack istack07 = new ItemStack(Material.IRON_FENCE);
    71. ItemMeta istackMeta07 = istack07.getItemMeta();
    72. istackMeta07.setDisplayName(" ");
    73. istack07.setItemMeta(istackMeta07);
    74.  
    75. //gui 6
    76. ItemStack istack06 = new ItemStack(Material.IRON_FENCE);
    77. ItemMeta istackMeta06 = istack06.getItemMeta();
    78. istackMeta06.setDisplayName(" ");
    79. istack06.setItemMeta(istackMeta06);
    80.  
    81. //gui 5
    82. ItemStack istack05 = new ItemStack(Material.IRON_FENCE);
    83. ItemMeta istackMeta05 = istack05.getItemMeta();
    84. istackMeta05.setDisplayName(" ");
    85. istack05.setItemMeta(istackMeta05);
    86.  
    87. //gui 4
    88. ItemStack istack04 = new ItemStack(Material.BOOK);
    89. ItemMeta istackMeta04 = istack04.getItemMeta();
    90. istackMeta04.setDisplayName("§aAll Kits");
    91. istack04.setItemMeta(istackMeta04);
    92.  
    93. //gui 3
    94. ItemStack istack03 = new ItemStack(Material.IRON_FENCE);
    95. ItemMeta istackMeta03 = istack03.getItemMeta();
    96. istackMeta03.setDisplayName(" ");
    97. istack03.setItemMeta(istackMeta03);
    98.  
    99. //gui 2
    100. ItemStack istack02 = new ItemStack(Material.IRON_FENCE);
    101. ItemMeta istackMeta02 = istack02.getItemMeta();
    102. istackMeta02.setDisplayName(" ");
    103. istack02.setItemMeta(istackMeta02);
    104.  
    105. //gui 1
    106. ItemStack istack01 = new ItemStack(Material.IRON_FENCE);
    107. ItemMeta istackMeta01 = istack01.getItemMeta();
    108. istackMeta01.setDisplayName(" ");
    109. istack01.setItemMeta(istackMeta01);
    110.  
    111. //gui 0
    112. ItemStack istack0 = new ItemStack(Material.IRON_FENCE);
    113. ItemMeta istackMeta0 = istack0.getItemMeta();
    114. istackMeta0.setDisplayName(" ");
    115. istack0.setItemMeta(istackMeta0);
    116.  
    117. //Kit 1
    118. ItemStack istack = new ItemStack(Material.STONE_SWORD);
    119. ItemMeta istackMeta = istack.getItemMeta();
    120. istackMeta.setDisplayName("§rPvP");
    121. istack.setItemMeta(istackMeta);
    122.  
    123. //kit 2
    124. ItemStack istack2 = new ItemStack(Material.BOW);
    125. ItemMeta istackMeta2 = istack2.getItemMeta();
    126. istackMeta2.setDisplayName("§rArcher");
    127. istack2.setItemMeta(istackMeta2);
    128.  
    129.  
    130. //kit 3
    131. ItemStack istack3 = new ItemStack(Material.GOLD_SWORD);
    132. ItemMeta istackMeta3 = istack3.getItemMeta();
    133. istackMeta3.setDisplayName("§rAssassine");
    134. istack3.setItemMeta(istackMeta3);
    135.  
    136.  
    137. //kit 4
    138. ItemStack istack4 = new ItemStack(Material.FLINT_AND_STEEL);
    139. ItemMeta istackMeta4 = istack4.getItemMeta();
    140. istackMeta4.setDisplayName("§rFireman");
    141. istack4.setItemMeta(istackMeta4);
    142.  
    143. //kit 5
    144. ItemStack istack5 = new ItemStack(Material.TNT);
    145. ItemMeta istackMeta5 = istack5.getItemMeta();
    146. istackMeta5.setDisplayName("§rNuker");
    147. istack5.setItemMeta(istackMeta5);
    148.  
    149.  
    150. //kit 6
    151. ItemStack istack6 = new ItemStack(Material.FEATHER);
    152. ItemMeta istackMeta6 = istack6.getItemMeta();
    153. istackMeta6.setDisplayName("§rPhantom");
    154. istack6.setItemMeta(istackMeta6);
    155.  
    156.  
    157. //kit 7
    158. ItemStack istack7 = new ItemStack(Material.NETHER_STAR);
    159. ItemMeta istackMeta7 = istack7.getItemMeta();
    160. istackMeta7.setDisplayName("§rFlash");
    161. istack7.setItemMeta(istackMeta7);
    162.  
    163.  
    164.  
    165.  
    166.  
    167.  
    168.  
    169. inv.setItem(0, istack0);
    170. inv.setItem(1, istack01);
    171. inv.setItem(2, istack02);
    172. inv.setItem(3, istack03);
    173. inv.setItem(4, istack04);
    174. inv.setItem(5, istack05);
    175. inv.setItem(6, istack06);
    176. inv.setItem(7, istack07);
    177. inv.setItem(8, istack08);
    178.  
    179. inv.setItem(9, istack); //kit 1
    180. inv.setItem(10, istack2); //kit 2
    181. inv.setItem(11, istack3); //kit 3
    182. inv.setItem(12, istack4); //kit 4
    183. inv.setItem(13, istack5); //kit 5
    184. inv.setItem(14, istack6); //kit 6
    185. inv.setItem(15, istack7); //kit 7
    186.  
    187.  
    188.  
    189.  
    190.  
    191.  
    192. p.getPlayer().openInventory(inv);
    193.  
    194.  
    195.  
    196. }
    197.  
    198.  
    199.  
    200.  
    201. return false;
    202.  
    203. }
    204.  
    205.  
    206.  
    207.  
    208. }
    209.  
    210.  
    211.  


    sorry that i am so irritating...

    Ok now it works... i dont know why but, ok

    but LastUsage is wierd... it sas you are on cooldown foe 142568732839 sek....

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

    d3v1n302418

    MrRedstone3000 because thats the current time of the System class. Try
    Code:java
    1. ((lastusage/1000)+15)-(System.currentTimeMillis()/1000)
     
  11. Offline

    MrRedstone3000

    Ok, but where do i put it?
    here it wont work:

    Code:java
    1. p.sendMessage("§8You are still on Cooldown for " + lastUsage + " Sekunds");


    i mean if i type you code instead of "lastUsage" it wont work
    the error is : The operator - is undefined for the argument type(s) String, long
     
  12. Offline

    YoloEnderman


    You can't put expressions inside a String like that. You need to use parenthesis.

    Code:java
    1. p.sendMessage("§8You are still on Cooldown for " + (((lastusage/1000)+15)-(System.currentTimeMillis()/1000)) + " Sekunds");
     
  13. Offline

    MrRedstone3000

    Thanks it works fine :)!!
     
Thread Status:
Not open for further replies.

Share This Page