package net.minecraft.core.component.predicates; import com.mojang.serialization.Codec; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; import net.minecraft.core.component.DataComponentGetter; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; public interface DataComponentPredicate { Codec, DataComponentPredicate>> CODEC = Codec.dispatchedMap( BuiltInRegistries.DATA_COMPONENT_PREDICATE_TYPE.byNameCodec(), DataComponentPredicate.Type::codec ); StreamCodec> SINGLE_STREAM_CODEC = ByteBufCodecs.registry(Registries.DATA_COMPONENT_PREDICATE_TYPE) .dispatch(DataComponentPredicate.Single::type, DataComponentPredicate.Type::singleStreamCodec); StreamCodec, DataComponentPredicate>> STREAM_CODEC = SINGLE_STREAM_CODEC.apply( ByteBufCodecs.list(64) ) .map( list -> (Map)list.stream().collect(Collectors.toMap(DataComponentPredicate.Single::type, DataComponentPredicate.Single::predicate)), map -> map.entrySet().stream().map(DataComponentPredicate.Single::fromEntry).toList() ); static MapCodec> singleCodec(String name) { return BuiltInRegistries.DATA_COMPONENT_PREDICATE_TYPE .byNameCodec() .dispatchMap(name, DataComponentPredicate.Single::type, DataComponentPredicate.Type::wrappedCodec); } boolean matches(DataComponentGetter componentGetter); public record Single(DataComponentPredicate.Type type, T predicate) { private static DataComponentPredicate.Single fromEntry(Entry, T> entry) { return new DataComponentPredicate.Single<>((DataComponentPredicate.Type)entry.getKey(), (T)entry.getValue()); } } public static final class Type { private final Codec codec; private final MapCodec> wrappedCodec; private final StreamCodec> singleStreamCodec; public Type(Codec codec) { this.codec = codec; this.wrappedCodec = RecordCodecBuilder.mapCodec( instance -> instance.group(codec.fieldOf("value").forGetter(DataComponentPredicate.Single::predicate)) .apply(instance, dataComponentPredicate -> new DataComponentPredicate.Single<>(this, (T)dataComponentPredicate)) ); this.singleStreamCodec = ByteBufCodecs.fromCodecWithRegistries(codec) .map(dataComponentPredicate -> new DataComponentPredicate.Single<>(this, (T)dataComponentPredicate), DataComponentPredicate.Single::predicate); } public Codec codec() { return this.codec; } public MapCodec> wrappedCodec() { return this.wrappedCodec; } public StreamCodec> singleStreamCodec() { return this.singleStreamCodec; } } }