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 com.aionemu.gameserver.skillengine.task;
  
  import com.aionemu.gameserver.configs.main.CraftConfig;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.gameobjects.StaticObject;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.templates.recipe.RecipeTemplate;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_CRAFT_ANIMATION;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_CRAFT_UPDATE;
  import com.aionemu.gameserver.services.craft.CraftService;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  import com.aionemu.gameserver.utils.ThreadPoolManager;
  
  /**
   * @author Antraxx
   * @author Kamikaze
   */
  public class MorphingTask extends CraftingTask {
  
  	/**
  	 * MorphingTask
  	 *
  	 * @param requestor
  	 * @param responder
  	 * @param recipeTemplates
  	 */
  	public MorphingTask(Player requestor, StaticObject responder, RecipeTemplate recipeTemplates) {
  		super(requestor, responder, recipeTemplates, 0, 0);
  		this.maxSuccessValue = 50;
  		this.maxFailureValue = 75;
  	}
  
  	@Override
  	public void start() {
  		onInteractionStart();
  		task = ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable() {
  
  			@Override
  			public void run() {
  				if (!validateParticipants()) {
  					stop(true);
  				}
  				boolean stopTask = onInteraction();
  				if (stopTask) {
  					stop(false);
  				}
  			}
  		}, CraftConfig.CRAFT_TIMERMORPH_DELAY, CraftConfig.CRAFT_TIMERMORPH_PERIOD);
  	}
  
  	@Override
  	protected void analyzeInteraction() {
  	}
  
  	@Override
  	protected void onFailureFinish() {
  		PacketSendUtility.sendPacket(requestor, new SM_CRAFT_UPDATE(recipeTemplate.getSkillid(), itemTemplate, currentSuccessValue, currentFailureValue, 6));
  		PacketSendUtility.broadcastPacket(requestor, new SM_CRAFT_ANIMATION(requestor.getObjectId(), 0, 0, 3), true);
  	}
  
  	@Override
  	protected boolean onSuccessFinish() {
  		PacketSendUtility.broadcastPacket(requestor, new SM_CRAFT_ANIMATION(requestor.getObjectId(), 0, 0, 2), true);
  		PacketSendUtility.sendPacket(requestor, new SM_CRAFT_UPDATE(recipeTemplate.getSkillid(), itemTemplate, 0, 0, 5));
  		CraftService.finishCrafting(requestor, recipeTemplate, critCount, 0);
  		return true;
  	}
  
  	@Override
  	protected void onInteractionFinish() {
  		requestor.setCraftingTask(null);
  	}
  
  	@Override
  	protected boolean onInteraction() {
  		currentSuccessValue = maxSuccessValue;
  		return onSuccessFinish();
  	}
  
  	@Override
  	protected void onInteractionStart() {
  		this.itemTemplate = DataManager.ITEM_DATA.getItemTemplate(recipeTemplate.getProductid());
  		PacketSendUtility.sendPacket(requestor, new SM_CRAFT_UPDATE(recipeTemplate.getSkillid(), itemTemplate, maxSuccessValue, maxFailureValue, 0));
  		PacketSendUtility.sendPacket(requestor, new SM_CRAFT_UPDATE(recipeTemplate.getSkillid(), itemTemplate, 0, 0, 1));
  		PacketSendUtility.broadcastPacket(requestor, new SM_CRAFT_ANIMATION(requestor.getObjectId(), 0, recipeTemplate.getSkillid(), 0), true);
  		PacketSendUtility.broadcastPacket(requestor, new SM_CRAFT_ANIMATION(requestor.getObjectId(), 0, recipeTemplate.getSkillid(), 1), true);
  	}
  
  	@Override
  	protected void onInteractionAbort() {
  		PacketSendUtility.sendPacket(requestor, new SM_CRAFT_UPDATE(recipeTemplate.getSkillid(), itemTemplate, 0, 0, 4));
  		PacketSendUtility.broadcastPacket(requestor, new SM_CRAFT_ANIMATION(requestor.getObjectId(), 0, 0, 2), true);
  		requestor.setCraftingTask(null);
  	}
  }
  