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.resources.ResourceLocation;
import net.minecraft.core.Vec3i;
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 bb, final LootContext.EntityTarget c) {
        this.predicate = bb;
        this.entityTarget = c;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.BLOCK_POS, (LootContextParam<BlockPos>)this.entityTarget.getParam());
    }
    
    @Override
    public boolean test(final LootContext crx) {
        final Entity akq3 = crx.<Entity>getParamOrNull(this.entityTarget.getParam());
        final BlockPos fk4 = crx.<BlockPos>getParamOrNull(LootContextParams.BLOCK_POS);
        return this.predicate.matches(crx.getLevel(), (fk4 != null) ? new Vec3(fk4) : null, akq3);
    }
    
    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 class Serializer extends LootItemCondition.Serializer<LootItemEntityPropertyCondition> {
        protected Serializer() {
            super(new ResourceLocation("entity_properties"), LootItemEntityPropertyCondition.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final LootItemEntityPropertyCondition cuo, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("predicate", cuo.predicate.serializeToJson());
            jsonObject.add("entity", jsonSerializationContext.serialize(cuo.entityTarget));
        }
        
        @Override
        public LootItemEntityPropertyCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final EntityPredicate bb4 = EntityPredicate.fromJson(jsonObject.get("predicate"));
            return new LootItemEntityPropertyCondition(bb4, GsonHelper.<LootContext.EntityTarget>getAsObject(jsonObject, "entity", jsonDeserializationContext, LootContext.EntityTarget.class), null);
        }
    }
}
