package com.enderio.core.common.network;

import java.lang.reflect.TypeVariable;

import javax.annotation.Nonnull;

import com.google.common.reflect.TypeToken;

import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public abstract class MessageTileEntity<T extends TileEntity> implements IMessage {

  private long pos;

  protected MessageTileEntity() {
  }

  protected MessageTileEntity(@Nonnull T tile) {
    pos = tile.getPos().toLong();
  }

  @Override
  public void toBytes(ByteBuf buf) {
    buf.writeLong(pos);
  }

  @Override
  public void fromBytes(ByteBuf buf) {
    pos = buf.readLong();
  }

  public @Nonnull BlockPos getPos() {
    return BlockPos.fromLong(pos);
  }

  @SuppressWarnings("unchecked")
  protected T getTileEntity(World worldObj) {
    // Sanity check, and prevent malicious packets from loading chunks
    if (worldObj == null || !worldObj.isBlockLoaded(getPos())) {
      return null;
    }
    TileEntity te = worldObj.getTileEntity(getPos());
    if (te == null) {
      return null;
    }
    @SuppressWarnings("rawtypes")
    final Class<? extends MessageTileEntity> ourClass = getClass();
    @SuppressWarnings("rawtypes")
    final TypeVariable<Class<MessageTileEntity>>[] typeParameters = MessageTileEntity.class.getTypeParameters();
    if (typeParameters.length > 0) {
      @SuppressWarnings("rawtypes")
      final TypeVariable<Class<MessageTileEntity>> typeParam0 = typeParameters[0];
      if (typeParam0 != null) {
        TypeToken<?> teType = TypeToken.of(ourClass).resolveType(typeParam0);
        final Class<? extends TileEntity> teClass = te.getClass();
        if (teType.isAssignableFrom(teClass)) {
          return (T) te;
        }
      }
    }
    return null;
  }

  protected @Nonnull World getWorld(MessageContext ctx) {
    return ctx.getServerHandler().player.world;
  }
}

@@ -109,16 +109,51 @@
 nnull;%0A%0A
+import com.enderio.core.EnderCore;%0A
 import c
@@ -477,16 +477,63 @@
 Context;
+%0Aimport net.minecraftforge.fml.relauncher.Side;
 %0A%0Apublic
@@ -2093,24 +2093,61 @@
 text ctx) %7B%0A
+    if (ctx.side == Side.SERVER) %7B%0A  
     return c
@@ -2182,14 +2182,251 @@
 .world;%0A
+    %7D else %7B%0A      final World clientWorld = EnderCore.proxy.getClientWorld();%0A      if (clientWorld == null) %7B%0A        throw new NullPointerException(%22Recieved network packet ouside any world!%22);%0A      %7D%0A      return clientWorld;%0A    %7D%0A
   %7D%0A%7D%0A

Fix packet helper method being overly limited