package net.minecraft.advancements.critereon;

import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.tags.FluidTags;
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> aaz, @Nullable final Fluid coi, final StatePropertiesPredicate cc) {
        this.tag = aaz;
        this.fluid = coi;
        this.properties = cc;
    }
    
    public boolean matches(final ServerLevel xd, final BlockPos fk) {
        if (this == FluidPredicate.ANY) {
            return true;
        }
        if (!xd.isLoaded(fk)) {
            return false;
        }
        final FluidState coj4 = xd.getFluidState(fk);
        final Fluid coi5 = coj4.getType();
        return (this.tag == null || this.tag.contains(coi5)) && (this.fluid == null || coi5 == this.fluid) && this.properties.matches(coj4);
    }
    
    public static FluidPredicate fromJson(@Nullable final JsonElement jsonElement) {
        if (jsonElement == null || jsonElement.isJsonNull()) {
            return FluidPredicate.ANY;
        }
        final JsonObject jsonObject2 = GsonHelper.convertToJsonObject(jsonElement, "fluid");
        Fluid coi3 = null;
        if (jsonObject2.has("fluid")) {
            final ResourceLocation sm4 = new ResourceLocation(GsonHelper.getAsString(jsonObject2, "fluid"));
            coi3 = Registry.FLUID.get(sm4);
        }
        Tag<Fluid> aaz4 = null;
        if (jsonObject2.has("tag")) {
            final ResourceLocation sm5 = new ResourceLocation(GsonHelper.getAsString(jsonObject2, "tag"));
            aaz4 = FluidTags.getAllTags().getTag(sm5);
            if (aaz4 == null) {
                throw new JsonSyntaxException("Unknown fluid tag '" + sm5 + "'");
            }
        }
        final StatePropertiesPredicate cc5 = StatePropertiesPredicate.fromJson(jsonObject2.get("state"));
        return new FluidPredicate(aaz4, coi3, cc5);
    }
    
    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", this.tag.getId().toString());
        }
        jsonObject2.add("state", this.properties.serializeToJson());
        return jsonObject2;
    }
    
    static {
        ANY = new FluidPredicate(null, null, StatePropertiesPredicate.ANY);
    }
}
