| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.util.ExtraCodecs;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public record WaypointStyle(int nearDistance, int farDistance, List<Identifier> sprites, List<Identifier> spriteLocations) {
|
| 16 | @VisibleForTesting
|
| 17 | public static final String ICON_LOCATION_PREFIX = "hud/locator_bar_dot/";
|
| 18 | public static final int DEFAULT_NEAR_DISTANCE = 128;
|
| 19 | public static final int DEFAULT_FAR_DISTANCE = 332;
|
| 20 | private static final Codec<Integer> DISTANCE_CODEC = Codec.intRange(0, 60000000);
|
| 21 | public static final Codec<WaypointStyle> CODEC = RecordCodecBuilder.<WaypointStyle>create(
|
| 22 | i -> i.group(
|
| 23 | DISTANCE_CODEC.optionalFieldOf("near_distance", 128).forGetter(WaypointStyle::nearDistance),
|
| 24 | DISTANCE_CODEC.optionalFieldOf("far_distance", 332).forGetter(WaypointStyle::farDistance),
|
| 25 | ExtraCodecs.nonEmptyList(Identifier.CODEC.listOf()).fieldOf("sprites").forGetter(WaypointStyle::sprites)
|
| 26 | )
|
| 27 | .apply(i, WaypointStyle::new)
|
| 28 | )
|
| 29 | .validate(WaypointStyle::validate);
|
| 30 |
|
| 31 | public WaypointStyle(final int nearDistance, final int farDistance, final List<Identifier> sprites) {
|
| 32 | this(nearDistance, farDistance, sprites, sprites.stream().map(sprite -> sprite.withPrefix("hud/locator_bar_dot/")).toList());
|
| 33 | }
|
| 34 |
|
| 35 | @VisibleForTesting
|
| 36 | public DataResult<WaypointStyle> validate() {
|
| 37 | if (this.sprites.isEmpty()) {
|
| 38 | return DataResult.error(() -> "Must have at least one sprite icon");
|
| 39 | } else if (this.nearDistance <= 0) {
|
| 40 | return DataResult.error(() -> "Near distance (" + this.nearDistance + ") must be greater than zero");
|
| 41 | } else {
|
| 42 | return this.nearDistance >= this.farDistance
|
| 43 | ? DataResult.error(() -> "Far distance (" + this.farDistance + ") cannot be closer or equal to near distance (" + this.nearDistance + ")")
|
| 44 | : DataResult.success(this);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public Identifier sprite(final float distance) {
|
| 49 | if (distance < this.nearDistance) {
|
| 50 | return this.spriteLocations.getFirst();
|
| 51 | }
|
| 52 |
|
| 53 | if (distance >= this.farDistance) {
|
| 54 | return this.spriteLocations.getLast();
|
| 55 | }
|
| 56 |
|
| 57 | if (this.spriteLocations.size() == 1) {
|
| 58 | return this.spriteLocations.getFirst();
|
| 59 | }
|
| 60 |
|
| 61 | if (this.spriteLocations.size() == 3) {
|
| 62 | return this.spriteLocations.get(1);
|
| 63 | }
|
| 64 |
|
| 65 | int index = Mth.lerpInt((distance - this.nearDistance) / (this.farDistance - this.nearDistance), 1, this.spriteLocations.size() - 1);
|
| 66 | return this.spriteLocations.get(index);
|
| 67 | }
|
| 68 | }
|
| 69 |
|