/*
* Copyright (C) 2004-2015 L2J DataPack
*
* This file is part of L2J DataPack.
*
* L2J DataPack 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.
*
* L2J DataPack 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 <http://www.gnu.org/licenses/>.
*/
package l2j.scripts.handlers.itemhandlers;
import java.util.List;
import l2j.commons.util.Rnd;
import l2j.gameserver.enums.ItemSkillType;
import l2j.gameserver.enums.ShotType;
import l2j.gameserver.handler.IItemHandler;
import l2j.gameserver.handler.ItemHandler;
import l2j.gameserver.model.actor.Playable;
import l2j.gameserver.model.actor.instance.PlayerInstance;
import l2j.gameserver.model.holders.ItemSkillHolder;
import l2j.gameserver.model.items.Weapon;
import l2j.gameserver.model.items.instance.ItemInstance;
import l2j.gameserver.model.items.type.ActionType;
import l2j.gameserver.network.client.send.MagicSkillUse;
import l2j.gameserver.network.client.send.string.SystemMessageId;
import l2j.gameserver.scripting.annotations.GameScript;
import l2j.gameserver.util.Broadcast;
public class SoulShots implements IItemHandler
{
@Override
public boolean useItem(Playable playable, ItemInstance item, boolean forceUse)
{
if (!playable.isPlayer())
{
playable.sendPacket(SystemMessageId.YOUR_PET_CANNOT_CARRY_THIS_ITEM);
return false;
}
final PlayerInstance activeChar = playable.getActingPlayer();
final ItemInstance weaponInst = activeChar.getActiveWeaponInstance();
final Weapon weaponItem = activeChar.getActiveWeaponItem();
final List<ItemSkillHolder> skills = item.getItem().getSkills(ItemSkillType.NORMAL);
if (skills == null)
{
LOGGER.warn("Item skill list is empty!");
return false;
}
final int itemId = item.getId();
// Check if Soul shot can be used
if ((weaponInst == null) || (weaponItem.getSoulShotCount() == 0))
{
if (!activeChar.getAutoSoulShot().contains(itemId))
{
activeChar.sendPacket(SystemMessageId.CANNOT_USE_SOULSHOTS);
}
return false;
}
boolean gradeCheck = item.isEtcItem() && (item.getEtcItem().getDefaultAction() == ActionType.SOULSHOT) && (weaponInst.getItem().getCrystalTypePlus() == item.getItem().getCrystalTypePlus());
if (!gradeCheck)
{
if (!activeChar.getAutoSoulShot().contains(itemId))
{
activeChar.sendPacket(SystemMessageId.THE_SOULSHOT_YOU_ARE_ATTEMPTING_TO_USE_DOES_NOT_MATCH_THE_GRADE_OF_YOUR_EQUIPPED_WEAPON);
}
return false;
}
activeChar.soulShotLock.lock();
try
{
// Check if Soul shot is already active
if (activeChar.isChargedShot(ShotType.SOULSHOTS))
{
return false;
}
// Consume Soul shots if player has enough of them
int SSCount = weaponItem.getSoulShotCount();
if ((weaponItem.getReducedSoulShot() > 0) && (Rnd.get(100) < weaponItem.getReducedSoulShotChance()))
{
SSCount = weaponItem.getReducedSoulShot();
}
if (!activeChar.destroyItemWithoutTrace("Consume", item.getObjectId(), SSCount, null, false))
{
if (!activeChar.disableAutoShot(itemId))
{
activeChar.sendPacket(SystemMessageId.YOU_DO_NOT_HAVE_ENOUGH_SOULSHOTS_FOR_THAT);
}
return false;
}
// Charge soul shot
activeChar.chargeShot(ShotType.SOULSHOTS);
}
finally
{
activeChar.soulShotLock.unlock();
}
// Send message to client
activeChar.sendPacket(SystemMessageId.YOUR_SOULSHOTS_ARE_ENABLED);
skills.forEach(holder -> Broadcast.toSelfAndKnownPlayersInRadius(activeChar, new MagicSkillUse(activeChar, activeChar, holder.getSkillId(), holder.getSkillLevel(), 0, 0), 600));
return true;
}
@GameScript
public static void main()
{
ItemHandler.getInstance().registerHandler(new SoulShots());
}
}