PcFreight.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.util.List;
  17. import javolution.util.FastList;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  21. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance.ItemLocation;
  22. import com.l2jserver.gameserver.model.stats.Stats;
  23. public class PcFreight extends ItemContainer
  24. {
  25. private final L2PcInstance _owner;
  26. private int _ownerId = 0;
  27. public PcFreight(int object_id)
  28. {
  29. _owner = null;
  30. _ownerId = object_id;
  31. restore();
  32. }
  33. public PcFreight(L2PcInstance owner)
  34. {
  35. _owner = owner;
  36. _ownerId = owner.getObjectId();
  37. }
  38. @Override
  39. public L2PcInstance getOwner()
  40. {
  41. return _owner;
  42. }
  43. @Override
  44. public ItemLocation getBaseLocation()
  45. {
  46. return ItemLocation.FREIGHT;
  47. }
  48. /**
  49. * Returns the quantity of items in the inventory
  50. * @return int
  51. */
  52. @Override
  53. public int getSize()
  54. {
  55. int size = 0;
  56. for (L2ItemInstance item : _items)
  57. {
  58. if (item.getLocation() == getBaseLocation())
  59. {
  60. size++;
  61. }
  62. }
  63. return size;
  64. }
  65. /**
  66. * Returns the list of items in inventory
  67. * @return L2ItemInstance : items in inventory
  68. */
  69. @Override
  70. public L2ItemInstance[] getItems()
  71. {
  72. List<L2ItemInstance> list = new FastList<>();
  73. for (L2ItemInstance item : _items)
  74. {
  75. if (item.isFreightable())
  76. {
  77. list.add(item);
  78. }
  79. }
  80. return list.toArray(new L2ItemInstance[list.size()]);
  81. }
  82. /**
  83. * Returns the item from inventory by using its <B>itemId</B>
  84. * @param itemId : int designating the ID of the item
  85. * @return L2ItemInstance designating the item or null if not found in inventory
  86. */
  87. @Override
  88. public L2ItemInstance getItemByItemId(int itemId)
  89. {
  90. for (L2ItemInstance item : _items)
  91. {
  92. if ((item.getItemId() == itemId) && (item.getLocation() == ItemLocation.INVENTORY))
  93. {
  94. return item;
  95. }
  96. }
  97. return null;
  98. }
  99. @Override
  100. public int getOwnerId()
  101. {
  102. return _ownerId;
  103. }
  104. @Override
  105. public boolean validateCapacity(long slots)
  106. {
  107. int curSlots = _owner == null ? Config.ALT_FREIGHT_SLOTS : Config.ALT_FREIGHT_SLOTS + (int) _owner.getStat().calcStat(Stats.FREIGHT_LIM, 0, null, null);
  108. return ((getSize() + slots) <= curSlots);
  109. }
  110. }