Getting the time remaining from an essentials kit. (Converting timestamp to seconds)

Discussion in 'Plugin Development' started by krizzdawg, Jul 27, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    krizzdawg

    Essentials stores the cooldown in a timestamp like this:
    Code:
    lastAccountName: aimbotcalvin
    timestamps:
      login: 1501212429368
      logout: 1501210425005
      kits:
        tools: 1501213477592 //timestamp
        donator: 1501214870174 //timestamp
      lastteleport: 1501198701816
    ipAddress: 12.345.678.91
    
    I need to know how to convert this into seconds.

    I think I am suppose to take the timestamp, get the difference between the timestamp & and the current time. Then convert to seconds somehow. I am kinda lost so any help is appreciated .
     
  2. Offline

    Zombie_Striker

    @krizzdawg
    Those look like Milliseconds. Just take the current time in millis, subtract the timestamp, and divide by 1000 to get the difference in seconds.
     
  3. Offline

    krizzdawg

    Okay, for the most part that worked. But I have found out another way. I'm not sure how to do it but if you know how please let me know. I would basically use the "Kit.java" but currently I don't know how to create the Kit object. I tried taking a look here: https://github.com/essentials/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/Kit.java
    but I'm having issues. Please let me know if you have a solution.
     
  4. Offline

    Horsey

    Don't hook into essentials, you need to do a lot of messing around with instances to get:
    1. A User from a Player (I have no idea how)
    2. An instance of Essentials (I have an idea, but idrk)
    It's better for you to just read that timestamp from the data file that essentials creates, here's how they do it:
    Code:Java
    1.  
    2. public long getNextUse(final User user) throws Exception
    3. {
    4. if (user.isAuthorized("essentials.kit.exemptdelay"))
    5. {
    6. return 0L;
    7. }
    8.  
    9. final Calendar time = new GregorianCalendar();
    10.  
    11. double delay = 0;
    12. try
    13. {
    14. // Make sure delay is valid
    15. delay = kit.containsKey("delay") ? ((Number)kit.get("delay")).doubleValue() : 0.0d;
    16. }
    17. catch (Exception e)
    18. {
    19. throw new Exception(tl("kitError2"));
    20. }
    21.  
    22. // When was the last kit used?
    23. final long lastTime = user.getKitTimestamp(kitName);
    24.  
    25. // When can be use the kit again?
    26. final Calendar delayTime = new GregorianCalendar();
    27. delayTime.setTimeInMillis(lastTime);
    28. delayTime.add(Calendar.SECOND, (int)delay);
    29. delayTime.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
    30.  
    31. if (lastTime == 0L || lastTime > time.getTimeInMillis())
    32. {
    33. // If we have no record of kit use, or its corrupted, give them benifit of the doubt.
    34. return 0L;
    35. }
    36. else if (delay < 0d)
    37. {
    38. // If the kit has a negative kit time, it can only be used once.
    39. return -1;
    40. }
    41. else if (delayTime.before(time))
    42. {
    43. // If the kit was used in the past, but outside the delay time, it can be used.
    44. return 0L;
    45. }
    46. else
    47. {
    48. // If the kit has been used recently, return the next time it can be used.
    49. return delayTime.getTimeInMillis();
    50. }
    51. }
     
  5. Offline

    krizzdawg

    I managed to get everything to work while hooking into essentials, thanks for the help!
    Code:Java
    1.  
     
  6. Offline

    Machine Maker

    @krizzdawg
    Please mark this thread as Solved if your issue has been resolved.
     
Thread Status:
Not open for further replies.

Share This Page