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.resources.ResourceLocation;
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 as) {
        this.predicate = as;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.BLOCK_POS, (LootContextParam<BlockPos>)LootContextParams.DAMAGE_SOURCE);
    }
    
    @Override
    public boolean test(final LootContext crx) {
        final DamageSource ajz3 = crx.<DamageSource>getParamOrNull(LootContextParams.DAMAGE_SOURCE);
        final BlockPos fk4 = crx.<BlockPos>getParamOrNull(LootContextParams.BLOCK_POS);
        return fk4 != null && ajz3 != null && this.predicate.matches(crx.getLevel(), new Vec3(fk4), ajz3);
    }
    
    public static Builder hasDamageSource(final DamageSourcePredicate.Builder a) {
        return () -> new DamageSourceCondition(a.build());
    }
    
    public static class Serializer extends LootItemCondition.Serializer<DamageSourceCondition> {
        protected Serializer() {
            super(new ResourceLocation("damage_source_properties"), DamageSourceCondition.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final DamageSourceCondition cug, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("predicate", cug.predicate.serializeToJson());
        }
        
        @Override
        public DamageSourceCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final DamageSourcePredicate as4 = DamageSourcePredicate.fromJson(jsonObject.get("predicate"));
            return new DamageSourceCondition(as4, null);
        }
    }
}
