-JRGames-

Time Itens jFrozen

Oct 30th, 2024
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.47 KB | None | 0 0
  1. ### Eclipse Workspace Patch 1.0
  2. #P L2jFrozen_GameServer
  3. Index: head-src/com/l2jfrozen/gameserver/managers/TimedItemManager.java
  4. ===================================================================
  5. --- head-src/com/l2jfrozen/gameserver/managers/TimedItemManager.java (revision 0)
  6. +++ head-src/com/l2jfrozen/gameserver/managers/TimedItemManager.java (working copy)
  7. @@ -0,0 +1,332 @@
  8. +package com.l2jfrozen.gameserver.managers;
  9. +
  10. +import java.sql.Connection;
  11. +import java.sql.PreparedStatement;
  12. +import java.sql.ResultSet;
  13. +import java.sql.SQLException;
  14. +import java.util.logging.Logger;
  15. +
  16. +import javolution.util.FastMap;
  17. +
  18. +import com.l2jfrozen.Config;
  19. +import com.l2jfrozen.gameserver.model.L2Object;
  20. +import com.l2jfrozen.gameserver.model.L2World;
  21. +import com.l2jfrozen.gameserver.model.actor.instance.L2ItemInstance;
  22. +import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  23. +import com.l2jfrozen.gameserver.network.SystemMessageId;
  24. +import com.l2jfrozen.gameserver.network.serverpackets.ItemList;
  25. +import com.l2jfrozen.gameserver.network.serverpackets.SystemMessage;
  26. +import com.l2jfrozen.gameserver.taskmanager.ExclusiveTask;
  27. +import com.l2jfrozen.util.database.L2DatabaseFactory;
  28. +
  29. +public class TimedItemManager
  30. +{
  31. + public final FastMap<Integer, Info> _timedItems = new FastMap<Integer, Info>();
  32. + private static Logger _log = Logger.getLogger(TimedItemManager.class.getName());
  33. + private static Connection con;
  34. +
  35. + public class Info
  36. + {
  37. + int _charId;
  38. + int _itemId;
  39. + long _activationTime;
  40. + }
  41. +
  42. + public static final TimedItemManager getInstance()
  43. + {
  44. + return SingletonHolder._instance;
  45. + }
  46. +
  47. + private static class SingletonHolder
  48. + {
  49. + protected static final TimedItemManager _instance = new TimedItemManager();
  50. + }
  51. +
  52. + public TimedItemManager()
  53. + {
  54. + restore();
  55. + _startControlTask.schedule(60000);
  56. + }
  57. +
  58. + public boolean getActiveTimed(L2PcInstance pl, boolean trade)
  59. + {
  60. + for (Info i : _timedItems.values())
  61. + {
  62. + if ((i != null) && (i._charId == pl.getObjectId()))
  63. + {
  64. + L2ItemInstance item = pl.getInventory().getItemByObjectId(i._itemId);
  65. + if (item != null)
  66. + {
  67. + if (System.currentTimeMillis() < i._activationTime)
  68. + {
  69. + return true;
  70. + }
  71. + }
  72. + }
  73. + }
  74. + return false;
  75. + }
  76. +
  77. + public synchronized void destroy(L2ItemInstance item)
  78. + {
  79. + Info inf = _timedItems.get(item.getObjectId());
  80. + if (inf != null)
  81. + {
  82. + _timedItems.remove(inf._itemId);
  83. + con = null;
  84. + try
  85. + {
  86. + con = L2DatabaseFactory.getInstance().getConnection();
  87. + PreparedStatement statement;
  88. + statement = con.prepareStatement("DELETE FROM character_timed_items WHERE charId = ? AND itemId = ?");
  89. + statement.setInt(1, inf._charId);
  90. + statement.setInt(2, inf._itemId);
  91. + statement.execute();
  92. + statement.close();
  93. + }
  94. + catch (Exception e)
  95. + {
  96. + e.printStackTrace();
  97. + }
  98. + finally
  99. + {
  100. + try
  101. + {
  102. + con.close();
  103. + }
  104. + catch (Exception e)
  105. + {
  106. +
  107. + }
  108. + }
  109. + }
  110. + }
  111. +
  112. + public synchronized void setTimed(L2ItemInstance item)
  113. + {
  114. + Info inf = _timedItems.get(item.getObjectId());
  115. + if (inf != null)
  116. + {
  117. + inf._charId = item.getOwnerId();
  118. + }
  119. + else
  120. + {
  121. + inf = new Info();
  122. + inf._activationTime = (System.currentTimeMillis() / 1000) + (Config.TIMED_ITEM_TIME * 60);
  123. + inf._charId = item.getOwnerId();
  124. + inf._itemId = item.getObjectId();
  125. + _timedItems.put(inf._itemId, inf);
  126. + }
  127. + saveToDb(inf);
  128. + }
  129. +
  130. + public boolean isActive(L2ItemInstance item)
  131. + {
  132. + for (Info i : _timedItems.values())
  133. + {
  134. + if (i._itemId == item.getObjectId())
  135. + {
  136. + return true;
  137. + }
  138. + }
  139. + return false;
  140. + }
  141. +
  142. + private void restore()
  143. + {
  144. + try
  145. + {
  146. + con = L2DatabaseFactory.getInstance().getConnection();
  147. + PreparedStatement statement = con.prepareStatement("SELECT charId, itemId, time FROM character_timed_items");
  148. + ResultSet rs = statement.executeQuery();
  149. +
  150. + while (rs.next())
  151. + {
  152. + Info inf = new Info();
  153. + inf._activationTime = rs.getLong("time");
  154. + inf._charId = rs.getInt("charId");
  155. + inf._itemId = rs.getInt("itemId");
  156. + _timedItems.put(inf._itemId, inf);
  157. + }
  158. + rs.close();
  159. + statement.close();
  160. + _log.info("TimedItems: loaded " + _timedItems.size() + " items ");
  161. + }
  162. + catch (Exception e)
  163. + {
  164. + e.printStackTrace();
  165. + }
  166. + finally
  167. + {
  168. + try
  169. + {
  170. + if (con != null)
  171. + {
  172. + con.close();
  173. + }
  174. + }
  175. + catch (SQLException e)
  176. + {
  177. + e.printStackTrace();
  178. + }
  179. + }
  180. + }
  181. +
  182. + private static void saveToDb(Info temp)
  183. + {
  184. + try
  185. + {
  186. + con = L2DatabaseFactory.getInstance().getConnection();
  187. + PreparedStatement statement;
  188. + statement = con.prepareStatement("update character_timed_items set charId = ? where itemId = ?");
  189. + statement.setInt(1, temp._charId);
  190. + statement.setInt(2, temp._itemId);
  191. + if (statement.executeUpdate() == 0)
  192. + {
  193. + statement.close();
  194. + statement = con.prepareStatement("INSERT INTO character_timed_items (charId, itemId, time) VALUES (?, ?, ?)");
  195. + statement.setInt(1, temp._charId);
  196. + statement.setInt(2, temp._itemId);
  197. + statement.setLong(3, temp._activationTime);
  198. + statement.execute();
  199. + }
  200. + statement.close();
  201. + }
  202. + catch (Exception e)
  203. + {
  204. + e.printStackTrace();
  205. + }
  206. + finally
  207. + {
  208. + try
  209. + {
  210. + if (con != null)
  211. + {
  212. + con.close();
  213. + }
  214. + }
  215. + catch (SQLException e)
  216. + {
  217. + e.printStackTrace();
  218. + }
  219. + }
  220. + }
  221. +
  222. + public void delete(Info temp)
  223. + {
  224. + _timedItems.remove(temp._itemId);
  225. + try
  226. + {
  227. + con = L2DatabaseFactory.getInstance().getConnection();
  228. + PreparedStatement statement;
  229. + statement = con.prepareStatement("DELETE FROM character_timed_items WHERE charId = ? AND itemId = ?");
  230. + statement.setInt(1, temp._charId);
  231. + statement.setInt(2, temp._itemId);
  232. + statement.execute();
  233. + statement.close();
  234. + }
  235. + catch (Exception e)
  236. + {
  237. + e.printStackTrace();
  238. + }
  239. + finally
  240. + {
  241. + try
  242. + {
  243. + if (con != null)
  244. + {
  245. + con.close();
  246. + }
  247. + }
  248. + catch (SQLException e)
  249. + {
  250. + e.printStackTrace();
  251. + }
  252. + }
  253. + L2PcInstance pl = L2World.getInstance().getPlayer(temp._charId);
  254. + if (pl != null)
  255. + {
  256. + item = pl.getInventory().getItemByObjectId(temp._itemId);
  257. + int itemId = item.getItemId();
  258. + if (item != null)
  259. + {
  260. + if (item.isEquipped())
  261. + {
  262. + pl.getInventory().unEquipItemInSlot(item.getLocationSlot());
  263. + }
  264. + pl.getInventory().destroyItem("timeLost", item, pl, pl);
  265. + pl.sendPacket(new ItemList(pl, false));
  266. + }
  267. +
  268. + SystemMessage msg = new SystemMessage(SystemMessageId.S1_DISAPPEARED);
  269. + msg.addItemName(itemId);
  270. + pl.sendPacket(msg);
  271. +
  272. + }
  273. + else
  274. + {
  275. + con = null;
  276. + try
  277. + {
  278. + con = L2DatabaseFactory.getInstance().getConnection();
  279. + PreparedStatement statement;
  280. + if (temp._charId != 0)
  281. + {
  282. + statement = con.prepareStatement("DELETE FROM items WHERE owner_id = ? AND object_id = ?");
  283. + statement.setInt(1, temp._charId);
  284. + statement.setInt(2, temp._itemId);
  285. + statement.execute();
  286. + statement.close();
  287. + }
  288. + else
  289. + {
  290. + for (L2Object o : L2World.getInstance().getAllVisibleObjects())
  291. + {
  292. + if (o.getObjectId() == temp._itemId)
  293. + {
  294. + L2World.getInstance().removeVisibleObject(o, o.getWorldRegion());
  295. + break;
  296. + }
  297. + }
  298. + }
  299. + }
  300. + catch (Exception e)
  301. + {
  302. + e.printStackTrace();
  303. + }
  304. + finally
  305. + {
  306. + try
  307. + {
  308. + if (con != null)
  309. + {
  310. + con.close();
  311. + }
  312. + }
  313. + catch (SQLException e)
  314. + {
  315. + e.printStackTrace();
  316. + }
  317. + }
  318. + }
  319. + }
  320. +
  321. + private final ExclusiveTask _startControlTask = new ExclusiveTask()
  322. + {
  323. + @Override
  324. + protected void onElapsed()
  325. + {
  326. + for (Info temp : _timedItems.values())
  327. + {
  328. + if (temp._activationTime < (System.currentTimeMillis() / 1000))
  329. + {
  330. +
  331. + delete(temp);
  332. + }
  333. + }
  334. + schedule(60000);
  335. + }
  336. + };
  337. + private L2ItemInstance item;
  338. +
  339. +}
  340. \ No newline at end of file
  341.  
  342. #=======================================#
  343. # Timed item
  344. #=======================================#
  345. # Timed Item ID use , for separate (57,3441,5588...)
  346. ListOfTimedItems = 57,3441,5588
  347.  
  348. # Time for item disappear in Hour's
  349. TimedItemTime = 2
  350.  
  351.  
  352. ### Eclipse Workspace Patch 1.0
  353. #P Dream_DataPack
  354. Index: sql/server/character_timed_items.sql
  355. ===================================================================
  356. --- sql/server/character_timed_items.sql (revision 1754)
  357. +++ sql/server/character_timed_items.sql (working copy)
  358. @@ -1,6 +0,0 @@
  359. +DROP TABLE IF EXISTS `character_timed_items`;
  360. +CREATE TABLE `character_timed_items` (
  361. + `charId` int(11) NOT NULL,
  362. + `itemId` int(11) NOT NULL,
  363. + `time` decimal(20,0) NOT NULL DEFAULT '0'
  364. +) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  365. \ No newline at end of file
  366.  
  367.  
Add Comment
Please, Sign In to add comment