PcFreight.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 net.sf.l2j.gameserver.model;
  16. import java.util.List;
  17. import javolution.util.FastList;
  18. import net.sf.l2j.gameserver.model.L2ItemInstance.ItemLocation;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. public class PcFreight extends ItemContainer
  21. {
  22. //private static final Logger _log = Logger.getLogger(PcFreight.class.getName());
  23. private L2PcInstance _owner; // This is the L2PcInstance that owns this Freight;
  24. private int _activeLocationId;
  25. public PcFreight(L2PcInstance owner)
  26. {
  27. _owner = owner;
  28. }
  29. @Override
  30. public L2PcInstance getOwner() { return _owner; }
  31. @Override
  32. public ItemLocation getBaseLocation() { return ItemLocation.FREIGHT; }
  33. public void setActiveLocation(int locationId) { _activeLocationId = locationId; }
  34. public int getactiveLocation() { return _activeLocationId; }
  35. /**
  36. * Returns the quantity of items in the inventory
  37. * @return int
  38. */
  39. @Override
  40. public int getSize()
  41. {
  42. int size = 0;
  43. for (L2ItemInstance item : _items)
  44. {
  45. if (item.getLocationSlot() == 0 || _activeLocationId == 0
  46. || item.getLocationSlot() == _activeLocationId) size++;
  47. }
  48. return size;
  49. }
  50. /**
  51. * Returns the list of items in inventory
  52. * @return L2ItemInstance : items in inventory
  53. */
  54. @Override
  55. public L2ItemInstance[] getItems()
  56. {
  57. List<L2ItemInstance> list = new FastList<L2ItemInstance>();
  58. for (L2ItemInstance item : _items)
  59. {
  60. if (item.getLocationSlot() == 0 || item.getLocationSlot() == _activeLocationId) list.add(item);
  61. }
  62. return list.toArray(new L2ItemInstance[list.size()]);
  63. }
  64. /**
  65. * Returns the item from inventory by using its <B>itemId</B>
  66. * @param itemId : int designating the ID of the item
  67. * @return L2ItemInstance designating the item or null if not found in inventory
  68. */
  69. @Override
  70. public L2ItemInstance getItemByItemId(int itemId)
  71. {
  72. for (L2ItemInstance item : _items)
  73. if ((item.getItemId() == itemId)
  74. && (item.getLocationSlot() == 0 || _activeLocationId == 0
  75. || item.getLocationSlot() == _activeLocationId)
  76. ) return item;
  77. return null;
  78. }
  79. /**
  80. * Adds item to PcFreight for further adjustments.
  81. * @param item : L2ItemInstance to be added from inventory
  82. */
  83. @Override
  84. protected void addItem(L2ItemInstance item)
  85. {
  86. super.addItem(item);
  87. if (_activeLocationId > 0) item.setLocation(item.getLocation(), _activeLocationId);
  88. }
  89. /**
  90. * Get back items in PcFreight from database
  91. */
  92. @Override
  93. public void restore()
  94. {
  95. int locationId = _activeLocationId;
  96. _activeLocationId = 0;
  97. super.restore();
  98. _activeLocationId = locationId;
  99. }
  100. @Override
  101. public boolean validateCapacity(int slots)
  102. {
  103. return (getSize() + slots <= _owner.getFreightLimit());
  104. }
  105. }