ItemsAutoDestroy.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver;
  20. import java.util.List;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import net.sf.l2j.Config;
  24. import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
  25. import net.sf.l2j.gameserver.model.L2ItemInstance;
  26. import net.sf.l2j.gameserver.model.L2World;
  27. import net.sf.l2j.gameserver.templates.L2EtcItemType;
  28. public class ItemsAutoDestroy
  29. {
  30. protected static final Logger _log = Logger.getLogger("ItemsAutoDestroy");
  31. private static ItemsAutoDestroy _instance;
  32. protected List<L2ItemInstance> _items = null;
  33. protected static long _sleep;
  34. private ItemsAutoDestroy()
  35. {
  36. _items = new FastList<L2ItemInstance>();
  37. _sleep = Config.AUTODESTROY_ITEM_AFTER * 1000;
  38. if(_sleep == 0) // it should not happend as it is not called when AUTODESTROY_ITEM_AFTER = 0 but we never know..
  39. _sleep = 3600000;
  40. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new CheckItemsForDestroy(),5000,5000);
  41. }
  42. public static ItemsAutoDestroy getInstance()
  43. {
  44. if (_instance == null)
  45. {
  46. System.out.println("Initializing ItemsAutoDestroy.");
  47. _instance = new ItemsAutoDestroy();
  48. }
  49. return _instance;
  50. }
  51. public synchronized void addItem (L2ItemInstance item)
  52. {
  53. item.setDropTime(System.currentTimeMillis());
  54. _items.add(item);
  55. }
  56. public synchronized void removeItems()
  57. {
  58. if (Config.DEBUG)
  59. _log.info("[ItemsAutoDestroy] : "+_items.size()+" items to check.");
  60. if (_items.isEmpty()) return;
  61. long curtime = System.currentTimeMillis();
  62. for (L2ItemInstance item : _items)
  63. {
  64. if (item == null || item.getDropTime()==0 || item.getLocation() != L2ItemInstance.ItemLocation.VOID)
  65. _items.remove(item);
  66. else
  67. {
  68. if(item.getItemType() == L2EtcItemType.HERB )
  69. {
  70. if((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  71. {
  72. L2World.getInstance().removeVisibleObject(item,item.getWorldRegion());
  73. L2World.getInstance().removeObject(item);
  74. _items.remove(item);
  75. if (Config.SAVE_DROPPED_ITEM)
  76. ItemsOnGroundManager.getInstance().removeObject(item);
  77. }
  78. }
  79. else if ( (curtime - item.getDropTime()) > _sleep)
  80. {
  81. L2World.getInstance().removeVisibleObject(item,item.getWorldRegion());
  82. L2World.getInstance().removeObject(item);
  83. _items.remove(item);
  84. if (Config.SAVE_DROPPED_ITEM)
  85. ItemsOnGroundManager.getInstance().removeObject(item);
  86. }
  87. }
  88. }
  89. if (Config.DEBUG)
  90. _log.info("[ItemsAutoDestroy] : "+_items.size()+" items remaining.");
  91. }
  92. protected class CheckItemsForDestroy extends Thread
  93. {
  94. @Override
  95. public void run()
  96. {
  97. removeItems();
  98. }
  99. }
  100. }