package net.minecraft.world.level.storage.loot.predicates;

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.world.level.storage.loot.Serializer;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import java.util.Set;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.advancements.critereon.EntityPredicate;

public class LootItemEntityPropertyCondition implements LootItemCondition {
    private final EntityPredicate predicate;
    private final LootContext.EntityTarget entityTarget;
    
    private LootItemEntityPropertyCondition(final EntityPredicate be, final LootContext.EntityTarget c) {
        this.predicate = be;
        this.entityTarget = c;
    }
    
    @Override
    public LootItemConditionType getType() {
        return LootItemConditions.ENTITY_PROPERTIES;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.BLOCK_POS, (LootContextParam<BlockPos>)this.entityTarget.getParam());
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final Entity aom3 = dat.<Entity>getParamOrNull(this.entityTarget.getParam());
        final Vec3 dem4 = dat.<Vec3>getParamOrNull(LootContextParams.ORIGIN);
        return this.predicate.matches(dat.getLevel(), dem4, aom3);
    }
    
    public static Builder entityPresent(final LootContext.EntityTarget c) {
        return hasProperties(c, EntityPredicate.Builder.entity());
    }
    
    public static Builder hasProperties(final LootContext.EntityTarget c, final EntityPredicate.Builder a) {
        return () -> new LootItemEntityPropertyCondition(a.build(), c);
    }
    
    public static Builder hasProperties(final LootContext.EntityTarget c, final EntityPredicate be) {
        return () -> new LootItemEntityPropertyCondition(be, c);
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<LootItemEntityPropertyCondition> {
        @Override
        public void serialize(final JsonObject jsonObject, final LootItemEntityPropertyCondition ddp, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("predicate", ddp.predicate.serializeToJson());
            jsonObject.add("entity", jsonSerializationContext.serialize(ddp.entityTarget));
        }
        
        @Override
        public LootItemEntityPropertyCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final EntityPredicate be4 = EntityPredicate.fromJson(jsonObject.get("predicate"));
            return new LootItemEntityPropertyCondition(be4, GsonHelper.<LootContext.EntityTarget>getAsObject(jsonObject, "entity", jsonDeserializationContext, LootContext.EntityTarget.class), null);
        }
    }
}
