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

import com.google.common.base.Optional;
import lombok.ToString;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.WorldServer;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueTypeNamed;

/**
 * Value type with values that are itemstacks.
 * @author rubensworks
 */
public class ValueObjectTypeEntity extends ValueObjectTypeBase<ValueObjectTypeEntity.ValueEntity> implements IValueTypeNamed<ValueObjectTypeEntity.ValueEntity> {

    private static final String DELIMITER = ";";

    public ValueObjectTypeEntity() {
        super("entity");
    }

    @Override
    public ValueEntity getDefault() {
        return ValueEntity.of(null);
    }

    @Override
    public String toCompactString(ValueEntity value) {
        Optional<Entity> entity = value.getRawValue();
        if(entity.isPresent()) {
            Entity e = entity.get();
            if(e instanceof EntityItem) {
                return ((EntityItem) e).getEntityItem().getDisplayName();
            } else {
                return e.getName();
            }
        }
        return "";
    }

    @Override
    public String serialize(ValueEntity value) {
        Optional<Entity> entity = value.getRawValue();
        if(entity.isPresent()) {
            int world = entity.get().worldObj.provider.getDimensionId();
            int id = entity.get().getEntityId();
            return world + DELIMITER + id;
        }
        return "";
    }

    @Override
    public ValueEntity deserialize(String value) {
        String[] split = value.split(DELIMITER);
        Entity entity = null;
        if(split.length == 2) {
            try {
                int world = Integer.parseInt(split[0]);
                int id = Integer.parseInt(split[1]);
                WorldServer[] servers = MinecraftServer.getServer().worldServers;
                if(servers.length > world) {
                    entity = servers[world].getEntityByID(id);
                }
            } catch (NumberFormatException e) {}
        }
        return ValueEntity.of(entity);
    }

    @Override
    public String getName(ValueEntity a) {
        return toCompactString(a);
    }

    @ToString
    public static class ValueEntity extends ValueOptionalBase<Entity> {

        private ValueEntity(Entity entity) {
            super(ValueTypes.OBJECT_ENTITY, entity);
        }

        public static ValueEntity of(Entity entity) {
            return new ValueEntity(entity);
        }

        @Override
        protected boolean isEqual(Entity a, Entity b) {
            return a.getEntityId() == b.getEntityId();
        }
    }

}

@@ -121,16 +121,55 @@
 String;%0A
+import net.minecraft.client.Minecraft;%0A
 import n
@@ -322,24 +322,80 @@
 orldServer;%0A
+import org.cyclops.cyclopscore.helper.MinecraftHelpers;%0A
 import org.c
@@ -1984,24 +1984,189 @@
 (split%5B1%5D);%0A
+                if(MinecraftHelpers.isClientSide()) %7B%0A                    entity = Minecraft.getMinecraft().theWorld.getEntityByID(id);%0A                %7D else %7B%0A    
             
@@ -2251,18 +2251,23 @@
         
+    
 if
+ 
 (servers
@@ -2297,32 +2297,36 @@
                 
+  
+  
 entity = servers
@@ -2344,32 +2344,54 @@
 EntityByID(id);%0A
+                    %7D%0A
                 

Fix entity value type not correctly being deserialized client-side