Advertisement
Guest User

PcInventory.java

a guest
Aug 3rd, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.58 KB | None | 0 0
  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.itemcontainer;
  16.  
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. import net.sf.l2j.commons.config.Config;
  24. import net.sf.l2j.commons.db.DatabaseFactory;
  25. import net.sf.l2j.gameserver.datatables.ItemTable;
  26. import net.sf.l2j.gameserver.instancemanager.PremiumManager;
  27. import net.sf.l2j.gameserver.model.L2Object;
  28. import net.sf.l2j.gameserver.model.TradeList;
  29. import net.sf.l2j.gameserver.model.TradeList.TradeItem;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  31. import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
  32. import net.sf.l2j.gameserver.model.item.instance.ItemInstance.ItemLocation;
  33. import net.sf.l2j.gameserver.model.item.kind.Item;
  34. import net.sf.l2j.gameserver.model.item.type.EtcItemType;
  35. import net.sf.l2j.gameserver.model.itemcontainer.listeners.ArmorSetListener;
  36. import net.sf.l2j.gameserver.model.itemcontainer.listeners.BowRodListener;
  37. import net.sf.l2j.gameserver.model.itemcontainer.listeners.ItemPassiveSkillsListener;
  38. import net.sf.l2j.gameserver.model.itemcontainer.listeners.ShadowWeaponListener;
  39. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  40. import net.sf.l2j.gameserver.network.serverpackets.ItemList;
  41. import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
  42. import net.sf.l2j.gameserver.util.Util;
  43.  
  44. public class PcInventory extends Inventory
  45. {
  46.     public static final int ADENA_ID = 57;
  47.     public static final int ANCIENT_ADENA_ID = 5575;
  48.    
  49.     private final L2PcInstance _owner;
  50.     private ItemInstance _adena;
  51.     private ItemInstance _ancientAdena;
  52.    
  53.     private int[] _blockItems = null;
  54.    
  55.     private int _questSlots;
  56.     /**
  57.      * Block modes:
  58.      * <UL>
  59.      * <LI>-1 - no block
  60.      * <LI>0 - block items from _invItems, allow usage of other items
  61.      * <LI>1 - allow usage of items from _invItems, block other items
  62.      * </UL>
  63.      */
  64.     private int _blockMode = -1;
  65.    
  66.     public PcInventory(L2PcInstance owner)
  67.     {
  68.         super();
  69.         _owner = owner;
  70.        
  71.         addPaperdollListener(ArmorSetListener.getInstance());
  72.         addPaperdollListener(BowRodListener.getInstance());
  73.         addPaperdollListener(ItemPassiveSkillsListener.getInstance());
  74.         addPaperdollListener(ShadowWeaponListener.getInstance());
  75.     }
  76.    
  77.     @Override
  78.     public L2PcInstance getOwner()
  79.     {
  80.         return _owner;
  81.     }
  82.    
  83.     @Override
  84.     protected ItemLocation getBaseLocation()
  85.     {
  86.         return ItemLocation.INVENTORY;
  87.     }
  88.    
  89.     @Override
  90.     protected ItemLocation getEquipLocation()
  91.     {
  92.         return ItemLocation.PAPERDOLL;
  93.     }
  94.    
  95.     public ItemInstance getAdenaInstance()
  96.     {
  97.         return _adena;
  98.     }
  99.    
  100.     @Override
  101.     public int getAdena()
  102.     {
  103.         return _adena != null ? _adena.getCount() : 0;
  104.     }
  105.    
  106.     public ItemInstance getAncientAdenaInstance()
  107.     {
  108.         return _ancientAdena;
  109.     }
  110.    
  111.     public int getAncientAdena()
  112.     {
  113.         return (_ancientAdena != null) ? _ancientAdena.getCount() : 0;
  114.     }
  115.    
  116.     public ItemInstance[] getUniqueItems(boolean allowAdena, boolean allowAncientAdena)
  117.     {
  118.         return getUniqueItems(allowAdena, allowAncientAdena, true);
  119.     }
  120.    
  121.     /**
  122.      * Returns the list of items in inventory available for transaction
  123.      * @param allowAdena
  124.      * @param allowAncientAdena
  125.      * @param onlyAvailable
  126.      * @return ItemInstance : items in inventory
  127.      */
  128.     public ItemInstance[] getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable)
  129.     {
  130.         List<ItemInstance> list = new ArrayList<>();
  131.         for (ItemInstance item : _items)
  132.         {
  133.             if (item == null)
  134.             {
  135.                 continue;
  136.             }
  137.            
  138.             if (!allowAdena && item.getItemId() == ADENA_ID)
  139.             {
  140.                 continue;
  141.             }
  142.            
  143.             if (!allowAncientAdena && item.getItemId() == ANCIENT_ADENA_ID)
  144.             {
  145.                 continue;
  146.             }
  147.            
  148.             boolean isDuplicate = false;
  149.             for (ItemInstance litem : list)
  150.             {
  151.                 if (litem.getItemId() == item.getItemId())
  152.                 {
  153.                     isDuplicate = true;
  154.                     break;
  155.                 }
  156.             }
  157.             if (!isDuplicate && (!onlyAvailable || (item.isSellable() && item.isAvailable(getOwner(), false, false))))
  158.             {
  159.                 list.add(item);
  160.             }
  161.         }
  162.         return list.toArray(new ItemInstance[list.size()]);
  163.     }
  164.    
  165.     /**
  166.      * Returns the list of items in inventory available for transaction Allows an item to appear twice if and only if there is a difference in enchantment level.
  167.      * @param allowAdena
  168.      * @param allowAncientAdena
  169.      * @return ItemInstance : items in inventory
  170.      */
  171.     public ItemInstance[] getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena)
  172.     {
  173.         return getUniqueItemsByEnchantLevel(allowAdena, allowAncientAdena, true);
  174.     }
  175.    
  176.     public ItemInstance[] getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable)
  177.     {
  178.         List<ItemInstance> list = new ArrayList<>();
  179.         for (ItemInstance item : _items)
  180.         {
  181.             if (item == null)
  182.             {
  183.                 continue;
  184.             }
  185.            
  186.             if (!allowAdena && item.getItemId() == ADENA_ID)
  187.             {
  188.                 continue;
  189.             }
  190.            
  191.             if (!allowAncientAdena && item.getItemId() == ANCIENT_ADENA_ID)
  192.             {
  193.                 continue;
  194.             }
  195.            
  196.             boolean isDuplicate = false;
  197.             for (ItemInstance litem : list)
  198.             {
  199.                 if ((litem.getItemId() == item.getItemId()) && (litem.getEnchantLevel() == item.getEnchantLevel()))
  200.                 {
  201.                     isDuplicate = true;
  202.                     break;
  203.                 }
  204.             }
  205.             if (!isDuplicate && (!onlyAvailable || (item.isSellable() && item.isAvailable(getOwner(), false, false))))
  206.             {
  207.                 list.add(item);
  208.             }
  209.         }
  210.         return list.toArray(new ItemInstance[list.size()]);
  211.     }
  212.    
  213.     /**
  214.      * @param itemId
  215.      * @return
  216.      * @see net.sf.l2j.gameserver.model.itemcontainer.PcInventory#getAllItemsByItemId(int, boolean)
  217.      */
  218.     public ItemInstance[] getAllItemsByItemId(int itemId)
  219.     {
  220.         return getAllItemsByItemId(itemId, true);
  221.     }
  222.    
  223.     /**
  224.      * Returns the list of all items in inventory that have a given item id.
  225.      * @param itemId : ID of item
  226.      * @param includeEquipped : include equipped items
  227.      * @return ItemInstance[] : matching items from inventory
  228.      */
  229.     public ItemInstance[] getAllItemsByItemId(int itemId, boolean includeEquipped)
  230.     {
  231.         List<ItemInstance> list = new ArrayList<>();
  232.         for (ItemInstance item : _items)
  233.         {
  234.             if (item == null)
  235.             {
  236.                 continue;
  237.             }
  238.            
  239.             if (item.getItemId() == itemId && (includeEquipped || !item.isEquipped()))
  240.             {
  241.                 list.add(item);
  242.             }
  243.         }
  244.         return list.toArray(new ItemInstance[list.size()]);
  245.     }
  246.    
  247.     /**
  248.      * @param itemId
  249.      * @param enchantment
  250.      * @return
  251.      * @see net.sf.l2j.gameserver.model.itemcontainer.PcInventory#getAllItemsByItemId(int, int, boolean)
  252.      */
  253.     public ItemInstance[] getAllItemsByItemId(int itemId, int enchantment)
  254.     {
  255.         return getAllItemsByItemId(itemId, enchantment, true);
  256.     }
  257.    
  258.     /**
  259.      * Returns the list of all items in inventory that have a given item id AND a given enchantment level.
  260.      * @param itemId : ID of item
  261.      * @param enchantment : enchant level of item
  262.      * @param includeEquipped : include equipped items
  263.      * @return ItemInstance[] : matching items from inventory
  264.      */
  265.     public ItemInstance[] getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped)
  266.     {
  267.         List<ItemInstance> list = new ArrayList<>();
  268.         for (ItemInstance item : _items)
  269.         {
  270.             if (item == null)
  271.             {
  272.                 continue;
  273.             }
  274.            
  275.             if ((item.getItemId() == itemId) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped()))
  276.             {
  277.                 list.add(item);
  278.             }
  279.         }
  280.         return list.toArray(new ItemInstance[list.size()]);
  281.     }
  282.    
  283.     /**
  284.      * Returns the list of items in inventory available for transaction
  285.      * @param allowAdena
  286.      * @param allowNonTradeable
  287.      * @return ItemInstance : items in inventory
  288.      */
  289.     public ItemInstance[] getAvailableItems(boolean allowAdena, boolean allowNonTradeable)
  290.     {
  291.         List<ItemInstance> list = new ArrayList<>();
  292.         for (ItemInstance item : _items)
  293.         {
  294.             if (item != null && item.isAvailable(getOwner(), allowAdena, allowNonTradeable) && canManipulateWithItemId(item.getItemId()))
  295.             {
  296.                 list.add(item);
  297.             }
  298.         }
  299.         return list.toArray(new ItemInstance[list.size()]);
  300.     }
  301.    
  302.     /**
  303.      * Get all augmented items
  304.      * @return
  305.      */
  306.     public ItemInstance[] getAugmentedItems()
  307.     {
  308.         List<ItemInstance> list = new ArrayList<>();
  309.         for (ItemInstance item : _items)
  310.         {
  311.             if (item != null && item.isAugmented())
  312.             {
  313.                 list.add(item);
  314.             }
  315.         }
  316.         return list.toArray(new ItemInstance[list.size()]);
  317.     }
  318.    
  319.     /**
  320.      * Returns the list of items in inventory available for transaction adjusetd by tradeList
  321.      * @param tradeList
  322.      * @return ItemInstance : items in inventory
  323.      */
  324.     public TradeItem[] getAvailableItems(TradeList tradeList)
  325.     {
  326.         List<TradeItem> list = new ArrayList<>();
  327.         for (ItemInstance item : _items)
  328.         {
  329.             if (item != null && item.isAvailable(getOwner(), false, false))
  330.             {
  331.                 TradeItem adjItem = tradeList.adjustAvailableItem(item);
  332.                 if (adjItem != null)
  333.                 {
  334.                     list.add(adjItem);
  335.                 }
  336.             }
  337.         }
  338.         return list.toArray(new TradeItem[list.size()]);
  339.     }
  340.    
  341.     /**
  342.      * Adjust TradeItem according his status in inventory
  343.      * @param item : ItemInstance to be adjusten
  344.      */
  345.     public void adjustAvailableItem(TradeItem item)
  346.     {
  347.         boolean notAllEquipped = false;
  348.         for (ItemInstance adjItem : getItemsByItemId(item.getItem().getItemId()))
  349.         {
  350.             if (adjItem.isEquipable())
  351.             {
  352.                 if (!adjItem.isEquipped())
  353.                 {
  354.                     notAllEquipped |= true;
  355.                 }
  356.             }
  357.             else
  358.             {
  359.                 notAllEquipped |= true;
  360.                 break;
  361.             }
  362.         }
  363.         if (notAllEquipped)
  364.         {
  365.             ItemInstance adjItem = getItemByItemId(item.getItem().getItemId());
  366.             item.setObjectId(adjItem.getObjectId());
  367.             item.setEnchant(adjItem.getEnchantLevel());
  368.            
  369.             if (adjItem.getCount() < item.getCount())
  370.             {
  371.                 item.setCount(adjItem.getCount());
  372.             }
  373.            
  374.             return;
  375.         }
  376.        
  377.         item.setCount(0);
  378.     }
  379.    
  380.     /**
  381.      * Adds adena to PCInventory
  382.      * @param process : String Identifier of process triggering this action
  383.      * @param count : int Quantity of adena to be added
  384.      * @param actor : L2PcInstance Player requesting the item add
  385.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  386.      */
  387.     public void addAdena(String process, int count, L2PcInstance actor, L2Object reference)
  388.     {
  389.         if (count > 0)
  390.         {
  391.             addItem(process, ADENA_ID, count, actor, reference);
  392.         }
  393.     }
  394.    
  395.     /**
  396.      * Removes adena to PCInventory
  397.      * @param process : String Identifier of process triggering this action
  398.      * @param count : int Quantity of adena to be removed
  399.      * @param actor : L2PcInstance Player requesting the item add
  400.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  401.      * @return true if successful.
  402.      */
  403.     public boolean reduceAdena(String process, int count, L2PcInstance actor, L2Object reference)
  404.     {
  405.         if (count > 0)
  406.         {
  407.             return destroyItemByItemId(process, ADENA_ID, count, actor, reference) != null;
  408.         }
  409.        
  410.         return false;
  411.     }
  412.    
  413.     /**
  414.      * Adds specified amount of ancient adena to player inventory.
  415.      * @param process : String Identifier of process triggering this action
  416.      * @param count : int Quantity of adena to be added
  417.      * @param actor : L2PcInstance Player requesting the item add
  418.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  419.      */
  420.     public void addAncientAdena(String process, int count, L2PcInstance actor, L2Object reference)
  421.     {
  422.         if (count > 0)
  423.         {
  424.             addItem(process, ANCIENT_ADENA_ID, count, actor, reference);
  425.         }
  426.     }
  427.    
  428.     /**
  429.      * Removes specified amount of ancient adena from player inventory.
  430.      * @param process : String Identifier of process triggering this action
  431.      * @param count : int Quantity of adena to be removed
  432.      * @param actor : L2PcInstance Player requesting the item add
  433.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  434.      * @return true if successful.
  435.      */
  436.     public boolean reduceAncientAdena(String process, int count, L2PcInstance actor, L2Object reference)
  437.     {
  438.         if (count > 0)
  439.         {
  440.             return destroyItemByItemId(process, ANCIENT_ADENA_ID, count, actor, reference) != null;
  441.         }
  442.        
  443.         return false;
  444.     }
  445.    
  446.     /**
  447.      * Adds item in inventory and checks _adena and _ancientAdena
  448.      * @param process : String Identifier of process triggering this action
  449.      * @param item : ItemInstance to be added
  450.      * @param actor : L2PcInstance Player requesting the item add
  451.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  452.      * @return ItemInstance corresponding to the new item or the updated item in inventory
  453.      */
  454.     @Override
  455.     public ItemInstance addItem(String process, ItemInstance item, L2PcInstance actor, L2Object reference)
  456.     {
  457.         item = super.addItem(process, item, actor, reference);
  458.         if (item == null)
  459.         {
  460.             return null;
  461.         }
  462.        
  463.         if (item.getItemId() == ADENA_ID && !item.equals(_adena))
  464.         {
  465.             _adena = item;
  466.         }
  467.         else if (item.getItemId() == ANCIENT_ADENA_ID && !item.equals(_ancientAdena))
  468.         {
  469.             _ancientAdena = item;
  470.         }
  471.        
  472.         if (Config.PREMIUM_ITEM_ENABLED)
  473.         {
  474.             for (int i = 0; i < Config.PREMIUM_ITEM_IDS.length; i++)
  475.             {
  476.                 if (item.getItemId() == Config.PREMIUM_ITEM_IDS[i])
  477.                 {
  478.                     PremiumManager.getInstance().addPremiumItem(item, i);
  479.                 }
  480.             }
  481.             actor.calculatePremiumAttributes();
  482.         }
  483.        
  484.         return item;
  485.     }
  486.    
  487.     /**
  488.      * Adds item in inventory and checks _adena and _ancientAdena
  489.      * @param process : String Identifier of process triggering this action
  490.      * @param itemId : int Item Identifier of the item to be added
  491.      * @param count : int Quantity of items to be added
  492.      * @param actor : L2PcInstance Player requesting the item creation
  493.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  494.      * @return ItemInstance corresponding to the new item or the updated item in inventory
  495.      */
  496.     @Override
  497.     public ItemInstance addItem(String process, int itemId, int count, L2PcInstance actor, L2Object reference)
  498.     {
  499.         ItemInstance item = super.addItem(process, itemId, count, actor, reference);
  500.         if (item == null)
  501.         {
  502.             return null;
  503.         }
  504.        
  505.         if (item.getItemId() == ADENA_ID && !item.equals(_adena))
  506.         {
  507.             _adena = item;
  508.         }
  509.         else if (item.getItemId() == ANCIENT_ADENA_ID && !item.equals(_ancientAdena))
  510.         {
  511.             _ancientAdena = item;
  512.         }
  513.        
  514.         if (actor != null)
  515.         {
  516.             if (Config.PREMIUM_ITEM_ENABLED)
  517.             {
  518.                 for (int i = 0; i < Config.PREMIUM_ITEM_IDS.length; i++)
  519.                 {
  520.                     if (item.getItemId() == Config.PREMIUM_ITEM_IDS[i])
  521.                     {
  522.                         PremiumManager.getInstance().addPremiumItem(item, i);
  523.                     }
  524.                 }
  525.                 actor.calculatePremiumAttributes();
  526.             }
  527.            
  528.             // Send inventory update packet
  529.             InventoryUpdate playerIU = new InventoryUpdate();
  530.             playerIU.addItem(item);
  531.             actor.sendPacket(playerIU);
  532.            
  533.             // Update current load as well
  534.             StatusUpdate su = new StatusUpdate(actor);
  535.             su.addAttribute(StatusUpdate.CUR_LOAD, actor.getCurrentLoad());
  536.             actor.sendPacket(su);
  537.         }
  538.        
  539.         return item;
  540.     }
  541.    
  542.     /**
  543.      * Transfers item to another inventory and checks _adena and _ancientAdena
  544.      * @param process : String Identifier of process triggering this action
  545.      * @param objectId : int Item Identifier of the item to be transfered
  546.      * @param count : int Quantity of items to be transfered
  547.      * @param actor : L2PcInstance Player requesting the item transfer
  548.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  549.      * @return ItemInstance corresponding to the new item or the updated item in inventory
  550.      */
  551.     @Override
  552.     public ItemInstance transferItem(String process, int objectId, int count, ItemContainer target, L2PcInstance actor, L2Object reference)
  553.     {
  554.         ItemInstance item = super.transferItem(process, objectId, count, target, actor, reference);
  555.        
  556.         if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  557.         {
  558.             _adena = null;
  559.         }
  560.        
  561.         if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  562.         {
  563.             _ancientAdena = null;
  564.         }
  565.        
  566.         return item;
  567.     }
  568.    
  569.     /**
  570.      * Destroy item from inventory and checks _adena and _ancientAdena
  571.      * @param process : String Identifier of process triggering this action
  572.      * @param item : ItemInstance to be destroyed
  573.      * @param actor : L2PcInstance Player requesting the item destroy
  574.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  575.      * @return ItemInstance corresponding to the destroyed item or the updated item in inventory
  576.      */
  577.     @Override
  578.     public ItemInstance destroyItem(String process, ItemInstance item, L2PcInstance actor, L2Object reference)
  579.     {
  580.         return this.destroyItem(process, item, item.getCount(), actor, reference);
  581.     }
  582.    
  583.     /**
  584.      * Destroy item from inventory and checks _adena and _ancientAdena
  585.      * @param process : String Identifier of process triggering this action
  586.      * @param item : ItemInstance to be destroyed
  587.      * @param actor : L2PcInstance Player requesting the item destroy
  588.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  589.      * @return ItemInstance corresponding to the destroyed item or the updated item in inventory
  590.      */
  591.     @Override
  592.     public ItemInstance destroyItem(String process, ItemInstance item, int count, L2PcInstance actor, L2Object reference)
  593.     {
  594.         item = super.destroyItem(process, item, count, actor, reference);
  595.        
  596.         if (_adena != null && _adena.getCount() <= 0)
  597.         {
  598.             _adena = null;
  599.         }
  600.        
  601.         if (_ancientAdena != null && _ancientAdena.getCount() <= 0)
  602.         {
  603.             _ancientAdena = null;
  604.         }
  605.        
  606.         if (Config.PREMIUM_ITEM_ENABLED)
  607.         {
  608.             for (int i = 0; i < Config.PREMIUM_ITEM_IDS.length; i++)
  609.             {
  610.                 if (item.getItemId() == Config.PREMIUM_ITEM_IDS[i])
  611.                 {
  612.                     PremiumManager.getInstance().destroyItem(item);
  613.                 }
  614.             }
  615.             actor.calculatePremiumAttributes();
  616.         }
  617.        
  618.         return item;
  619.     }
  620.    
  621.     /**
  622.      * Destroys item from inventory and checks _adena and _ancientAdena
  623.      * @param process : String Identifier of process triggering this action
  624.      * @param objectId : int Item Instance identifier of the item to be destroyed
  625.      * @param count : int Quantity of items to be destroyed
  626.      * @param actor : L2PcInstance Player requesting the item destroy
  627.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  628.      * @return ItemInstance corresponding to the destroyed item or the updated item in inventory
  629.      */
  630.     @Override
  631.     public ItemInstance destroyItem(String process, int objectId, int count, L2PcInstance actor, L2Object reference)
  632.     {
  633.         ItemInstance item = getItemByObjectId(objectId);
  634.         if (item == null)
  635.         {
  636.             return null;
  637.         }
  638.        
  639.         return this.destroyItem(process, item, count, actor, reference);
  640.     }
  641.    
  642.     /**
  643.      * Destroy item from inventory by using its <B>itemId</B> and checks _adena and _ancientAdena
  644.      * @param process : String Identifier of process triggering this action
  645.      * @param itemId : int Item identifier of the item to be destroyed
  646.      * @param count : int Quantity of items to be destroyed
  647.      * @param actor : L2PcInstance Player requesting the item destroy
  648.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  649.      * @return ItemInstance corresponding to the destroyed item or the updated item in inventory
  650.      */
  651.     @Override
  652.     public ItemInstance destroyItemByItemId(String process, int itemId, int count, L2PcInstance actor, L2Object reference)
  653.     {
  654.         ItemInstance item = getItemByItemId(itemId);
  655.         if (item == null)
  656.         {
  657.             return null;
  658.         }
  659.        
  660.         return this.destroyItem(process, item, count, actor, reference);
  661.     }
  662.    
  663.     /**
  664.      * Drop item from inventory and checks _adena and _ancientAdena
  665.      * @param process : String Identifier of process triggering this action
  666.      * @param item : ItemInstance to be dropped
  667.      * @param actor : L2PcInstance Player requesting the item drop
  668.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  669.      * @return ItemInstance corresponding to the destroyed item or the updated item in inventory
  670.      */
  671.     @Override
  672.     public ItemInstance dropItem(String process, ItemInstance item, L2PcInstance actor, L2Object reference)
  673.     {
  674.         item = super.dropItem(process, item, actor, reference);
  675.        
  676.         if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  677.         {
  678.             _adena = null;
  679.         }
  680.        
  681.         if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  682.         {
  683.             _ancientAdena = null;
  684.         }
  685.        
  686.         return item;
  687.     }
  688.    
  689.     /**
  690.      * Drop item from inventory by using its <B>objectID</B> and checks _adena and _ancientAdena
  691.      * @param process : String Identifier of process triggering this action
  692.      * @param objectId : int Item Instance identifier of the item to be dropped
  693.      * @param count : int Quantity of items to be dropped
  694.      * @param actor : L2PcInstance Player requesting the item drop
  695.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  696.      * @return ItemInstance corresponding to the destroyed item or the updated item in inventory
  697.      */
  698.     @Override
  699.     public ItemInstance dropItem(String process, int objectId, int count, L2PcInstance actor, L2Object reference)
  700.     {
  701.         ItemInstance item = super.dropItem(process, objectId, count, actor, reference);
  702.        
  703.         if (_adena != null && (_adena.getCount() <= 0 || _adena.getOwnerId() != getOwnerId()))
  704.         {
  705.             _adena = null;
  706.         }
  707.        
  708.         if (_ancientAdena != null && (_ancientAdena.getCount() <= 0 || _ancientAdena.getOwnerId() != getOwnerId()))
  709.         {
  710.             _ancientAdena = null;
  711.         }
  712.        
  713.         return item;
  714.     }
  715.    
  716.     /**
  717.      * <b>Overloaded</b>, when removes item from inventory, remove also owner shortcuts.
  718.      * @param item : ItemInstance to be removed from inventory
  719.      */
  720.     @Override
  721.     protected boolean removeItem(ItemInstance item)
  722.     {
  723.         // Removes any reference to the item from Shortcut bar
  724.         getOwner().removeItemFromShortCut(item.getObjectId());
  725.        
  726.         // Removes active Enchant Scroll
  727.         if (item.equals(getOwner().getActiveEnchantItem()))
  728.         {
  729.             getOwner().setActiveEnchantItem(null);
  730.         }
  731.        
  732.         if (item.getItemId() == ADENA_ID)
  733.         {
  734.             _adena = null;
  735.         }
  736.         else if (item.getItemId() == ANCIENT_ADENA_ID)
  737.         {
  738.             _ancientAdena = null;
  739.         }
  740.        
  741.         synchronized (_items)
  742.         {
  743.             if (item.isQuestItem())
  744.             {
  745.                 _questSlots--;
  746.                 if (_questSlots < 0)
  747.                 {
  748.                     _questSlots = 0;
  749.                     _log.warn(this + ": QuestInventory size < 0!");
  750.                 }
  751.             }
  752.             return super.removeItem(item);
  753.         }
  754.     }
  755.    
  756.     /**
  757.      * Refresh the weight of equipment loaded
  758.      */
  759.     @Override
  760.     public void refreshWeight()
  761.     {
  762.         super.refreshWeight();
  763.         getOwner().refreshOverloaded();
  764.     }
  765.    
  766.     /**
  767.      * Get back items in inventory from database
  768.      */
  769.     @Override
  770.     public void restore()
  771.     {
  772.         super.restore();
  773.         _adena = getItemByItemId(ADENA_ID);
  774.         _ancientAdena = getItemByItemId(ANCIENT_ADENA_ID);
  775.     }
  776.    
  777.     /**
  778.      * Method used only while logout.<br>
  779.      * For every equipped shadow item, stop associated timer.
  780.      */
  781.     public void stopShadowTimers()
  782.     {
  783.         for (ItemInstance item : getPaperdollItems())
  784.         {
  785.             if (item.isShadowItem())
  786.             {
  787.                 item.stopTimer();
  788.             }
  789.         }
  790.     }
  791.    
  792.     public static int[][] restoreVisibleInventory(int objectId)
  793.     {
  794.         int[][] paperdoll = new int[0x12][3];
  795.         try (Connection con = DatabaseFactory.getInstance().getConnection())
  796.         {
  797.             PreparedStatement statement2 = con.prepareStatement("SELECT object_id,item_id,loc_data,enchant_level FROM items WHERE owner_id=? AND loc='PAPERDOLL'");
  798.             statement2.setInt(1, objectId);
  799.             ResultSet invdata = statement2.executeQuery();
  800.            
  801.             while (invdata.next())
  802.             {
  803.                 int slot = invdata.getInt("loc_data");
  804.                 paperdoll[slot][0] = invdata.getInt("object_id");
  805.                 paperdoll[slot][1] = invdata.getInt("item_id");
  806.                 paperdoll[slot][2] = invdata.getInt("enchant_level");
  807.             }
  808.            
  809.             invdata.close();
  810.             statement2.close();
  811.         }
  812.         catch (Exception e)
  813.         {
  814.             _log.warn("Could not restore inventory: " + e.getMessage(), e);
  815.         }
  816.         return paperdoll;
  817.     }
  818.    
  819.     public boolean validateCapacity(ItemInstance item)
  820.     {
  821.         int slots = 0;
  822.        
  823.         if (!(item.isStackable() && getItemByItemId(item.getItemId()) != null) && item.getItemType() != EtcItemType.HERB)
  824.         {
  825.             slots++;
  826.         }
  827.        
  828.         return validateCapacity(slots, item.isQuestItem());
  829.     }
  830.    
  831.     public boolean validateCapacityByItemId(int ItemId)
  832.     {
  833.         int slots = 0;
  834.         Item item = ItemTable.getInstance().getTemplate(ItemId);
  835.         ItemInstance invItem = getItemByItemId(ItemId);
  836.         if (!(invItem != null && invItem.isStackable()))
  837.         {
  838.             slots++;
  839.         }
  840.        
  841.         return validateCapacity(slots, item.isQuestItem());
  842.     }
  843.    
  844.     @Override
  845.     public boolean validateCapacity(int slots)
  846.     {
  847.         return validateCapacity(slots, false);
  848.     }
  849.    
  850.     public boolean validateCapacity(int slots, boolean questItem)
  851.     {
  852.         if (!questItem)
  853.         {
  854.             return (_items.size() - _questSlots + slots <= _owner.getInventoryLimit());
  855.         }
  856.        
  857.         return _questSlots + slots <= L2PcInstance.getQuestInventoryLimit();
  858.     }
  859.    
  860.     @Override
  861.     public boolean validateWeight(int weight)
  862.     {
  863.         return (_totalWeight + weight <= _owner.getMaxLoad());
  864.     }
  865.    
  866.     /**
  867.      * Set inventory block for specified IDs<br>
  868.      * array reference is used for {@link PcInventory#_blockItems}
  869.      * @param items array of Ids to block/allow
  870.      * @param mode blocking mode {@link PcInventory#_blockMode}
  871.      */
  872.     public void setInventoryBlock(int[] items, int mode)
  873.     {
  874.         _blockMode = mode;
  875.         _blockItems = items;
  876.        
  877.         _owner.sendPacket(new ItemList(_owner, false));
  878.     }
  879.    
  880.     /**
  881.      * Unblock blocked itemIds
  882.      */
  883.     public void unblock()
  884.     {
  885.         _blockMode = -1;
  886.         _blockItems = null;
  887.        
  888.         _owner.sendPacket(new ItemList(_owner, false));
  889.     }
  890.    
  891.     /**
  892.      * Check if player inventory is in block mode.
  893.      * @return true if some itemIds blocked
  894.      */
  895.     public boolean hasInventoryBlock()
  896.     {
  897.         return (_blockMode > -1 && _blockItems != null && _blockItems.length > 0);
  898.     }
  899.    
  900.     /**
  901.      * Return block mode
  902.      * @return int {@link PcInventory#_blockMode}
  903.      */
  904.     public int getBlockMode()
  905.     {
  906.         return _blockMode;
  907.     }
  908.    
  909.     /**
  910.      * Return TIntArrayList with blocked item ids
  911.      * @return TIntArrayList
  912.      */
  913.     public int[] getBlockItems()
  914.     {
  915.         return _blockItems;
  916.     }
  917.    
  918.     /**
  919.      * Check if player can use item by itemid
  920.      * @param itemId int
  921.      * @return true if can use
  922.      */
  923.     public boolean canManipulateWithItemId(int itemId)
  924.     {
  925.         if ((_blockMode == 0 && Util.contains(_blockItems, itemId)) || (_blockMode == 1 && !Util.contains(_blockItems, itemId)))
  926.         {
  927.             return false;
  928.         }
  929.         return true;
  930.     }
  931.    
  932.     @Override
  933.     protected void addItem(ItemInstance item)
  934.     {
  935.         synchronized (_items)
  936.         {
  937.             if (item.isQuestItem())
  938.             {
  939.                 _questSlots++;
  940.             }
  941.            
  942.             super.addItem(item);
  943.         }
  944.     }
  945.    
  946.     public int getSize(boolean quest)
  947.     {
  948.         if (quest)
  949.         {
  950.             return _questSlots;
  951.         }
  952.        
  953.         return getSize() - _questSlots;
  954.     }
  955.    
  956.     @Override
  957.     public String toString()
  958.     {
  959.         return getClass().getSimpleName() + "[" + _owner + "]";
  960.     }
  961. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement