System.currentTimeMillis Granularity

Discussion in 'Spigot Plugin Development' started by WAS, Apr 14, 2017.

  1. WAS

    WAS

    Can granularity in System#currentTimeMillis() perhaps cause issues with setting time in the future?

    The Method: For example. I am using it for cooldowns, settings a time period 30 seconds in the future. If the the player executes a command before 30 seconds, they'll get a message. If it's past 30 seconds the plugin will remove the timestamp and let them continue.

    The Bug: However I have gotten a report from one person that sometimes when they invite someone the invite will always be removed as if 30 seconds old. (I have seen this bug recorded and in screenshot form, it does happen)

    The Issue: I cannot reproduce this on my Windows 10 machine, or Debian, or dedicated Windows Server 2008 so I'm not sure how to go about diagnosing and fixing it without returning to a bukkit task.
     
    #1 WAS, Apr 14, 2017
    Last edited: Apr 14, 2017
  2. I would review the your code before contemplating an issue with currentTimeMillis.

    I use a piece of code that tracks millis by the second, the same idea you have but for abilities and the cool downs are about 1-2 seconds without problem.
     
  3. WAS

    WAS

    Thanks. I'll keep an eye out though not sure how you can go wrong with systemTimestampInFuture <= System.currentTimeMillis() and again, only one person has an issue, I can not reproduce it under any circumstances.

    According to this, if the returned format is longer than if I am adding a set time to this timestamp, I'd be adding less time.
     
  4. What are you using systemTimestampInFuture? I generally add to the currentTimeMillis() with additional milliseconds.
     
  5. WAS

    WAS

    systemTimestampInFuture is a safe sentence way of putting
    Code (Text):
    System.currentTimeMillis() + ( 30 * 1000 )
    as in whatever timestamp you set in the future and previously set to a user. Just a placeholder.

    Just saying it's hard to mess up