ItemsOnGroundManager.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.ItemsAutoDestroy;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  30. import com.l2jserver.util.L2FastList;
  31. /**
  32. * This class manage all items on ground.
  33. * @author DiezelMax, original idea.
  34. * @author Enforcer, actual build.
  35. */
  36. public class ItemsOnGroundManager
  37. {
  38. static final Logger _log = Logger.getLogger(ItemsOnGroundManager.class.getName());
  39. protected List<L2ItemInstance> _items = new L2FastList<>(true);
  40. private final StoreInDb _task = new StoreInDb();
  41. protected ItemsOnGroundManager()
  42. {
  43. if (Config.SAVE_DROPPED_ITEM_INTERVAL > 0)
  44. {
  45. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(_task, Config.SAVE_DROPPED_ITEM_INTERVAL, Config.SAVE_DROPPED_ITEM_INTERVAL);
  46. }
  47. load();
  48. }
  49. private void load()
  50. {
  51. // If SaveDroppedItem is false, may want to delete all items previously stored to avoid add old items on reactivate
  52. if (!Config.SAVE_DROPPED_ITEM && Config.CLEAR_DROPPED_ITEM_TABLE)
  53. {
  54. emptyTable();
  55. }
  56. if (!Config.SAVE_DROPPED_ITEM)
  57. {
  58. return;
  59. }
  60. // if DestroyPlayerDroppedItem was previously false, items currently protected will be added to ItemsAutoDestroy
  61. if (Config.DESTROY_DROPPED_PLAYER_ITEM)
  62. {
  63. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  64. {
  65. String str = null;
  66. if (!Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  67. {
  68. // Recycle misc. items only
  69. str = "update itemsonground set drop_time=? where drop_time=-1 and equipable=0";
  70. }
  71. else if (Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  72. {
  73. // Recycle all items including equip-able
  74. str = "update itemsonground set drop_time=? where drop_time=-1";
  75. }
  76. PreparedStatement statement = con.prepareStatement(str);
  77. statement.setLong(1, System.currentTimeMillis());
  78. statement.execute();
  79. statement.close();
  80. }
  81. catch (Exception e)
  82. {
  83. _log.log(Level.SEVERE, "Error while updating table ItemsOnGround " + e.getMessage(), e);
  84. }
  85. }
  86. // Add items to world
  87. L2ItemInstance item;
  88. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  89. {
  90. PreparedStatement statement = con.prepareStatement("SELECT object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable FROM itemsonground");
  91. ResultSet rset;
  92. int count = 0;
  93. rset = statement.executeQuery();
  94. while (rset.next())
  95. {
  96. item = new L2ItemInstance(rset.getInt(1), rset.getInt(2));
  97. L2World.getInstance().storeObject(item);
  98. // this check and..
  99. if (item.isStackable() && (rset.getInt(3) > 1))
  100. {
  101. item.setCount(rset.getInt(3));
  102. }
  103. // this, are really necessary?
  104. if (rset.getInt(4) > 0)
  105. {
  106. item.setEnchantLevel(rset.getInt(4));
  107. }
  108. item.getPosition().setWorldPosition(rset.getInt(5), rset.getInt(6), rset.getInt(7));
  109. item.getPosition().setWorldRegion(L2World.getInstance().getRegion(item.getPosition().getWorldPosition()));
  110. item.getPosition().getWorldRegion().addVisibleObject(item);
  111. final long dropTime = rset.getLong(8);
  112. item.setDropTime(dropTime);
  113. item.setProtected(dropTime == -1);
  114. item.setIsVisible(true);
  115. L2World.getInstance().addVisibleObject(item, item.getPosition().getWorldRegion());
  116. _items.add(item);
  117. count++;
  118. // add to ItemsAutoDestroy only items not protected
  119. if (!Config.LIST_PROTECTED_ITEMS.contains(item.getItemId()))
  120. {
  121. if (dropTime > -1)
  122. {
  123. if (((Config.AUTODESTROY_ITEM_AFTER > 0) && (item.getItemType() != L2EtcItemType.HERB)) || ((Config.HERB_AUTO_DESTROY_TIME > 0) && (item.getItemType() == L2EtcItemType.HERB)))
  124. {
  125. ItemsAutoDestroy.getInstance().addItem(item);
  126. }
  127. }
  128. }
  129. }
  130. rset.close();
  131. statement.close();
  132. _log.info("ItemsOnGroundManager: Loaded " + count + " items.");
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.SEVERE, "Error while loading ItemsOnGround " + e.getMessage(), e);
  137. }
  138. if (Config.EMPTY_DROPPED_ITEM_TABLE_AFTER_LOAD)
  139. {
  140. emptyTable();
  141. }
  142. }
  143. public void save(L2ItemInstance item)
  144. {
  145. if (!Config.SAVE_DROPPED_ITEM)
  146. {
  147. return;
  148. }
  149. _items.add(item);
  150. }
  151. public void removeObject(L2ItemInstance item)
  152. {
  153. if (Config.SAVE_DROPPED_ITEM)
  154. {
  155. _items.remove(item);
  156. }
  157. }
  158. public void saveInDb()
  159. {
  160. _task.run();
  161. }
  162. public void cleanUp()
  163. {
  164. _items.clear();
  165. }
  166. public void emptyTable()
  167. {
  168. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  169. {
  170. PreparedStatement statement = con.prepareStatement("DELETE FROM itemsonground");
  171. statement.execute();
  172. statement.close();
  173. }
  174. catch (Exception e1)
  175. {
  176. _log.log(Level.SEVERE, "Error while cleaning table ItemsOnGround " + e1.getMessage(), e1);
  177. }
  178. }
  179. protected class StoreInDb extends Thread
  180. {
  181. @Override
  182. public synchronized void run()
  183. {
  184. if (!Config.SAVE_DROPPED_ITEM)
  185. {
  186. return;
  187. }
  188. emptyTable();
  189. if (_items.isEmpty())
  190. {
  191. if (Config.DEBUG)
  192. {
  193. _log.warning("ItemsOnGroundManager: nothing to save...");
  194. }
  195. return;
  196. }
  197. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  198. {
  199. PreparedStatement statement = con.prepareStatement("INSERT INTO itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) VALUES(?,?,?,?,?,?,?,?,?)");
  200. for (L2ItemInstance item : _items)
  201. {
  202. if (item == null)
  203. {
  204. continue;
  205. }
  206. if (CursedWeaponsManager.getInstance().isCursed(item.getItemId()))
  207. {
  208. continue; // Cursed Items not saved to ground, prevent double save
  209. }
  210. try
  211. {
  212. statement.setInt(1, item.getObjectId());
  213. statement.setInt(2, item.getItemId());
  214. statement.setLong(3, item.getCount());
  215. statement.setInt(4, item.getEnchantLevel());
  216. statement.setInt(5, item.getX());
  217. statement.setInt(6, item.getY());
  218. statement.setInt(7, item.getZ());
  219. statement.setLong(8, (item.isProtected() ? -1 : item.getDropTime())); // item is protected or AutoDestroyed
  220. statement.setLong(9, (item.isEquipable() ? 1 : 0)); // set equip-able
  221. statement.execute();
  222. statement.clearParameters();
  223. }
  224. catch (Exception e)
  225. {
  226. _log.log(Level.SEVERE, "Error while inserting into table ItemsOnGround: " + e.getMessage(), e);
  227. }
  228. }
  229. statement.close();
  230. }
  231. catch (SQLException e)
  232. {
  233. _log.log(Level.SEVERE, "SQL error while storing items on ground: " + e.getMessage(), e);
  234. }
  235. if (Config.DEBUG)
  236. {
  237. _log.info(getClass().getSimpleName() + ": Saved " + _items.size() + " items on ground.");
  238. }
  239. }
  240. }
  241. public static final ItemsOnGroundManager getInstance()
  242. {
  243. return SingletonHolder._instance;
  244. }
  245. private static class SingletonHolder
  246. {
  247. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  248. }
  249. }