ItemsOnGroundManager.java 8.4 KB

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