content: /**
   * This file is part of Aion-Lightning <aion-lightning.org>.
   *
   *  Aion-Lightning 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.
   *
   *  Aion-Lightning 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 Aion-Lightning.
   *  If not, see <http://www.gnu.org/licenses/>.
   */
  package ai.quests;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import com.aionemu.gameserver.ai2.AI2Actions;
  import com.aionemu.gameserver.ai2.AI2Actions.SelectDialogResult;
  import com.aionemu.gameserver.ai2.AIName;
  import com.aionemu.gameserver.ai2.handler.CreatureEventHandler;
  import com.aionemu.gameserver.model.DialogAction;
  import com.aionemu.gameserver.model.gameobjects.Creature;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
  import com.aionemu.gameserver.questEngine.QuestEngine;
  import com.aionemu.gameserver.questEngine.model.QuestActionType;
  import com.aionemu.gameserver.questEngine.model.QuestEnv;
  import com.aionemu.gameserver.services.QuestService;
  import com.aionemu.gameserver.services.drop.DropService;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  
  import ai.ActionItemNpcAI2;
  
  /**
   * @author xTz
   */
  @AIName("quest_use_item")
  public class QuestItemNpcAI2 extends ActionItemNpcAI2 {
  
  	private List<Player> registeredPlayers = new ArrayList<Player>();
  
  	@Override
  	protected void handleDialogStart(Player player) {
  		if (!(QuestEngine.getInstance().onCanAct(new QuestEnv(getOwner(), player, 0, 0), getObjectTemplate().getTemplateId(), QuestActionType.ACTION_ITEM_USE))) {
  			return;
  		}
  		super.handleDialogStart(player);
  	}
  
  	@Override
  	protected void handleUseItemFinish(Player player) {
  		SelectDialogResult dialogResult = AI2Actions.selectDialog(this, player, 0, -1);
  		if (getNpcId() == 730229) {
  			if (dialogResult.isSuccess()) {
  				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), 3057, 10034)); // Dialog, QuestId
  			}
  		}
  
  		if (!dialogResult.isSuccess()) {
  			if (isDialogNpc()) {
  				// show default dialog
  				PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), DialogAction.SELECT_ACTION_1011.id()));
  			}
  			return;
  		}
  		QuestEnv questEnv = dialogResult.getEnv();
  		if (QuestService.getQuestDrop(getNpcId()).isEmpty()) {
  			return;
  		}
  
  		if (registeredPlayers.isEmpty()) {
  			AI2Actions.scheduleRespawn(this);
  			if (player.isInGroup2()) {
  				registeredPlayers = QuestService.getEachDropMembersGroup(player.getPlayerGroup2(), getNpcId(), questEnv.getQuestId());
  				if (registeredPlayers.isEmpty()) {
  					registeredPlayers.add(player);
  				}
  			}
  			else if (player.isInAlliance2()) {
  				registeredPlayers = QuestService.getEachDropMembersAlliance(player.getPlayerAlliance2(), getNpcId(), questEnv.getQuestId());
  				if (registeredPlayers.isEmpty()) {
  					registeredPlayers.add(player);
  				}
  			}
  			else {
  				registeredPlayers.add(player);
  			}
  			AI2Actions.registerDrop(this, player, registeredPlayers);
  			DropService.getInstance().requestDropList(player, getObjectId());
  		}
  		else if (registeredPlayers.contains(player)) {
  			DropService.getInstance().requestDropList(player, getObjectId());
  		}
  	}
  
  	private boolean isDialogNpc() {
  		return getObjectTemplate().isDialogNpc();
  	}
  
  	@Override
  	protected void handleDespawned() {
  		super.handleDespawned();
  		registeredPlayers.clear();
  	}
  
  	@Override
  	protected void handleCreatureSee(Creature creature) {
  		CreatureEventHandler.onCreatureSee(this, creature);
  	}
  
  	@Override
  	protected void handleCreatureMoved(Creature creature) {
  		CreatureEventHandler.onCreatureMoved(this, creature);
  	}
  }
  