2
0

ItemsOnGroundManager.java 8.4 KB

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