ItemsOnGroundManager.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.ItemsAutoDestroy;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  30. import com.l2jserver.util.L2FastList;
  31. /**
  32. * This class manage all items on ground.
  33. * @author DiezelMax, original idea.
  34. * @author Enforcer, actual build.
  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. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  63. {
  64. String str = null;
  65. if (!Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  66. {
  67. // Recycle misc. items only
  68. str = "update itemsonground set drop_time=? where drop_time=-1 and equipable=0";
  69. }
  70. else if (Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  71. {
  72. // Recycle all items including equip-able
  73. str = "update itemsonground set drop_time=? where drop_time=-1";
  74. }
  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, getClass().getSimpleName() + ": Error while updating table ItemsOnGround " + e.getMessage(), e);
  83. }
  84. }
  85. // Add items to world
  86. L2ItemInstance item;
  87. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  88. {
  89. PreparedStatement statement = con.prepareStatement("SELECT object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable FROM itemsonground");
  90. ResultSet rset;
  91. int count = 0;
  92. rset = statement.executeQuery();
  93. while (rset.next())
  94. {
  95. item = new L2ItemInstance(rset.getInt(1), rset.getInt(2));
  96. L2World.getInstance().storeObject(item);
  97. // this check and..
  98. if (item.isStackable() && (rset.getInt(3) > 1))
  99. {
  100. item.setCount(rset.getInt(3));
  101. }
  102. // this, are really necessary?
  103. if (rset.getInt(4) > 0)
  104. {
  105. item.setEnchantLevel(rset.getInt(4));
  106. }
  107. item.getPosition().setWorldPosition(rset.getInt(5), rset.getInt(6), rset.getInt(7));
  108. item.getPosition().setWorldRegion(L2World.getInstance().getRegion(item.getPosition().getWorldPosition()));
  109. item.getPosition().getWorldRegion().addVisibleObject(item);
  110. final long dropTime = rset.getLong(8);
  111. item.setDropTime(dropTime);
  112. item.setProtected(dropTime == -1);
  113. item.setIsVisible(true);
  114. L2World.getInstance().addVisibleObject(item, item.getPosition().getWorldRegion());
  115. _items.add(item);
  116. count++;
  117. // add to ItemsAutoDestroy only items not protected
  118. if (!Config.LIST_PROTECTED_ITEMS.contains(item.getItemId()))
  119. {
  120. if (dropTime > -1)
  121. {
  122. if (((Config.AUTODESTROY_ITEM_AFTER > 0) && (item.getItemType() != L2EtcItemType.HERB)) || ((Config.HERB_AUTO_DESTROY_TIME > 0) && (item.getItemType() == L2EtcItemType.HERB)))
  123. {
  124. ItemsAutoDestroy.getInstance().addItem(item);
  125. }
  126. }
  127. }
  128. }
  129. rset.close();
  130. statement.close();
  131. _log.info(getClass().getSimpleName() + ": Loaded " + count + " items.");
  132. }
  133. catch (Exception e)
  134. {
  135. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while loading ItemsOnGround " + e.getMessage(), e);
  136. }
  137. if (Config.EMPTY_DROPPED_ITEM_TABLE_AFTER_LOAD)
  138. {
  139. emptyTable();
  140. }
  141. }
  142. public void save(L2ItemInstance item)
  143. {
  144. if (!Config.SAVE_DROPPED_ITEM)
  145. {
  146. return;
  147. }
  148. _items.add(item);
  149. }
  150. public void removeObject(L2ItemInstance item)
  151. {
  152. if (Config.SAVE_DROPPED_ITEM)
  153. {
  154. _items.remove(item);
  155. }
  156. }
  157. public void saveInDb()
  158. {
  159. run();
  160. }
  161. public void cleanUp()
  162. {
  163. _items.clear();
  164. }
  165. public void emptyTable()
  166. {
  167. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  168. {
  169. PreparedStatement statement = con.prepareStatement("DELETE FROM itemsonground");
  170. statement.execute();
  171. statement.close();
  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 = L2DatabaseFactory.getInstance().getConnection())
  191. {
  192. PreparedStatement statement = con.prepareStatement("INSERT INTO itemsonground(object_id,item_id,count,enchant_level,x,y,z,drop_time,equipable) VALUES(?,?,?,?,?,?,?,?,?)");
  193. for (L2ItemInstance item : _items)
  194. {
  195. if (item == null)
  196. {
  197. continue;
  198. }
  199. if (CursedWeaponsManager.getInstance().isCursed(item.getItemId()))
  200. {
  201. continue; // Cursed Items not saved to ground, prevent double save
  202. }
  203. try
  204. {
  205. statement.setInt(1, item.getObjectId());
  206. statement.setInt(2, item.getItemId());
  207. statement.setLong(3, item.getCount());
  208. statement.setInt(4, item.getEnchantLevel());
  209. statement.setInt(5, item.getX());
  210. statement.setInt(6, item.getY());
  211. statement.setInt(7, item.getZ());
  212. statement.setLong(8, (item.isProtected() ? -1 : item.getDropTime())); // item is protected or AutoDestroyed
  213. statement.setLong(9, (item.isEquipable() ? 1 : 0)); // set equip-able
  214. statement.execute();
  215. statement.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. statement.close();
  223. }
  224. catch (SQLException e)
  225. {
  226. _log.log(Level.SEVERE, getClass().getSimpleName() + ": SQL error while storing items on ground: " + e.getMessage(), e);
  227. }
  228. }
  229. public static final ItemsOnGroundManager getInstance()
  230. {
  231. return SingletonHolder._instance;
  232. }
  233. private static class SingletonHolder
  234. {
  235. protected static final ItemsOnGroundManager _instance = new ItemsOnGroundManager();
  236. }
  237. }