package net.minecraft.world.entity.ai.memory; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.Optional; import net.minecraft.util.VisibleForDebug; public class ExpirableValue { private final T value; private long timeToLive; public ExpirableValue(T value, long timeToLive) { this.value = value; this.timeToLive = timeToLive; } public void tick() { if (this.canExpire()) { this.timeToLive--; } } public static ExpirableValue of(T value) { return new ExpirableValue<>(value, Long.MAX_VALUE); } public static ExpirableValue of(T value, long timeToLive) { return new ExpirableValue<>(value, timeToLive); } public long getTimeToLive() { return this.timeToLive; } public T getValue() { return this.value; } public boolean hasExpired() { return this.timeToLive <= 0L; } public String toString() { return this.value + (this.canExpire() ? " (ttl: " + this.timeToLive + ")" : ""); } @VisibleForDebug public boolean canExpire() { return this.timeToLive != Long.MAX_VALUE; } public static Codec> codec(Codec valueCodec) { return RecordCodecBuilder.create( instance -> instance.group( valueCodec.fieldOf("value").forGetter(expirableValue -> expirableValue.value), Codec.LONG .lenientOptionalFieldOf("ttl") .forGetter(expirableValue -> expirableValue.canExpire() ? Optional.of(expirableValue.timeToLive) : Optional.empty()) ) .apply(instance, (object, optional) -> new ExpirableValue<>(object, (Long)optional.orElse(Long.MAX_VALUE))) ); } }