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 package me.Magic.JonathanNLD; import java.util.ArrayList;import java.util.List; import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.AsyncPlayerChatEvent; public class SpellsListener implements Listener { private List<Player> cantDoCommand = new ArrayList<Player>(); Countdown d = new Countdown(); @EventHandler public void onInteract(AsyncPlayerChatEvent event){ //AsyncPlayerChatEvent is used for chat commands Player player = event.getPlayer(); //This 'gets' the player String message = event.getMessage(); //This replaces event.getMessage with 'message' if (message.trim().equalsIgnoreCase("fulmen")){ //The chat command + trims the command down to only 'fulmen' player.getWorld().strikeLightning(player.getTargetBlock(null, 100).getLocation()); //This activates the lightning if(!cantDoCommand.contains(player)){ cantDoCommand.add(player); d.setList(cantDoCommand); d.setPlayer(player); new Thread(d).start(); } } } public class Countdown implements Runnable { public Player player1 = null; public List<Player> cantDoCommand1 = new ArrayList<Player>(); public void setPlayer(Player player) { this.player1 = player; } public void setList(List<Player> list) { this.cantDoCommand1 = list; } public void run() { try { Thread.sleep(80); cantDoCommand1.remove(player1); } catch (Exception ignored) { } } }}
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.
When they cast a spell, for example fulmen they have to wait a few seconds before they can use it again.
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.
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?