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.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
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.ItemPredicate;

public class MatchTool implements LootItemCondition {
    private final ItemPredicate predicate;
    
    public MatchTool(final ItemPredicate bo) {
        this.predicate = bo;
    }
    
    @Override
    public LootItemConditionType getType() {
        return LootItemConditions.MATCH_TOOL;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(LootContextParams.TOOL);
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final ItemStack bki3 = dat.<ItemStack>getParamOrNull(LootContextParams.TOOL);
        return bki3 != null && this.predicate.matches(bki3);
    }
    
    public static Builder toolMatches(final ItemPredicate.Builder a) {
        return () -> new MatchTool(a.build());
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<MatchTool> {
        @Override
        public void serialize(final JsonObject jsonObject, final MatchTool ddt, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("predicate", ddt.predicate.serializeToJson());
        }
        
        @Override
        public MatchTool deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final ItemPredicate bo4 = ItemPredicate.fromJson(jsonObject.get("predicate"));
            return new MatchTool(bo4);
        }
    }
}
