TimeStamp.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2004-2013 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.L2Skill;
  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. private final int _id1; // Item or Skill Id.
  31. private final int _id2; // Item Object Id or Skill Level.
  32. private final long _reuse;
  33. private final long _stamp;
  34. private final int _group;
  35. /**
  36. * @param skill the skill upon the stamp will be created.
  37. * @param reuse the reuse time for this skill.
  38. */
  39. public TimeStamp(L2Skill skill, long reuse)
  40. {
  41. _id1 = skill.getId();
  42. _id2 = skill.getLevel();
  43. _reuse = reuse;
  44. _stamp = System.currentTimeMillis() + reuse;
  45. _group = -1;
  46. }
  47. /**
  48. * @param skill the skill upon the stamp will be created.
  49. * @param reuse the reuse time for this skill.
  50. * @param systime overrides the system time with a customized one.
  51. */
  52. public TimeStamp(L2Skill skill, long reuse, long systime)
  53. {
  54. _id1 = skill.getId();
  55. _id2 = skill.getLevel();
  56. _reuse = reuse;
  57. _stamp = systime;
  58. _group = -1;
  59. }
  60. /**
  61. * @param item the item upon the stamp will be created.
  62. * @param reuse the reuse time for this item.
  63. */
  64. public TimeStamp(L2ItemInstance item, long reuse)
  65. {
  66. _id1 = item.getId();
  67. _id2 = item.getObjectId();
  68. _reuse = reuse;
  69. _stamp = System.currentTimeMillis() + reuse;
  70. _group = item.getSharedReuseGroup();
  71. }
  72. /**
  73. * @param item the item upon the stamp will be created.
  74. * @param reuse the reuse time for this item.
  75. * @param systime overrides the system time with a customized one.
  76. */
  77. public TimeStamp(L2ItemInstance item, long reuse, long systime)
  78. {
  79. _id1 = item.getId();
  80. _id2 = item.getObjectId();
  81. _reuse = reuse;
  82. _stamp = systime;
  83. _group = item.getSharedReuseGroup();
  84. }
  85. /**
  86. * @return the time stamp, either the system time where this time stamp was created or the custom time assigned.
  87. */
  88. public long getStamp()
  89. {
  90. return _stamp;
  91. }
  92. /**
  93. * @return the first Id for the item, the item Id.
  94. */
  95. public int getItemId()
  96. {
  97. return _id1;
  98. }
  99. /**
  100. * @return the second Id for the item, the item object Id.
  101. */
  102. public int getItemObjectId()
  103. {
  104. return _id2;
  105. }
  106. /**
  107. * @return the skill Id.
  108. */
  109. public int getSkillId()
  110. {
  111. return _id1;
  112. }
  113. /**
  114. * @return the skill level.
  115. */
  116. public int getSkillLvl()
  117. {
  118. return _id2;
  119. }
  120. /**
  121. * @return the reuse set for this Item/Skill.
  122. */
  123. public long getReuse()
  124. {
  125. return _reuse;
  126. }
  127. /**
  128. * @return the shared reuse group for the item, -1 for skills.
  129. */
  130. public int getSharedReuseGroup()
  131. {
  132. return _group;
  133. }
  134. /**
  135. * @return the remaining time for this time stamp to expire.
  136. */
  137. public long getRemaining()
  138. {
  139. return Math.max(_stamp - System.currentTimeMillis(), 0);
  140. }
  141. /**
  142. * 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.
  143. * @return {@code true} if this time stamp has expired, {@code false} otherwise.
  144. */
  145. public boolean hasNotPassed()
  146. {
  147. return System.currentTimeMillis() < _stamp;
  148. }
  149. }