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.portals;
  
  import com.aionemu.gameserver.utils.ThreadPoolManager;
  import com.aionemu.gameserver.ai2.AI2Actions;
  import com.aionemu.gameserver.ai2.AIName;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.TeleportAnimation;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.templates.portal.PortalPath;
  import com.aionemu.gameserver.model.templates.portal.PortalUse;
  import com.aionemu.gameserver.model.templates.teleport.TeleportLocation;
  import com.aionemu.gameserver.model.templates.teleport.TeleporterTemplate;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
  import com.aionemu.gameserver.services.teleport.PortalService;
  import com.aionemu.gameserver.services.teleport.TeleportService2;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  import com.aionemu.gameserver.world.World;
  import com.aionemu.gameserver.world.knownlist.Visitor;
  
  import ai.ActionItemNpcAI2;
  
  /**
   * @author xTz
   */
  @AIName("portal")
  public class PortalAI2 extends ActionItemNpcAI2 {
  
  	protected TeleporterTemplate teleportTemplate;
  	protected PortalUse portalUse;
  
  	@Override
  	public boolean onDialogSelect(Player player, int dialogId, int questId, int extendedRewardIndex) {
  		return true;
  	}
  
  	@Override
  	protected void handleSpawned() {
  		super.handleSpawned();
  		teleportTemplate = DataManager.TELEPORTER_DATA.getTeleporterTemplateByNpcId(getNpcId());
  		portalUse = DataManager.PORTAL2_DATA.getPortalUse(getNpcId());
  		switch (getNpcId()) {
  			case 802219: // Advance Corridor [Arcadian Fortress]
  			case 802221: // Advance Corridor [Umbral Fortress]
  			case 802223: // Advance Corridor [Eternum Fortress]
  			case 802225: // Advance Corridor [Skyclash Fortress]
  				ThreadPoolManager.getInstance().schedule(new Runnable() {
  
  					@Override
  					public void run() {
  						startLifeTask();
  					}
  				}, 1000);
  				break;
  		}
  	}
  
  	private void startLifeTask() {
  		ThreadPoolManager.getInstance().schedule(new Runnable() {
  
  			@Override
  			public void run() {
  				World.getInstance().doOnAllPlayers(new Visitor<Player>() {
  
  					@Override
  					public void visit(Player player) {
  						AI2Actions.deleteOwner(PortalAI2.this);
  						// You will be returned to the entrance you used upon closure of the Advance Corridor
  						PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_SVS_DIRECT_PORTAL_CLOSE_COMPULSION_TELEPORT);
  					}
  				});
  			}
  		}, 600000); // 10 Minutes
  	}
  
  	@Override
  	protected void handleDialogStart(Player player) {
  		AI2Actions.selectDialog(this, player, 0, -1);
  		if (getTalkDelay() != 0) {
  			super.handleDialogStart(player);
  		}
  		else {
  			handleUseItemFinish(player);
  		}
  	}
  
  	@Override
  	protected void handleUseItemFinish(Player player) {
  		if (portalUse != null) {
  			PortalPath portalPath = portalUse.getPortalPath(player.getRace());
  			if (portalPath != null) {
  				PortalService.port(portalPath, player, getObjectId());
  			}
  		}
  		else if (teleportTemplate != null) {
  			TeleportLocation loc = teleportTemplate.getTeleLocIdData().getTelelocations().get(0);
  			if (loc != null) {
  				TeleportService2.teleport(teleportTemplate, loc.getLocId(), player, getOwner(), TeleportAnimation.BEAM_ANIMATION);
  			}
  		}
  	}
  }
  