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

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.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.level.storage.loot.LootContext;
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.advancements.critereon.DamageSourcePredicate;

public class DamageSourceCondition implements LootItemCondition {
    private final DamageSourcePredicate predicate;
    
    private DamageSourceCondition(final DamageSourcePredicate au) {
        this.predicate = au;
    }
    
    @Override
    public LootItemConditionType getType() {
        return LootItemConditions.DAMAGE_SOURCE_PROPERTIES;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.BLOCK_POS, (LootContextParam<BlockPos>)LootContextParams.DAMAGE_SOURCE);
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final DamageSource anw3 = dat.<DamageSource>getParamOrNull(LootContextParams.DAMAGE_SOURCE);
        final BlockPos fu4 = dat.<BlockPos>getParamOrNull(LootContextParams.BLOCK_POS);
        return fu4 != null && anw3 != null && this.predicate.matches(dat.getLevel(), Vec3.atLowerCornerOf(fu4), anw3);
    }
    
    public static Builder hasDamageSource(final DamageSourcePredicate.Builder a) {
        return () -> new DamageSourceCondition(a.build());
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<DamageSourceCondition> {
        @Override
        public void serialize(final JsonObject jsonObject, final DamageSourceCondition ddg, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("predicate", ddg.predicate.serializeToJson());
        }
        
        @Override
        public DamageSourceCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final DamageSourcePredicate au4 = DamageSourcePredicate.fromJson(jsonObject.get("predicate"));
            return new DamageSourceCondition(au4, null);
        }
    }
}
