ItemsAutoDestroy.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  16. import java.util.List;
  17. import javolution.util.FastList;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  20. import com.l2jserver.gameserver.model.L2World;
  21. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  23. public class ItemsAutoDestroy
  24. {
  25. protected List<L2ItemInstance> _items = null;
  26. protected static long _sleep;
  27. protected ItemsAutoDestroy()
  28. {
  29. _items = new FastList<>();
  30. _sleep = Config.AUTODESTROY_ITEM_AFTER * 1000;
  31. if (_sleep == 0)
  32. {
  33. _sleep = 3600000;
  34. }
  35. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new CheckItemsForDestroy(), 5000, 5000);
  36. }
  37. public static ItemsAutoDestroy getInstance()
  38. {
  39. return SingletonHolder._instance;
  40. }
  41. public synchronized void addItem(L2ItemInstance item)
  42. {
  43. item.setDropTime(System.currentTimeMillis());
  44. _items.add(item);
  45. }
  46. public synchronized void removeItems()
  47. {
  48. if (_items.isEmpty())
  49. {
  50. return;
  51. }
  52. long curtime = System.currentTimeMillis();
  53. for (L2ItemInstance item : _items)
  54. {
  55. if ((item == null) || (item.getDropTime() == 0) || (item.getLocation() != L2ItemInstance.ItemLocation.VOID))
  56. {
  57. _items.remove(item);
  58. }
  59. else
  60. {
  61. if (item.getItem().getAutoDestroyTime() > 0)
  62. {
  63. if ((curtime - item.getDropTime()) > item.getItem().getAutoDestroyTime())
  64. {
  65. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  66. L2World.getInstance().removeObject(item);
  67. _items.remove(item);
  68. if (Config.SAVE_DROPPED_ITEM)
  69. {
  70. ItemsOnGroundManager.getInstance().removeObject(item);
  71. }
  72. }
  73. }
  74. else if (item.getItemType() == L2EtcItemType.HERB)
  75. {
  76. if ((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  77. {
  78. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  79. L2World.getInstance().removeObject(item);
  80. _items.remove(item);
  81. if (Config.SAVE_DROPPED_ITEM)
  82. {
  83. ItemsOnGroundManager.getInstance().removeObject(item);
  84. }
  85. }
  86. }
  87. else if ((curtime - item.getDropTime()) > _sleep)
  88. {
  89. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  90. L2World.getInstance().removeObject(item);
  91. _items.remove(item);
  92. if (Config.SAVE_DROPPED_ITEM)
  93. {
  94. ItemsOnGroundManager.getInstance().removeObject(item);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. protected class CheckItemsForDestroy extends Thread
  101. {
  102. @Override
  103. public void run()
  104. {
  105. removeItems();
  106. }
  107. }
  108. private static class SingletonHolder
  109. {
  110. protected static final ItemsAutoDestroy _instance = new ItemsAutoDestroy();
  111. }
  112. }