Skip to content

Commit 6526138

Browse files
committed
Better map tooltip
1 parent 2ce677a commit 6526138

File tree

4 files changed

+105
-75
lines changed

4 files changed

+105
-75
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.mixin.items;
19+
20+
import com.lambda.module.modules.render.MapPreview;
21+
import net.minecraft.item.FilledMapItem;
22+
import net.minecraft.item.Item;
23+
import net.minecraft.item.ItemStack;
24+
import net.minecraft.item.tooltip.TooltipData;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
27+
import java.util.Optional;
28+
29+
@Mixin(FilledMapItem.class)
30+
public class FilledMapItemMixin extends Item {
31+
public FilledMapItemMixin(Item.Settings settings) {
32+
super(settings);
33+
}
34+
35+
@Override
36+
public Optional<TooltipData> getTooltipData(ItemStack stack) {
37+
return Optional.of(new MapPreview.MapComponent(stack));
38+
}
39+
}

common/src/main/java/com/lambda/mixin/render/DrawContextMixin.java

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.mixin.render;
19+
20+
import com.lambda.module.modules.render.ContainerPreview;
21+
import com.lambda.module.modules.render.MapPreview;
22+
import net.minecraft.client.gui.tooltip.BundleTooltipComponent;
23+
import net.minecraft.client.gui.tooltip.ProfilesTooltipComponent;
24+
import net.minecraft.client.gui.tooltip.TooltipComponent;
25+
import net.minecraft.item.tooltip.BundleTooltipData;
26+
import net.minecraft.item.tooltip.TooltipData;
27+
import org.spongepowered.asm.mixin.Mixin;
28+
import org.spongepowered.asm.mixin.injection.At;
29+
import org.spongepowered.asm.mixin.injection.Inject;
30+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
31+
32+
import java.util.Objects;
33+
34+
@Mixin(TooltipComponent.class)
35+
public interface TooltipComponentMixin {
36+
@Inject(method = "of(Lnet/minecraft/item/tooltip/TooltipData;)Lnet/minecraft/client/gui/tooltip/TooltipComponent;", at = @At("HEAD"), cancellable = true)
37+
private static void of(TooltipData tooltipData, CallbackInfoReturnable<TooltipComponent> cir) {
38+
Objects.requireNonNull(tooltipData);
39+
40+
cir.setReturnValue((switch (tooltipData) {
41+
case MapPreview.MapComponent mapComponent -> mapComponent;
42+
case ContainerPreview.ShulkerComponent shulkerComponent -> shulkerComponent;
43+
44+
case BundleTooltipData bundleTooltipData -> new BundleTooltipComponent(bundleTooltipData.contents());
45+
case ProfilesTooltipComponent.ProfilesData profilesData -> new ProfilesTooltipComponent(profilesData);
46+
default -> throw new IllegalArgumentException("Unknown TooltipComponent");
47+
}));
48+
}
49+
}

common/src/main/kotlin/com/lambda/module/modules/render/MapPreview.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ import com.mojang.blaze3d.systems.RenderSystem
2424
import net.minecraft.client.font.TextRenderer
2525
import net.minecraft.client.gui.DrawContext
2626
import net.minecraft.client.gui.tooltip.TooltipComponent
27+
import net.minecraft.client.render.MapRenderState
28+
import net.minecraft.client.render.RenderLayer
29+
import net.minecraft.component.DataComponentTypes
30+
import net.minecraft.component.type.MapIdComponent
2731
import net.minecraft.item.FilledMapItem
2832
import net.minecraft.item.ItemStack
2933
import net.minecraft.item.map.MapState
34+
import net.minecraft.item.tooltip.TooltipData
3035
import net.minecraft.util.Identifier
3136

3237

@@ -35,35 +40,36 @@ object MapPreview : Module(
3540
description = "Preview maps in your inventory",
3641
defaultTags = setOf(ModuleTag.RENDER)
3742
) {
38-
private val background = Identifier("textures/map/map_background.png")
43+
private val background = Identifier.ofVanilla("textures/map/map_background.png")
3944

40-
// The map component is added via the draw context mixin, thanks mojang
41-
class MapComponent(val stack: ItemStack) : TooltipComponent {
45+
class MapComponent(val stack: ItemStack) : TooltipData, TooltipComponent {
4246
val state: MapState?
4347
get() = FilledMapItem.getMapState(stack, mc.world)
4448

45-
val mapId: Int?
46-
get() = FilledMapItem.getMapId(stack)
49+
val mapId: MapIdComponent?
50+
get() = stack.getOrDefault(DataComponentTypes.MAP_ID, null)
4751

48-
override fun drawItems(fontRenderer: TextRenderer, x: Int, y: Int, context: DrawContext) {
52+
override fun drawItems(textRenderer: TextRenderer, x: Int, y: Int, width: Int, height: Int, context: DrawContext) {
4953
mapId?.let { id ->
50-
// Values taken from net.minecraft.client.render.item.HeldItemRenderer.renderFirstPersonMap
51-
5254
val matrices = context.matrices
5355

5456
matrices.push()
5557
matrices.translate(x + 4.0, y + 4.0, 500.0)
5658
matrices.scale(0.7f, 0.7f, 1f)
5759

5860
RenderSystem.enableBlend()
59-
context.drawTexture(background, -7, -7, 0f, 0f, 142, 142, 142, 142)
61+
context.drawTexture(RenderLayer::getGuiTextured, background, -7, -7, 0f, 0f, 142, 142, 142, 142)
6062

6163
matrices.translate(0.0, 0.0, 1.0)
62-
mc.gameRenderer.mapRenderer.draw(matrices, context.vertexConsumers, id, state, true, 240)
64+
65+
val renderState = MapRenderState()
66+
mc.mapRenderer.update(id, state, renderState)
67+
context.draw { mc.mapRenderer.draw(renderState, matrices, it, true, 0xF000F0) }
68+
matrices.pop()
6369
}
6470
}
6571

66-
override fun getHeight(): Int {
72+
override fun getHeight(textRenderer: TextRenderer): Int {
6773
return if (FilledMapItem.getMapState(stack, mc.world) != null) 100
6874
else 0
6975
}

0 commit comments

Comments
 (0)