123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model.itemcontainer;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.logging.Level;
- import com.l2jserver.L2DatabaseFactory;
- import com.l2jserver.gameserver.model.L2World;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
- import com.l2jserver.gameserver.model.items.instance.L2ItemInstance.ItemLocation;
- /**
- *
- * @author DS
- *
- */
- public class Mail extends ItemContainer
- {
- private final int _ownerId;
- private int _messageId;
-
- public Mail(int objectId, int messageId)
- {
- _ownerId = objectId;
- _messageId = messageId;
- }
-
- @Override
- public String getName()
- {
- return "Mail";
- }
-
- @Override
- public L2PcInstance getOwner()
- {
- return null;
- }
-
- @Override
- public ItemLocation getBaseLocation()
- {
- return ItemLocation.MAIL;
- }
-
- public int getMessageId()
- {
- return _messageId;
- }
-
- public void setNewMessageId(int messageId)
- {
- _messageId = messageId;
- for (L2ItemInstance item : _items)
- {
- if (item == null)
- continue;
-
- item.setLocation(getBaseLocation(), messageId);
- }
-
- updateDatabase();
- }
-
- public void returnToWh(ItemContainer wh)
- {
- for (L2ItemInstance item : _items)
- {
- if (item == null)
- continue;
- if (wh == null)
- item.setLocation(ItemLocation.WAREHOUSE);
- else
- transferItem("Expire", item.getObjectId(), item.getCount(), wh, null, null);
- }
- }
-
- @Override
- protected void addItem(L2ItemInstance item)
- {
- super.addItem(item);
- item.setLocation(getBaseLocation(), _messageId);
- }
-
- /*
- * Allow saving of the items without owner
- */
- @Override
- public void updateDatabase()
- {
- for (L2ItemInstance item : _items)
- {
- if (item != null)
- {
- item.updateDatabase(true);
- }
- }
- }
-
- @Override
- public void restore()
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("SELECT object_id, item_id, count, enchant_level, loc, loc_data, custom_type1, custom_type2, mana_left, time FROM items WHERE owner_id=? AND loc=? AND loc_data=?");
- statement.setInt(1, getOwnerId());
- statement.setString(2, getBaseLocation().name());
- statement.setInt(3, getMessageId());
- ResultSet inv = statement.executeQuery();
-
- L2ItemInstance item;
- while (inv.next())
- {
- item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
- if (item == null)
- continue;
-
- L2World.getInstance().storeObject(item);
-
- // If stackable item is found just add to current quantity
- if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
- addItem("Restore", item, null, null);
- else
- addItem(item);
- }
- inv.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not restore container:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- @Override
- public int getOwnerId()
- {
- return _ownerId;
- }
- }
|