package com.lothrazar.samsmagic.spell;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.world.World;

import com.lothrazar.samsmagic.ItemRegistry;
import com.lothrazar.samsmagic.ModSpells;
import com.lothrazar.samsmagic.SpellRegistry; 

public class SpellPhasing extends BaseSpellExp implements ISpell
{ 
	@Override
	public String getSpellName()
	{
		return "phase";
	}
	@Override
	public int getSpellID() 
	{
		return 6;
	}
	
	@Override
	public boolean canPlayerCast(World world, EntityPlayer player, BlockPos pos)
	{
		if(super.canPlayerCast(world, player, pos) == false) {return false;}
	
		BlockPos offs = getPosOffset(player,pos);
		
		if(pos != null && offs != null && world.isAirBlock(offs) && world.isAirBlock(offs.up()))
		{
			return true;
		}
		
		return false;
	}
	
	@Override
	public void cast(World world, EntityPlayer player, BlockPos pos)
	{
		if(pos == null){return;}//covered also by canPlayerCast
		
 
		BlockPos offs = getPosOffset(player,pos);//was .getOpposite()
		
		//not 2, depends on block pos?
		if(world.isAirBlock(offs) && world.isAirBlock(offs.up()))
		{
			player.setPositionAndUpdate(offs.getX(), offs.getY(), offs.getZ()); 
		}
	}
	
	private BlockPos getPosOffset(EntityPlayer player,BlockPos pos) 
	{
		if(pos == null){return pos;}
		EnumFacing face = EnumFacing.getFacingFromVector(
				(float)player.getLookVec().xCoord
				, (float)player.getLookVec().yCoord
				, (float)player.getLookVec().zCoord);
		int dist = 1;
		if(face.getOpposite() == EnumFacing.DOWN)
		{
			 dist = 2;//only move two when going down - player is 2 tall
		}
		//was .getOpposite()
		BlockPos offs = pos.offset(face, dist);
		return offs;
	}
 
	@Override
	public void onCastSuccess(World world, EntityPlayer player, BlockPos pos)
	{
		world.playSoundAtEntity(player, "mob.endermen.portal", 1.0F, 1.0F);  
		ModSpells.spawnParticle(world, EnumParticleTypes.PORTAL, pos);

		super.onCastSuccess(world, player, pos);
	}

	@Override
	public int getExpCost()
	{
		return ModSpells.cfg.phase;
	}
	
	@Override
	public ItemStack getIconDisplay()
	{
		return new ItemStack(ItemRegistry.spell_dummy_phasing);
	}
}

@@ -1461,17 +1461,772 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
+    public static EnumFacing getFacingFromVector(float p_176737_0_, float p_176737_1_, float p_176737_2_)%0A    %7B%0A        EnumFacing enumfacing = EnumFacing.NORTH;%0A        float f3 = Float.MIN_VALUE;%0A        EnumFacing%5B%5D aenumfacing = EnumFacing.values();%0A        int i = aenumfacing.length;%0A%0A        for (int j = 0; j %3C i; ++j)%0A        %7B%0A            EnumFacing enumfacing1 = aenumfacing%5Bj%5D;%0A            float f4 = p_176737_0_ * (float)enumfacing1.getDirectionVec().getX() + p_176737_1_ * (float)enumfacing1.getDirectionVec().getY() + p_176737_2_ * (float)enumfacing1.getDirectionVec().getZ();%0A%0A            if (f4 %3E f3)%0A            %7B%0A                f3 = f4;%0A                enumfacing = enumfacing1;%0A            %7D%0A        %7D%0A%0A        return enumfacing;%0A    %7D
 %0A%09privat
@@ -2318,16 +2318,91 @@
 n pos;%7D%0A
+%09%09//oops this is sideonly client...%0A%09%09//WAS EnumFacing.getFacingFromVector%0A
 %09%09EnumFa
@@ -2413,27 +2413,16 @@
  face = 
-EnumFacing.
 getFacin

fix crash in phasing - that method was clientside only