Add cooldown to sub kits?

Discussion in 'Plugin Development' started by BeastCraft3, Dec 3, 2014.

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

    BeastCraft3

    Hello, I'm making a kit plugin. All I need to add now is the part I don't know how to add. I want my kit diamond to have 30 minutes cooldown on it. This is my code so far:
    Code:java
    1. package com.BeastCraft3.SoccerKitZ;
    2.  
    3. import java.util.HashSet;
    4. import java.util.UUID;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.Sound;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.PlayerDeathEvent;
    16. import org.bukkit.event.inventory.InventoryClickEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.PlayerInventory;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class KitZ extends JavaPlugin implements Listener{
    24.  
    25. public HashSet<UUID> set = new HashSet<UUID>();
    26.  
    27. public void onEnable() {
    28. getServer().getPluginManager().registerEvents(this, this);
    29. }
    30.  
    31. public void onDisable() {
    32.  
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String []args) {
    36. if (cmd.getName().equalsIgnoreCase("kit")) {
    37. Player p = (Player) sender;
    38. PlayerInventory pi = p.getInventory();
    39. if (args.length == 0) {
    40. openGUI(p);
    41. return true;
    42. }
    43. else if (args.length == 1) {
    44. if (args[0].equalsIgnoreCase("diamond")) {
    45. if(set.contains(p.getUniqueId())) {
    46. p.sendMessage("§cYou have already selected a kit!");
    47. } else {
    48. set.add(p.getUniqueId());
    49. p.sendMessage(ChatColor.DARK_AQUA + "Kit recieved!");
    50. p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
    51. p.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE, 1));
    52. p.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
    53. p.getInventory().addItem(new ItemStack(Material.IRON_HOE, 1));
    54. p.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 320));
    55. p.getInventory().addItem(new ItemStack(Material.FLINT, 320));
    56. p.getInventory().addItem(new ItemStack(Material.SEEDS, 128));
    57. p.getInventory().addItem(new ItemStack(Material.CLAY_BALL, 128));
    58. p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    59. pi.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    60. pi.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    61. pi.setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    62. return true;
    63. }
    64. }
    65. if (args[0].equalsIgnoreCase("basic")) {
    66. if(set.contains(p.getUniqueId())) {
    67. p.sendMessage("§cYou have already selected a kit!");
    68. } else {
    69. p.sendMessage(ChatColor.DARK_AQUA + "Kit recieved!");
    70. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    71. p.getInventory().addItem(new ItemStack(Material.STONE_HOE, 1));
    72. p.getInventory().addItem(new ItemStack(Material.IRON_AXE, 1));
    73. p.getInventory().addItem(new ItemStack(Material.IRON_PICKAXE, 1));
    74. p.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 160));
    75. p.getInventory().addItem(new ItemStack(Material.FLINT, 128));
    76. p.getInventory().addItem(new ItemStack(Material.SEEDS, 96));
    77. p.getInventory().addItem(new ItemStack(Material.CLAY_BALL, 96));
    78. p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
    79. pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    80. pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    81. pi.setHelmet(new ItemStack(Material.IRON_HELMET));
    82. return true;
    83. }
    84. }
    85. }
    86. }
    87. return false;
    88. }
    89.  
    90. @EventHandler
    91. public void onPlayerDeath(PlayerDeathEvent e) {
    92. Player pla = e.getEntity();
    93. if(set.contains(pla.getUniqueId())) {
    94. set.remove(pla.getUniqueId());
    95. }
    96. }
    97.  
    98. private void openGUI(Player player) {
    99. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "Kit Selector");
    100.  
    101. ItemStack Kit_Diamond = new ItemStack (Material.DIAMOND_SWORD);
    102. ItemMeta Kit_DiamondMeta = Kit_Diamond.getItemMeta();
    103.  
    104. ItemStack Kit_Basic = new ItemStack (Material.IRON_SWORD);
    105. ItemMeta Kit_BasicMeta = Kit_Basic.getItemMeta();
    106.  
    107. Kit_DiamondMeta.setDisplayName(ChatColor.BLUE + "Kit Diamond");
    108. Kit_Diamond.setItemMeta(Kit_DiamondMeta);
    109.  
    110. Kit_BasicMeta.setDisplayName(ChatColor.BLUE + "Kit Basic");
    111. Kit_Basic.setItemMeta(Kit_BasicMeta);
    112.  
    113. inv.setItem(1, Kit_Basic);
    114. inv.setItem(2, Kit_Diamond);
    115.  
    116. player.openInventory(inv);
    117. }
    118.  
    119. @EventHandler
    120. public void onInventoryClick(InventoryClickEvent event) {
    121. if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Kit Selector"))
    122. return;
    123. Player player = (Player) event.getWhoClicked();
    124. event.setCancelled(true);
    125.  
    126. if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
    127. player.closeInventory();
    128. return;
    129. }
    130.  
    131. switch (event.getCurrentItem().getType()){
    132. case DIAMOND_SWORD:
    133. player.getWorld().playSound(player.getLocation(),
    134. Sound.GHAST_FIREBALL, 3f, 3f);
    135. player.performCommand("kit diamond");
    136. player.closeInventory();
    137. player.sendMessage(String.format("%sEnjoy %sKit Diamond", ChatColor.RED, ChatColor.BLUE));
    138. break;
    139. case IRON_SWORD:
    140. player.getWorld().playSound(player.getLocation(),
    141. Sound.GHAST_FIREBALL, 3f, 3f);
    142. player.performCommand("kit basic");
    143. player.closeInventory();
    144. player.sendMessage(String.format("%sEnjoy %sKit Basic", ChatColor.RED, ChatColor.BLUE));
    145. break;
    146. default:
    147. player.closeInventory();
    148. break;
    149. }
    150.  
    151. }
    152.  
    153. }
    154.  
     
  2. Offline

    Darkpicasa

    Use a scheduler.
     
  3. Offline

    BeastCraft3

  4. Offline

    Darkpicasa

    Then what are you asking?
     
  5. Offline

    BeastCraft3

    How to use a runnable and make it with a cooldown on 30 minutes.
    If I remember right after tutorials 1 seconds = 20 ticks.
    so 1 minute should be: 1200 ticks
    30 minutes = 36000 ticks
     
  6. Offline

    mythbusterma


    No. There is absolutely no reason to waste CPU cycles on this.

    BeastCraft3

    Attach metadata to the Player that contains the the last time they performed an action, then when they try to use it again, check the metadata to see if enough time has elapsed.
     
    BeastCraft3 likes this.
  7. Offline

    BeastCraft3

    mythbusterma
    Ive never done something like that ;P example code?
     
  8. Offline

    mythbusterma

    BeastCraft3

    Code:java
    1. Player player;
    2. player.setMetadata("plugincooldown", new FixedMetadataValue(plugin, System.currentTimeMillis());
    3.  
    4.  
    5. // later on:
    6.  
    7. if(System.currentTimeMillis - player.getMetadata("plugincooldown").get(0).asLong() > differenceInMillis) {
    8. // allowed
    9. }
    10. else {
    11. // disallowed
    12. }
    13.  


    The String that you use to identify it should be a constant, along with differenceInMillis, but that's the general idea.
     
  9. Offline

    BeastCraft3

    mythbusterma
    honestly I didn't understand much of that ;3 Never worked with that system, any easier ways or more detailed code ?
     
  10. BeastCraft3 More detailed code? He gave you all of the relevant code.
     
  11. Offline

    mythbusterma

    BeastCraft3

    Basically you attach extra information to the Player, and the retrieve that information later on. That is the concept of Bukkit's metadata system.

    You later retrieve the original time that the player executed the action, and compare it to the current time, that will give you the difference in times, using this you can find out if enough time has elapsed for them to perform the action again.
     
  12. Offline

    BeastCraft3

    mythbusterma AdamQpzm
    I'm just guessing after knowledge now. Something like this?
    Code:java
    1. Player player;
    2. player.setMetadata("cooldown", new FixedMetadataValue(plugin, System.currentTimeMillis());
    3.  
    4.  
    5. Don't understand what I should put here
    6.  
    7. if(System.currentTimeMillis -((long)player.getMetadata("plugincooldown")) > differenceInMillis) {
    8. if(set.contains(p.getUniqueId())) {
    9. p.sendMessage("§cYou have already selected a kit!");
    10. } else {
    11. set.add(p.getUniqueId());
    12. p.sendMessage(ChatColor.DARK_AQUA + "Kit recieved!");
    13. p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
    14. p.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE, 1));
    15. p.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
    16. p.getInventory().addItem(new ItemStack(Material.IRON_HOE, 1));
    17. p.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 320));
    18. p.getInventory().addItem(new ItemStack(Material.FLINT, 320));
    19. p.getInventory().addItem(new ItemStack(Material.SEEDS, 128));
    20. p.getInventory().addItem(new ItemStack(Material.CLAY_BALL, 128));
    21. p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    22. pi.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    23. pi.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    24. pi.setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    25. return true;
    26. }
    27. }
    28. else {
    29. p.sendMessage("You cannot use this kit until " + cooldown);
    30. }
     
  13. Offline

    mythbusterma

    BeastCraft3

    That's fine, as long as you define cooldown to be some meaningful variable.

    It's two different pieces of code, and they go in two separate places, one is placement, the other retrieval.
     
  14. Offline

    BeastCraft3

    mythbusterma
    ok, this is to hard for someone who has never tried it and isn't the best in english.
    easier ways?
     
  15. Offline

    mythbusterma

    BeastCraft3

    This isn't very difficult, even if you don't speak English very well.
     
  16. Offline

    BeastCraft3

    mythbusterma
    ok ;P I'll try to find some easy tutorials cause I dindn't understand the code you gave me, thanks anyway ;)
     
  17. Offline

    Totom3

    AdamQpzm I think he was asking for explanations.

    BeastCraft3 I'm kinda bored, so.... there we go. In the code mythbusterma posted :
    Code:java
    1. player.setMetadata("plugincooldown", new FixedMetadataValue(plugin, System.currentTimeMillis());

    In this line, you set the player's metadata with Player#setMetadata(). The"FixedMetadataValue" thing is just a class used to wrap the value you are setting. The first argument of it's constructor takes the plugin, and the second is the value you are really setting. In this code, the current time in milliseconds is being set. In short: this is like noting down the last time where a player performed an action.
    Code:java
    1. if(System.currentTimeMillis -((long)player.getMetadata("plugincooldown")) > differenceInMillis) {
    2. // allowed
    3. } else {
    4. // disallowed
    5. }

    Apart form the ClassCastException you'll get when trying to cast a FixedMetadataValue to a long, this if statement is supposed to check whether or not the player can execute again the action. The way it works is simple: it checks down the note indicating the last time a player performed the action. Then, it substract it from the current time :
    Code:java
    1. // **Invalid code, don't use it**
    2. if (System.currentTimeMillis() - player.getMetadata([...]).get(0).value() > [...])

    This returns the difference in milliseconds between the last time a player performed the action, and the current time. In other words, how long ago the player executed the action. This value is then compared to 'differenceInMillis':
    Code:java
    1. if ([...] > differenceInMillis)

    If I understand right, here differenceInMillis is the time the player has to wait before performing the action again. If you want to impose a delay of 5 seconds, this would be 5000. Now if the last time the player executed the action is bigger than differenceInMillis, then logically, enough time has passed so that the player can perform the action again; that is why it says "allowed". In the other case, the player is still under cooldown; therefore he cannot perform the action again.

    To conclude, I'd like to point out a few things to keep in mind:
    • The player may not have the metadata indicating the last time he performed the action; this would mean he never performed it, and that it should be allowed.
    • The label you use for getting/setting metadata must be the same. In the code you posted there is "cooldown" and "plugincooldown".
    • Speaking of the label, I'd choose another one, that you know no one else would think of. "cooldown" is a very common label and other plugins might use it too. This can create big conflicts later.
    • If the action is allowed you must put the player under cooldown by setting his metadata. You forgot to do that in the code you posted.
     
    mythbusterma likes this.
  18. Offline

    mythbusterma

    Totom3

    Right, sorry. I meant:

    Code:java
    1. player.getMetadata().get(0).asLong()


    My mistake.
     
    Totom3 likes this.
  19. Offline

    JordyPwner

    BeastCraft3 Use a Arraylist and and a scheduler. Check if the player is in the scheduler if so deny them if not allow them. then add them to the list. then make a scheduler of 30min then after 30min remove them from the list ;)
     
  20. Offline

    ColonelHedgehog

  21. Offline

    JordyPwner

  22. Offline

    Hex_27

  23. Offline

    BeastCraft3

    Hex_27
    cool, could you give me one of your codes that use it so I have someone to work after? I've been learning java for a little while now, just starting learning the bukkit api properly.
     
  24. Offline

    Skionz

  25. Offline

    mythbusterma

    Hex_27

    Hooray! Abusing static like there's no tomorrow!
     
    Skionz likes this.
  26. Offline

    BeastCraft3

  27. Offline

    mythbusterma

    BeastCraft3

    ?
    Is this thread not solved? We've done what we can to help.
     
  28. Offline

    BeastCraft3

    mythbusterma
    not really done :p but i'll try to find it out my self(for better experience) using the info you guys told me. Bye!
     
  29. Offline

    ColonelHedgehog

    Static has its uses, but only as a Utility function or for things that last for the entire runtime. i.e. Date.getTime(whatever); might be a valid use. Or:

    Code:
    private Main plugin;
    public static Plugin getPlugin()
    {
        return plugin;
    }
    People usually frown upon the use of the static modifier unless you're using it when it's necessary. It's a lot easier to use the static modifier, but like volatile, transient, and synchronous modifiers, most of the time it's best to tie things to the object.
     
  30. Offline

    mythbusterma

    ColonelHedgehog

    I don't see what those modifiers have to do with this, unless you mean commonly abused modifiers, in which case, I agree.

    And the example you gave is a valid example of a Singleton design pattern, which there is nothing wrong with. However, the other class that was referenced was not designed with a singleton sensibility about it, and was extremely flawed.
     
Thread Status:
Not open for further replies.

Share This Page