Mail.java 4.0 KB

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