Mail.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.itemcontainer;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.logging.Level;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.enums.ItemLocation;
  26. import com.l2jserver.gameserver.model.L2World;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. /**
  30. * @author DS
  31. */
  32. public class Mail extends ItemContainer
  33. {
  34. private final int _ownerId;
  35. private int _messageId;
  36. public Mail(int objectId, int messageId)
  37. {
  38. _ownerId = objectId;
  39. _messageId = messageId;
  40. }
  41. @Override
  42. public String getName()
  43. {
  44. return "Mail";
  45. }
  46. @Override
  47. public L2PcInstance getOwner()
  48. {
  49. return null;
  50. }
  51. @Override
  52. public ItemLocation getBaseLocation()
  53. {
  54. return ItemLocation.MAIL;
  55. }
  56. public int getMessageId()
  57. {
  58. return _messageId;
  59. }
  60. public void setNewMessageId(int messageId)
  61. {
  62. _messageId = messageId;
  63. for (L2ItemInstance item : _items)
  64. {
  65. if (item == null)
  66. {
  67. continue;
  68. }
  69. item.setItemLocation(getBaseLocation(), messageId);
  70. }
  71. updateDatabase();
  72. }
  73. public void returnToWh(ItemContainer wh)
  74. {
  75. for (L2ItemInstance item : _items)
  76. {
  77. if (item == null)
  78. {
  79. continue;
  80. }
  81. if (wh == null)
  82. {
  83. item.setItemLocation(ItemLocation.WAREHOUSE);
  84. }
  85. else
  86. {
  87. transferItem("Expire", item.getObjectId(), item.getCount(), wh, null, null);
  88. }
  89. }
  90. }
  91. @Override
  92. protected void addItem(L2ItemInstance item)
  93. {
  94. super.addItem(item);
  95. item.setItemLocation(getBaseLocation(), _messageId);
  96. }
  97. /*
  98. * Allow saving of the items without owner
  99. */
  100. @Override
  101. public void updateDatabase()
  102. {
  103. for (L2ItemInstance item : _items)
  104. {
  105. if (item != null)
  106. {
  107. item.updateDatabase(true);
  108. }
  109. }
  110. }
  111. @Override
  112. public void restore()
  113. {
  114. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  115. 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=?"))
  116. {
  117. statement.setInt(1, getOwnerId());
  118. statement.setString(2, getBaseLocation().name());
  119. statement.setInt(3, getMessageId());
  120. try (ResultSet inv = statement.executeQuery())
  121. {
  122. L2ItemInstance item;
  123. while (inv.next())
  124. {
  125. item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
  126. if (item == null)
  127. {
  128. continue;
  129. }
  130. L2World.getInstance().storeObject(item);
  131. // If stackable item is found just add to current quantity
  132. if (item.isStackable() && (getItemByItemId(item.getId()) != null))
  133. {
  134. addItem("Restore", item, null, null);
  135. }
  136. else
  137. {
  138. addItem(item);
  139. }
  140. }
  141. }
  142. }
  143. catch (Exception e)
  144. {
  145. _log.log(Level.WARNING, "could not restore container:", e);
  146. }
  147. }
  148. @Override
  149. public int getOwnerId()
  150. {
  151. return _ownerId;
  152. }
  153. }