java - System.currentTimeMillis() keeps resetting -


i'm trying make cooldown utility in spigot plugin:

package net.gettrillium.trillium.api.cooldown;  import com.google.common.collect.hashbasedtable; import com.google.common.collect.table; import net.gettrillium.trillium.utils; import org.bukkit.bukkit; import org.bukkit.entity.player;  import java.util.uuid;  public class cooldown {  private static table<uuid, cooldowntype, long> cooldown = hashbasedtable.create();  public static void setcooldown(player p, cooldowntype type) {     cooldown.put(p.getuniqueid(), type, system.currenttimemillis()); }  public static boolean hascooldown(player p, cooldowntype type) {     if (cooldown.contains(p.getuniqueid(), type)) {         bukkit.broadcastmessage("get: " + cooldown.get(p.getuniqueid(), type));         bukkit.broadcastmessage("current millis: " + system.currenttimemillis());         bukkit.broadcastmessage("subtracted: " + (system.currenttimemillis() - cooldown.get(p.getuniqueid(), type)));         bukkit.broadcastmessage("in seconds: " + (system.currenttimemillis() - cooldown.get(p.getuniqueid(), type)) / 1000.0);         bukkit.broadcastmessage("> with: " + (type.gettimeinticks() / 20));         bukkit.broadcastmessage("has cooldown: " + (((system.currenttimemillis() - cooldown.get(p.getuniqueid(), type)) / 1000.0) > (type.gettimeinticks() / 20)));         if (((system.currenttimemillis() - cooldown.get(p.getuniqueid(), type)) / 1000.0) > (type.gettimeinticks() / 20)) {             return true;         } else {             cooldown.remove(p.getuniqueid(), type);             return false;         }     } else {         return false;     } }  public static string gettime(player p, cooldowntype type) {     if (hascooldown(p, type)) {         return utils.timetostring((int) ((system.currenttimemillis() - cooldown.get(p.getuniqueid(), type)) / 1000.0));     } else {         return null;     } } } 

the bukkit.broadcastmessage() method sends message console , in game debugging.

my problem every time check cooldown table, cooldown.contains(p.getuniqueid(), type) new system.currenttimemillis(). it's not saving 1 registered in setcooldown.

this cooldown class used here in teleport module, need note if statements related cooldown, else teleport related code.

the debug output:

get: 1433433920944 current millis: 1433433928830 subtracted: 7888 in seconds: 7.889 with: 20 has cooldown: false 

can explain why?

this simple logic bug in hascooldown(). can see debug output, though time in seconds less cooldown length, it's returning false having cooldown.

you can see why easier using temporary variables in calculations. when find entry in map, you're doing equivalent of this:

long startmillis = cooldown.get(p.getuniqueid(), type); double elapsedsecs = (system.currenttimemillis() - startmillis) / 1000.0; long cooldownsecs = type.gettimeinticks() / 20; boolean hascooldown = elapsedsecs > cooldownsecs  // wrong! 

that's backwards: if elapsedsecs > cooldownsecs, cooldown has timed out. cooldown still valid if cooldownsecs < elapsedsecs.

so, when elapsedsecs < cooldownsecs, hascooldown() erroneously thinks cooldown has timed out, deletes , returns false. i'm sure other part of code, finding no cooldown, inserts new one, why you're seeing new one.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -