/*
* 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 3 of the License, 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, see
* 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 == _locData)
return;
_loc = loc;
_locData = loc_data;
_storedInDb = false;
}
public ItemLocation getLocation()
{
return _loc;
}
/**
* Sets the quantity of the item.
* @param count the new count to set
*/
public void setCount(int count)
{
if (getCount() == count)
{
return;
}
_count = count >= -1 ? count : 0;
_storedInDb = false;
}
/**
* @return Returns the count.
*/
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;
}
if ( count > 0 && getCount() > Integer.MAX_VALUE - count)
{
setCount(Integer.MAX_VALUE);
}
else
{
setCount(getCount() + count);
}
if (getCount() < 0)
{
setCount(0);
}
_storedInDb = false;
if (Config.LOG_ITEMS && process != null)
{
LogRecord record = new LogRecord(Level.INFO, "CHANGE:" + process);
record.setLoggerName("item");
record.setParameters(new Object[]{this, creator, reference});
_logItems.log(record);
}
if (creator != null)
{
if (creator.isGM())
{
String referenceName = "no-reference";
if (reference != null)
{
referenceName = (reference.getName() != null?reference.getName():"no-name");
}
String targetName = (creator.getTarget() != null?creator.getTarget().getName():"no-target");
GMAudit.auditGMAction(creator.getName(), process + "(id: "+getItemId()+" objId: "+getObjectId()+" name: "+getName()+" count: "+count+")", targetName, "L2Object referencing this action is: " + referenceName);
}
}
}
// No logging (function designed for shots only)
public void changeCountWithoutTrace(int count, L2PcInstance creator, L2Object reference)
{
this.changeCount(null, count, creator, reference);
}
/**
* 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 getLocationSlot()
{
if (Config.ASSERT) assert _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP || _loc == ItemLocation.FREIGHT || _loc == ItemLocation.INVENTORY;
return _locData;
}
/**
* 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
*/
@SuppressWarnings("unchecked")
public Enum getItemType()
{
return _item.getItemType();
}
/**
* Returns the ID of the item
* @return int
*/
public int getItemId()
{
return _itemId;
}
/**
* Returns true if item is an EtcItem
* @return boolean
*/
public boolean isEtcItem()
{
return (_item instanceof L2EtcItem);
}
/**
* Returns true if item is a Weapon/Shield
* @return boolean
*/
public boolean isWeapon()
{
return (_item instanceof L2Weapon);
}
/**
* Returns true if item is an Armor
* @return boolean
*/
public boolean isArmor()
{
return (_item instanceof L2Armor);
}
/**
* Returns the characteristics of the L2EtcItem
* @return L2EtcItem
*/
public L2EtcItem getEtcItem()
{
if (_item instanceof L2EtcItem)
{
return (L2EtcItem) _item;
}
return null;
}
/**
* Returns the characteristics of the L2Weapon
* @return L2Weapon
*/
public L2Weapon getWeaponItem()
{
if (_item instanceof L2Weapon)
{
return (L2Weapon) _item;
}
return null;
}
/**
* Returns the characteristics of the L2Armor
* @return L2Armor
*/
public L2Armor getArmorItem()
{
if (_item instanceof L2Armor)
{
return (L2Armor) _item;
}
return null;
}
/**
* 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 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 dropable
* @return boolean
*/
public boolean isDropable()
{
return isAugmented() ? false : _item.isDropable();
}
/**
* Returns if item is destroyable
* @return boolean
*/
public boolean isDestroyable()
{
return _item.isDestroyable();
}
/**
* Returns if item is tradeable
* @return boolean
*/
public boolean isTradeable()
{
return isAugmented() ? false : _item.isTradeable();
}
/**
* Returns if item is consumable
* @return boolean
*/
public boolean isConsumable()
{
return _item.isConsumable();
}
public boolean isHeroItem()
{
return ((_itemId >= 6611 && _itemId <= 6621) || (_itemId >= 9388 && _itemId <= 9390) || _itemId == 6842);
}
public boolean isOlyRestrictedItem()
{
return Config.LIST_OLY_RESTRICTED_ITEMS.contains(_itemId);
}
/**
* 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) // Not adena
&& (player.getCurrentSkill() == null || player.getCurrentSkill().getSkill().getItemConsumeId() != getItemId())
&& (isTradeable())
);
}
/* (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
*/
@Override
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.
int castleId = MercTicketManager.getInstance().getTicketCastleId(_itemId);
if (castleId > 0 &&
(!player.isCastleLord(castleId) || player.isInParty()))
{
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(ActionFailed.STATIC_PACKET);
}
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 whether this item is augmented or not
* @return true if augmented
*/
public boolean isAugmented()
{
return _augmentation == null ? false : true;
}
/**
* Returns the augmentation object for this item
* @return augmentation
*/
public L2Augmentation getAugmentation()
{
return _augmentation;
}
/**
* Sets a new augmentation
* @param augmentation
* @return return true if sucessfull
*/
public boolean setAugmentation(L2Augmentation augmentation)
{
// there shall be no previous augmentation..
if (_augmentation != null) return false;
_augmentation = augmentation;
return true;
}
/**
* Remove the augmentation
*
*/
public void removeAugmentation()
{
if (_augmentation == null) return;
_augmentation.deleteAugmentationData();
_augmentation = null;
}
/**
* Used to decrease mana
* (mana means life time for shadow items)
*/
public class ScheduleConsumeManaTask implements Runnable
{
private L2ItemInstance _shadowItem;
public ScheduleConsumeManaTask(L2ItemInstance item)
{
_shadowItem = item;
}
public void run()
{
try
{
// decrease mana
if (_shadowItem != null) _shadowItem.decreaseMana(true);
}
catch (Throwable t)
{
}
}
}
/**
* Returns true if this item is a shadow item
* Shadow items have a limited life-time
* @return
*/
public boolean isShadowItem()
{
return (_mana >= 0);
}
/**
* Sets the mana for this shadow item
* NOTE: does not send an inventory update packet
* @param mana
*/
public void setMana(int mana)
{
_mana = mana;
}
/**
* Returns the remaining mana of this shadow item
* @return lifeTime
*/
public int getMana()
{
return _mana;
}
/**
* Decreases the mana of this shadow item,
* sends a inventory update
* schedules a new consumption task if non is running
* optionally one could force a new task
* @param forces a new consumption task if item is equipped
*/
public void decreaseMana(boolean resetConsumingMana)
{
if (!isShadowItem()) return;
if (_mana > 0) _mana--;
if (_storedInDb) _storedInDb = false;
if (resetConsumingMana) _consumingMana = false;
L2PcInstance player = ((L2PcInstance)L2World.getInstance().findObject(getOwnerId()));
if (player != null && !player.isDead())
{
SystemMessage sm;
switch (_mana)
{
case 10:
sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_10);
sm.addString(getItemName());
player.sendPacket(sm);
break;
case 5:
sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_5);
sm.addString(getItemName());
player.sendPacket(sm);
break;
case 1:
sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_1);
sm.addString(getItemName());
player.sendPacket(sm);
break;
}
if (_mana == 0) // The life time has expired
{
sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_0);
sm.addString(getItemName());
player.sendPacket(sm);
// unequip
if (isEquipped())
{
L2ItemInstance[] unequiped = player.getInventory().unEquipItemInSlotAndRecord(getLocationSlot());
InventoryUpdate iu = new InventoryUpdate();
for (int i = 0; i < unequiped.length; i++)
{
player.checkSSMatch(null, unequiped[i]);
iu.addModifiedItem(unequiped[i]);
}
player.sendPacket(iu);
}
if (getLocation() != ItemLocation.WAREHOUSE)
{
// destroy
player.getInventory().destroyItem("L2ItemInstance", this, player, null);
// send update
InventoryUpdate iu = new InventoryUpdate();
iu.addRemovedItem(this);
player.sendPacket(iu);
StatusUpdate su = new StatusUpdate(player.getObjectId());
su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
player.sendPacket(su);
}
else
{
player.getWarehouse().destroyItem("L2ItemInstance", this, player, null);
}
// delete from world
L2World.getInstance().removeObject(this);
}
else
{
// Reschedule if still equipped
if (!_consumingMana && isEquipped())
{
scheduleConsumeManaTask();
}
if (getLocation() != ItemLocation.WAREHOUSE)
{
InventoryUpdate iu = new InventoryUpdate();
iu.addModifiedItem(this);
player.sendPacket(iu);
}
}
}
}
private void scheduleConsumeManaTask()
{
_consumingMana = true;
ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleConsumeManaTask(this), MANA_CONSUMPTION_RATE);
}
/**
* Returns false cause item can't be attacked
* @return boolean false
*/
@Override
public boolean isAutoAttackable(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 the database.
*/
public void updateDatabase()
{
this.updateDatabase(false);
}
/**
* Updates the database.
*
* @param force if the update should necessarilly be done.
*/
public void updateDatabase(boolean force)
{
if (isWear()) //avoid saving weared items
{
return;
}
if (_existsInDb)
{
if (_ownerId == 0 || _loc == ItemLocation.VOID || (getCount() == 0 && _loc != ItemLocation.LEASE))
{
removeFromDb();
}
else if (!Config.LAZY_ITEMS_UPDATE || force)
{
updateInDb();
}
}
else
{
if (getCount() == 0 && _loc != ItemLocation.LEASE)
{
return;
}
if (_loc == ItemLocation.VOID || _loc == ItemLocation.NPC || _ownerId == 0)
{
return;
}
insertIntoDb();
}
}
/**
* Returns a L2ItemInstance stored in database from its objectID
* @param objectId : int designating the objectID of the item
* @return L2ItemInstance
*/
public static L2ItemInstance restoreFromDb(int ownerId, ResultSet rs)
{
L2ItemInstance inst = null;
int objectId, item_id, count, loc_data, enchant_level, custom_type1, custom_type2, manaLeft;
ItemLocation loc;
try
{
objectId = rs.getInt(1);
item_id = rs.getInt("item_id");
count = rs.getInt("count");
loc = ItemLocation.valueOf(rs.getString("loc"));
loc_data = rs.getInt("loc_data");
enchant_level = rs.getInt("enchant_level");
custom_type1 = rs.getInt("custom_type1");
custom_type2 = rs.getInt("custom_type2");
manaLeft = rs.getInt("mana_left");
} catch (Exception e) {
_log.log(Level.SEVERE, "Could not restore an item owned by "+ownerId+" from DB:", e);
return null;
}
L2Item item = ItemTable.getInstance().getTemplate(item_id);
if (item == null) {
_log.severe("Item item_id="+item_id+" not known, object_id="+objectId);
return null;
}
inst = new L2ItemInstance(objectId, item);
inst._ownerId = ownerId;
inst.setCount(count);
inst._enchantLevel = enchant_level;
inst._type1 = custom_type1;
inst._type2 = custom_type2;
inst._loc = loc;
inst._locData = loc_data;
inst._existsInDb = true;
inst._storedInDb = true;
// Setup life time for shadow weapons
inst._mana = manaLeft;
// consume 1 mana
if (inst._mana > 0 && inst.getLocation() == ItemLocation.PAPERDOLL)
inst.decreaseMana(false);
// if mana left is 0 delete this item
if (inst._mana == 0)
{
inst.removeFromDb();
return null;
}
else if (inst._mana > 0 && inst.getLocation() == ItemLocation.PAPERDOLL)
inst.scheduleConsumeManaTask();
//load augmentation
if (inst.isEquipable())
{
java.sql.Connection con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT attributes,skill,level FROM augmentations WHERE item_id=?");
statement.setInt(1, objectId);
rs = statement.executeQuery();
if (rs.next())
{
inst._augmentation = new L2Augmentation(inst, rs.getInt("attributes"), rs.getInt("skill"), rs.getInt("level"), false);
}
rs.close();
statement.close();
}
catch (Exception e)
{
_log.log(Level.SEVERE, "Could not restore augmentation for item "+objectId+" from DB: "+e.getMessage(), e);
}
finally
{
try { con.close(); } catch (Exception e) {}
}
}
return inst;
}
/**
* Init a dropped L2ItemInstance and add it in the world as a visible object.
*
* Actions :
*