TimeStamp.java 3.8 KB

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