ItemsOnGroundManager.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.List;
  26. import java.util.concurrent.CopyOnWriteArrayList;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  31. import com.l2jserver.gameserver.ItemsAutoDestroy;
  32. import com.l2jserver.gameserver.ThreadPoolManager;
  33. import com.l2jserver.gameserver.model.L2World;
  34. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  35. /**
  36. * This class manage all items on ground.
  37. * @author Enforcer
  38. */
  39. public final class ItemsOnGroundManager implements Runnable
  40. {
  41. private static final Logger _log = Logger.getLogger(ItemsOnGroundManager.class.getName());
  42. private final List<L2ItemInstance> _items = new CopyOnWriteArrayList<>();
  43. protected ItemsOnGroundManager()
  44. {
  45. if (Config.SAVE_DROPPED_ITEM_INTERVAL > 0)
  46. {
  47. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(this, Config.SAVE_DROPPED_ITEM_INTERVAL, Config.SAVE_DROPPED_ITEM_INTERVAL);
  48. }
  49. load();
  50. }
  51. private void load()
  52. {
  53. // If SaveDroppedItem is false, may want to delete all items previously stored to avoid add old items on reactivate
  54. if (!Config.SAVE_DROPPED_ITEM && Config.CLEAR_DROPPED_ITEM_TABLE)
  55. {
  56. emptyTable();
  57. }
  58. if (!Config.SAVE_DROPPED_ITEM)
  59. {
  60. return;
  61. }
  62. // if DestroyPlayerDroppedItem was previously false, items currently protected will be added to ItemsAutoDestroy
  63. if (Config.DESTROY_DROPPED_PLAYER_ITEM)
  64. {
  65. String str = null;
  66. if (!Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  67. {
  68. // Recycle misc. items only
  69. str = "UPDATE itemsonground SET drop_time = ? WHERE drop_time = -1 AND equipable = 0";
  70. }
  71. else if (Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  72. {
  73. // Recycle all items including equip-able
  74. str = "UPDATE itemsonground SET drop_time = ? WHERE drop_time = -1";
  75. }
  76. try (Connection con = ConnectionFactory.getInstance().getConnection();
  77. PreparedStatement ps = con.prepareStatement(str))
  78. {
  79. ps.setLong(1, System.currentTimeMillis());
  80. ps.execute();
  81. }
  82. catch (Exception e)
  83. {
  84. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while updating table ItemsOnGround " + e.getMessage(), e);
  85. }
  86. }
  87. // Add items to world
  88. try (Connection con = ConnectionFactory.getInstance().getConnection();
  89. PreparedStatement ps = con.prepareStatement("SELECT object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable FROM itemsonground"))
  90. {
  91. int count = 0;
  92. try (ResultSet rs = ps.executeQuery())
  93. {
  94. L2ItemInstance item;
  95. while (rs.next())
  96. {
  97. item = new L2ItemInstance(rs.getInt(1), rs.getInt(2));
  98. L2World.getInstance().storeObject(item);
  99. // this check and..
  100. if (item.isStackable() && (rs.getInt(3) > 1))
  101. {
  102. item.setCount(rs.getInt(3));
  103. }
  104. // this, are really necessary?
  105. if (rs.getInt(4) > 0)
  106. {
  107. item.setEnchantLevel(rs.getInt(4));
  108. }
  109. item.setXYZ(rs.getInt(5), rs.getInt(6), rs.getInt(7));
  110. item.setWorldRegion(L2World.getInstance().getRegion(item.getLocation()));
  111. item.getWorldRegion().addVisibleObject(item);
  112. final long dropTime = rs.getLong(8);
  113. item.setDropTime(dropTime);
  114. item.setProtected(dropTime == -1);
  115. item.setIsVisible(true);
  116. L2World.getInstance().addVisibleObject(item, item.getWorldRegion());
  117. _items.add(item);
  118. count++;
  119. // add to ItemsAutoDestroy only items not protected
  120. if (!Config.LIST_PROTECTED_ITEMS.contains(item.getId()))
  121. {
  122. if (dropTime > -1)
  123. {
  124. if (((Config.AUTODESTROY_ITEM_AFTER > 0) && !item.getItem().hasExImmediateEffect()) || ((Config.HERB_AUTO_DESTROY_TIME > 0) && item.getItem().hasExImmediateEffect()))
  125. {
  126. ItemsAutoDestroy.getInstance().addItem(item);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. _log.info(getClass().getSimpleName() + ": Loaded " + count + " items.");
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while loading ItemsOnGround " + e.getMessage(), e);
  137. }
  138. if (Config.EMPTY_DROPPED_ITEM_TABLE_AFTER_LOAD)
  139. {
  140. emptyTable();
  141. }
  142. }
  143. public void save(L2ItemInstance item)
  144. {
  145. if (!Config.SAVE_DROPPED_ITEM)
  146. {
  147. return;
  148. }
  149. _items.add(item);
  150. }
  151. public void removeObject(L2ItemInstance item)
  152. {
  153. if (Config.SAVE_DROPPED_ITEM)
  154. {
  155. _items.remove(item);
  156. }
  157. }
  158. public void saveInDb()
  159. {
  160. run();
  161. }
  162. public void cleanUp()
  163. {
  164. _items.clear();
  165. }
  166. public void emptyTable()
  167. {
  168. try (Connection con = ConnectionFactory.getInstance().getConnection();
  169. Statement s = con.createStatement())
  170. {
  171. s.executeUpdate("DELETE FROM itemsonground");
  172. }
  173. catch (Exception e1)
  174. {
  175. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while cleaning table ItemsOnGround " + e1.getMessage(), e1);
  176. }
  177. }
  178. @Override
  179. public synchronized void run()
  180. {
  181. if (!Config.SAVE_DROPPED_ITEM)
  182. {
  183. return;
  184. }
  185. emptyTable();
  186. if (_items.isEmpty())
  187. {
  188. return;
  189. }
  190. try (Connection con = ConnectionFactory.getInstance().getConnection();
  191. PreparedStatement ps = con.prepareStatement("INSERT INTO itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) VALUES(?,?,?,?,?,?,?,?,?)"))
  192. {
  193. for (L2ItemInstance item : _items)
  194. {
  195. if (item == null)
  196. {
  197. continue;
  198. }
  199. if (CursedWeaponsManager.getInstance().isCursed(item.getId()))
  200. {
  201. continue; // Cursed Items not saved to ground, prevent double save
  202. }
  203. try
  204. {
  205. ps.setInt(1, item.getObjectId());
  206. ps.setInt(2, item.getId());
  207. ps.setLong(3, item.getCount());
  208. ps.setInt(4, item.getEnchantLevel());
  209. ps.setInt(5, item.getX());
  210. ps.setInt(6, item.getY());
  211. ps.setInt(7, item.getZ());
  212. ps.setLong(8, (item.isProtected() ? -1 : item.getDropTime())); // item is protected or AutoDestroyed
  213. ps.setLong(9, (item.isEquipable() ? 1 : 0)); // set equip-able
  214. ps.execute();
  215. ps.clearParameters();
  216. }
  217. catch (Exception e)
  218. {
  219. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while inserting into table ItemsOnGround: " + e.getMessage(), e);
  220. }
  221. }
  222. }
  223. catch (SQLException e)
  224. {
  225. _log.log(Level.SEVERE, getClass().getSimpleName() + ": SQL error while storing items on ground: " + e.getMessage(), e);
  226. }
  227. }
  228. /**
  229. * Gets the single instance of {@code ItemsOnGroundManager}.
  230. * @return single instance of {@code ItemsOnGroundManager}
  231. */
  232. public static final ItemsOnGroundManager getInstance()
  233. {
  234. return SingletonHolder._instance;
  235. }
  236. private static class SingletonHolder
  237. {
  238. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  239. }
  240. }