Cooldown

Discussion in 'Plugin Development' started by klofno1, Jul 16, 2013.

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

    klofno1

    Hello, I am creating a magic plugin which uses chat command (that's what I call them) to cast spells. For example, if you say 'fulmen' in chat it will cast a spell. Now I'd like to set a cooldown on the spells but I am used to commands using booleans. This is what I have got now but I need some help.

    Code:java
    1. package me.Magic.JonathanNLD;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.AsyncPlayerChatEvent;
    10.  
    11. public class SpellsListener implements Listener {
    12.  
    13. private List<Player> cantDoCommand = new ArrayList<Player>();
    14.  
    15. Countdown d = new Countdown();
    16.  
    17. @EventHandler
    18. public void onInteract(AsyncPlayerChatEvent event){
    19. //AsyncPlayerChatEvent is used for chat commands
    20. Player player = event.getPlayer();
    21. //This 'gets' the player
    22. String message = event.getMessage();
    23. //This replaces event.getMessage with 'message'
    24. if (message.trim().equalsIgnoreCase("fulmen")){
    25. //The chat command + trims the command down to only 'fulmen'
    26. player.getWorld().strikeLightning(player.getTargetBlock(null, 100).getLocation());
    27. //This activates the lightning
    28. if(!cantDoCommand.contains(player)){
    29. cantDoCommand.add(player);
    30. d.setList(cantDoCommand);
    31. d.setPlayer(player);
    32. new Thread(d).start();
    33.  
    34. }
    35. }
    36.  
    37. }
    38. public class Countdown implements Runnable {
    39.  
    40. public Player player1 = null;
    41. public List<Player> cantDoCommand1 = new ArrayList<Player>();
    42. public void setPlayer(Player player) {
    43. this.player1 = player;
    44. }
    45.  
    46. public void setList(List<Player> list) {
    47. this.cantDoCommand1 = list;
    48.  
    49. }
    50.  
    51. public void run() {
    52. try {
    53. Thread.sleep(80);
    54. cantDoCommand1.remove(player1);
    55. } catch (Exception ignored) {
    56. }
    57.  
    58.  
    59. }
    60.  
    61. }
    62. }
    63.  
    64.  
     
  2. Offline

    Pawnguy7

    What kind of cooldown, exactly? Is it global (i.e., any spell use causes cooldown before you can use any other spell), or per-spell? If global, you could store a hashmap with player name/cooldown time, and every tick go through it, decrease it by 1. Maybe wrap this up in a class with functions like canCast() {return hashmap_instance.containsKey(player.getName());} Oh, and adding them when they cast, removing them when it reaches 0.
     
  3. Offline

    klofno1

    When they cast a spell, for example fulmen they have to wait a few seconds before they can use it again.
     
  4. You should save the last time the spell was cast (time in miliseconds as long) somewhere (HashMap/text document/database) on cast and then check if the currenttime - lastcasttime >= cooldown. Using the Scheduler is not really neccessairy, unless you want a notification if the spell goes off cooldown.
     
  5. Offline

    Pawnguy7

    Yes, I know. But like... are there multiple spells?
     
  6. Offline

    klofno1

    Not yet but there are going to be
     
  7. Offline

    Pawnguy7

    Ok. So, let's say we have two spells, A and B. If I use spell A, can I use spell B immediately, or do I have to wait?
     
Thread Status:
Not open for further replies.

Share This Page