ItemsOnGroundManager.java 7.9 KB

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