ItemsAutoDestroy.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.instancemanager.ItemsOnGroundManager;
  24. import com.l2jserver.gameserver.model.L2World;
  25. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  26. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance.ItemLocation;
  27. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  28. public class ItemsAutoDestroy
  29. {
  30. protected List<L2ItemInstance> _items = null;
  31. protected static long _sleep;
  32. protected ItemsAutoDestroy()
  33. {
  34. _items = new FastList<>();
  35. _sleep = Config.AUTODESTROY_ITEM_AFTER * 1000;
  36. if (_sleep == 0)
  37. {
  38. _sleep = 3600000;
  39. }
  40. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new CheckItemsForDestroy(), 5000, 5000);
  41. }
  42. public static ItemsAutoDestroy getInstance()
  43. {
  44. return SingletonHolder._instance;
  45. }
  46. public synchronized void addItem(L2ItemInstance item)
  47. {
  48. item.setDropTime(System.currentTimeMillis());
  49. _items.add(item);
  50. }
  51. public synchronized void removeItems()
  52. {
  53. if (_items.isEmpty())
  54. {
  55. return;
  56. }
  57. long curtime = System.currentTimeMillis();
  58. for (L2ItemInstance item : _items)
  59. {
  60. if ((item == null) || (item.getDropTime() == 0) || (item.getItemLocation() != ItemLocation.VOID))
  61. {
  62. _items.remove(item);
  63. }
  64. else
  65. {
  66. if (item.getItem().getAutoDestroyTime() > 0)
  67. {
  68. if ((curtime - item.getDropTime()) > item.getItem().getAutoDestroyTime())
  69. {
  70. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  71. L2World.getInstance().removeObject(item);
  72. _items.remove(item);
  73. if (Config.SAVE_DROPPED_ITEM)
  74. {
  75. ItemsOnGroundManager.getInstance().removeObject(item);
  76. }
  77. }
  78. }
  79. else if (item.getItemType() == L2EtcItemType.HERB)
  80. {
  81. if ((curtime - item.getDropTime()) > Config.HERB_AUTO_DESTROY_TIME)
  82. {
  83. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  84. L2World.getInstance().removeObject(item);
  85. _items.remove(item);
  86. if (Config.SAVE_DROPPED_ITEM)
  87. {
  88. ItemsOnGroundManager.getInstance().removeObject(item);
  89. }
  90. }
  91. }
  92. else if ((curtime - item.getDropTime()) > _sleep)
  93. {
  94. L2World.getInstance().removeVisibleObject(item, item.getWorldRegion());
  95. L2World.getInstance().removeObject(item);
  96. _items.remove(item);
  97. if (Config.SAVE_DROPPED_ITEM)
  98. {
  99. ItemsOnGroundManager.getInstance().removeObject(item);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. protected class CheckItemsForDestroy extends Thread
  106. {
  107. @Override
  108. public void run()
  109. {
  110. removeItems();
  111. }
  112. }
  113. private static class SingletonHolder
  114. {
  115. protected static final ItemsAutoDestroy _instance = new ItemsAutoDestroy();
  116. }
  117. }