package net.minecraft.advancements.critereon;

import javax.annotation.Nullable;
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.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class BrewedPotionTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return BrewedPotionTrigger.ID;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        Potion bmb5 = null;
        if (jsonObject.has("potion")) {
            final ResourceLocation uh6 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "potion"));
            final Object o;
            final Object obj;
            bmb5 = Registry.POTION.getOptional(uh6).<Throwable>orElseThrow(() -> {
                new JsonSyntaxException("Unknown potion '" + obj + "'");
                return o;
            });
        }
        return new TriggerInstance(b, bmb5);
    }
    
    public void trigger(final ServerPlayer ze, final Potion bmb) {
        this.trigger(ze, a -> a.matches(bmb));
    }
    
    static {
        ID = new ResourceLocation("brewed_potion");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final Potion potion;
        
        public TriggerInstance(final EntityPredicate.Composite b, @Nullable final Potion bmb) {
            super(BrewedPotionTrigger.ID, b);
            this.potion = bmb;
        }
        
        public static TriggerInstance brewedPotion() {
            return new TriggerInstance(EntityPredicate.Composite.ANY, null);
        }
        
        public boolean matches(final Potion bmb) {
            return this.potion == null || this.potion == bmb;
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            if (this.potion != null) {
                jsonObject3.addProperty("potion", Registry.POTION.getKey(this.potion).toString());
            }
            return jsonObject3;
        }
    }
}
