/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.common.collect.Comparators * com.google.common.collect.ImmutableList * com.google.common.collect.ImmutableList$Builder * com.mojang.datafixers.kinds.App * com.mojang.datafixers.kinds.Applicative * com.mojang.serialization.Codec * com.mojang.serialization.DataResult * com.mojang.serialization.MapCodec * com.mojang.serialization.codecs.RecordCodecBuilder */ package net.minecraft.util; import com.google.common.collect.Comparators; import com.google.common.collect.ImmutableList; import com.mojang.datafixers.kinds.App; import com.mojang.datafixers.kinds.Applicative; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.Comparator; import java.util.List; import java.util.Optional; import net.minecraft.util.EasingType; import net.minecraft.util.Keyframe; import net.minecraft.util.KeyframeTrackSampler; import net.minecraft.world.attribute.LerpFunction; public record KeyframeTrack(List> keyframes, EasingType easingType) { public KeyframeTrack { if (keyframes.isEmpty()) { throw new IllegalArgumentException("Track has no keyframes"); } } public static MapCodec> mapCodec(Codec valueCodec) { Codec keyframesCodec = Keyframe.codec(valueCodec).listOf().validate(KeyframeTrack::validateKeyframes); return RecordCodecBuilder.mapCodec((T i) -> i.group((App)keyframesCodec.fieldOf("keyframes").forGetter(KeyframeTrack::keyframes), (App)EasingType.CODEC.optionalFieldOf("ease", (Object)EasingType.LINEAR).forGetter(KeyframeTrack::easingType)).apply((Applicative)i, KeyframeTrack::new)); } private static DataResult>> validateKeyframes(List> keyframes) { if (keyframes.isEmpty()) { return DataResult.error(() -> "Keyframes must not be empty"); } if (!Comparators.isInOrder(keyframes, Comparator.comparingInt(Keyframe::ticks))) { return DataResult.error(() -> "Keyframes must be ordered by ticks field"); } if (keyframes.size() > 1) { int repeatCount = 0; int lastTicks = keyframes.getLast().ticks(); for (Keyframe keyframe : keyframes) { if (keyframe.ticks() == lastTicks) { if (++repeatCount > 2) { return DataResult.error(() -> "More than 2 keyframes on same tick: " + keyframe.ticks()); } } else { repeatCount = 0; } lastTicks = keyframe.ticks(); } } return DataResult.success(keyframes); } public static DataResult> validatePeriod(KeyframeTrack track, int periodTicks) { for (Keyframe keyframe : track.keyframes()) { int tick = keyframe.ticks(); if (tick >= 0 && tick <= periodTicks) continue; return DataResult.error(() -> "Keyframe at tick " + keyframe.ticks() + " must be in range [0; " + periodTicks + "]"); } return DataResult.success(track); } public KeyframeTrackSampler bakeSampler(Optional periodTicks, LerpFunction lerp) { return new KeyframeTrackSampler(this, periodTicks, lerp); } public static class Builder { private final ImmutableList.Builder> keyframes = ImmutableList.builder(); private EasingType easing = EasingType.LINEAR; public Builder addKeyframe(int ticks, T value) { this.keyframes.add(new Keyframe(ticks, value)); return this; } public Builder setEasing(EasingType easing) { this.easing = easing; return this; } public KeyframeTrack build() { List keyframes = (List)KeyframeTrack.validateKeyframes(this.keyframes.build()).getOrThrow(); return new KeyframeTrack(keyframes, this.easing); } } }