package main.flowstoneenergy.enchants;

import java.util.Random;

import main.flowstoneenergy.FlowstoneEnergy;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class EnchantRandTeleHandler {
	@SubscribeEvent
	public void RandomTeleport(LivingAttackEvent event) {
		EntityPlayer attacker = event.source;
		Entity defender = event.entity;
		World world = event.entity.worldObj;
		if(world.isRemote) return;
        	if(event.source.getEntity() == null) return;
        	if(!(event.source.getEntity() instanceof EntityPlayer)) return;
        	if(attacker.getHeldItem() == null) return;
        	if(!attacker.getHeldItem().isItemEnchanted());
        	if(!(EnchantmentHelper.getEnchantments(attacker.getHeldItem()).containsKey(EnchantRegistry.randTeleEnchant.effectId))) return;
        	teleport(world, event.source.getEntity());
	}
	
	public void teleport(World world, Entity entity) {
		Random rand = new Random();
		if (rand.nextBoolean()) entity.posX += rand.nextDouble() + (rand.nextInt(5 - 0) + 0); else entity.posX -= rand.nextDouble() + (rand.nextInt(5 - 0) + 0);
		entity.posY += 40;
		if (rand.nextBoolean()) entity.posZ += rand.nextDouble() + (rand.nextInt(5 - 0) + 0); else entity.posZ -= rand.nextDouble() + (rand.nextInt(5 - 0) + 0);
		while (world.getBlock((int)entity.posX, (int)entity.posY - 1, (int)entity.posZ) == Blocks.air) {
			entity.posY -= 1;
		}
				
		entity.setPosition(entity.posX, entity.posY, entity.posZ);
	}
}

@@ -61,54 +61,8 @@
 dom;
-%0A%0Aimport main.flowstoneenergy.FlowstoneEnergy;
 %0Aimp
@@ -521,28 +521,55 @@
 acker = 
-event.source
+(EntityPlayer) event.source.getEntity()
 ;%0A%09%09Enti
@@ -672,17 +672,16 @@
         
-%09
 if(event
@@ -721,25 +721,24 @@
 rn;%0A        
-%09
 if(!(event.s
@@ -797,17 +797,16 @@
         
-%09
 if(attac
@@ -844,25 +844,24 @@
 rn;%0A        
-%09
 if(!attacker
@@ -903,17 +903,16 @@
         
-%09
 if(!(Enc
@@ -1038,17 +1038,16 @@
         
-%09
 teleport
@@ -1058,32 +1058,16 @@
 ld, 
-event.source.getEntity()
+defender
 );%0A%09
@@ -1152,16 +1152,168 @@
 ndom();%0A
+%09%09world.playSoundEffect(entity.posX + 0.5D, entity.posY + 0.5D, entity.posZ + 0.5D, %22mob.endermen.portal%22, 1.0F, world.rand.nextFloat() * 0.1F + 0.9F);%0A
 %09%09if (ra
@@ -1763,13 +1763,8 @@
 %09%09%7D%0A
-%09%09%09%09%0A
 %09%09en

Update EnchantRandTeleHandler.java