|
| 1 | +/* |
| 2 | + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). |
| 3 | + * Copyright (c) Meteor Development. |
| 4 | + */ |
| 5 | + |
| 6 | +package meteordevelopment.meteorclient.systems.hud.elements; |
| 7 | + |
| 8 | +import meteordevelopment.meteorclient.settings.*; |
| 9 | +import meteordevelopment.meteorclient.systems.hud.Hud; |
| 10 | +import meteordevelopment.meteorclient.systems.hud.HudElement; |
| 11 | +import meteordevelopment.meteorclient.systems.hud.HudElementInfo; |
| 12 | +import meteordevelopment.meteorclient.systems.hud.HudRenderer; |
| 13 | +import meteordevelopment.meteorclient.systems.hud.screens.HudEditorScreen; |
| 14 | +import meteordevelopment.meteorclient.utils.Utils; |
| 15 | +import meteordevelopment.meteorclient.utils.player.FindItemResult; |
| 16 | +import meteordevelopment.meteorclient.utils.player.InvUtils; |
| 17 | +import meteordevelopment.meteorclient.utils.render.color.SettingColor; |
| 18 | +import net.minecraft.client.render.MapRenderState; |
| 19 | +import net.minecraft.component.DataComponentTypes; |
| 20 | +import net.minecraft.component.type.MapIdComponent; |
| 21 | +import net.minecraft.item.FilledMapItem; |
| 22 | +import net.minecraft.item.ItemStack; |
| 23 | +import net.minecraft.item.map.MapState; |
| 24 | +import org.jetbrains.annotations.Nullable; |
| 25 | +import org.joml.Matrix3x2fStack; |
| 26 | + |
| 27 | +import static meteordevelopment.meteorclient.MeteorClient.mc; |
| 28 | + |
| 29 | +public class MapHud extends HudElement { |
| 30 | + public static final HudElementInfo<MapHud> INFO = new HudElementInfo<>(Hud.GROUP, "map", "Displays the contents of a map on your Hud.", MapHud::new); |
| 31 | + |
| 32 | + private final SettingGroup sgGeneral = settings.getDefaultGroup(); |
| 33 | + private final SettingGroup sgVisual = settings.createGroup("Visual"); |
| 34 | + |
| 35 | + // General |
| 36 | + |
| 37 | + private final Setting<Mode> mode = sgGeneral.add(new EnumSetting.Builder<Mode>() |
| 38 | + .name("mode") |
| 39 | + .description("How to determine which map to render.") |
| 40 | + .defaultValue(Mode.Simple) |
| 41 | + .build() |
| 42 | + ); |
| 43 | + |
| 44 | + private final Setting<Integer> slotIndex = sgGeneral.add(new IntSetting.Builder() |
| 45 | + .name("slot-index") |
| 46 | + .description("Which slot to grab the map from.") |
| 47 | + .visible(() -> mode.get() == Mode.SlotIndex) |
| 48 | + .defaultValue(0) |
| 49 | + .sliderRange(0, 40) |
| 50 | + .build() |
| 51 | + ); |
| 52 | + |
| 53 | + private final Setting<Integer> mapId = sgGeneral.add(new IntSetting.Builder() |
| 54 | + .name("map-id") |
| 55 | + .description("Which map id to render from. Must be in your inventory!") |
| 56 | + .visible(() -> mode.get() == Mode.MapId) |
| 57 | + .defaultValue(0) |
| 58 | + .noSlider() |
| 59 | + .build() |
| 60 | + ); |
| 61 | + |
| 62 | + // Visual |
| 63 | + |
| 64 | + private final Setting<Double> scale = sgVisual.add(new DoubleSetting.Builder() |
| 65 | + .name("scale") |
| 66 | + .description("How big to render the map.") |
| 67 | + .defaultValue(1) |
| 68 | + .min(0.5) |
| 69 | + .sliderRange(0.5, 3) |
| 70 | + .build() |
| 71 | + ); |
| 72 | + |
| 73 | + private final Setting<Boolean> background = sgVisual.add(new BoolSetting.Builder() |
| 74 | + .name("background") |
| 75 | + .description("Displays background.") |
| 76 | + .defaultValue(false) |
| 77 | + .build() |
| 78 | + ); |
| 79 | + |
| 80 | + private final Setting<SettingColor> backgroundColor = sgVisual.add(new ColorSetting.Builder() |
| 81 | + .name("background-color") |
| 82 | + .description("Color used for the background.") |
| 83 | + .visible(background::get) |
| 84 | + .defaultValue(new SettingColor(25, 25, 25, 50)) |
| 85 | + .build() |
| 86 | + ); |
| 87 | + |
| 88 | + private final MapRenderState renderState = new MapRenderState(); |
| 89 | + private @Nullable MapIdComponent mapComponent; |
| 90 | + private @Nullable MapState mapState; |
| 91 | + |
| 92 | + public MapHud() { |
| 93 | + super(INFO); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void tick(HudRenderer renderer) { |
| 98 | + double scale = this.scale.get(); |
| 99 | + this.setSize(128 * scale, 128 * scale); |
| 100 | + |
| 101 | + if (!Utils.canUpdate()) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + ItemStack mapStack = ItemStack.EMPTY; |
| 106 | + switch (mode.get()) { |
| 107 | + case SlotIndex -> mapStack = mc.player.getInventory().getStack(slotIndex.get()); |
| 108 | + case MapId -> { |
| 109 | + FindItemResult mapResult = InvUtils.find(stack -> { |
| 110 | + MapIdComponent mapIdComponent = stack.get(DataComponentTypes.MAP_ID); |
| 111 | + return mapIdComponent != null && mapIdComponent.id() == mapId.get(); |
| 112 | + }); |
| 113 | + if (mapResult.found()) mapStack = mc.player.getInventory().getStack(mapResult.slot()); |
| 114 | + } |
| 115 | + case Simple -> { |
| 116 | + FindItemResult mapResult = InvUtils.find(stack -> { |
| 117 | + MapIdComponent mapIdComponent = stack.get(DataComponentTypes.MAP_ID); |
| 118 | + return mapIdComponent != null; |
| 119 | + }); |
| 120 | + if (mapResult.found()) mapStack = mc.player.getInventory().getStack(mapResult.slot()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (mapStack.isEmpty() || !mapStack.contains(DataComponentTypes.MAP_ID)) { |
| 125 | + mapComponent = null; |
| 126 | + mapState = null; |
| 127 | + } else { |
| 128 | + mapComponent = mapStack.get(DataComponentTypes.MAP_ID); |
| 129 | + mapState = FilledMapItem.getMapState(mapComponent, mc.world); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void render(HudRenderer renderer) { |
| 135 | + if (mapComponent == null || mapState == null) { |
| 136 | + if (HudEditorScreen.isOpen()) { |
| 137 | + renderer.quad(this.x, this.y, getWidth(), getHeight(), backgroundColor.get()); |
| 138 | + renderer.line(this.x, this.y, this.x + getWidth(), this.y + this.getHeight(), SettingColor.GRAY); |
| 139 | + renderer.line(this.x + getWidth(), this.y, this.x, this.y + this.getHeight(), SettingColor.GRAY); |
| 140 | + } |
| 141 | + |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + if (background.get()) { |
| 146 | + renderer.quad(this.x, this.y, getWidth(), getHeight(), backgroundColor.get()); |
| 147 | + } |
| 148 | + |
| 149 | + renderer.post(() -> { |
| 150 | + mc.getMapRenderer().update(mapComponent, mapState, renderState); |
| 151 | + |
| 152 | + Matrix3x2fStack matrices = renderer.drawContext.getMatrices(); |
| 153 | + matrices.pushMatrix(); |
| 154 | + matrices.scale(1f / mc.getWindow().getScaleFactor()); |
| 155 | + matrices.translate(this.x, this.y); |
| 156 | + matrices.scale(scale.get().floatValue()); |
| 157 | + renderer.drawContext.drawMap(renderState); |
| 158 | + matrices.popMatrix(); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + private enum Mode { |
| 163 | + SlotIndex, |
| 164 | + MapId, |
| 165 | + Simple |
| 166 | + } |
| 167 | +} |
0 commit comments