package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class RecipeUnlockedTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return RecipeUnlockedTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final ResourceLocation sm4 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "recipe"));
        return new TriggerInstance(sm4);
    }
    
    public void trigger(final ServerPlayer xe, final Recipe<?> bgw) {
        this.trigger(xe.getAdvancements(), a -> a.matches(bgw));
    }
    
    static {
        ID = new ResourceLocation("recipe_unlocked");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final ResourceLocation recipe;
        
        public TriggerInstance(final ResourceLocation sm) {
            super(RecipeUnlockedTrigger.ID);
            this.recipe = sm;
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.addProperty("recipe", this.recipe.toString());
            return jsonObject2;
        }
        
        public boolean matches(final Recipe<?> bgw) {
            return this.recipe.equals(bgw.getId());
        }
    }
}
