ItemsAutoDestroy.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 java.util.logging.Logger;
  18. import javolution.util.FastList;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  21. import com.l2jserver.gameserver.model.L2World;
  22. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  23. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  24. public class ItemsAutoDestroy
  25. {
  26. private static final Logger _log = Logger.getLogger(ItemsAutoDestroy.class.getName());
  27. protected List<L2ItemInstance> _items = null;
  28. protected static long _sleep;
  29. protected ItemsAutoDestroy()
  30. {
  31. _items = new FastList<L2ItemInstance>();
  32. _sleep = Config.AUTODESTROY_ITEM_AFTER * 1000;
  33. if (_sleep == 0) // it should not happend as it is not called when AUTODESTROY_ITEM_AFTER = 0 but we never know..
  34. _sleep = 3600000;
  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 (Config.DEBUG)
  49. _log.info("[ItemsAutoDestroy] : " + _items.size() + " items to check.");
  50. if (_items.isEmpty())
  51. return;
  52. long curtime = System.currentTimeMillis();
  53. for (L2ItemInstance item : _items)
  54. {
  55. if (item == null || item.getDropTime() == 0 || item.getLocation() != L2ItemInstance.ItemLocation.VOID)
  56. _items.remove(item);
  57. else
  58. {
  59. if (item.getItem().getAutoDestroyTime() > 0)
  60. {
  61. if ((curtime - item.getDropTime()) > item.getItem().getAutoDestroyTime())
  62. {
  63. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  64. L2World.getInstance().removeObject(item);
  65. _items.remove(item);
  66. if (Config.SAVE_DROPPED_ITEM)
  67. ItemsOnGroundManager.getInstance().removeObject(item);
  68. }
  69. }
  70. else if (item.getItemType() == L2EtcItemType.HERB)
  71. {
  72. if ((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  73. {
  74. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  75. L2World.getInstance().removeObject(item);
  76. _items.remove(item);
  77. if (Config.SAVE_DROPPED_ITEM)
  78. ItemsOnGroundManager.getInstance().removeObject(item);
  79. }
  80. }
  81. else if ((curtime - item.getDropTime()) > _sleep)
  82. {
  83. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  84. L2World.getInstance().removeObject(item);
  85. _items.remove(item);
  86. if (Config.SAVE_DROPPED_ITEM)
  87. ItemsOnGroundManager.getInstance().removeObject(item);
  88. }
  89. }
  90. }
  91. if (Config.DEBUG)
  92. _log.info("[ItemsAutoDestroy] : " + _items.size() + " items remaining.");
  93. }
  94. protected class CheckItemsForDestroy extends Thread
  95. {
  96. @Override
  97. public void run()
  98. {
  99. removeItems();
  100. }
  101. }
  102. private static class SingletonHolder
  103. {
  104. protected static final ItemsAutoDestroy _instance = new ItemsAutoDestroy();
  105. }
  106. }