package net.minecraft.advancements.critereon;

import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.tags.SerializationTags;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonElement;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import javax.annotation.Nullable;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;

public class FluidPredicate {
    public static final FluidPredicate ANY;
    @Nullable
    private final Tag<Fluid> tag;
    @Nullable
    private final Fluid fluid;
    private final StatePropertiesPredicate properties;
    
    public FluidPredicate(@Nullable final Tag<Fluid> adf, @Nullable final Fluid cwz, final StatePropertiesPredicate ck) {
        this.tag = adf;
        this.fluid = cwz;
        this.properties = ck;
    }
    
    public boolean matches(final ServerLevel zd, final BlockPos fu) {
        if (this == FluidPredicate.ANY) {
            return true;
        }
        if (!zd.isLoaded(fu)) {
            return false;
        }
        final FluidState cxa4 = zd.getFluidState(fu);
        final Fluid cwz5 = cxa4.getType();
        return (this.tag == null || this.tag.contains(cwz5)) && (this.fluid == null || cwz5 == this.fluid) && this.properties.matches(cxa4);
    }
    
    public static FluidPredicate fromJson(@Nullable final JsonElement jsonElement) {
        if (jsonElement == null || jsonElement.isJsonNull()) {
            return FluidPredicate.ANY;
        }
        final JsonObject jsonObject2 = GsonHelper.convertToJsonObject(jsonElement, "fluid");
        Fluid cwz3 = null;
        if (jsonObject2.has("fluid")) {
            final ResourceLocation uh4 = new ResourceLocation(GsonHelper.getAsString(jsonObject2, "fluid"));
            cwz3 = Registry.FLUID.get(uh4);
        }
        Tag<Fluid> adf4 = null;
        if (jsonObject2.has("tag")) {
            final ResourceLocation uh5 = new ResourceLocation(GsonHelper.getAsString(jsonObject2, "tag"));
            adf4 = SerializationTags.getInstance().getFluids().getTag(uh5);
            if (adf4 == null) {
                throw new JsonSyntaxException("Unknown fluid tag '" + uh5 + "'");
            }
        }
        final StatePropertiesPredicate ck5 = StatePropertiesPredicate.fromJson(jsonObject2.get("state"));
        return new FluidPredicate(adf4, cwz3, ck5);
    }
    
    public JsonElement serializeToJson() {
        if (this == FluidPredicate.ANY) {
            return JsonNull.INSTANCE;
        }
        final JsonObject jsonObject2 = new JsonObject();
        if (this.fluid != null) {
            jsonObject2.addProperty("fluid", Registry.FLUID.getKey(this.fluid).toString());
        }
        if (this.tag != null) {
            jsonObject2.addProperty("tag", SerializationTags.getInstance().getFluids().getIdOrThrow(this.tag).toString());
        }
        jsonObject2.add("state", this.properties.serializeToJson());
        return jsonObject2;
    }
    
    static {
        ANY = new FluidPredicate(null, null, StatePropertiesPredicate.ANY);
    }
}
