package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import javax.annotation.Nullable;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonSyntaxException;
import net.minecraft.core.Registry;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class BrewedPotionTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return BrewedPotionTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        Potion bgd4 = null;
        if (jsonObject.has("potion")) {
            final ResourceLocation sm5 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "potion"));
            final Object o;
            final Object obj;
            bgd4 = Registry.POTION.getOptional(sm5).<Throwable>orElseThrow(() -> {
                new JsonSyntaxException("Unknown potion '" + obj + "'");
                return o;
            });
        }
        return new TriggerInstance(bgd4);
    }
    
    public void trigger(final ServerPlayer xe, final Potion bgd) {
        this.trigger(xe.getAdvancements(), a -> a.matches(bgd));
    }
    
    static {
        ID = new ResourceLocation("brewed_potion");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final Potion potion;
        
        public TriggerInstance(@Nullable final Potion bgd) {
            super(BrewedPotionTrigger.ID);
            this.potion = bgd;
        }
        
        public static TriggerInstance brewedPotion() {
            return new TriggerInstance((Potion)null);
        }
        
        public boolean matches(final Potion bgd) {
            return this.potion == null || this.potion == bgd;
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            if (this.potion != null) {
                jsonObject2.addProperty("potion", Registry.POTION.getKey(this.potion).toString());
            }
            return jsonObject2;
        }
    }
}
