133 lines
5.7 KiB
Java
133 lines
5.7 KiB
Java
/*
|
|
* 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<Holder<Timeline>> CODEC = RegistryFixedCodec.create(Registries.TIMELINE);
|
|
private static final Codec<Map<EnvironmentAttribute<?>, AttributeTrack<?, ?>>> TRACKS_CODEC = Codec.dispatchedMap(EnvironmentAttributes.CODEC, Util.memoize(AttributeTrack::createCodec));
|
|
public static final Codec<Timeline> 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<Timeline> NETWORK_CODEC = DIRECT_CODEC.xmap(Timeline::filterSyncableTracks, Timeline::filterSyncableTracks);
|
|
private final Optional<Integer> periodTicks;
|
|
private final Map<EnvironmentAttribute<?>, AttributeTrack<?, ?>> tracks;
|
|
|
|
private static Timeline filterSyncableTracks(Timeline timeline) {
|
|
Map<EnvironmentAttribute<?>, AttributeTrack<?, ?>> syncableTracks = Map.copyOf(Maps.filterKeys(timeline.tracks, EnvironmentAttribute::isSyncable));
|
|
return new Timeline(timeline.periodTicks, syncableTracks);
|
|
}
|
|
|
|
private Timeline(Optional<Integer> periodTicks, Map<EnvironmentAttribute<?>, AttributeTrack<?, ?>> tracks) {
|
|
this.periodTicks = periodTicks;
|
|
this.tracks = tracks;
|
|
}
|
|
|
|
private static DataResult<Timeline> 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<Integer> periodTicks() {
|
|
return this.periodTicks;
|
|
}
|
|
|
|
public Set<EnvironmentAttribute<?>> attributes() {
|
|
return this.tracks.keySet();
|
|
}
|
|
|
|
public <Value> AttributeTrackSampler<Value, ?> createTrackSampler(EnvironmentAttribute<Value> 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<Integer> periodTicks = Optional.empty();
|
|
private final ImmutableMap.Builder<EnvironmentAttribute<?>, AttributeTrack<?, ?>> tracks = ImmutableMap.builder();
|
|
|
|
private Builder() {
|
|
}
|
|
|
|
public Builder setPeriodTicks(int periodTicks) {
|
|
this.periodTicks = Optional.of(periodTicks);
|
|
return this;
|
|
}
|
|
|
|
public <Value, Argument> Builder addModifierTrack(EnvironmentAttribute<Value> attribute, AttributeModifier<Value, Argument> modifier, Consumer<KeyframeTrack.Builder<Argument>> builder) {
|
|
attribute.type().checkAllowedModifier(modifier);
|
|
KeyframeTrack.Builder argumentTrack = new KeyframeTrack.Builder();
|
|
builder.accept(argumentTrack);
|
|
this.tracks.put(attribute, new AttributeTrack<Value, Argument>(modifier, argumentTrack.build()));
|
|
return this;
|
|
}
|
|
|
|
public <Value> Builder addTrack(EnvironmentAttribute<Value> attribute, Consumer<KeyframeTrack.Builder<Value>> builder) {
|
|
return this.addModifierTrack(attribute, AttributeModifier.override(), builder);
|
|
}
|
|
|
|
public Timeline build() {
|
|
return new Timeline(this.periodTicks, (Map<EnvironmentAttribute<?>, AttributeTrack<?, ?>>)this.tracks.build());
|
|
}
|
|
}
|
|
}
|
|
|