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.effect;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlType;
  
  import com.aionemu.gameserver.controllers.observer.ActionObserver;
  import com.aionemu.gameserver.controllers.observer.ObserverType;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.gameobjects.Creature;
  import com.aionemu.gameserver.model.gameobjects.Item;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.templates.item.EquipType;
  import com.aionemu.gameserver.model.templates.item.ItemTemplate;
  import com.aionemu.gameserver.model.templates.robot.RobotInfo;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_RIDE_ROBOT;
  import com.aionemu.gameserver.skillengine.model.Effect;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  
  /**
   * @author Rolandas
   * @Reworked Kill3r
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "RideRobotEffect")
  public class RideRobotEffect extends EffectTemplate {
  
  	@Override
  	public void applyEffect(final Effect effect) {
  		effect.addToEffectedController();
  		Creature effected = effect.getEffected();
  		Player player = (Player) effected;
  		player.setUseRobot(true);
  		PacketSendUtility.broadcastPacketAndReceive(player, new SM_RIDE_ROBOT(player, getRobotInfo(player).getRobotId()));
  		player.setRobotId(getRobotInfo(player).getRobotId());
  		ActionObserver observer = new ActionObserver(ObserverType.UNEQUIP) {
  
  			@Override
  			public void unequip(Item item, Player owner) {
  				if (item.getEquipmentType() == EquipType.WEAPON) {
  					effect.endEffect();
  				}
  			}
  		};
  		player.getObserveController().addObserver(observer);
  		effect.setActionObserver(observer, position);
  	}
  
  	@Override
  	public void endEffect(Effect effect) {
  		super.endEffect(effect);
  		Creature effected = effect.getEffected();
  		Player player = (Player) effected;
  		if (player.isUseRobot()) {
  			PacketSendUtility.broadcastPacket(player, new SM_RIDE_ROBOT(player, 0), true);
  			player.setUseRobot(false);
  			player.setRobotId(0);
  		}
  		ActionObserver observer = effect.getActionObserver(position);
  		if (observer != null) {
  			effect.getEffected().getObserveController().removeObserver(observer);
  		}
  	}
  
  	public RobotInfo getRobotInfo(Player player) {
  		ItemTemplate template = player.getEquipment().getMainHandWeapon().getItemSkinTemplate();
  		return DataManager.ROBOT_DATA.getRobotInfo(template.getRobotId());
  	}
  }
  