| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.util.ExtraCodecs;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public record CustomModelData(List<Float> floats, List<Boolean> flags, List<String> strings, List<Integer> colors) {
|
| 13 | public static final CustomModelData EMPTY = new CustomModelData(List.of(), List.of(), List.of(), List.of());
|
| 14 | public static final Codec<CustomModelData> CODEC = RecordCodecBuilder.create(
|
| 15 | i -> i.group(
|
| 16 | Codec.FLOAT.listOf().optionalFieldOf("floats", List.of()).forGetter(CustomModelData::floats),
|
| 17 | Codec.BOOL.listOf().optionalFieldOf("flags", List.of()).forGetter(CustomModelData::flags),
|
| 18 | Codec.STRING.listOf().optionalFieldOf("strings", List.of()).forGetter(CustomModelData::strings),
|
| 19 | ExtraCodecs.RGB_COLOR_CODEC.listOf().optionalFieldOf("colors", List.of()).forGetter(CustomModelData::colors)
|
| 20 | )
|
| 21 | .apply(i, CustomModelData::new)
|
| 22 | );
|
| 23 | public static final StreamCodec<ByteBuf, CustomModelData> STREAM_CODEC = StreamCodec.composite(
|
| 24 | ByteBufCodecs.FLOAT.apply(ByteBufCodecs.list()),
|
| 25 | CustomModelData::floats,
|
| 26 | ByteBufCodecs.BOOL.apply(ByteBufCodecs.list()),
|
| 27 | CustomModelData::flags,
|
| 28 | ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list()),
|
| 29 | CustomModelData::strings,
|
| 30 | ByteBufCodecs.INT.apply(ByteBufCodecs.list()),
|
| 31 | CustomModelData::colors,
|
| 32 | CustomModelData::new
|
| 33 | );
|
| 34 |
|
| 35 | private static <T> @Nullable T getSafe(final List<T> values, final int index) {
|
| 36 | return index >= 0 && index < values.size() ? values.get(index) : null;
|
| 37 | }
|
| 38 |
|
| 39 | public @Nullable Float getFloat(final int index) {
|
| 40 | return getSafe(this.floats, index);
|
| 41 | }
|
| 42 |
|
| 43 | public @Nullable Boolean getBoolean(final int index) {
|
| 44 | return getSafe(this.flags, index);
|
| 45 | }
|
| 46 |
|
| 47 | public @Nullable String getString(final int index) {
|
| 48 | return getSafe(this.strings, index);
|
| 49 | }
|
| 50 |
|
| 51 | public @Nullable Integer getColor(final int index) {
|
| 52 | return getSafe(this.colors, index);
|
| 53 | }
|
| 54 | }
|
| 55 |
|