I am creating some magic spells, and obviously I want a delay between each usage.
Now According to google, there are 1000000000 nanoseconds in a second.
However, the magic spell is recharging after less than two seconds.
@EventHandler
public void onInteract(PlayerInteractEvent e){
Player p = e.getPlayer();
if (p.getItemInHand().getType().equals(Material.BLAZE_ROD)){
{
{
if (!kam.containsKey(p.getName())) kam.put(p.getName(), (long) 0);
if (kam.get(p.getName()) < System.nanoTime()){
kam.remove(p.getName());
kam.put(p.getName(), System.nanoTime() + (1000000000 * 30));
//Do magic spell attack here.
}
}
}else{
p.sendMessage(ChatColor.AQUA + "Skill recharging!");
}
}
Obviously my code is trying to add the System.nanoTime()
plus 30 seconds, and then checking if the current system nanoTime
is greater than the one I put into the hashmap, and if it is, do the spell.
I suggest you print out the value
1000000000 * 30
and check it. You'll find it's wrapping around because it's too big to fit into an integer.In other words, the following program:
prints out:
So, if you use the
long
variant, it will work fine.