ItemsOnGroundManager.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 net.sf.l2j.gameserver.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.Statement;
  20. import java.util.ArrayList;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import net.sf.l2j.Config;
  24. import net.sf.l2j.L2DatabaseFactory;
  25. import net.sf.l2j.gameserver.ItemsAutoDestroy;
  26. import net.sf.l2j.gameserver.ThreadPoolManager;
  27. import net.sf.l2j.gameserver.model.L2ItemInstance;
  28. import net.sf.l2j.gameserver.model.L2Object;
  29. import net.sf.l2j.gameserver.model.L2World;
  30. import net.sf.l2j.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 ArrayList<L2ItemInstance> _items = null;
  42. private ItemsOnGroundManager()
  43. {
  44. if (Config.SAVE_DROPPED_ITEM)
  45. _items = new ArrayList<L2ItemInstance>();
  46. if (Config.SAVE_DROPPED_ITEM_INTERVAL > 0)
  47. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new storeInDb(), Config.SAVE_DROPPED_ITEM_INTERVAL, Config.SAVE_DROPPED_ITEM_INTERVAL);
  48. load();
  49. }
  50. public static final ItemsOnGroundManager getInstance()
  51. {
  52. return SingletonHolder._instance;
  53. }
  54. private void load()
  55. {
  56. // If SaveDroppedItem is false, may want to delete all items previously stored to avoid add old items on reactivate
  57. if (!Config.SAVE_DROPPED_ITEM && Config.CLEAR_DROPPED_ITEM_TABLE)
  58. emptyTable();
  59. if (!Config.SAVE_DROPPED_ITEM)
  60. return;
  61. // if DestroyPlayerDroppedItem was previously false, items curently protected will be added to ItemsAutoDestroy
  62. if (Config.DESTROY_DROPPED_PLAYER_ITEM)
  63. {
  64. Connection con = null;
  65. try
  66. {
  67. String str = null;
  68. if (!Config.DESTROY_EQUIPABLE_PLAYER_ITEM) //Recycle misc. items only
  69. str = "update itemsonground set drop_time=? where drop_time=-1 and equipable=0";
  70. else if (Config.DESTROY_EQUIPABLE_PLAYER_ITEM) //Recycle all items including equip-able
  71. str = "update itemsonground set drop_time=? where drop_time=-1";
  72. con = L2DatabaseFactory.getInstance().getConnection();
  73. PreparedStatement statement = con.prepareStatement(str);
  74. statement.setLong(1, System.currentTimeMillis());
  75. statement.execute();
  76. statement.close();
  77. }
  78. catch (Exception e)
  79. {
  80. _log.log(Level.SEVERE, "error while updating table ItemsOnGround " + e);
  81. e.printStackTrace();
  82. }
  83. finally
  84. {
  85. try
  86. {
  87. con.close();
  88. }
  89. catch (Exception e)
  90. {
  91. }
  92. }
  93. }
  94. //Add items to world
  95. Connection con = null;
  96. try
  97. {
  98. con = L2DatabaseFactory.getInstance().getConnection();
  99. Statement s = con.createStatement();
  100. ResultSet result;
  101. int count = 0;
  102. result = s.executeQuery("select object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable from itemsonground");
  103. while (result.next())
  104. {
  105. L2ItemInstance item = new L2ItemInstance(result.getInt(1), result.getInt(2));
  106. L2World.getInstance().storeObject(item);
  107. if (item.isStackable() && result.getInt(3) > 1) //this check and..
  108. item.setCount(result.getInt(3));
  109. if (result.getInt(4) > 0) // this, are really necessary?
  110. item.setEnchantLevel(result.getInt(4));
  111. item.getPosition().setWorldPosition(result.getInt(5), result.getInt(6), result.getInt(7));
  112. item.getPosition().setWorldRegion(L2World.getInstance().getRegion(item.getPosition().getWorldPosition()));
  113. item.getPosition().getWorldRegion().addVisibleObject(item);
  114. item.setDropTime(result.getLong(8));
  115. if (result.getLong(8) == -1)
  116. item.setProtected(true);
  117. else
  118. item.setProtected(false);
  119. item.setIsVisible(true);
  120. L2World.getInstance().addVisibleObject(item, item.getPosition().getWorldRegion(), null);
  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. new storeInDb().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 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. for (L2ItemInstance item : _items)
  221. {
  222. if (item == null)
  223. continue;
  224. if (CursedWeaponsManager.getInstance().isCursed(item.getItemId()))
  225. continue; // Cursed Items not saved to ground, prevent double save
  226. Connection con = null;
  227. try
  228. {
  229. con = L2DatabaseFactory.getInstance().getConnection();
  230. PreparedStatement statement = con.prepareStatement("insert into itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) values(?,?,?,?,?,?,?,?,?)");
  231. statement.setInt(1, item.getObjectId());
  232. statement.setInt(2, item.getItemId());
  233. statement.setLong(3, item.getCount());
  234. statement.setInt(4, item.getEnchantLevel());
  235. statement.setInt(5, item.getX());
  236. statement.setInt(6, item.getY());
  237. statement.setInt(7, item.getZ());
  238. if (item.isProtected())
  239. statement.setLong(8, -1); //item will be protected
  240. else
  241. statement.setLong(8, item.getDropTime()); //item will be added to ItemsAutoDestroy
  242. if (item.isEquipable())
  243. statement.setLong(9, 1); //set equip-able
  244. else
  245. statement.setLong(9, 0);
  246. statement.execute();
  247. statement.close();
  248. }
  249. catch (Exception e)
  250. {
  251. _log.log(Level.SEVERE, "error while inserting into table ItemsOnGround " + e);
  252. e.printStackTrace();
  253. }
  254. finally
  255. {
  256. try
  257. {
  258. con.close();
  259. }
  260. catch (Exception e)
  261. {
  262. }
  263. }
  264. }
  265. if (Config.DEBUG)
  266. _log.warning("ItemsOnGroundManager: " + _items.size() + " items on ground saved");
  267. }
  268. }
  269. @SuppressWarnings("synthetic-access")
  270. private static class SingletonHolder
  271. {
  272. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  273. }
  274. }