ItemsAutoDestroy.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 net.sf.l2j.gameserver;
  16. import java.util.List;
  17. import java.util.logging.Logger;
  18. import javolution.util.FastList;
  19. import net.sf.l2j.Config;
  20. import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2World;
  23. import net.sf.l2j.gameserver.templates.L2EtcItemType;
  24. public class ItemsAutoDestroy
  25. {
  26. protected static final Logger _log = Logger.getLogger("ItemsAutoDestroy");
  27. private static ItemsAutoDestroy _instance;
  28. protected List<L2ItemInstance> _items = null;
  29. protected static long _sleep;
  30. private ItemsAutoDestroy()
  31. {
  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. if (_instance == null)
  41. {
  42. _log.info("Initializing ItemsAutoDestroy.");
  43. _instance = new ItemsAutoDestroy();
  44. }
  45. return _instance;
  46. }
  47. public synchronized void addItem (L2ItemInstance item)
  48. {
  49. item.setDropTime(System.currentTimeMillis());
  50. _items.add(item);
  51. }
  52. public synchronized void removeItems()
  53. {
  54. if (Config.DEBUG)
  55. _log.info("[ItemsAutoDestroy] : "+_items.size()+" items to check.");
  56. if (_items.isEmpty()) return;
  57. long curtime = System.currentTimeMillis();
  58. for (L2ItemInstance item : _items)
  59. {
  60. if (item == null || item.getDropTime()==0 || item.getLocation() != L2ItemInstance.ItemLocation.VOID)
  61. _items.remove(item);
  62. else
  63. {
  64. if(item.getItemType() == L2EtcItemType.HERB )
  65. {
  66. if((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  67. {
  68. L2World.getInstance().removeVisibleObject(item,item.getWorldRegion());
  69. L2World.getInstance().removeObject(item);
  70. _items.remove(item);
  71. if (Config.SAVE_DROPPED_ITEM)
  72. ItemsOnGroundManager.getInstance().removeObject(item);
  73. }
  74. }
  75. else if ( (curtime - item.getDropTime()) > _sleep)
  76. {
  77. L2World.getInstance().removeVisibleObject(item,item.getWorldRegion());
  78. L2World.getInstance().removeObject(item);
  79. _items.remove(item);
  80. if (Config.SAVE_DROPPED_ITEM)
  81. ItemsOnGroundManager.getInstance().removeObject(item);
  82. }
  83. }
  84. }
  85. if (Config.DEBUG)
  86. _log.info("[ItemsAutoDestroy] : "+_items.size()+" items remaining.");
  87. }
  88. protected class CheckItemsForDestroy extends Thread
  89. {
  90. @Override
  91. public void run()
  92. {
  93. removeItems();
  94. }
  95. }
  96. }