package com.skcraft.plume.module;

import com.skcraft.plume.common.util.module.Module;
import com.skcraft.plume.util.Location3d;
import com.skcraft.plume.util.Locations;
import com.skcraft.plume.util.Materials;
import com.skcraft.plume.util.Messages;
import com.skcraft.plume.util.TeleportHelper;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.EnderTeleportEvent;

import static com.skcraft.plume.common.util.SharedLocale.tr;

@Module(name = "enderpearl-homes", desc = "Teleport to your own bed or spawn using just Ender pearls")
public class EnderPearlHomes {

    private static final int HOME_DISTANCE_SQ = 5 * 5;
    private static final int SPAWN_HORIZ_DISTANCE_SQ = 3 * 3;
    private static final int SPAWN_HEAD_BUFFER = 3;

    @SubscribeEvent
    public void onEnderTeleport(EnderTeleportEvent event) {
        World world = event.entity.worldObj;
        World spawnWorld = MinecraftServer.getServer().getEntityWorld();
        EntityPlayerMP player = (EntityPlayerMP) event.entity;
        Location3d target = new Location3d(world, event.targetX, event.targetY, event.targetZ);
        Location3d current = Locations.getLocation3d(player);

        boolean inWater = Materials.isWater(world.getBlock((int) player.posX, (int) player.posY, (int) player.posZ));
        double distanceSq = current.distanceSq(target);
        double horizDistanceSq =  current.setY(0).distanceSq(target.setY(0));

        if (inWater && (distanceSq <= HOME_DISTANCE_SQ || horizDistanceSq < SPAWN_HORIZ_DISTANCE_SQ && current.getY() <= target.getY() + SPAWN_HEAD_BUFFER)) {
            ChunkCoordinates coords = spawnWorld.getSpawnPoint();
            TeleportHelper.teleport(player, Locations.getLocation3d(spawnWorld, coords));
            player.addChatMessage(Messages.info(tr("enderpearlHomes.toSpawn")));
            event.setCanceled(true);
        } else if (current.distanceSq(target) <= HOME_DISTANCE_SQ) {
            ChunkCoordinates coords = player.getBedLocation(spawnWorld.provider.dimensionId);
            if (coords != null) {
                TeleportHelper.teleport(player, Locations.getLocation3d(spawnWorld, coords));
                player.addChatMessage(Messages.info(tr("enderpearlHomes.nowAtBed")));
            } else {
                player.addChatMessage(Messages.error(tr("enderpearlHomes.noBed")));
            }
            event.setCanceled(true);
        }
    }

}

@@ -1030,24 +1030,82 @@
 nt event) %7B%0A
+        if (event.entity instanceof EntityPlayerMP) %7B%0A    
         Worl
@@ -1141,24 +1141,28 @@
 bj;%0A        
+ 
+   
 World spawnW
@@ -1222,16 +1222,20 @@
         
+    
 EntityPl
@@ -1281,32 +1281,36 @@
 entity;%0A        
+   
+ 
 Location3d targe
@@ -1381,32 +1381,36 @@
 rgetZ);%0A        
+    
 Location3d curre
@@ -1444,24 +1444,28 @@
 d(player);%0A%0A
+    
         bool
@@ -1574,24 +1574,28 @@
 ));%0A        
+    
 double dista
@@ -1630,16 +1630,20 @@
 arget);%0A
+    
         
@@ -1717,24 +1717,28 @@
 );%0A%0A        
+  
+  
 if (inWater 
@@ -1880,32 +1880,36 @@
 ) %7B%0A            
+    
 ChunkCoordinates
@@ -1942,24 +1942,28 @@
 awnPoint();%0A
+    
             
@@ -2044,32 +2044,36 @@
 ));%0A            
+    
 player.addChatMe
@@ -2129,32 +2129,36 @@
 ));%0A            
+    
 event.setCancele
@@ -2170,24 +2170,28 @@
 e);%0A        
+  
+  
 %7D else if (c
@@ -2235,24 +2235,28 @@
 TANCE_SQ) %7B%0A
+    
             
@@ -2345,24 +2345,28 @@
             
+   
+ 
 if (coords !
@@ -2371,24 +2371,28 @@
  != null) %7B%0A
+    
             
@@ -2481,32 +2481,36 @@
                 
+    
 player.addChatMe
@@ -2571,24 +2571,28 @@
             
+ 
+   
 %7D else %7B%0A   
@@ -2588,16 +2588,20 @@
  else %7B%0A
+    
         
@@ -2684,26 +2684,34 @@
             
+   
+ 
 %7D%0A
+    
             
@@ -2727,32 +2727,46 @@
 Canceled(true);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Make sure the EnderTeleportEvent has a non-null EntityPlayerMP.