/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.common.collect.ImmutableMap * com.google.common.collect.ImmutableMap$Builder * com.google.common.collect.Maps * com.mojang.datafixers.kinds.App * com.mojang.datafixers.kinds.Applicative * com.mojang.serialization.Codec * com.mojang.serialization.DataResult * com.mojang.serialization.codecs.RecordCodecBuilder */ package net.minecraft.world.timeline; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; 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.codecs.RecordCodecBuilder; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.function.LongSupplier; import net.minecraft.core.Holder; import net.minecraft.core.registries.Registries; import net.minecraft.resources.RegistryFixedCodec; import net.minecraft.util.ExtraCodecs; import net.minecraft.util.KeyframeTrack; import net.minecraft.util.Util; import net.minecraft.world.attribute.EnvironmentAttribute; import net.minecraft.world.attribute.EnvironmentAttributes; import net.minecraft.world.attribute.modifier.AttributeModifier; import net.minecraft.world.level.Level; import net.minecraft.world.timeline.AttributeTrack; import net.minecraft.world.timeline.AttributeTrackSampler; public class Timeline { public static final Codec> CODEC = RegistryFixedCodec.create(Registries.TIMELINE); private static final Codec, AttributeTrack>> TRACKS_CODEC = Codec.dispatchedMap(EnvironmentAttributes.CODEC, Util.memoize(AttributeTrack::createCodec)); public static final Codec DIRECT_CODEC = RecordCodecBuilder.create(i -> i.group((App)ExtraCodecs.POSITIVE_INT.optionalFieldOf("period_ticks").forGetter(t -> t.periodTicks), (App)TRACKS_CODEC.optionalFieldOf("tracks", Map.of()).forGetter(t -> t.tracks)).apply((Applicative)i, Timeline::new)).validate(Timeline::validateInternal); public static final Codec NETWORK_CODEC = DIRECT_CODEC.xmap(Timeline::filterSyncableTracks, Timeline::filterSyncableTracks); private final Optional periodTicks; private final Map, AttributeTrack> tracks; private static Timeline filterSyncableTracks(Timeline timeline) { Map, AttributeTrack> syncableTracks = Map.copyOf(Maps.filterKeys(timeline.tracks, EnvironmentAttribute::isSyncable)); return new Timeline(timeline.periodTicks, syncableTracks); } private Timeline(Optional periodTicks, Map, AttributeTrack> tracks) { this.periodTicks = periodTicks; this.tracks = tracks; } private static DataResult validateInternal(Timeline timeline) { if (timeline.periodTicks.isEmpty()) { return DataResult.success((Object)timeline); } int periodTicks = timeline.periodTicks.get(); DataResult result = DataResult.success((Object)timeline); for (AttributeTrack track : timeline.tracks.values()) { result = result.apply2stable((t, $) -> t, AttributeTrack.validatePeriod(track, periodTicks)); } return result; } public static Builder builder() { return new Builder(); } public long getCurrentTicks(Level level) { long totalTicks = this.getTotalTicks(level); if (this.periodTicks.isEmpty()) { return totalTicks; } return totalTicks % (long)this.periodTicks.get().intValue(); } public long getTotalTicks(Level level) { return level.getDayTime(); } public Optional periodTicks() { return this.periodTicks; } public Set> attributes() { return this.tracks.keySet(); } public AttributeTrackSampler createTrackSampler(EnvironmentAttribute attribute, LongSupplier dayTimeGetter) { AttributeTrack track = this.tracks.get(attribute); if (track == null) { throw new IllegalStateException("Timeline has no track for " + String.valueOf(attribute)); } return track.bakeSampler(attribute, this.periodTicks, dayTimeGetter); } public static class Builder { private Optional periodTicks = Optional.empty(); private final ImmutableMap.Builder, AttributeTrack> tracks = ImmutableMap.builder(); private Builder() { } public Builder setPeriodTicks(int periodTicks) { this.periodTicks = Optional.of(periodTicks); return this; } public Builder addModifierTrack(EnvironmentAttribute attribute, AttributeModifier modifier, Consumer> builder) { attribute.type().checkAllowedModifier(modifier); KeyframeTrack.Builder argumentTrack = new KeyframeTrack.Builder(); builder.accept(argumentTrack); this.tracks.put(attribute, new AttributeTrack(modifier, argumentTrack.build())); return this; } public Builder addTrack(EnvironmentAttribute attribute, Consumer> builder) { return this.addModifierTrack(attribute, AttributeModifier.override(), builder); } public Timeline build() { return new Timeline(this.periodTicks, (Map, AttributeTrack>)this.tracks.build()); } } }