/*
* 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 .
*/
package handlers.bypasshandlers;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import com.l2jserver.gameserver.handler.IBypassHandler;
import com.l2jserver.gameserver.instancemanager.QuestManager;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.events.EventType;
import com.l2jserver.gameserver.model.events.listeners.AbstractEventListener;
import com.l2jserver.gameserver.model.quest.Quest;
import com.l2jserver.gameserver.model.quest.QuestState;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.util.StringUtil;
public class QuestLink implements IBypassHandler
{
private static final String[] COMMANDS =
{
"Quest"
};
@Override
public boolean useBypass(String command, L2PcInstance activeChar, L2Character target)
{
String quest = "";
try
{
quest = command.substring(5).trim();
}
catch (IndexOutOfBoundsException ioobe)
{
}
if (quest.length() == 0)
{
showQuestWindow(activeChar, (L2Npc) target);
}
else
{
int questNameEnd = quest.indexOf(" ");
if (questNameEnd == -1)
{
showQuestWindow(activeChar, (L2Npc) target, quest);
}
else
{
activeChar.processQuestEvent(quest.substring(0, questNameEnd), quest.substring(questNameEnd).trim());
}
}
return true;
}
/**
* Open a choose quest window on client with all quests available of the L2NpcInstance.
* Actions:
*
Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance
* @param player The L2PcInstance that talk with the L2NpcInstance
* @param npc The table containing quests of the L2NpcInstance
* @param quests
*/
private static void showQuestChooseWindow(L2PcInstance player, L2Npc npc, Collection quests)
{
final StringBuilder sb = StringUtil.startAppend(150, "");
final StringBuilder qStarted = StringUtil.startAppend(150, "");
final StringBuilder qCanStart = StringUtil.startAppend(150, "");
final StringBuilder qCannotstart = StringUtil.startAppend(150, "");
final StringBuilder qComplete = StringUtil.startAppend(150, "");
String state = "";
String color = "";
//@formatter:off
final Set startingQuests = npc.getListeners(EventType.ON_NPC_QUEST_START).stream()
.map(AbstractEventListener::getOwner)
.filter(Quest.class::isInstance)
.map(Quest.class::cast)
.distinct()
.collect(Collectors.toSet());
//@formatter:on
for (Quest quest : quests)
{
final QuestState qs = player.getQuestState(quest.getName());
if ((qs == null) || qs.isCreated())
{
state = quest.isCustomQuest() ? "" : "01";
if (startingQuests.contains(quest) && quest.canStartQuest(player))
{
color = "bbaa88";
}
else
{
color = "a62f31";
}
}
else if (qs.isStarted())
{
state = quest.isCustomQuest() ? " (In Progress)" : "02";
color = "ffdd66";
}
else if (qs.isCompleted())
{
state = quest.isCustomQuest() ? " (Done)" : "03";
color = "787878";
}
switch (color)
{
case "ffdd66": // started
{
StringUtil.append(qStarted, "");
StringUtil.append(qStarted, "