package l2p.gameserver.clientpackets;
import l2p.extensions.scripts.Functions;
import l2p.gameserver.cache.Msg;
import l2p.gameserver.model.L2Player;
import l2p.gameserver.model.L2ShortCut;
import l2p.gameserver.model.L2Skill;
import l2p.gameserver.model.base.L2EnchantSkillLearn;
import l2p.gameserver.serverpackets.ExEnchantSkillInfo;
import l2p.gameserver.serverpackets.ExEnchantSkillResult;
import l2p.gameserver.serverpackets.ShortCutRegister;
import l2p.gameserver.serverpackets.SkillList;
import l2p.gameserver.serverpackets.SystemMessage;
import l2p.gameserver.tables.SkillTable;
import l2p.gameserver.tables.SkillTreeTable;
import l2p.util.Log;
import l2p.util.Rnd;
/**
* Format (ch) dd
*/
public final class RequestExEnchantSkillSafe extends L2GameClientPacket
{
private int _skillId;
private int _skillLvl;
@Override
protected void readImpl()
{
_skillId = readD();
_skillLvl = readD();
}
@Override
public void runImpl()
{
L2Player activeChar = getClient().getActiveChar();
if(activeChar == null)
{
return;
}
if(activeChar.getTransformation() != 0)
{
activeChar.sendMessage("You must leave transformation mode first.");
return;
}
if(activeChar.getLevel() < 76 || activeChar.getClassId().getLevel() < 4)
{
activeChar.sendMessage("You must have 3rd class change quest completed.");
return;
}
L2EnchantSkillLearn sl = SkillTreeTable.getSkillEnchant(_skillId, _skillLvl);
if(sl == null)
{
return;
}
short slevel = activeChar.getSkillLevel(_skillId);
if(slevel == -1)
{
return;
}
int enchantLevel = SkillTreeTable.convertEnchantLevel(sl.getBaseLevel(), _skillLvl, sl.getMaxLevel());
// already knows the skill with this level
if(slevel >= enchantLevel)
{
return;
}
// Можем ли мы перейти с текущего уровня скилла на данную заточку
if(slevel == sl.getBaseLevel() ? _skillLvl % 100 != 1 : slevel != enchantLevel - 1)
{
activeChar.sendMessage("Incorrect enchant level.");
return;
}
L2Skill skill = SkillTable.getInstance().getInfo(_skillId, enchantLevel);
if(skill == null)
{
return;
}
int[] cost = sl.getCost();
int requiredSp = cost[1] * SkillTreeTable.SAFE_ENCHANT_COST_MULTIPLIER * sl.getCostMult();
int requiredAdena = cost[0] * SkillTreeTable.SAFE_ENCHANT_COST_MULTIPLIER * sl.getCostMult();
int rate = sl.getRate(activeChar);
if(activeChar.getSp() < requiredSp)
{
sendPacket(Msg.SP_REQUIRED_FOR_SKILL_ENCHANT_IS_INSUFFICIENT);
return;
}
if(activeChar.getAdena() < requiredAdena)
{
sendPacket(Msg.YOU_DO_NOT_HAVE_ENOUGH_ADENA);
return;
}
if(Functions.getItemCount(activeChar, SkillTreeTable.SAFE_ENCHANT_BOOK) == 0)
{
activeChar.sendPacket(Msg.ITEMS_REQUIRED_FOR_SKILL_ENCHANT_ARE_INSUFFICIENT);
return;
}
Functions.removeItem(activeChar, SkillTreeTable.SAFE_ENCHANT_BOOK, 1);
if(Rnd.chance(rate))
{
activeChar.addSkill(skill, true);
activeChar.addExpAndSp(0, -1 * requiredSp, false, false);
Functions.removeItem(activeChar, 57, requiredAdena);
activeChar.sendPacket(new SystemMessage(SystemMessage.SP_HAS_DECREASED_BY_S1).addNumber(requiredSp), new SystemMessage(SystemMessage.SUCCEEDED_IN_ENCHANTING_SKILL_S1).addSkillName(_skillId, _skillLvl), new ExEnchantSkillResult(1));
activeChar.sendPacket(new SkillList(activeChar));
updateSkillShortcuts(activeChar);
Log.add(activeChar.getName() + "|Successfully safe enchanted|" + _skillId + "|to+" + _skillLvl + "|" + rate, "enchant_skills");
}
else
{
activeChar.sendPacket(new SystemMessage(SystemMessage.Skill_enchant_failed_Current_level_of_enchant_skill_S1_will_remain_unchanged).addSkillName(_skillId, _skillLvl), new ExEnchantSkillResult(0));
Log.add(activeChar.getName() + "|Failed to safe enchant|" + _skillId + "|to+" + _skillLvl + "|" + rate, "enchant_skills");
}
activeChar.sendPacket(new ExEnchantSkillInfo(_skillId, activeChar.getSkillDisplayLevel(_skillId)));
}
private void updateSkillShortcuts(L2Player player)
{
// update all the shortcuts to this skill
for(L2ShortCut sc : player.getAllShortCuts())
{
if(sc.id == _skillId && sc.type == L2ShortCut.TYPE_SKILL)
{
L2ShortCut newsc = new L2ShortCut(sc.slot, sc.page, sc.type, sc.id, _skillLvl);
player.sendPacket(new ShortCutRegister(newsc));
player.registerShortCut(newsc);
}
}
}
}