Created
July 23, 2013 05:35
-
-
Save Compressions/6060082 to your computer and use it in GitHub Desktop.
Utility class for creating dynamic cooldowns with the Bukkit API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.compressions.example; | |
import java.util.HashMap; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import org.bukkit.scheduler.BukkitRunnable; | |
public class Cooldown { | |
// change Main to your plugin's main class | |
public Main p; | |
public Cooldown(Main i) { | |
p = i; | |
} | |
int task; | |
public void setCooldownLength(Player player, int time, HashMap<String, Integer> hashmap) { | |
hashmap.put(player.getName(), time); | |
} | |
public int getTimeLeft(Player player, HashMap<String, Integer> hashmap) { | |
int time = hashmap.get(player.getName()); | |
return time; | |
} | |
public void startCooldown(final Player player, final HashMap<String, Integer> hashmap) { | |
task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(p, new BukkitRunnable() { | |
public void run() { | |
int time = hashmap.get(player.getName()); | |
if(time != 0) { | |
hashmap.put(player.getName(), time - 1); | |
} else { | |
hashmap.remove(player.getName()); | |
Bukkit.getServer().getScheduler().cancelTask(task); | |
} | |
} | |
}, 0L, 20L); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment