ItemsAutoDestroy.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver;
  20. import java.util.List;
  21. import javolution.util.FastList;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.enums.ItemLocation;
  24. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  25. import com.l2jserver.gameserver.model.L2World;
  26. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  27. public class ItemsAutoDestroy
  28. {
  29. protected List<L2ItemInstance> _items = null;
  30. protected static long _sleep;
  31. protected ItemsAutoDestroy()
  32. {
  33. _items = new FastList<>();
  34. _sleep = Config.AUTODESTROY_ITEM_AFTER * 1000;
  35. if (_sleep == 0)
  36. {
  37. _sleep = 3600000;
  38. }
  39. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new CheckItemsForDestroy(), 5000, 5000);
  40. }
  41. public static ItemsAutoDestroy getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. public synchronized void addItem(L2ItemInstance item)
  46. {
  47. item.setDropTime(System.currentTimeMillis());
  48. _items.add(item);
  49. }
  50. public synchronized void removeItems()
  51. {
  52. if (_items.isEmpty())
  53. {
  54. return;
  55. }
  56. long curtime = System.currentTimeMillis();
  57. for (L2ItemInstance item : _items)
  58. {
  59. if ((item == null) || (item.getDropTime() == 0) || (item.getItemLocation() != ItemLocation.VOID))
  60. {
  61. _items.remove(item);
  62. }
  63. else
  64. {
  65. if (item.getItem().getAutoDestroyTime() > 0)
  66. {
  67. if ((curtime - item.getDropTime()) > item.getItem().getAutoDestroyTime())
  68. {
  69. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  70. L2World.getInstance().removeObject(item);
  71. _items.remove(item);
  72. if (Config.SAVE_DROPPED_ITEM)
  73. {
  74. ItemsOnGroundManager.getInstance().removeObject(item);
  75. }
  76. }
  77. }
  78. else if (item.getItem().hasExImmediateEffect())
  79. {
  80. if ((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  81. {
  82. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  83. L2World.getInstance().removeObject(item);
  84. _items.remove(item);
  85. if (Config.SAVE_DROPPED_ITEM)
  86. {
  87. ItemsOnGroundManager.getInstance().removeObject(item);
  88. }
  89. }
  90. }
  91. else if ((curtime - item.getDropTime()) > _sleep)
  92. {
  93. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  94. L2World.getInstance().removeObject(item);
  95. _items.remove(item);
  96. if (Config.SAVE_DROPPED_ITEM)
  97. {
  98. ItemsOnGroundManager.getInstance().removeObject(item);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. protected class CheckItemsForDestroy extends Thread
  105. {
  106. @Override
  107. public void run()
  108. {
  109. removeItems();
  110. }
  111. }
  112. private static class SingletonHolder
  113. {
  114. protected static final ItemsAutoDestroy _instance = new ItemsAutoDestroy();
  115. }
  116. }