package net.minecraft.advancements.critereon;

import java.util.Iterator;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.storage.loot.LootContext;
import java.util.Collection;
import net.minecraft.world.entity.projectile.FishingHook;
import net.minecraft.world.item.ItemStack;
import net.minecraft.server.level.ServerPlayer;
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;
    }
    
    public TriggerInstance createInstance(final JsonObject jsonObject, final EntityPredicate.Composite b, final DeserializationContext av) {
        final ItemPredicate bo5 = ItemPredicate.fromJson(jsonObject.get("rod"));
        final EntityPredicate.Composite b2 = EntityPredicate.Composite.fromJson(jsonObject, "entity", av);
        final ItemPredicate bo6 = ItemPredicate.fromJson(jsonObject.get("item"));
        return new TriggerInstance(b, bo5, b2, bo6);
    }
    
    public void trigger(final ServerPlayer ze, final ItemStack bki, final FishingHook beo, final Collection<ItemStack> collection) {
        final LootContext dat6 = EntityPredicate.createContext(ze, (beo.getHookedIn() != null) ? beo.getHookedIn() : beo);
        this.trigger(ze, a -> a.matches(bki, dat6, collection));
    }
    
    static {
        ID = new ResourceLocation("fishing_rod_hooked");
    }
    
    public static class TriggerInstance extends AbstractCriterionTriggerInstance {
        private final ItemPredicate rod;
        private final EntityPredicate.Composite entity;
        private final ItemPredicate item;
        
        public TriggerInstance(final EntityPredicate.Composite b1, final ItemPredicate bo2, final EntityPredicate.Composite b3, final ItemPredicate bo4) {
            super(FishingRodHookedTrigger.ID, b1);
            this.rod = bo2;
            this.entity = b3;
            this.item = bo4;
        }
        
        public static TriggerInstance fishedItem(final ItemPredicate bo1, final EntityPredicate be, final ItemPredicate bo3) {
            return new TriggerInstance(EntityPredicate.Composite.ANY, bo1, EntityPredicate.Composite.wrap(be), bo3);
        }
        
        public boolean matches(final ItemStack bki, final LootContext dat, final Collection<ItemStack> collection) {
            if (!this.rod.matches(bki)) {
                return false;
            }
            if (!this.entity.matches(dat)) {
                return false;
            }
            if (this.item != ItemPredicate.ANY) {
                boolean boolean5 = false;
                final Entity aom6 = dat.<Entity>getParamOrNull(LootContextParams.THIS_ENTITY);
                if (aom6 instanceof ItemEntity) {
                    final ItemEntity bbg7 = (ItemEntity)aom6;
                    if (this.item.matches(bbg7.getItem())) {
                        boolean5 = true;
                    }
                }
                for (final ItemStack bki2 : collection) {
                    if (this.item.matches(bki2)) {
                        boolean5 = true;
                        break;
                    }
                }
                if (!boolean5) {
                    return false;
                }
            }
            return true;
        }
        
        @Override
        public JsonObject serializeToJson(final SerializationContext cg) {
            final JsonObject jsonObject3 = super.serializeToJson(cg);
            jsonObject3.add("rod", this.rod.serializeToJson());
            jsonObject3.add("entity", this.entity.toJson(cg));
            jsonObject3.add("item", this.item.serializeToJson());
            return jsonObject3;
        }
    }
}
