| 1 | package net.minecraft.client.gui.contextualbar;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.DeltaTracker;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.renderer.RenderPipelines;
|
| 9 | import net.minecraft.client.resources.WaypointStyle;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.util.ARGB;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.world.TickRateManager;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.waypoints.PartialTickSupplier;
|
| 17 | import net.minecraft.world.waypoints.TrackedWaypoint.PitchDirection;
|
| 18 | import net.minecraft.world.waypoints.Waypoint.Icon;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class LocatorBar implements ContextualBar {
|
| 22 | private static final Identifier LOCATOR_BAR_BACKGROUND = Identifier.withDefaultNamespace("hud/locator_bar_background");
|
| 23 | private static final Identifier LOCATOR_BAR_ARROW_UP = Identifier.withDefaultNamespace("hud/locator_bar_arrow_up");
|
| 24 | private static final Identifier LOCATOR_BAR_ARROW_DOWN = Identifier.withDefaultNamespace("hud/locator_bar_arrow_down");
|
| 25 | private static final int DOT_SIZE = 9;
|
| 26 | private static final int VISIBLE_DEGREE_RANGE = 60;
|
| 27 | private static final int ARROW_WIDTH = 7;
|
| 28 | private static final int ARROW_HEIGHT = 5;
|
| 29 | private static final int ARROW_LEFT = 1;
|
| 30 | private static final int ARROW_PADDING = 1;
|
| 31 | private final Minecraft minecraft;
|
| 32 |
|
| 33 | public LocatorBar(final Minecraft minecraft) {
|
| 34 | this.minecraft = minecraft;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void extractBackground(final GuiGraphicsExtractor graphics, final DeltaTracker deltaTracker) {
|
| 39 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, LOCATOR_BAR_BACKGROUND, this.left(this.minecraft.getWindow()), this.top(this.minecraft.getWindow()), 182, 5);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void extractRenderState(final GuiGraphicsExtractor graphics, final DeltaTracker deltaTracker) {
|
| 44 | int top = this.top(this.minecraft.getWindow());
|
| 45 | Entity cameraEntity = this.minecraft.getCameraEntity();
|
| 46 | if (cameraEntity != null) {
|
| 47 | Level level = cameraEntity.level();
|
| 48 | TickRateManager tickRateManager = level.tickRateManager();
|
| 49 | PartialTickSupplier partialTickSupplier = entity -> deltaTracker.getGameTimeDeltaPartialTick(!tickRateManager.isEntityFrozen(entity));
|
| 50 | this.minecraft
|
| 51 | .player
|
| 52 | .connection
|
| 53 | .getWaypointManager()
|
| 54 | .forEachWaypoint(
|
| 55 | cameraEntity,
|
| 56 | waypoint -> {
|
| 57 | if (!waypoint.id().left().map(uuid -> uuid.equals(cameraEntity.getUUID())).orElse(false)) {
|
| 58 | double angle = waypoint.yawAngleToCamera(level, this.minecraft.gameRenderer.mainCamera(), partialTickSupplier);
|
| 59 | if (!(angle <= -60.0) && !(angle > 60.0)) {
|
| 60 | int screenMiddle = Mth.ceil((graphics.guiWidth() - 9) / 2.0F);
|
| 61 | Icon icon = waypoint.icon();
|
| 62 | WaypointStyle style = this.minecraft.gui.hud.getWaypointStyles().get(icon.style);
|
| 63 | float distance = Mth.sqrt((float)waypoint.distanceSquared(cameraEntity));
|
| 64 | Identifier sprite = style.sprite(distance);
|
| 65 | int color = icon.color
|
| 66 | .orElseGet(
|
| 67 | () -> waypoint.id()
|
| 68 | .map(uuid -> ARGB.setBrightness(ARGB.color(255, uuid.hashCode()), 0.9F), name -> ARGB.setBrightness(ARGB.color(255, name.hashCode()), 0.9F))
|
| 69 | );
|
| 70 | int dotPosition = Mth.floor(angle * 173.0 / 2.0 / 60.0);
|
| 71 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, screenMiddle + dotPosition, top - 2, 9, 9, color);
|
| 72 | PitchDirection pitchDirection = waypoint.pitchDirectionToCamera(level, this.minecraft.gameRenderer, partialTickSupplier);
|
| 73 | if (pitchDirection != PitchDirection.NONE) {
|
| 74 | int arrowTop;
|
| 75 | Identifier arrowSprite;
|
| 76 | if (pitchDirection == PitchDirection.DOWN) {
|
| 77 | arrowTop = 6;
|
| 78 | arrowSprite = LOCATOR_BAR_ARROW_DOWN;
|
| 79 | } else {
|
| 80 | arrowTop = -6;
|
| 81 | arrowSprite = LOCATOR_BAR_ARROW_UP;
|
| 82 | }
|
| 83 |
|
| 84 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, arrowSprite, screenMiddle + dotPosition + 1, top + arrowTop, 7, 5);
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 | );
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|