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;
public final class RequestExEnchantSkillUntrain extends L2GameClientPacket
{
private int _skillId;
private int _skillLvl;
@Override
protected void readImpl()
{
_skillId = readD();
_skillLvl = readD();
}
@Override
protected 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;
}
short oldSkillLevel = activeChar.getSkillDisplayLevel(_skillId);
if(oldSkillLevel == -1)
{
return;
}
L2EnchantSkillLearn sl = SkillTreeTable.getSkillEnchant(_skillId, oldSkillLevel);
if(sl == null)
{
return;
}
L2Skill newSkill;
if(_skillLvl % 100 == 0)
{
_skillLvl = sl.getBaseLevel();
newSkill = SkillTable.getInstance().getInfo(_skillId, _skillLvl);
}
else
{
newSkill = SkillTable.getInstance().getInfo(_skillId, SkillTreeTable.convertEnchantLevel(sl.getBaseLevel(), _skillLvl, sl.getMaxLevel()));
}
if(newSkill == null)
{
return;
}
if(Functions.getItemCount(activeChar, SkillTreeTable.UNTRAIN_ENCHANT_BOOK) == 0)
{
activeChar.sendPacket(Msg.ITEMS_REQUIRED_FOR_SKILL_ENCHANT_ARE_INSUFFICIENT);
return;
}
Functions.removeItem(activeChar, SkillTreeTable.UNTRAIN_ENCHANT_BOOK, 1);
activeChar.addExpAndSp(0, sl.getCost()[1] * sl.getCostMult(), false, false);
activeChar.addSkill(newSkill, true);
if(_skillLvl > 100)
{
SystemMessage sm = new SystemMessage(SystemMessage.Untrain_of_enchant_skill_was_successful_Current_level_of_enchant_skill_S1_has_been_decreased_by_1);
sm.addSkillName(_skillId, _skillLvl);
activeChar.sendPacket(sm);
}
else
{
SystemMessage sm = new SystemMessage(SystemMessage.Untrain_of_enchant_skill_was_successful_Current_level_of_enchant_skill_S1_became_0_and_enchant_skill_will_be_initialized);
sm.addSkillName(_skillId, _skillLvl);
activeChar.sendPacket(sm);
}
Log.add(activeChar.getName() + "|Successfully untranes|" + _skillId + "|to+" + _skillLvl + "|---", "enchant_skills");
activeChar.sendPacket(new ExEnchantSkillInfo(_skillId, newSkill.getDisplayLevel()), new ExEnchantSkillResult(1), new SkillList(activeChar));
updateSkillShortcuts(activeChar);
}
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);
}
}
}
}