package net.minecraft.advancements.critereon; import com.mojang.serialization.Codec; import it.unimi.dsi.fastutil.ints.IntList; import java.util.Map; import java.util.Map.Entry; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.SlotAccess; import net.minecraft.world.inventory.SlotRange; import net.minecraft.world.inventory.SlotRanges; public record SlotsPredicate(Map slots) { public static final Codec CODEC = Codec.unboundedMap(SlotRanges.CODEC, ItemPredicate.CODEC).xmap(SlotsPredicate::new, SlotsPredicate::slots); public boolean matches(Entity entity) { for (Entry entry : this.slots.entrySet()) { if (!matchSlots(entity, (ItemPredicate)entry.getValue(), ((SlotRange)entry.getKey()).slots())) { return false; } } return true; } private static boolean matchSlots(Entity entity, ItemPredicate predicate, IntList slots) { for (int i = 0; i < slots.size(); i++) { int j = slots.getInt(i); SlotAccess slotAccess = entity.getSlot(j); if (predicate.test(slotAccess.get())) { return true; } } return false; } }