ItemsOnGroundManager.java 8.3 KB

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