Mail.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.L2World;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  24. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance.ItemLocation;
  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. @Override
  40. public String getName()
  41. {
  42. return "Mail";
  43. }
  44. @Override
  45. public L2PcInstance getOwner()
  46. {
  47. return null;
  48. }
  49. @Override
  50. public ItemLocation getBaseLocation()
  51. {
  52. return ItemLocation.MAIL;
  53. }
  54. public int getMessageId()
  55. {
  56. return _messageId;
  57. }
  58. public void setNewMessageId(int messageId)
  59. {
  60. _messageId = messageId;
  61. for (L2ItemInstance item : _items)
  62. {
  63. if (item == null)
  64. continue;
  65. item.setLocation(getBaseLocation(), messageId);
  66. }
  67. updateDatabase();
  68. }
  69. public void returnToWh(ItemContainer wh)
  70. {
  71. for (L2ItemInstance item : _items)
  72. {
  73. if (item == null)
  74. continue;
  75. if (wh == null)
  76. item.setLocation(ItemLocation.WAREHOUSE);
  77. else
  78. transferItem("Expire", item.getObjectId(), item.getCount(), wh, null, null);
  79. }
  80. }
  81. @Override
  82. protected void addItem(L2ItemInstance item)
  83. {
  84. super.addItem(item);
  85. item.setLocation(getBaseLocation(), _messageId);
  86. }
  87. /*
  88. * Allow saving of the items without owner
  89. */
  90. @Override
  91. public void updateDatabase()
  92. {
  93. for (L2ItemInstance item : _items)
  94. {
  95. if (item != null)
  96. {
  97. item.updateDatabase(true);
  98. }
  99. }
  100. }
  101. @Override
  102. public void restore()
  103. {
  104. Connection con = null;
  105. try
  106. {
  107. con = L2DatabaseFactory.getInstance().getConnection();
  108. 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=?");
  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. L2DatabaseFactory.close(con);
  136. }
  137. }
  138. @Override
  139. public int getOwnerId()
  140. {
  141. return _ownerId;
  142. }
  143. }