Mail.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. continue;
  63. item.setLocation(getBaseLocation(), messageId);
  64. }
  65. updateDatabase();
  66. }
  67. public void returnToWh(ItemContainer wh)
  68. {
  69. for (L2ItemInstance item : _items)
  70. {
  71. if (item == null)
  72. continue;
  73. if (wh == null)
  74. item.setLocation(ItemLocation.WAREHOUSE);
  75. else
  76. transferItem("Expire", item.getObjectId(), item.getCount(), wh, null, null);
  77. }
  78. }
  79. @Override
  80. protected void addItem(L2ItemInstance item)
  81. {
  82. super.addItem(item);
  83. item.setLocation(getBaseLocation(), _messageId);
  84. }
  85. /*
  86. * Allow saving of the items without owner
  87. */
  88. @Override
  89. public void updateDatabase()
  90. {
  91. for (L2ItemInstance item : _items)
  92. {
  93. if (item != null)
  94. {
  95. item.updateDatabase(true);
  96. }
  97. }
  98. }
  99. @Override
  100. public void restore()
  101. {
  102. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  103. 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=?"))
  104. {
  105. statement.setInt(1, getOwnerId());
  106. statement.setString(2, getBaseLocation().name());
  107. statement.setInt(3, getMessageId());
  108. try (ResultSet inv = statement.executeQuery())
  109. {
  110. L2ItemInstance item;
  111. while (inv.next())
  112. {
  113. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  114. if (item == null)
  115. continue;
  116. L2World.getInstance().storeObject(item);
  117. // If stackable item is found just add to current quantity
  118. if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
  119. addItem("Restore", item, null, null);
  120. else
  121. addItem(item);
  122. }
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. _log.log(Level.WARNING, "could not restore container:", e);
  128. }
  129. }
  130. @Override
  131. public int getOwnerId()
  132. {
  133. return _ownerId;
  134. }
  135. }