package net.minecraft.advancements.critereon;

import com.google.gson.JsonElement;
import java.util.Iterator;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.advancements.CriterionTriggerInstance;
import java.util.Collection;
import net.minecraft.world.entity.fishing.FishingHook;
import net.minecraft.world.item.ItemStack;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

public class FishingRodHookedTrigger extends SimpleCriterionTrigger<TriggerInstance> {
    private static final ResourceLocation ID;
    
    @Override
    public ResourceLocation getId() {
        return FishingRodHookedTrigger.ID;
    }
    
    @Override
    public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final ItemPredicate bj4 = ItemPredicate.fromJson(jsonObject.get("rod"));
        final EntityPredicate bb5 = EntityPredicate.fromJson(jsonObject.get("entity"));
        final ItemPredicate bj5 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(bj4, bb5, bj5);
    }
    
    public void trigger(final ServerPlayer xe, final ItemStack ben, final FishingHook avw, final Collection<ItemStack> collection) {
        this.trigger(xe.getAdvancements(), a -> a.matches(xe, ben, avw, collection));
    }
    
    static {
        ID = new ResourceLocation("fishing_rod_hooked");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final ItemPredicate rod;
        private final EntityPredicate entity;
        private final ItemPredicate item;
        
        public TriggerInstance(final ItemPredicate bj1, final EntityPredicate bb, final ItemPredicate bj3) {
            super(FishingRodHookedTrigger.ID);
            this.rod = bj1;
            this.entity = bb;
            this.item = bj3;
        }
        
        public static TriggerInstance fishedItem(final ItemPredicate bj1, final EntityPredicate bb, final ItemPredicate bj3) {
            return new TriggerInstance(bj1, bb, bj3);
        }
        
        public boolean matches(final ServerPlayer xe, final ItemStack ben, final FishingHook avw, final Collection<ItemStack> collection) {
            if (!this.rod.matches(ben)) {
                return false;
            }
            if (!this.entity.matches(xe, avw.hookedIn)) {
                return false;
            }
            if (this.item != ItemPredicate.ANY) {
                boolean boolean6 = false;
                if (avw.hookedIn instanceof ItemEntity) {
                    final ItemEntity awb7 = (ItemEntity)avw.hookedIn;
                    if (this.item.matches(awb7.getItem())) {
                        boolean6 = true;
                    }
                }
                for (final ItemStack ben2 : collection) {
                    if (this.item.matches(ben2)) {
                        boolean6 = true;
                        break;
                    }
                }
                if (!boolean6) {
                    return false;
                }
            }
            return true;
        }
        
        @Override
        public JsonElement serializeToJson() {
            final JsonObject jsonObject2 = new JsonObject();
            jsonObject2.add("rod", this.rod.serializeToJson());
            jsonObject2.add("entity", this.entity.serializeToJson());
            jsonObject2.add("item", this.item.serializeToJson());
            return jsonObject2;
        }
    }
}
