ItemsOnGroundManager.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 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.L2World;
  29. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  30. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  31. import com.l2jserver.util.L2FastList;
  32. /**
  33. * This class manage all items on ground.
  34. * @author Enforcer
  35. */
  36. public class ItemsOnGroundManager implements Runnable
  37. {
  38. private static final Logger _log = Logger.getLogger(ItemsOnGroundManager.class.getName());
  39. protected List<L2ItemInstance> _items = new L2FastList<>(true);
  40. protected ItemsOnGroundManager()
  41. {
  42. if (Config.SAVE_DROPPED_ITEM_INTERVAL > 0)
  43. {
  44. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(this, Config.SAVE_DROPPED_ITEM_INTERVAL, Config.SAVE_DROPPED_ITEM_INTERVAL);
  45. }
  46. load();
  47. }
  48. private void load()
  49. {
  50. // If SaveDroppedItem is false, may want to delete all items previously stored to avoid add old items on reactivate
  51. if (!Config.SAVE_DROPPED_ITEM && Config.CLEAR_DROPPED_ITEM_TABLE)
  52. {
  53. emptyTable();
  54. }
  55. if (!Config.SAVE_DROPPED_ITEM)
  56. {
  57. return;
  58. }
  59. // if DestroyPlayerDroppedItem was previously false, items currently protected will be added to ItemsAutoDestroy
  60. if (Config.DESTROY_DROPPED_PLAYER_ITEM)
  61. {
  62. String str = null;
  63. if (!Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  64. {
  65. // Recycle misc. items only
  66. str = "UPDATE itemsonground SET drop_time = ? WHERE drop_time = -1 AND equipable = 0";
  67. }
  68. else if (Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  69. {
  70. // Recycle all items including equip-able
  71. str = "UPDATE itemsonground SET drop_time = ? WHERE drop_time = -1";
  72. }
  73. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  74. PreparedStatement ps = con.prepareStatement(str))
  75. {
  76. ps.setLong(1, System.currentTimeMillis());
  77. ps.execute();
  78. }
  79. catch (Exception e)
  80. {
  81. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while updating table ItemsOnGround " + e.getMessage(), e);
  82. }
  83. }
  84. // Add items to world
  85. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  86. PreparedStatement ps = con.prepareStatement("SELECT object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable FROM itemsonground"))
  87. {
  88. int count = 0;
  89. try (ResultSet rs = ps.executeQuery())
  90. {
  91. L2ItemInstance item;
  92. while (rs.next())
  93. {
  94. item = new L2ItemInstance(rs.getInt(1), rs.getInt(2));
  95. L2World.getInstance().storeObject(item);
  96. // this check and..
  97. if (item.isStackable() && (rs.getInt(3) > 1))
  98. {
  99. item.setCount(rs.getInt(3));
  100. }
  101. // this, are really necessary?
  102. if (rs.getInt(4) > 0)
  103. {
  104. item.setEnchantLevel(rs.getInt(4));
  105. }
  106. item.getPosition().setWorldPosition(rs.getInt(5), rs.getInt(6), rs.getInt(7));
  107. item.getPosition().setWorldRegion(L2World.getInstance().getRegion(item.getPosition().getWorldPosition()));
  108. item.getPosition().getWorldRegion().addVisibleObject(item);
  109. final long dropTime = rs.getLong(8);
  110. item.setDropTime(dropTime);
  111. item.setProtected(dropTime == -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 (dropTime > -1)
  120. {
  121. if (((Config.AUTODESTROY_ITEM_AFTER > 0) && (item.getItemType() != L2EtcItemType.HERB)) || ((Config.HERB_AUTO_DESTROY_TIME > 0) && (item.getItemType() == L2EtcItemType.HERB)))
  122. {
  123. ItemsAutoDestroy.getInstance().addItem(item);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. _log.info(getClass().getSimpleName() + ": Loaded " + count + " items.");
  130. }
  131. catch (Exception e)
  132. {
  133. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while loading ItemsOnGround " + e.getMessage(), e);
  134. }
  135. if (Config.EMPTY_DROPPED_ITEM_TABLE_AFTER_LOAD)
  136. {
  137. emptyTable();
  138. }
  139. }
  140. public void save(L2ItemInstance item)
  141. {
  142. if (!Config.SAVE_DROPPED_ITEM)
  143. {
  144. return;
  145. }
  146. _items.add(item);
  147. }
  148. public void removeObject(L2ItemInstance item)
  149. {
  150. if (Config.SAVE_DROPPED_ITEM)
  151. {
  152. _items.remove(item);
  153. }
  154. }
  155. public void saveInDb()
  156. {
  157. run();
  158. }
  159. public void cleanUp()
  160. {
  161. _items.clear();
  162. }
  163. public void emptyTable()
  164. {
  165. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  166. Statement s = con.createStatement())
  167. {
  168. s.executeQuery("DELETE FROM itemsonground");
  169. }
  170. catch (Exception e1)
  171. {
  172. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while cleaning table ItemsOnGround " + e1.getMessage(), e1);
  173. }
  174. }
  175. @Override
  176. public synchronized void run()
  177. {
  178. if (!Config.SAVE_DROPPED_ITEM)
  179. {
  180. return;
  181. }
  182. emptyTable();
  183. if (_items.isEmpty())
  184. {
  185. return;
  186. }
  187. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  188. PreparedStatement statement = con.prepareStatement("INSERT INTO itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) VALUES(?,?,?,?,?,?,?,?,?)"))
  189. {
  190. for (L2ItemInstance item : _items)
  191. {
  192. if (item == null)
  193. {
  194. continue;
  195. }
  196. if (CursedWeaponsManager.getInstance().isCursed(item.getItemId()))
  197. {
  198. continue; // Cursed Items not saved to ground, prevent double save
  199. }
  200. try
  201. {
  202. statement.setInt(1, item.getObjectId());
  203. statement.setInt(2, item.getItemId());
  204. statement.setLong(3, item.getCount());
  205. statement.setInt(4, item.getEnchantLevel());
  206. statement.setInt(5, item.getX());
  207. statement.setInt(6, item.getY());
  208. statement.setInt(7, item.getZ());
  209. statement.setLong(8, (item.isProtected() ? -1 : item.getDropTime())); // item is protected or AutoDestroyed
  210. statement.setLong(9, (item.isEquipable() ? 1 : 0)); // set equip-able
  211. statement.execute();
  212. statement.clearParameters();
  213. }
  214. catch (Exception e)
  215. {
  216. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while inserting into table ItemsOnGround: " + e.getMessage(), e);
  217. }
  218. }
  219. }
  220. catch (SQLException e)
  221. {
  222. _log.log(Level.SEVERE, getClass().getSimpleName() + ": SQL error while storing items on ground: " + e.getMessage(), e);
  223. }
  224. }
  225. public static final ItemsOnGroundManager getInstance()
  226. {
  227. return SingletonHolder._instance;
  228. }
  229. private static class SingletonHolder
  230. {
  231. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  232. }
  233. }