Solved Simple question regarding cool downs

Discussion in 'Plugin Development' started by camman00, Oct 7, 2015.

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

    camman00

    Hello bukkit,
    Sorry for my English my native language is Spanish. I have a plugin that opens up a inventory which displays an iron chest plate. If the player clicks the Chest plate then the player who clicked will get some items. My question is how would you add a cool down for the player clicking the iron chest plate.
    Thanks for all your help
     
  2. Offline

    mcdorli

    You could store, when the player last clicked the chestplate, and only allow him to click it again, if a certain amount of time passed, this method doesn't use timer events, and can't be messed up because of reloading.
     
  3. Offline

    camman00

    Ok I think I got an idea I'll post the code if it works. Thanks for the guidance

    Sorry, Im stuck, This is the last plugin I need to complete and then I will release. So, How would you go about making a cooldown. Thanks. I tried using hash maps and that didn't work out well :V.

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 8, 2015
  4. Offline

    RoboticPlayer

    @camman00 If you want the cooldown to be saved through server restarts, when they get the items get the current time and add however long you want the cooldown to be, then save it to a config.yml file. Make sure to put in the player's UUID in case they change their name.
    If you don't want it to be saved after a server restarts, it is much simpler. Just us a BukkitRunnable and .runTaskLater() to remove the player from an ArrayList of players who can't use the item (don't actually do an ArrayList storing the player object, use the player's name instead).
     
  5. Offline

    boomboompower

    @camman00 You can just use (as henderry said) bukkit runnables and also something like
    Code:
    getConfig().getInt(playerOnCooldown.getName() + "-Chestplate")
    And have 5 runnables that start when the player clicks the chestplate. You can make the number start at 5 and every second makes it go down till its at 0, then let them use it again.
    Also remember to make it check if the number is over 0, then maybe send them a message saying something like "This is on cooldown!"

    I really don't know how this will work out for you, but then again, its only an example ;D
     
  6. Offline

    camman00

    Lol xD. Thanks for the feedback Ill try it out as soon as I get home.
    EDIT: I don't need the cooldown to save through server restarts due to the cooldowns will be short for each kit, like 1 - 5 mins. Thanks all

    EDIT2: I was able to complete this but I have one more question for y'all, What if I wanted to do this via a HashMap, Would I set the hashMap above my onIventoryClick event (to click the chest plate). Thanks All. <3 bukkit
     
    Last edited: Oct 7, 2015
  7. Offline

    Scimiguy

    mcdorli's suggestion of using timestamps is probably the better option here than using runnables as boom suggested..

    Just have a HashMap somewhere in your plugin (just not inside a method), and store into it the player as the key, and the timestamp as the value.

    In your eventhandler, just check to see if your player is in the map, and compare the stored timestamp to the current one if so.
     
  8. Offline

    camman00

    Ok I got it to work with both thank y'all so much
     
  9. Offline

    Zombie_Striker

    camman00 and boomboompower like this.
Thread Status:
Not open for further replies.

Share This Page