Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Eclipse Workspace Patch 1.0
- #P L2jFrozen_GameServer
- Index: head-src/com/l2jfrozen/gameserver/managers/TimedItemManager.java
- ===================================================================
- --- head-src/com/l2jfrozen/gameserver/managers/TimedItemManager.java (revision 0)
- +++ head-src/com/l2jfrozen/gameserver/managers/TimedItemManager.java (working copy)
- @@ -0,0 +1,332 @@
- +package com.l2jfrozen.gameserver.managers;
- +
- +import java.sql.Connection;
- +import java.sql.PreparedStatement;
- +import java.sql.ResultSet;
- +import java.sql.SQLException;
- +import java.util.logging.Logger;
- +
- +import javolution.util.FastMap;
- +
- +import com.l2jfrozen.Config;
- +import com.l2jfrozen.gameserver.model.L2Object;
- +import com.l2jfrozen.gameserver.model.L2World;
- +import com.l2jfrozen.gameserver.model.actor.instance.L2ItemInstance;
- +import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
- +import com.l2jfrozen.gameserver.network.SystemMessageId;
- +import com.l2jfrozen.gameserver.network.serverpackets.ItemList;
- +import com.l2jfrozen.gameserver.network.serverpackets.SystemMessage;
- +import com.l2jfrozen.gameserver.taskmanager.ExclusiveTask;
- +import com.l2jfrozen.util.database.L2DatabaseFactory;
- +
- +public class TimedItemManager
- +{
- + public final FastMap<Integer, Info> _timedItems = new FastMap<Integer, Info>();
- + private static Logger _log = Logger.getLogger(TimedItemManager.class.getName());
- + private static Connection con;
- +
- + public class Info
- + {
- + int _charId;
- + int _itemId;
- + long _activationTime;
- + }
- +
- + public static final TimedItemManager getInstance()
- + {
- + return SingletonHolder._instance;
- + }
- +
- + private static class SingletonHolder
- + {
- + protected static final TimedItemManager _instance = new TimedItemManager();
- + }
- +
- + public TimedItemManager()
- + {
- + restore();
- + _startControlTask.schedule(60000);
- + }
- +
- + public boolean getActiveTimed(L2PcInstance pl, boolean trade)
- + {
- + for (Info i : _timedItems.values())
- + {
- + if ((i != null) && (i._charId == pl.getObjectId()))
- + {
- + L2ItemInstance item = pl.getInventory().getItemByObjectId(i._itemId);
- + if (item != null)
- + {
- + if (System.currentTimeMillis() < i._activationTime)
- + {
- + return true;
- + }
- + }
- + }
- + }
- + return false;
- + }
- +
- + public synchronized void destroy(L2ItemInstance item)
- + {
- + Info inf = _timedItems.get(item.getObjectId());
- + if (inf != null)
- + {
- + _timedItems.remove(inf._itemId);
- + con = null;
- + try
- + {
- + con = L2DatabaseFactory.getInstance().getConnection();
- + PreparedStatement statement;
- + statement = con.prepareStatement("DELETE FROM character_timed_items WHERE charId = ? AND itemId = ?");
- + statement.setInt(1, inf._charId);
- + statement.setInt(2, inf._itemId);
- + statement.execute();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + e.printStackTrace();
- + }
- + finally
- + {
- + try
- + {
- + con.close();
- + }
- + catch (Exception e)
- + {
- +
- + }
- + }
- + }
- + }
- +
- + public synchronized void setTimed(L2ItemInstance item)
- + {
- + Info inf = _timedItems.get(item.getObjectId());
- + if (inf != null)
- + {
- + inf._charId = item.getOwnerId();
- + }
- + else
- + {
- + inf = new Info();
- + inf._activationTime = (System.currentTimeMillis() / 1000) + (Config.TIMED_ITEM_TIME * 60);
- + inf._charId = item.getOwnerId();
- + inf._itemId = item.getObjectId();
- + _timedItems.put(inf._itemId, inf);
- + }
- + saveToDb(inf);
- + }
- +
- + public boolean isActive(L2ItemInstance item)
- + {
- + for (Info i : _timedItems.values())
- + {
- + if (i._itemId == item.getObjectId())
- + {
- + return true;
- + }
- + }
- + return false;
- + }
- +
- + private void restore()
- + {
- + try
- + {
- + con = L2DatabaseFactory.getInstance().getConnection();
- + PreparedStatement statement = con.prepareStatement("SELECT charId, itemId, time FROM character_timed_items");
- + ResultSet rs = statement.executeQuery();
- +
- + while (rs.next())
- + {
- + Info inf = new Info();
- + inf._activationTime = rs.getLong("time");
- + inf._charId = rs.getInt("charId");
- + inf._itemId = rs.getInt("itemId");
- + _timedItems.put(inf._itemId, inf);
- + }
- + rs.close();
- + statement.close();
- + _log.info("TimedItems: loaded " + _timedItems.size() + " items ");
- + }
- + catch (Exception e)
- + {
- + e.printStackTrace();
- + }
- + finally
- + {
- + try
- + {
- + if (con != null)
- + {
- + con.close();
- + }
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- + }
- +
- + private static void saveToDb(Info temp)
- + {
- + try
- + {
- + con = L2DatabaseFactory.getInstance().getConnection();
- + PreparedStatement statement;
- + statement = con.prepareStatement("update character_timed_items set charId = ? where itemId = ?");
- + statement.setInt(1, temp._charId);
- + statement.setInt(2, temp._itemId);
- + if (statement.executeUpdate() == 0)
- + {
- + statement.close();
- + statement = con.prepareStatement("INSERT INTO character_timed_items (charId, itemId, time) VALUES (?, ?, ?)");
- + statement.setInt(1, temp._charId);
- + statement.setInt(2, temp._itemId);
- + statement.setLong(3, temp._activationTime);
- + statement.execute();
- + }
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + e.printStackTrace();
- + }
- + finally
- + {
- + try
- + {
- + if (con != null)
- + {
- + con.close();
- + }
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- + }
- +
- + public void delete(Info temp)
- + {
- + _timedItems.remove(temp._itemId);
- + try
- + {
- + con = L2DatabaseFactory.getInstance().getConnection();
- + PreparedStatement statement;
- + statement = con.prepareStatement("DELETE FROM character_timed_items WHERE charId = ? AND itemId = ?");
- + statement.setInt(1, temp._charId);
- + statement.setInt(2, temp._itemId);
- + statement.execute();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + e.printStackTrace();
- + }
- + finally
- + {
- + try
- + {
- + if (con != null)
- + {
- + con.close();
- + }
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- + L2PcInstance pl = L2World.getInstance().getPlayer(temp._charId);
- + if (pl != null)
- + {
- + item = pl.getInventory().getItemByObjectId(temp._itemId);
- + int itemId = item.getItemId();
- + if (item != null)
- + {
- + if (item.isEquipped())
- + {
- + pl.getInventory().unEquipItemInSlot(item.getLocationSlot());
- + }
- + pl.getInventory().destroyItem("timeLost", item, pl, pl);
- + pl.sendPacket(new ItemList(pl, false));
- + }
- +
- + SystemMessage msg = new SystemMessage(SystemMessageId.S1_DISAPPEARED);
- + msg.addItemName(itemId);
- + pl.sendPacket(msg);
- +
- + }
- + else
- + {
- + con = null;
- + try
- + {
- + con = L2DatabaseFactory.getInstance().getConnection();
- + PreparedStatement statement;
- + if (temp._charId != 0)
- + {
- + statement = con.prepareStatement("DELETE FROM items WHERE owner_id = ? AND object_id = ?");
- + statement.setInt(1, temp._charId);
- + statement.setInt(2, temp._itemId);
- + statement.execute();
- + statement.close();
- + }
- + else
- + {
- + for (L2Object o : L2World.getInstance().getAllVisibleObjects())
- + {
- + if (o.getObjectId() == temp._itemId)
- + {
- + L2World.getInstance().removeVisibleObject(o, o.getWorldRegion());
- + break;
- + }
- + }
- + }
- + }
- + catch (Exception e)
- + {
- + e.printStackTrace();
- + }
- + finally
- + {
- + try
- + {
- + if (con != null)
- + {
- + con.close();
- + }
- + }
- + catch (SQLException e)
- + {
- + e.printStackTrace();
- + }
- + }
- + }
- + }
- +
- + private final ExclusiveTask _startControlTask = new ExclusiveTask()
- + {
- + @Override
- + protected void onElapsed()
- + {
- + for (Info temp : _timedItems.values())
- + {
- + if (temp._activationTime < (System.currentTimeMillis() / 1000))
- + {
- +
- + delete(temp);
- + }
- + }
- + schedule(60000);
- + }
- + };
- + private L2ItemInstance item;
- +
- +}
- \ No newline at end of file
- #=======================================#
- # Timed item
- #=======================================#
- # Timed Item ID use , for separate (57,3441,5588...)
- ListOfTimedItems = 57,3441,5588
- # Time for item disappear in Hour's
- TimedItemTime = 2
- ### Eclipse Workspace Patch 1.0
- #P Dream_DataPack
- Index: sql/server/character_timed_items.sql
- ===================================================================
- --- sql/server/character_timed_items.sql (revision 1754)
- +++ sql/server/character_timed_items.sql (working copy)
- @@ -1,6 +0,0 @@
- +DROP TABLE IF EXISTS `character_timed_items`;
- +CREATE TABLE `character_timed_items` (
- + `charId` int(11) NOT NULL,
- + `itemId` int(11) NOT NULL,
- + `time` decimal(20,0) NOT NULL DEFAULT '0'
- +) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- \ No newline at end of file
Add Comment
Please, Sign In to add comment