Solved Cooldown

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

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

    klofno1

    Hi, I have made a plugin which adds magic to the world. Every spell needs a cooldown which I have added to the plugin but the cooldown somehow is for every spell in de class. When I use the spell 'noctem' which has a cooldown for 10 minutes or so. All my other spells have a 10min cooldown. How can I fix that?

    Code:java
    1. package me.JonathanNLD.Magic;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Effect;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12. import org.bukkit.scheduler.BukkitRunnable;
    13.  
    14. public class MagicListener implements Listener {
    15. private List<String> cantDoCommand = new ArrayList<>();
    16.  
    17. Main plugin;
    18. public MagicListener(Main plugin){
    19. this.plugin = plugin;
    20. }
    21.  
    22. @EventHandler
    23. public void onInteract(AsyncPlayerChatEvent event){
    24. //AsyncPlayerChatEvent is used for chat commands
    25. Player player = event.getPlayer();
    26. final String name = player.getName();
    27. //This 'gets' the player
    28. String message = event.getMessage();
    29. //This replaces event.getMessage with 'message'
    30. if (message.trim().equalsIgnoreCase("fulmen")){
    31. if (cantDoCommand.contains(name)){
    32. event.setCancelled(true);
    33. return;
    34. }
    35. //The chat command + trims the command down to only 'fulmen'
    36. player.getWorld().strikeLightning(player.getTargetBlock(null, 100).getLocation());
    37. cantDoCommand.add(name);
    38. //This activates the lightning
    39. new BukkitRunnable(){
    40. @Override
    41. public void run(){
    42. cantDoCommand.remove(name);
    43. }
    44. }.runTaskLater(plugin, 60);
    45. }
    46. }
    47.  
    48. @EventHandler
    49. public void onInteract1(final AsyncPlayerChatEvent event){
    50.  
    51. if(event.getPlayer().getItemInHand().getType()==Material.BOOK){
    52. Player player = event.getPlayer();
    53. event.getPlayer().getWorld().playEffect(event.getPlayer().getLocation(), Effect.SMOKE, 1);
    54. final String name = player.getName();
    55. final String message = event.getMessage();
    56. if (message.trim().equalsIgnoreCase("Noctem")){
    57. if (cantDoCommand.contains(name)){
    58. event.setCancelled(true);
    59.  
    60. return;
    61. }
    62. event.getPlayer().getWorld().setTime(12550);
    63. cantDoCommand.add(name);
    64. new BukkitRunnable(){
    65. @Override
    66. public void run(){
    67. cantDoCommand.remove(name);
    68. }
    69. }.runTaskLater(plugin, 12000);
    70.  
    71. }
    72.  
    73. }
    74. }
    75.  
    76. }


    Nevermind I got it, I needed 2 seperate lists obivously.

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

    CubieX

    You need to use a timer per spell per player.
    So I guess using a Hashmap with the players name as key and an ArrayList as Value could work.
    The arraylist for each player could hold the names of the spells currently on cooldown.
    So you only need to check if that spell is on the arraylist to decide if the player may use this spell.

    When issuing a spell command, check if the players name is already on the hashmap and if so,
    Check if there is an entry for that spell in the corresponding arraylist.
    If so, deny the spell usage.
    If not, allow the usage.

    You have to add the spells name when a spell is cast and delete it when the cooldown timer for this spell is done. (In the run() method of the timer)
     
Thread Status:
Not open for further replies.

Share This Page