PetInventory.java 3.3 KB

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