Mail.java 3.8 KB

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