package org.cyclops.everlastingabilities.network.packet;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.cyclops.cyclopscore.network.CodecField;
import org.cyclops.cyclopscore.network.PacketCodec;
import org.cyclops.everlastingabilities.api.Ability;
import org.cyclops.everlastingabilities.ability.AbilityTypes;
import org.cyclops.everlastingabilities.api.IAbilityType;
import org.cyclops.everlastingabilities.inventory.container.ContainerAbilityContainer;

/**
 * Packet from server to client to update capabilities.
 * @author rubensworks
 *
 */
public class MoveAbilityPacket extends PacketCodec {

	@CodecField
	private String abilityName;
	@CodecField
	private int abilityLevel;
	@CodecField
	private int movement;

    public MoveAbilityPacket() {

    }

	public MoveAbilityPacket(Ability ability, Movement movement) {
		this.abilityName = ability.getAbilityType().getUnlocalizedName();
		this.abilityLevel = ability.getLevel();
		this.movement = movement.ordinal();
	}

	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void actionClient(World world, EntityPlayer player) {

	}

	@Override
	public void actionServer(World world, EntityPlayerMP player) {
		if (player.openContainer instanceof ContainerAbilityContainer) {
			ContainerAbilityContainer container = (ContainerAbilityContainer) player.openContainer;
			IAbilityType abilityType = AbilityTypes.REGISTRY.getAbilityType(abilityName);
			if (abilityType != null) {
				Ability ability = new Ability(abilityType, abilityLevel);
				if (movement == Movement.FROM_PLAYER.ordinal()) {
					container.moveFromPlayer(ability);
				} else {
					container.moveToPlayer(ability);
				}
			}
		}
	}

	public static enum Movement {
		TO_PLAYER, FROM_PLAYER
	}
	
}
@@ -386,16 +386,80 @@
 tCodec;%0A
+import org.cyclops.everlastingabilities.ability.AbilityHelpers;%0A
 import o
@@ -1848,24 +1848,184 @@
 rdinal()) %7B%0A
+%09%09%09%09%09if (AbilityHelpers.canExtract(ability, container.getPlayerAbilityStore())%0A%09%09%09%09%09%09%09&& AbilityHelpers.canInsert(ability, container.getItemAbilityStore())) %7B%0A%09
 %09%09%09%09%09contain
@@ -2048,24 +2048,31 @@
 r(ability);%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D else %7B
@@ -2072,16 +2072,176 @@
  else %7B%0A
+%09%09%09%09%09if (AbilityHelpers.canExtract(ability, container.getItemAbilityStore())%0A%09%09%09%09%09%09%09&& AbilityHelpers.canInsert(ability, container.getPlayerAbilityStore())) %7B%0A%09
 %09%09%09%09%09con
@@ -2266,24 +2266,31 @@
 r(ability);%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09%09%7D%0A%09

Fix ability duplication when spam-clicking during gui movement