| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.collect.Comparators;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.Comparator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import net.minecraft.world.attribute.LerpFunction;
|
| 13 |
|
| 14 | public record KeyframeTrack<T>(List<Keyframe<T>> keyframes, EasingType easingType) {
|
| 15 | public KeyframeTrack {
|
| 16 | if (keyframes.isEmpty()) {
|
| 17 | throw new IllegalArgumentException("Track has no keyframes");
|
| 18 | }
|
| 19 | }
|
| 20 |
|
| 21 | public static <T> MapCodec<KeyframeTrack<T>> mapCodec(final Codec<T> valueCodec) {
|
| 22 | Codec<List<Keyframe<T>>> keyframesCodec = Keyframe.codec(valueCodec).listOf().validate(KeyframeTrack::validateKeyframes);
|
| 23 | return RecordCodecBuilder.mapCodec(
|
| 24 | i -> i.group(
|
| 25 | keyframesCodec.fieldOf("keyframes").forGetter(KeyframeTrack::keyframes),
|
| 26 | EasingType.CODEC.optionalFieldOf("ease", EasingType.LINEAR).forGetter(KeyframeTrack::easingType)
|
| 27 | )
|
| 28 | .apply(i, KeyframeTrack::new)
|
| 29 | );
|
| 30 | }
|
| 31 |
|
| 32 | private static <T> DataResult<List<Keyframe<T>>> validateKeyframes(final List<Keyframe<T>> keyframes) {
|
| 33 | if (keyframes.isEmpty()) {
|
| 34 | return DataResult.error(() -> "Keyframes must not be empty");
|
| 35 | }
|
| 36 |
|
| 37 | if (!Comparators.isInOrder(keyframes, Comparator.comparingInt(Keyframe::ticks))) {
|
| 38 | return DataResult.error(() -> "Keyframes must be ordered by ticks field");
|
| 39 | }
|
| 40 |
|
| 41 | if (keyframes.size() > 1) {
|
| 42 | int repeatCount = 0;
|
| 43 | int lastTicks = keyframes.getLast().ticks();
|
| 44 |
|
| 45 | for (Keyframe<T> keyframe : keyframes) {
|
| 46 | if (keyframe.ticks() == lastTicks) {
|
| 47 | if (++repeatCount > 2) {
|
| 48 | return DataResult.error(() -> "More than 2 keyframes on same tick: " + keyframe.ticks());
|
| 49 | }
|
| 50 | } else {
|
| 51 | repeatCount = 0;
|
| 52 | }
|
| 53 |
|
| 54 | lastTicks = keyframe.ticks();
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return DataResult.success(keyframes);
|
| 59 | }
|
| 60 |
|
| 61 | public static DataResult<KeyframeTrack<?>> validatePeriod(final KeyframeTrack<?> track, final int periodTicks) {
|
| 62 | for (Keyframe<?> keyframe : track.keyframes()) {
|
| 63 | int tick = keyframe.ticks();
|
| 64 | if (tick < 0 || tick > periodTicks) {
|
| 65 | return DataResult.error(() -> "Keyframe at tick " + keyframe.ticks() + " must be in range [0; " + periodTicks + "]");
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | return DataResult.success(track);
|
| 70 | }
|
| 71 |
|
| 72 | public KeyframeTrackSampler<T> bakeSampler(final Optional<Integer> periodTicks, final LerpFunction<T> lerp) {
|
| 73 | return new KeyframeTrackSampler<>(this, periodTicks, lerp);
|
| 74 | }
|
| 75 |
|
| 76 | public static class Builder<T> {
|
| 77 | private final ImmutableList.Builder<Keyframe<T>> keyframes = ImmutableList.builder();
|
| 78 | private EasingType easing = EasingType.LINEAR;
|
| 79 |
|
| 80 | public KeyframeTrack.Builder<T> addKeyframe(final int ticks, final T value) {
|
| 81 | this.keyframes.add(new Keyframe<>(ticks, value));
|
| 82 | return this;
|
| 83 | }
|
| 84 |
|
| 85 | public KeyframeTrack.Builder<T> setEasing(final EasingType easing) {
|
| 86 | this.easing = easing;
|
| 87 | return this;
|
| 88 | }
|
| 89 |
|
| 90 | public KeyframeTrack<T> build() {
|
| 91 | List<Keyframe<T>> keyframes = KeyframeTrack.validateKeyframes(this.keyframes.build()).getOrThrow();
|
| 92 | return new KeyframeTrack<>(keyframes, this.easing);
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|