ItemsOnGroundManager.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. private ItemsOnGroundManager()
  42. {
  43. if (Config.SAVE_DROPPED_ITEM)
  44. {
  45. _items = new FastList<L2ItemInstance>();
  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. if (count > 0)
  149. {
  150. _log.info("ItemsOnGroundManager: restored " + count + " items.");
  151. }
  152. else
  153. {
  154. _log.info("Initializing ItemsOnGroundManager.");
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. _log.log(Level.SEVERE, "Error while loading ItemsOnGround " + e.getMessage(), e);
  160. }
  161. finally
  162. {
  163. L2DatabaseFactory.close(con);
  164. }
  165. if (Config.EMPTY_DROPPED_ITEM_TABLE_AFTER_LOAD)
  166. {
  167. emptyTable();
  168. }
  169. }
  170. public void save(L2ItemInstance item)
  171. {
  172. if (!Config.SAVE_DROPPED_ITEM)
  173. {
  174. return;
  175. }
  176. _items.add(item);
  177. }
  178. public void removeObject(L2ItemInstance item)
  179. {
  180. if (Config.SAVE_DROPPED_ITEM && (_items != null))
  181. {
  182. _items.remove(item);
  183. }
  184. }
  185. public void saveInDb()
  186. {
  187. _task.run();
  188. }
  189. public void cleanUp()
  190. {
  191. _items.clear();
  192. }
  193. public void emptyTable()
  194. {
  195. Connection con = null;
  196. try
  197. {
  198. con = L2DatabaseFactory.getInstance().getConnection();
  199. PreparedStatement statement = con.prepareStatement("DELETE FROM itemsonground");
  200. statement.execute();
  201. statement.close();
  202. }
  203. catch (Exception e1)
  204. {
  205. _log.log(Level.SEVERE, "Error while cleaning table ItemsOnGround " + e1.getMessage(), e1);
  206. }
  207. finally
  208. {
  209. L2DatabaseFactory.close(con);
  210. }
  211. }
  212. protected class StoreInDb extends Thread
  213. {
  214. @Override
  215. public synchronized void run()
  216. {
  217. if (!Config.SAVE_DROPPED_ITEM)
  218. {
  219. return;
  220. }
  221. emptyTable();
  222. if (_items.isEmpty())
  223. {
  224. if (Config.DEBUG)
  225. {
  226. _log.warning("ItemsOnGroundManager: nothing to save...");
  227. }
  228. return;
  229. }
  230. Connection con = null;
  231. try
  232. {
  233. con = L2DatabaseFactory.getInstance().getConnection();
  234. PreparedStatement statement = con.prepareStatement("INSERT INTO itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) VALUES(?,?,?,?,?,?,?,?,?)");
  235. for (L2ItemInstance item : _items)
  236. {
  237. if (item == null)
  238. {
  239. continue;
  240. }
  241. if (CursedWeaponsManager.getInstance().isCursed(item.getItemId()))
  242. {
  243. continue; // Cursed Items not saved to ground, prevent double save
  244. }
  245. try
  246. {
  247. statement.setInt(1, item.getObjectId());
  248. statement.setInt(2, item.getItemId());
  249. statement.setLong(3, item.getCount());
  250. statement.setInt(4, item.getEnchantLevel());
  251. statement.setInt(5, item.getX());
  252. statement.setInt(6, item.getY());
  253. statement.setInt(7, item.getZ());
  254. statement.setLong(8, (item.isProtected() ? -1 : item.getDropTime())); // item is protected or AutoDestroyed
  255. statement.setLong(9, (item.isEquipable() ? 1 : 0)); // set equip-able
  256. statement.execute();
  257. statement.clearParameters();
  258. }
  259. catch (Exception e)
  260. {
  261. _log.log(Level.SEVERE, "Error while inserting into table ItemsOnGround: " + e.getMessage(), e);
  262. }
  263. }
  264. statement.close();
  265. }
  266. catch (SQLException e)
  267. {
  268. _log.log(Level.SEVERE, "SQL error while storing items on ground: " + e.getMessage(), e);
  269. }
  270. finally
  271. {
  272. L2DatabaseFactory.close(con);
  273. }
  274. if (Config.DEBUG)
  275. {
  276. _log.info(getClass().getSimpleName() + ": Saved " + _items.size() + " items on ground.");
  277. }
  278. }
  279. }
  280. @SuppressWarnings("synthetic-access")
  281. private static class SingletonHolder
  282. {
  283. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  284. }
  285. }