PetInventory.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 com.l2jserver.gameserver.datatables.ItemTable;
  17. import com.l2jserver.gameserver.model.L2ItemInstance;
  18. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  20. import com.l2jserver.gameserver.templates.item.L2EtcItemType;
  21. import com.l2jserver.gameserver.templates.item.L2Item;
  22. public class PetInventory extends Inventory
  23. {
  24. private final L2PetInstance _owner;
  25. public PetInventory(L2PetInstance owner)
  26. {
  27. _owner = owner;
  28. }
  29. @Override
  30. public L2PetInstance getOwner()
  31. {
  32. return _owner;
  33. }
  34. @Override
  35. public int getOwnerId()
  36. {
  37. // gets the L2PcInstance-owner's ID
  38. int id;
  39. try
  40. {
  41. id = _owner.getOwner().getObjectId();
  42. }
  43. catch (NullPointerException e)
  44. {
  45. return 0;
  46. }
  47. return id;
  48. }
  49. /**
  50. * Refresh the weight of equipment loaded
  51. */
  52. @Override
  53. protected void refreshWeight()
  54. {
  55. super.refreshWeight();
  56. getOwner().updateAndBroadcastStatus(1);
  57. }
  58. public boolean validateCapacity(L2ItemInstance item)
  59. {
  60. int slots = 0;
  61. if (!(item.isStackable() && getItemByItemId(item.getItemId()) != null) && item.getItemType() != L2EtcItemType.HERB)
  62. slots++;
  63. return validateCapacity(slots);
  64. }
  65. @Override
  66. public boolean validateCapacity(int slots)
  67. {
  68. return (_items.size() + slots <= _owner.getInventoryLimit());
  69. }
  70. public boolean validateWeight(L2ItemInstance item, long count)
  71. {
  72. int weight = 0;
  73. L2Item template = ItemTable.getInstance().getTemplate(item.getItemId());
  74. if (template == null) return false;
  75. weight += count * template.getWeight();
  76. return validateWeight(weight);
  77. }
  78. @Override
  79. public boolean validateWeight(int weight)
  80. {
  81. return (_totalWeight + weight <= _owner.getMaxLoad());
  82. }
  83. @Override
  84. protected ItemLocation getBaseLocation()
  85. {
  86. return ItemLocation.PET;
  87. }
  88. @Override
  89. protected ItemLocation getEquipLocation()
  90. {
  91. return ItemLocation.PET_EQUIP;
  92. }
  93. @Override
  94. public void restore()
  95. {
  96. super.restore();
  97. // check for equiped items from other pets
  98. for (L2ItemInstance item : _items)
  99. if (item.isEquipped())
  100. {
  101. if (!item.getItem().checkCondition(getOwner(), getOwner(), false))
  102. {
  103. unEquipItemInSlot(item.getLocationSlot());
  104. }
  105. }
  106. }
  107. public void transferItemsToOwner()
  108. {
  109. for (L2ItemInstance item : _items)
  110. {
  111. getOwner().transferItem("return", item.getObjectId(), item.getCount(), getOwner().getOwner().getInventory(), getOwner().getOwner(), getOwner());
  112. }
  113. }
  114. }