| 1 | package net.minecraft.core.component.predicates;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Map.Entry;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import net.minecraft.core.component.DataComponentGetter;
|
| 11 | import net.minecraft.core.component.DataComponentType;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.core.registries.Registries;
|
| 14 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 |
|
| 18 | public interface DataComponentPredicate {
|
| 19 | Codec<Map<DataComponentPredicate.Type<?>, DataComponentPredicate>> CODEC = Codec.dispatchedMap(
|
| 20 | DataComponentPredicate.Type.CODEC, DataComponentPredicate.Type::codec
|
| 21 | );
|
| 22 | StreamCodec<RegistryFriendlyByteBuf, DataComponentPredicate.Single<?>> SINGLE_STREAM_CODEC = DataComponentPredicate.Type.STREAM_CODEC
|
| 23 | .dispatch(DataComponentPredicate.Single::type, DataComponentPredicate.Type::singleStreamCodec);
|
| 24 | StreamCodec<RegistryFriendlyByteBuf, Map<DataComponentPredicate.Type<?>, DataComponentPredicate>> STREAM_CODEC = SINGLE_STREAM_CODEC.apply(
|
| 25 | ByteBufCodecs.list(64)
|
| 26 | )
|
| 27 | .map(
|
| 28 | singles -> singles.stream().collect(Collectors.toMap(DataComponentPredicate.Single::type, DataComponentPredicate.Single::predicate)),
|
| 29 | map -> map.entrySet().stream().map(DataComponentPredicate.Single::fromEntry).toList()
|
| 30 | );
|
| 31 |
|
| 32 | static MapCodec<DataComponentPredicate.Single<?>> singleCodec(final String name) {
|
| 33 | return DataComponentPredicate.Type.CODEC.dispatchMap(name, DataComponentPredicate.Single::type, DataComponentPredicate.Type::wrappedCodec);
|
| 34 | }
|
| 35 |
|
| 36 | boolean matches(DataComponentGetter components);
|
| 37 |
|
| 38 | final class AnyValueType extends DataComponentPredicate.TypeBase<AnyValue> {
|
| 39 | private final AnyValue predicate;
|
| 40 |
|
| 41 | public AnyValueType(final AnyValue predicate) {
|
| 42 | super(MapCodec.unitCodec(predicate));
|
| 43 | this.predicate = predicate;
|
| 44 | }
|
| 45 |
|
| 46 | public AnyValue predicate() {
|
| 47 | return this.predicate;
|
| 48 | }
|
| 49 |
|
| 50 | public DataComponentType<?> componentType() {
|
| 51 | return this.predicate.type();
|
| 52 | }
|
| 53 |
|
| 54 | public static DataComponentPredicate.AnyValueType create(final DataComponentType<?> componentType) {
|
| 55 | return new DataComponentPredicate.AnyValueType(new AnyValue(componentType));
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | final class ConcreteType<T extends DataComponentPredicate> extends DataComponentPredicate.TypeBase<T> {
|
| 60 | public ConcreteType(final Codec<T> codec) {
|
| 61 | super(codec);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | record Single<T extends DataComponentPredicate>(DataComponentPredicate.Type<T> type, T predicate) {
|
| 66 | private static <T extends DataComponentPredicate> MapCodec<DataComponentPredicate.Single<T>> wrapCodec(
|
| 67 | final DataComponentPredicate.Type<T> type, final Codec<T> codec
|
| 68 | ) {
|
| 69 | return RecordCodecBuilder.mapCodec(
|
| 70 | i -> i.group(codec.fieldOf("value").forGetter(DataComponentPredicate.Single::predicate))
|
| 71 | .apply(i, predicate -> new DataComponentPredicate.Single<>(type, predicate))
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | private static <T extends DataComponentPredicate> DataComponentPredicate.Single<T> fromEntry(final Entry<DataComponentPredicate.Type<?>, T> e) {
|
| 76 | return new DataComponentPredicate.Single<>((DataComponentPredicate.Type<T>)e.getKey(), e.getValue());
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | interface Type<T extends DataComponentPredicate> {
|
| 81 | Codec<DataComponentPredicate.Type<?>> CODEC = Codec.either(
|
| 82 | BuiltInRegistries.DATA_COMPONENT_PREDICATE_TYPE.byNameCodec(), BuiltInRegistries.DATA_COMPONENT_TYPE.byNameCodec()
|
| 83 | )
|
| 84 | .xmap(DataComponentPredicate.Type::copyOrCreateType, DataComponentPredicate.Type::unpackType);
|
| 85 | StreamCodec<RegistryFriendlyByteBuf, DataComponentPredicate.Type<?>> STREAM_CODEC = ByteBufCodecs.either(
|
| 86 | ByteBufCodecs.registry(Registries.DATA_COMPONENT_PREDICATE_TYPE), ByteBufCodecs.registry(Registries.DATA_COMPONENT_TYPE)
|
| 87 | )
|
| 88 | .map(DataComponentPredicate.Type::copyOrCreateType, DataComponentPredicate.Type::unpackType);
|
| 89 |
|
| 90 | private static <T extends DataComponentPredicate.Type<?>> Either<T, DataComponentType<?>> unpackType(final T type) {
|
| 91 | return type instanceof DataComponentPredicate.AnyValueType anyCheck ? Either.right(anyCheck.componentType()) : Either.left(type);
|
| 92 | }
|
| 93 |
|
| 94 | private static DataComponentPredicate.Type<?> copyOrCreateType(final Either<DataComponentPredicate.Type<?>, DataComponentType<?>> concreteTypeOrComponent) {
|
| 95 | return concreteTypeOrComponent.map(concrete -> concrete, DataComponentPredicate.AnyValueType::create);
|
| 96 | }
|
| 97 |
|
| 98 | Codec<T> codec();
|
| 99 |
|
| 100 | MapCodec<DataComponentPredicate.Single<T>> wrappedCodec();
|
| 101 |
|
| 102 | StreamCodec<RegistryFriendlyByteBuf, DataComponentPredicate.Single<T>> singleStreamCodec();
|
| 103 | }
|
| 104 |
|
| 105 | abstract class TypeBase<T extends DataComponentPredicate> implements DataComponentPredicate.Type<T> {
|
| 106 | private final Codec<T> codec;
|
| 107 | private final MapCodec<DataComponentPredicate.Single<T>> wrappedCodec;
|
| 108 | private final StreamCodec<RegistryFriendlyByteBuf, DataComponentPredicate.Single<T>> singleStreamCodec;
|
| 109 |
|
| 110 | public TypeBase(final Codec<T> codec) {
|
| 111 | this.codec = codec;
|
| 112 | this.wrappedCodec = DataComponentPredicate.Single.wrapCodec(this, codec);
|
| 113 | this.singleStreamCodec = ByteBufCodecs.fromCodecWithRegistries(codec)
|
| 114 | .map(v -> new DataComponentPredicate.Single<>(this, (T)v), DataComponentPredicate.Single::predicate);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public Codec<T> codec() {
|
| 119 | return this.codec;
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public MapCodec<DataComponentPredicate.Single<T>> wrappedCodec() {
|
| 124 | return this.wrappedCodec;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public StreamCodec<RegistryFriendlyByteBuf, DataComponentPredicate.Single<T>> singleStreamCodec() {
|
| 129 | return this.singleStreamCodec;
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|