package net.sf.l2j.gameserver.model.actor.instance;
import net.sf.l2j.Config;
import net.sf.l2j.gameserver.ai.CtrlIntention;
import net.sf.l2j.gameserver.data.CharTemplateTable;
import net.sf.l2j.gameserver.data.ItemTable;
import net.sf.l2j.gameserver.model.L2ItemInstance;
import net.sf.l2j.gameserver.model.base.ClassId;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.network.SystemMessageId;
import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
import net.sf.l2j.gameserver.network.serverpackets.MyTargetSelected;
import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
import net.sf.l2j.gameserver.network.serverpackets.ValidateLocation;
import net.sf.l2j.gameserver.templates.L2NpcTemplate;
public final class L2ClassMasterInstance extends L2FolkInstance
{
public L2ClassMasterInstance(int objectId, L2NpcTemplate template)
{
super(objectId, template);
}
@Override
public void onAction(L2PcInstance player)
{
if (!canTarget(player))
{
return;
}
if (getObjectId() != player.getTargetId())
{
player.setTarget(this);
player.sendPacket(new MyTargetSelected(getObjectId(), 0));
player.sendPacket(new ValidateLocation(this));
}
else
{
if (!canInteract(player))
{
player.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, this);
return;
}
NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
StringBuilder sb = new StringBuilder();
sb.append("<html><body>");
sb.append("<font color=\"006600\">" + getName() + "</font><br>");
sb.append("<br>");
ClassId classId = player.getClassId();
int level = player.getLevel();
int jobLevel = classId.level();
int newJobLevel = jobLevel + 1;
if ((((level >= 20 && jobLevel == 0) || (level >= 40 && jobLevel == 1) || (level >= 76 && jobLevel == 2)) && Config.CLASS_MASTER_SETTINGS.isAllowed(newJobLevel)) || Config.CLASS_MASTER_STRIDER_UPDATE)
{
if (((level >= 20 && jobLevel == 0) || (level >= 40 && jobLevel == 1) || (level >= 76 && jobLevel == 2)) && Config.CLASS_MASTER_SETTINGS.isAllowed(newJobLevel))
{
sb.append("Выберите желаемую профессию:<br>");
for (ClassId child : ClassId.values())
{
if (child.childOf(classId) && child.level() == newJobLevel)
{
sb.append("<br><a action=\"bypass -h npc_" + getObjectId() + "_change_class " + (child.getId()) + "\"> " + CharTemplateTable.getClassNameById(child.getId()) + "</a>");
}
}
if (Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel) != null && !Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).isEmpty())
{
sb.append("<br><br>Плата за смену профессии:");
sb.append("<table width=270>");
for (Integer _itemId : Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).keys())
{
int _count = Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).get(_itemId);
sb.append("<tr><td><font color=\"LEVEL\">" + _count + "</font></td><td>" + ItemTable.getInstance().getTemplate(_itemId).getName() + "</td></tr>");
}
sb.append("</table>");
}
}
if (Config.CLASS_MASTER_STRIDER_UPDATE)
{
sb.append("<br><table width=270>");
sb.append("<tr><td><br></td></tr>");
sb.append("<tr><td><a action=\"bypass -h npc_" + getObjectId() + "_upgrade_hatchling\">Обменять Hatchling на Strider</a></td></tr>");
sb.append("</table>");
}
sb.append("<br>");
}
else
{
switch (jobLevel)
{
case 0:
if (Config.CLASS_MASTER_SETTINGS.isAllowed(1))
{
sb.append("Вернитесь, когда достигните 20 уровня.<br>");
}
else if (Config.CLASS_MASTER_SETTINGS.isAllowed(2))
{
sb.append("Вернитесь после получения 1 профессии.<br>");
}
else if (Config.CLASS_MASTER_SETTINGS.isAllowed(3))
{
sb.append("Вернитесь после получения 2 профессии.<br>");
}
else
{
sb.append("Вы не межете изменить вашу профессию.<br>");
}
break;
case 1:
if (Config.CLASS_MASTER_SETTINGS.isAllowed(2))
{
sb.append("Вернитесь, когда достигните 40 уровня.<br>");
}
else if (Config.CLASS_MASTER_SETTINGS.isAllowed(3))
{
sb.append("Вернитесь после получения 2 профессии.<br>");
}
else
{
sb.append("Вы не межете изменить вашу профессию.<br>");
}
break;
case 2:
if (Config.CLASS_MASTER_SETTINGS.isAllowed(3))
{
sb.append("Вернитесь, когда достигните 76 уровня.<br>");
}
else
{
sb.append("Вы не межете изменить вашу профессию.<br>");
}
break;
case 3:
sb.append("Больше нет доступных профессий.<br>");
break;
}
if (Config.CLASS_MASTER_STRIDER_UPDATE)
{
sb.append("<br><table width=270>");
sb.append("<tr><td><br></td></tr>");
sb.append("<tr><td><a action=\"bypass -h npc_" + getObjectId() + "_upgrade_hatchling\">Обменять Hatchling на Strider</a></td></tr>");
sb.append("</table>");
}
sb.append("<br>");
}
for (Quest q : Quest.findAllEvents())
{
sb.append("Event: <a action=\"bypass -h Quest " + q.getName() + "\">" + q.getDescr() + "</a><br>");
}
sb.append("</body></html>");
html.setHtml(sb.toString());
player.sendPacket(html);
}
player.sendPacket(ActionFailed.STATIC_PACKET);
}
@Override
public void onBypassFeedback(L2PcInstance player, String command)
{
if (command.startsWith("change_class"))
{
int val = Integer.parseInt(command.substring(13));
ClassId classId = player.getClassId();
ClassId newClassId = ClassId.values()[val];
int level = player.getLevel();
int jobLevel = classId.level();
int newJobLevel = newClassId.level();
if (!Config.CLASS_MASTER_SETTINGS.isAllowed(newJobLevel))
{
return;
}
if (!newClassId.childOf(classId))
{
return;
}
if (newJobLevel != jobLevel + 1)
{
return;
}
if (level < 20 && newJobLevel > 1)
{
return;
}
if (level < 40 && newJobLevel > 2)
{
return;
}
if (level < 76 && newJobLevel > 3)
{
return;
}
for (Integer _itemId : Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).keys())
{
int _count = Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).get(_itemId);
if (player.getInventory().getInventoryItemCount(_itemId, -1) < _count)
{
player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
return;
}
}
for (Integer _itemId : Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).keys())
{
int _count = Config.CLASS_MASTER_SETTINGS.getRequireItems(newJobLevel).get(_itemId);
player.destroyItemByItemId("ClassMaster", _itemId, _count, player, true);
}
for (Integer _itemId : Config.CLASS_MASTER_SETTINGS.getRewardItems(newJobLevel).keys())
{
int _count = Config.CLASS_MASTER_SETTINGS.getRewardItems(newJobLevel).get(_itemId);
player.addItem("ClassMaster", _itemId, _count, player, true);
}
changeClass(player, val);
player.rewardSkills();
if (newJobLevel == 3)
{
player.sendPacket(new SystemMessage(SystemMessageId.THIRD_CLASS_TRANSFER));
}
else
{
player.sendPacket(new SystemMessage(SystemMessageId.CLASS_TRANSFER));
}
NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
StringBuilder sb = new StringBuilder();
sb.append("<html><body>");
sb.append("<font color=\"006600\">" + getName() + "</font><br>");
sb.append("<br>");
sb.append("Поздравляю, теперь вы: <font color=\"006600\">" + CharTemplateTable.getClassNameById(player.getClassId().getId()) + "</font>.");
sb.append("</body></html>");
html.setHtml(sb.toString());
player.sendPacket(html);
player.refreshOverloaded();
player.refreshExpertisePenalty();
}
else if (command.startsWith("upgrade_hatchling") && Config.CLASS_MASTER_STRIDER_UPDATE)
{
boolean canUpgrade = false;
if (player.getPet() != null)
{
if (player.getPet().getNpcId() == 12311 || player.getPet().getNpcId() == 12312 || player.getPet().getNpcId() == 12313)
{
if (player.getPet().getLevel() >= 55)
{
canUpgrade = true;
}
else
{
player.sendMessage("Уровень вашего питомца мал для обмена.");
}
}
else
{
player.sendMessage("Вызовите вашего питомца.");
}
}
else
{
player.sendMessage("Призовите питомца для его обмена.");
}
if (!canUpgrade)
{
return;
}
int[] hatchCollar = {3500, 3501, 3502};
int[] striderCollar = {4422, 4423, 4424};
for (int i = 0; i < 3; i++)
{
L2ItemInstance collar = player.getInventory().getItemByItemId(hatchCollar[i]);
if (collar != null)
{
player.getPet().unSummon(player);
player.destroyItem("ClassMaster", collar, player, true);
player.addItem("ClassMaster", striderCollar[i], 1, player, true);
return;
}
}
}
else
{
super.onBypassFeedback(player, command);
}
}
private void changeClass(L2PcInstance player, int val)
{
player.setClassId(val);
if (player.isSubClassActive())
{
player.getSubClasses().get(player.getClassIndex()).setClassId(player.getActiveClass());
}
else
{
player.setBaseClass(player.getActiveClass());
}
player.broadcastUserInfo();
}
}