package org.cyclops.integrateddynamics.core.evaluate.variable;

import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import org.cyclops.cyclopscore.persist.nbt.INBTProvider;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;

/**
 * A list proxy for the something of an entity.
 */
public abstract class ValueTypeListProxyEntityBase<T extends IValueType<V>, V extends IValue> extends ValueTypeListProxyBase<T, V> implements INBTProvider {

    private int world;
    private int entity;

    public ValueTypeListProxyEntityBase(String name, T valueType, World world, Entity entity) {
        super(name, valueType);
        this.world = world == null ? -1 : world.provider.getDimension();
        this.entity = entity == null ? -1 : entity.getEntityId();
    }

    protected Entity getEntity() {
        WorldServer[] servers = FMLCommonHandler.instance().getMinecraftServerInstance().worlds;
        if(world < servers.length) {
            Entity e = servers[world].getEntityByID(entity);
            return e;
        }
        return null;
    }

    @Override
    public void writeGeneratedFieldsToNBT(NBTTagCompound tag) {
        tag.setInteger("world", world);
        tag.setInteger("entity", entity);
    }

    @Override
    public void readGeneratedFieldsFromNBT(NBTTagCompound tag) {
        this.world = tag.getInteger("world");
        this.entity = tag.getInteger("entity");
    }
}

@@ -1055,18 +1055,20 @@
 rver
-%5B%5D s
+ worldS
 erver
-s
  = F
@@ -1127,14 +1127,23 @@
 e().
+getWorld(
 world
-s
+)
 ;%0A  
@@ -1160,25 +1160,22 @@
 orld
- %3C s
+S
 erver
-s.length
+ != null
 ) %7B%0A
@@ -1190,77 +1190,48 @@
     
-Entity e = servers%5Bworld%5D.getEntityByID(entity);%0A            return e
+return worldServer.getEntityByID(entity)
 ;%0A  

Fix crash when reading entity inventories in the nether, Closes #260