69 lines
2.2 KiB
Java
69 lines
2.2 KiB
Java
package net.minecraft.client.renderer.item.properties.numeric;
|
|
|
|
import com.mojang.serialization.Codec;
|
|
import com.mojang.serialization.MapCodec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.multiplayer.ClientLevel;
|
|
import net.minecraft.client.renderer.item.properties.numeric.NeedleDirectionHelper.Wobbler;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.util.StringRepresentable;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class Time extends NeedleDirectionHelper implements RangeSelectItemModelProperty {
|
|
public static final MapCodec<Time> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
|
instance -> instance.group(
|
|
Codec.BOOL.optionalFieldOf("wobble", true).forGetter(NeedleDirectionHelper::wobble), Time.TimeSource.CODEC.fieldOf("source").forGetter(time -> time.source)
|
|
)
|
|
.apply(instance, Time::new)
|
|
);
|
|
private final Time.TimeSource source;
|
|
private final RandomSource randomSource = RandomSource.create();
|
|
private final Wobbler wobbler;
|
|
|
|
public Time(boolean wobble, Time.TimeSource source) {
|
|
super(wobble);
|
|
this.source = source;
|
|
this.wobbler = this.newWobbler(0.9F);
|
|
}
|
|
|
|
@Override
|
|
protected float calculate(ItemStack stack, ClientLevel level, int seed, Entity entity) {
|
|
float f = this.source.get(level, stack, entity, this.randomSource);
|
|
long l = level.getGameTime();
|
|
if (this.wobbler.shouldUpdate(l)) {
|
|
this.wobbler.update(l, f);
|
|
}
|
|
|
|
return this.wobbler.rotation();
|
|
}
|
|
|
|
@Override
|
|
public MapCodec<Time> type() {
|
|
return MAP_CODEC;
|
|
}
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public static enum TimeSource implements StringRepresentable {
|
|
RANDOM("RANDOM", 0, "random"),
|
|
DAYTIME("DAYTIME", 1, "daytime"),
|
|
MOON_PHASE("MOON_PHASE", 2, "moon_phase");
|
|
|
|
public static final Codec<Time.TimeSource> CODEC = StringRepresentable.fromEnum(Time.TimeSource::values);
|
|
private final String name;
|
|
|
|
TimeSource(final String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
@Override
|
|
public String getSerializedName() {
|
|
return this.name;
|
|
}
|
|
|
|
abstract float get(ClientLevel level, ItemStack stack, Entity entity, RandomSource random);
|
|
}
|
|
}
|