2
0

ItemsOnGroundManager.java 8.4 KB

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