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");
    }
}

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 worldServer = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(world);
        if(worldServer != null) {
            return worldServer.getEntityByID(entity);
        }
        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");
    }
}

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