ItemsOnGroundManager.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.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.L2Object;
  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 FastList<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(new StoreInDb(), 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);
  83. e.printStackTrace();
  84. }
  85. finally
  86. {
  87. try
  88. {
  89. con.close();
  90. }
  91. catch (Exception e)
  92. {
  93. }
  94. }
  95. }
  96. //Add items to world
  97. Connection con = null;
  98. L2ItemInstance item;
  99. try
  100. {
  101. con = L2DatabaseFactory.getInstance().getConnection();
  102. Statement s = con.createStatement();
  103. ResultSet result;
  104. int count = 0;
  105. result = s.executeQuery("select object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable from itemsonground");
  106. while (result.next())
  107. {
  108. item = new L2ItemInstance(result.getInt(1), result.getInt(2));
  109. L2World.getInstance().storeObject(item);
  110. if (item.isStackable() && result.getInt(3) > 1) //this check and..
  111. item.setCount(result.getInt(3));
  112. if (result.getInt(4) > 0) // this, are really necessary?
  113. item.setEnchantLevel(result.getInt(4));
  114. item.getPosition().setWorldPosition(result.getInt(5), result.getInt(6), result.getInt(7));
  115. item.getPosition().setWorldRegion(L2World.getInstance().getRegion(item.getPosition().getWorldPosition()));
  116. item.getPosition().getWorldRegion().addVisibleObject(item);
  117. item.setDropTime(result.getLong(8));
  118. item.setProtected(result.getLong(8) == -1);
  119. item.setIsVisible(true);
  120. L2World.getInstance().addVisibleObject(item, item.getPosition().getWorldRegion());
  121. _items.add(item);
  122. count++;
  123. // add to ItemsAutoDestroy only items not protected
  124. if (!Config.LIST_PROTECTED_ITEMS.contains(item.getItemId()))
  125. {
  126. if (result.getLong(8) > -1)
  127. {
  128. if ((Config.AUTODESTROY_ITEM_AFTER > 0 && item.getItemType() != L2EtcItemType.HERB)
  129. || (Config.HERB_AUTO_DESTROY_TIME > 0 && item.getItemType() == L2EtcItemType.HERB))
  130. ItemsAutoDestroy.getInstance().addItem(item);
  131. }
  132. }
  133. }
  134. result.close();
  135. s.close();
  136. if (count > 0)
  137. _log.info("ItemsOnGroundManager: restored " + count + " items.");
  138. else
  139. _log.info("Initializing ItemsOnGroundManager.");
  140. }
  141. catch (Exception e)
  142. {
  143. _log.log(Level.SEVERE, "error while loading ItemsOnGround " + e);
  144. e.printStackTrace();
  145. }
  146. finally
  147. {
  148. try
  149. {
  150. con.close();
  151. }
  152. catch (Exception e)
  153. {
  154. }
  155. }
  156. if (Config.EMPTY_DROPPED_ITEM_TABLE_AFTER_LOAD)
  157. emptyTable();
  158. }
  159. public void save(L2ItemInstance item)
  160. {
  161. if (!Config.SAVE_DROPPED_ITEM)
  162. return;
  163. _items.add(item);
  164. }
  165. public void removeObject(L2Object item)
  166. {
  167. if (Config.SAVE_DROPPED_ITEM && _items != null)
  168. {
  169. _items.remove(item);
  170. }
  171. }
  172. public void saveInDb()
  173. {
  174. _task.run();
  175. }
  176. public void cleanUp()
  177. {
  178. _items.clear();
  179. }
  180. public void emptyTable()
  181. {
  182. Connection conn = null;
  183. try
  184. {
  185. conn = L2DatabaseFactory.getInstance().getConnection();
  186. PreparedStatement del = conn.prepareStatement("delete from itemsonground");
  187. del.execute();
  188. del.close();
  189. }
  190. catch (Exception e1)
  191. {
  192. _log.log(Level.SEVERE, "error while cleaning table ItemsOnGround " + e1);
  193. e1.printStackTrace();
  194. }
  195. finally
  196. {
  197. try
  198. {
  199. conn.close();
  200. }
  201. catch (Exception e1)
  202. {
  203. }
  204. }
  205. }
  206. protected class StoreInDb extends Thread
  207. {
  208. @Override
  209. public synchronized void run()
  210. {
  211. if (!Config.SAVE_DROPPED_ITEM)
  212. return;
  213. emptyTable();
  214. if (_items.isEmpty())
  215. {
  216. if (Config.DEBUG)
  217. _log.warning("ItemsOnGroundManager: nothing to save...");
  218. return;
  219. }
  220. Connection con = null;
  221. PreparedStatement statement = null;
  222. try
  223. {
  224. con = L2DatabaseFactory.getInstance().getConnection();
  225. for (L2ItemInstance item : _items)
  226. {
  227. if (item == null)
  228. continue;
  229. if (CursedWeaponsManager.getInstance().isCursed(item.getItemId()))
  230. continue; // Cursed Items not saved to ground, prevent double save
  231. try
  232. {
  233. statement = con.prepareStatement("insert into itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) values(?,?,?,?,?,?,?,?,?)");
  234. statement.setInt(1, item.getObjectId());
  235. statement.setInt(2, item.getItemId());
  236. statement.setLong(3, item.getCount());
  237. statement.setInt(4, item.getEnchantLevel());
  238. statement.setInt(5, item.getX());
  239. statement.setInt(6, item.getY());
  240. statement.setInt(7, item.getZ());
  241. if (item.isProtected())
  242. statement.setLong(8, -1); //item will be protected
  243. else
  244. statement.setLong(8, item.getDropTime()); //item will be added to ItemsAutoDestroy
  245. if (item.isEquipable())
  246. statement.setLong(9, 1); //set equip-able
  247. else
  248. statement.setLong(9, 0);
  249. statement.execute();
  250. statement.close();
  251. }
  252. catch (Exception e)
  253. {
  254. _log.log(Level.SEVERE, "error while inserting into table ItemsOnGround " + e);
  255. e.printStackTrace();
  256. }
  257. }
  258. }
  259. catch (SQLException e)
  260. {
  261. }
  262. finally
  263. {
  264. try
  265. {
  266. con.close();
  267. }
  268. catch (Exception e)
  269. {
  270. }
  271. }
  272. if (Config.DEBUG)
  273. _log.warning("ItemsOnGroundManager: " + _items.size() + " items on ground saved");
  274. }
  275. }
  276. @SuppressWarnings("synthetic-access")
  277. private static class SingletonHolder
  278. {
  279. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  280. }
  281. }