TimeStamp.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model;
  16. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  17. import com.l2jserver.gameserver.model.skills.L2Skill;
  18. /**
  19. * Simple class containing all necessary information to maintain<br>
  20. * valid time stamps and reuse for skills and items reuse upon re-login.<br>
  21. * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
  22. * @author Yesod, Zoey76
  23. */
  24. public class TimeStamp
  25. {
  26. private final int _id1; // Item or Skill Id.
  27. private final int _id2; // Item Object Id or Skill Level.
  28. private final long _reuse;
  29. private final long _stamp;
  30. private final int _group;
  31. /**
  32. * @param skill the skill upon the stamp will be created.
  33. * @param reuse the reuse time for this skill.
  34. */
  35. public TimeStamp(L2Skill skill, long reuse)
  36. {
  37. _id1 = skill.getId();
  38. _id2 = skill.getLevel();
  39. _reuse = reuse;
  40. _stamp = System.currentTimeMillis() + reuse;
  41. _group = -1;
  42. }
  43. /**
  44. * @param skill the skill upon the stamp will be created.
  45. * @param reuse the reuse time for this skill.
  46. * @param systime overrides the system time with a customized one.
  47. */
  48. public TimeStamp(L2Skill skill, long reuse, long systime)
  49. {
  50. _id1 = skill.getId();
  51. _id2 = skill.getLevel();
  52. _reuse = reuse;
  53. _stamp = systime;
  54. _group = -1;
  55. }
  56. /**
  57. * @param item the item upon the stamp will be created.
  58. * @param reuse the reuse time for this item.
  59. */
  60. public TimeStamp(L2ItemInstance item, long reuse)
  61. {
  62. _id1 = item.getItemId();
  63. _id2 = item.getObjectId();
  64. _reuse = reuse;
  65. _stamp = System.currentTimeMillis() + reuse;
  66. _group = item.getSharedReuseGroup();
  67. }
  68. /**
  69. * @param item the item upon the stamp will be created.
  70. * @param reuse the reuse time for this item.
  71. * @param systime overrides the system time with a customized one.
  72. */
  73. public TimeStamp(L2ItemInstance item, long reuse, long systime)
  74. {
  75. _id1 = item.getItemId();
  76. _id2 = item.getObjectId();
  77. _reuse = reuse;
  78. _stamp = systime;
  79. _group = item.getSharedReuseGroup();
  80. }
  81. /**
  82. * @return the time stamp, either the system time where this time stamp was created or the custom time assigned.
  83. */
  84. public long getStamp()
  85. {
  86. return _stamp;
  87. }
  88. /**
  89. * @return the first Id for the item, the item Id.
  90. */
  91. public int getItemId()
  92. {
  93. return _id1;
  94. }
  95. /**
  96. * @return the second Id for the item, the item object Id.
  97. */
  98. public int getItemObjectId()
  99. {
  100. return _id2;
  101. }
  102. /**
  103. * @return the skill Id.
  104. */
  105. public int getSkillId()
  106. {
  107. return _id1;
  108. }
  109. /**
  110. * @return the skill level.
  111. */
  112. public int getSkillLvl()
  113. {
  114. return _id2;
  115. }
  116. /**
  117. * @return the reuse set for this Item/Skill.
  118. */
  119. public long getReuse()
  120. {
  121. return _reuse;
  122. }
  123. /**
  124. * @return the shared reuse group for the item, -1 for skills.
  125. */
  126. public int getSharedReuseGroup()
  127. {
  128. return _group;
  129. }
  130. /**
  131. * @return the remaining time for this time stamp to expire.
  132. */
  133. public long getRemaining()
  134. {
  135. return Math.max(_stamp - System.currentTimeMillis(), 0);
  136. }
  137. /**
  138. * Check if the reuse delay has passed and if it has not then update the stored reuse time according to what is currently remaining on the delay.
  139. * @return {@code true} if this time stamp has expired, {@code false} otherwise.
  140. */
  141. public boolean hasNotPassed()
  142. {
  143. return System.currentTimeMillis() < _stamp;
  144. }
  145. }