ItemsAutoDestroy.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. protected static final Logger _log = Logger.getLogger(ItemsAutoDestroy.class.getName());
  27. protected List<L2ItemInstance> _items = null;
  28. protected static long _sleep;
  29. private ItemsAutoDestroy()
  30. {
  31. _log.info("Initializing ItemsAutoDestroy.");
  32. _items = new FastList<L2ItemInstance>();
  33. _sleep = Config.AUTODESTROY_ITEM_AFTER * 1000;
  34. if (_sleep == 0) // it should not happend as it is not called when AUTODESTROY_ITEM_AFTER = 0 but we never know..
  35. _sleep = 3600000;
  36. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new CheckItemsForDestroy(), 5000, 5000);
  37. }
  38. public static ItemsAutoDestroy getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. public synchronized void addItem(L2ItemInstance item)
  43. {
  44. item.setDropTime(System.currentTimeMillis());
  45. _items.add(item);
  46. }
  47. public synchronized void removeItems()
  48. {
  49. if (Config.DEBUG)
  50. _log.info("[ItemsAutoDestroy] : " + _items.size() + " items to check.");
  51. if (_items.isEmpty())
  52. return;
  53. long curtime = System.currentTimeMillis();
  54. for (L2ItemInstance item : _items)
  55. {
  56. if (item == null || item.getDropTime() == 0 || item.getLocation() != L2ItemInstance.ItemLocation.VOID)
  57. _items.remove(item);
  58. else
  59. {
  60. if (item.getItem().getAutoDestroyTime() > 0)
  61. {
  62. if ((curtime - item.getDropTime()) > item.getItem().getAutoDestroyTime())
  63. {
  64. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  65. L2World.getInstance().removeObject(item);
  66. _items.remove(item);
  67. if (Config.SAVE_DROPPED_ITEM)
  68. ItemsOnGroundManager.getInstance().removeObject(item);
  69. }
  70. }
  71. else if (item.getItemType() == L2EtcItemType.HERB)
  72. {
  73. if ((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  74. {
  75. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  76. L2World.getInstance().removeObject(item);
  77. _items.remove(item);
  78. if (Config.SAVE_DROPPED_ITEM)
  79. ItemsOnGroundManager.getInstance().removeObject(item);
  80. }
  81. }
  82. else if ((curtime - item.getDropTime()) > _sleep)
  83. {
  84. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  85. L2World.getInstance().removeObject(item);
  86. _items.remove(item);
  87. if (Config.SAVE_DROPPED_ITEM)
  88. ItemsOnGroundManager.getInstance().removeObject(item);
  89. }
  90. }
  91. }
  92. if (Config.DEBUG)
  93. _log.info("[ItemsAutoDestroy] : " + _items.size() + " items remaining.");
  94. }
  95. protected class CheckItemsForDestroy extends Thread
  96. {
  97. @Override
  98. public void run()
  99. {
  100. removeItems();
  101. }
  102. }
  103. @SuppressWarnings("synthetic-access")
  104. private static class SingletonHolder
  105. {
  106. protected static final ItemsAutoDestroy _instance = new ItemsAutoDestroy();
  107. }
  108. }