Mail.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.model.itemcontainer;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.logging.Level;
  20. import com.l2jserver.L2DatabaseFactory;
  21. import com.l2jserver.gameserver.model.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.L2World;
  23. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. /**
  26. *
  27. * @author DS
  28. *
  29. */
  30. public class Mail extends ItemContainer
  31. {
  32. private final int _ownerId;
  33. private int _messageId;
  34. public Mail(int objectId, int messageId)
  35. {
  36. _ownerId = objectId;
  37. _messageId = messageId;
  38. }
  39. public String getName()
  40. {
  41. return "Mail";
  42. }
  43. @Override
  44. public L2PcInstance getOwner()
  45. {
  46. return null;
  47. }
  48. @Override
  49. public ItemLocation getBaseLocation()
  50. {
  51. return ItemLocation.MAIL;
  52. }
  53. public int getMessageId()
  54. {
  55. return _messageId;
  56. }
  57. public void setNewMessageId(int messageId)
  58. {
  59. _messageId = messageId;
  60. for (L2ItemInstance item : _items)
  61. {
  62. if (item == null)
  63. continue;
  64. item.setLocation(getBaseLocation(), messageId);
  65. }
  66. updateDatabase();
  67. }
  68. public void returnToWh(ItemContainer wh)
  69. {
  70. for (L2ItemInstance item : _items)
  71. {
  72. if (item == null)
  73. continue;
  74. if (wh == null)
  75. item.setLocation(ItemLocation.WAREHOUSE);
  76. else
  77. transferItem("Expire", item.getObjectId(), item.getCount(), wh, null, null);
  78. }
  79. }
  80. @Override
  81. protected void addItem(L2ItemInstance item)
  82. {
  83. super.addItem(item);
  84. item.setLocation(getBaseLocation(), _messageId);
  85. }
  86. /*
  87. * Allow saving of the items without owner
  88. */
  89. @Override
  90. public void updateDatabase()
  91. {
  92. for (L2ItemInstance item : _items)
  93. {
  94. if (item != null)
  95. {
  96. item.updateDatabase(true);
  97. }
  98. }
  99. }
  100. @Override
  101. public void restore()
  102. {
  103. Connection con = null;
  104. PreparedStatement statement = null;
  105. try
  106. {
  107. con = L2DatabaseFactory.getInstance().getConnection();
  108. 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=?");
  109. statement.setInt(1, getOwnerId());
  110. statement.setString(2, getBaseLocation().name());
  111. statement.setInt(3, getMessageId());
  112. ResultSet inv = statement.executeQuery();
  113. L2ItemInstance item;
  114. while (inv.next())
  115. {
  116. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  117. if (item == null)
  118. continue;
  119. L2World.getInstance().storeObject(item);
  120. // If stackable item is found just add to current quantity
  121. if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
  122. addItem("Restore", item, null, null);
  123. else
  124. addItem(item);
  125. }
  126. inv.close();
  127. statement.close();
  128. }
  129. catch (Exception e)
  130. {
  131. _log.log(Level.WARNING, "could not restore container:", e);
  132. }
  133. finally
  134. {
  135. try
  136. {
  137. statement.close();
  138. }
  139. catch (Exception e) {}
  140. try
  141. {
  142. con.close();
  143. }
  144. catch (Exception e) {}
  145. }
  146. }
  147. @Override
  148. public int getOwnerId()
  149. {
  150. return _ownerId;
  151. }
  152. }