/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package net.sf.l2j.gameserver.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogRecord;
import net.sf.l2j.Config;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.ItemTable;
import net.sf.l2j.gameserver.ai.CtrlIntention;
import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.actor.knownlist.NullKnownList;
import net.sf.l2j.gameserver.serverpackets.ActionFailed;
import net.sf.l2j.gameserver.skills.Func;
import net.sf.l2j.gameserver.templates.L2Armor;
import net.sf.l2j.gameserver.templates.L2EtcItem;
import net.sf.l2j.gameserver.templates.L2Item;
/**
* This class manages items.
*
* @version $Revision: 1.4.2.1.2.11 $ $Date: 2005/03/31 16:07:50 $
*/
public final class L2ItemInstance extends L2Object
{
private static final Logger _log = Logger.getLogger(L2ItemInstance.class.getName());
private static final Logger _logItems = Logger.getLogger("item");
/** Enumeration of locations for item */
public static enum ItemLocation {
VOID,
INVENTORY,
PAPERDOLL,
WAREHOUSE,
CLANWH,
PET,
PET_EQUIP,
LEASE,
FREIGHT
}
/** ID of the owner */
private int _owner_id;
/** Quantity of the item */
private int _count;
/** ID of the item */
private final int _itemId;
/** Object L2Item associated to the item */
private final L2Item _item;
/** Location of the item : Inventory, PaperDoll, WareHouse */
private ItemLocation _loc;
/** Slot where item is stored */
private int _loc_data;
/** Level of enchantment of the item */
private int _enchantLevel;
/** Price of the item for selling */
private int _price_sell;
/** Price of the item for buying */
private int _price_buy;
/** Wear Item */
private boolean _wear;
/** Custom item types (used loto, race tickets) */
private int _type1;
private int _type2;
/** Serial do item */
private int _serial;
private long _dropTime;
public static final int CHARGED_NONE = 0;
public static final int CHARGED_SOULSHOT = 1;
public static final int CHARGED_SPIRITSHOT = 1;
public static final int CHARGED_BLESSED_SOULSHOT = 2; // It's a realy exists? ;-)
public static final int CHARGED_BLESSED_SPIRITSHOT = 2;
/** Item charged with SoulShot (type of SoulShot) */
private int _chargedSoulshot = CHARGED_NONE;
/** Item charged with SpiritShot (type of SpiritShot) */
private int _chargedSpiritshot = CHARGED_NONE;
private boolean _chargedFishtshot = false;
private boolean _protected;
public static final int UNCHANGED = 0;
public static final int ADDED = 1;
public static final int REMOVED = 3;
public static final int MODIFIED = 2;
private int _lastChange = 2; //1 ??, 2 modified, 3 removed
private boolean _existsInDb; // if a record exists in DB.
private boolean _storedInDb; // if DB data is up-to-date.
private ScheduledFuture itemLootShedule = null;
/**
* Constructor of the L2ItemInstance from the objectId and the itemId.
* @param objectId : int designating the ID of the object in the world
* @param itemId : int designating the ID of the item
*/
public L2ItemInstance(int objectId, int itemId)
{
super(objectId);
super.setKnownList(new NullKnownList(new L2ItemInstance[] {this}));
_itemId = itemId;
_item = ItemTable.getInstance().getTemplate(itemId);
if (_itemId == 0 || _item == null)
throw new IllegalArgumentException();
_count = 1;
_loc = ItemLocation.VOID;
_type1 = 0;
_type2 = 0;
_dropTime = 0;
}
/**
* Constructor of the L2ItemInstance from the objetId and the description of the item given by the L2Item.
* @param objectId : int designating the ID of the object in the world
* @param item : L2Item containing informations of the item
*/
public L2ItemInstance(int objectId, L2Item item)
{
super(objectId);
super.setKnownList(new NullKnownList(new L2ItemInstance[] {this}));
_itemId = item.getItemId();
_item = item;
if (_itemId == 0 || _item == null)
throw new IllegalArgumentException();
_count = 1;
_loc = ItemLocation.VOID;
}
/**
* Sets the ownerID of the item
* @param process : String Identifier of process triggering this action
* @param owner_id : int designating the ID of the owner
* @param creator : L2PcInstance Player requesting the item creation
* @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
*/
public void setOwnerId(String process, int owner_id, L2PcInstance creator, L2Object reference)
{
setOwnerId(owner_id);
if (Config.LOG_ITEMS)
{
LogRecord record = new LogRecord(Level.INFO, "CHANGE:" + process);
record.setLoggerName("item");
record.setParameters(new Object[]{this, creator, reference});
_logItems.log(record);
}
}
/**
* Sets the ownerID of the item
* @param owner_id : int designating the ID of the owner
*/
public void setOwnerId(int owner_id)
{
if (owner_id == _owner_id) return;
_owner_id = owner_id;
_storedInDb = false;
}
/**
* Returns the ownerID of the item
* @return int : ownerID of the item
*/
public int getOwnerId()
{
return _owner_id;
}
/**
* Sets the location of the item
* @param loc : ItemLocation (enumeration)
*/
public void setLocation(ItemLocation loc)
{
setLocation(loc, 0);
}
/**
* Sets the location of the item.
* Remark : If loc and loc_data different from database, say datas not up-to-date
* @param loc : ItemLocation (enumeration)
* @param loc_data : int designating the slot where the item is stored or the village for freights
*/
public void setLocation(ItemLocation loc, int loc_data)
{
if (loc == _loc && loc_data == _loc_data)
return;
_loc = loc;
_loc_data = loc_data;
_storedInDb = false;
}
public ItemLocation getLocation()
{
return _loc;
}
/**
* Returns the quantity of item
* @return int
*/
public int getCount()
{
return _count;
}
/**
* Sets the quantity of the item.
* Remark : If loc and loc_data different from database, say datas not up-to-date
* @param process : String Identifier of process triggering this action
* @param count : int
* @param creator : L2PcInstance Player requesting the item creation
* @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
*/
public void changeCount(String process, int count, L2PcInstance creator, L2Object reference)
{
if (count == 0) return;
_count += count;
if (_count < 0) _count = 0;
_storedInDb = false;
if (Config.LOG_ITEMS)
{
LogRecord record = new LogRecord(Level.INFO, "CHANGE:" + process);
record.setLoggerName("item");
record.setParameters(new Object[]{this, creator, reference});
_logItems.log(record);
}
}
/**
* Sets the quantity of the item.
* Remark : If loc and loc_data different from database, say datas not up-to-date
* @param count : int
*/
public void setCount(int count)
{
if (_count == count) return;
_count = count > 0 ? count : 0;
_storedInDb = false;
}
/**
* Returns if item is equipable
* @return boolean
*/
public boolean isEquipable()
{
return !(_item.getBodyPart() == 0 || _item instanceof L2EtcItem );
}
/**
* Returns if item is equipped
* @return boolean
*/
public boolean isEquipped()
{
return _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP;
}
/**
* Returns the slot where the item is stored
* @return int
*/
public int getEquipSlot()
{
if (Config.ASSERT) assert _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP || _loc == ItemLocation.FREIGHT;
return _loc_data;
}
/**
* Returns the characteristics of the item
* @return L2Item
*/
public L2Item getItem()
{
return _item;
}
public int getCustomType1()
{
return _type1;
}
public int getCustomType2()
{
return _type2;
}
public void setCustomType1(int newtype)
{
_type1=newtype;
}
public void setCustomType2(int newtype)
{
_type2=newtype;
}
public void setDropTime(long time)
{
_dropTime=time;
}
public long getDropTime()
{
return _dropTime;
}
public boolean isWear()
{
return _wear;
}
public void setWear(boolean newwear)
{
_wear=newwear;
}
/**
* Returns the type of item
* @return Enum
*/
public Enum getItemType()
{
return _item.getItemType();
}
/**
* Returns the ID of the item
* @return int
*/
public int getItemId()
{
return _itemId;
}
/**
* Returns the quantity of crystals for crystallization
* @return int
*/
public final int getCrystalCount()
{
return _item.getCrystalCount(_enchantLevel);
}
/**
* Returns the reference price of the item
* @return int
*/
public int getReferencePrice()
{
return _item.getReferencePrice();
}
/**
* Returns the name of the item
* @return String
*/
public String getItemName()
{
return _item.getName();
}
/**
* Returns the price of the item for selling
* @return int
*/
public int getPriceToSell()
{
return (isConsumable() ? (int)(_price_sell * Config.RATE_CONSUMABLE_COST) : _price_sell);
}
/**
* Sets the price of the item for selling
* Remark : If loc and loc_data different from database, say datas not up-to-date
* @param price : int designating the price
*/
public void setPriceToSell(int price)
{
_price_sell = price;
_storedInDb = false;
}
/**
* Returns the price of the item for buying
* @return int
*/
public int getPriceToBuy()
{
return (isConsumable() ? (int)(_price_buy * Config.RATE_CONSUMABLE_COST) : _price_buy);
}
/**
* Sets the price of the item for buying
* Remark : If loc and loc_data different from database, say datas not up-to-date
* @param price : int
*/
public void setPriceToBuy(int price)
{
_price_buy = price;
_storedInDb = false;
}
/**
* Returns the last change of the item
* @return int
*/
public int getLastChange()
{
return _lastChange;
}
/**
* Sets the last change of the item
* @param lastChange : int
*/
public void setLastChange(int lastChange)
{
_lastChange = lastChange;
}
/**
* Returns if item is stackable
* @return boolean
*/
public boolean isStackable()
{
return _item.isStackable();
}
/**
* Returns if item is consumable
* @return boolean
*/
public boolean isConsumable()
{
return _item.isConsumable();
}
/**
* Returns if item is available for manipulation
* @return boolean
*/
public boolean isAvailable(L2PcInstance player, boolean allowAdena)
{
return ((!isEquipped()) // Not equipped
&& (getItem().getType2() != 3) // Not Quest Item
&& (getItem().getType2() != 4 || getItem().getType1() != 1) // TODO: what does this mean?
&& (player.getPet() == null || getObjectId() != player.getPet().getControlItemId()) // Not Control item of currently summoned pet
&& (player.getActiveEnchantItem() != this) // Not momentarily used enchant scroll
&& (allowAdena || getItemId() != 57)
&& (player.getCurrentSkill() == null || player.getCurrentSkill().getSkill().getItemConsumeId() != getItemId())
);
}
/* (non-Javadoc)
* @see net.sf.l2j.gameserver.model.L2Object#onAction(net.sf.l2j.gameserver.model.L2PcInstance)
* also check constraints: only soloing castle owners may pick up mercenary tickets of their castle
*/
public void onAction(L2PcInstance player)
{
// this causes the validate position handler to do the pickup if the location is reached.
// mercenary tickets can only be picked up by the castle owner.
if (
(_itemId >=3960 && _itemId<=4021 && player.isInParty()) ||
(_itemId >=3960 && _itemId<=3969 && !player.isCastleLord(1)) ||
(_itemId >=3973 && _itemId<=3982 && !player.isCastleLord(2)) ||
(_itemId >=3986 && _itemId<=3995 && !player.isCastleLord(3)) ||
(_itemId >=3999 && _itemId<=4008 && !player.isCastleLord(4)) ||
(_itemId >=4012 && _itemId<=4021 && !player.isCastleLord(5))
)
{
if (player.isInParty()) //do not allow owner who is in party to pick tickets up
player.sendMessage("You cannot pickup mercenaries while in a party.");
else
player.sendMessage("Only the castle lord can pickup mercenaries.");
player.setTarget(this);
player.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
// Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
player.sendPacket(new ActionFailed());
}
else
player.getAI().setIntention(CtrlIntention.AI_INTENTION_PICK_UP, this);
}
/**
* Returns the level of enchantment of the item
* @return int
*/
public int getEnchantLevel()
{
return _enchantLevel;
}
/**
* Sets the level of enchantment of the item
* @param int
*/
public void setEnchantLevel(int enchantLevel)
{
if (_enchantLevel == enchantLevel)
return;
_enchantLevel = enchantLevel;
_storedInDb = false;
}
/**
* Returns the physical defense of the item
* @return int
*/
public int getPDef()
{
if (_item instanceof L2Armor)
return ((L2Armor)_item).getPDef();
return 0;
}
/**
* Returns false cause item can't be attacked
* @return boolean false
*/
public boolean isAutoAttackable(@SuppressWarnings("unused") L2Character attacker)
{
return false;
}
/**
* Returns the type of charge with SoulShot of the item.
* @return int (CHARGED_NONE, CHARGED_SOULSHOT)
*/
public int getChargedSoulshot()
{
return _chargedSoulshot;
}
/**
* Returns the type of charge with SpiritShot of the item
* @return int (CHARGED_NONE, CHARGED_SPIRITSHOT, CHARGED_BLESSED_SPIRITSHOT)
*/
public int getChargedSpiritshot()
{
return _chargedSpiritshot;
}
public boolean getChargedFishshot()
{
return _chargedFishtshot;
}
/**
* Sets the type of charge with SoulShot of the item
* @param type : int (CHARGED_NONE, CHARGED_SOULSHOT)
*/
public void setChargedSoulshot(int type)
{
_chargedSoulshot = type;
}
/**
* Sets the type of charge with SpiritShot of the item
* @param type : int (CHARGED_NONE, CHARGED_SPIRITSHOT, CHARGED_BLESSED_SPIRITSHOT)
*/
public void setChargedSpiritshot(int type)
{
_chargedSpiritshot = type;
}
public void setChargedFishshot(boolean type)
{
_chargedFishtshot = type;
}
/**
* This function basically returns a set of functions from
* L2Item/L2Armor/L2Weapon, but may add additional
* functions, if this particular item instance is enhanched
* for a particular player.
* @param player : L2Character designating the player
* @return Func[]
*/
public Func[] getStatFuncs(L2Character player)
{
return getItem().getStatFuncs(this, player);
}
/**
* Updates database.
* Concept :
*
* IF the item exists in database :
*