Skip to content

Commit 4f4c374

Browse files
committed
feat: map preview
1 parent 0f6f0a3 commit 4f4c374

File tree

2 files changed

+104
-21
lines changed

2 files changed

+104
-21
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.Lambda;
21+
import com.lambda.module.modules.render.MapPreview;
22+
import net.minecraft.client.MinecraftClient;
23+
import net.minecraft.client.font.TextRenderer;
24+
import net.minecraft.client.gui.DrawContext;
25+
import net.minecraft.client.gui.screen.Screen;
26+
import net.minecraft.client.gui.screen.ingame.HandledScreen;
27+
import net.minecraft.client.gui.tooltip.HoveredTooltipPositioner;
28+
import net.minecraft.client.gui.tooltip.TooltipBackgroundRenderer;
29+
import net.minecraft.client.gui.tooltip.TooltipComponent;
30+
import net.minecraft.client.gui.tooltip.TooltipPositioner;
31+
import net.minecraft.client.item.TooltipData;
32+
import net.minecraft.item.ItemStack;
33+
import net.minecraft.item.Items;
34+
import net.minecraft.text.Text;
35+
import org.spongepowered.asm.mixin.Final;
36+
import org.spongepowered.asm.mixin.Mixin;
37+
import org.spongepowered.asm.mixin.Shadow;
38+
import org.spongepowered.asm.mixin.injection.At;
39+
import org.spongepowered.asm.mixin.injection.Inject;
40+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
41+
42+
import java.util.List;
43+
import java.util.Optional;
44+
import java.util.stream.Collectors;
45+
46+
@Mixin(DrawContext.class)
47+
public abstract class DrawContextMixin {
48+
@Shadow protected abstract void drawTooltip(TextRenderer textRenderer, List<TooltipComponent> components, int x, int y, TooltipPositioner positioner);
49+
50+
@Inject(method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;Ljava/util/Optional;II)V", at = @At("HEAD"), cancellable = true)
51+
void drawItemTooltip(TextRenderer textRenderer, List<Text> text, Optional<TooltipData> data, int x, int y, CallbackInfo ci) {
52+
List<TooltipComponent> list = text.stream().map(Text::asOrderedText).map(TooltipComponent::of).collect(Collectors.toList());
53+
data.ifPresent(datax -> list.add(1, TooltipComponent.of(datax)));
54+
55+
var screen = (HandledScreen) Lambda.getMc().currentScreen;
56+
if (screen.focusedSlot != null) {
57+
var stack = screen.focusedSlot.getStack();
58+
if (stack.isOf(Items.FILLED_MAP)) list.add(1, new MapPreview.MapComponent(stack));
59+
}
60+
61+
drawTooltip(textRenderer, list, x, y, HoveredTooltipPositioner.INSTANCE);
62+
ci.cancel();
63+
}
64+
}

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

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,60 @@
1717

1818
package com.lambda.module.modules.render
1919

20-
import com.lambda.graphics.buffer.pixel.PixelBuffer
21-
import com.lambda.graphics.texture.Texture
20+
import com.lambda.Lambda.mc
2221
import com.lambda.module.Module
2322
import com.lambda.module.tag.ModuleTag
24-
import com.lambda.threading.runSafe
25-
import com.lambda.util.math.Vec2d
23+
import com.mojang.blaze3d.systems.RenderSystem
24+
import net.minecraft.client.font.TextRenderer
25+
import net.minecraft.client.gui.DrawContext
26+
import net.minecraft.client.gui.tooltip.TooltipComponent
27+
import net.minecraft.client.render.MapRenderer
2628
import net.minecraft.item.FilledMapItem
2729
import net.minecraft.item.ItemStack
28-
import org.lwjgl.BufferUtils
29-
import org.lwjgl.opengl.GL11.GL_RGB
30-
import org.lwjgl.opengl.GL11.GL_RGBA
31-
import org.lwjgl.opengl.GL45C.GL_TEXTURE_2D
32-
import org.lwjgl.opengl.GL45C.glBindTexture
30+
import net.minecraft.item.map.MapState
31+
import net.minecraft.util.Identifier
32+
3333

3434
object MapPreview : Module(
3535
name = "MapPreview",
3636
description = "Preview maps in your inventory",
3737
defaultTags = setOf(ModuleTag.RENDER)
3838
) {
39-
private val scale by setting("Scale", 0.7, 0.1..1.0, 0.05)
39+
private val scale by setting("Scale", 0.7f, 0.1f..1.0f, 0.05f)
40+
41+
private val background = Identifier("textures/map/map_background.png")
42+
43+
// The map component is added via the draw context mixin, thanks mojang
44+
class MapComponent(val stack: ItemStack) : TooltipComponent {
45+
val state: MapState?
46+
get() = FilledMapItem.getMapState(stack, mc.world)
47+
48+
val mapId: Int?
49+
get() = FilledMapItem.getMapId(stack)
50+
51+
override fun drawItems(fontRenderer: TextRenderer, x: Int, y: Int, context: DrawContext) {
52+
mapId?.let { id ->
53+
// Values taken from net.minecraft.client.render.item.HeldItemRenderer.renderFirstPersonMap
54+
55+
val matrices = context.matrices
4056

41-
private val buffer = BufferUtils.createByteBuffer(128*128)
42-
private val texture = Texture(buffer, 128, 128, format = GL_RGB, levels = 1)
57+
matrices.push()
58+
matrices.translate(x + 3.0, y + 3.0, 500.0)
59+
matrices.scale(scale, scale, 1f)
4360

44-
@JvmStatic
45-
fun drawMap(stack: ItemStack, x: Int, y: Int) = runSafe {
46-
val state = FilledMapItem.getMapState(stack, world) ?: return@runSafe
47-
buffer.put(state.colors)
48-
buffer.flip()
61+
RenderSystem.enableBlend()
62+
context.drawTexture(background, -7, -7, 0f, 0f, 142, 142, 142, 142)
4963

50-
val base = Vec2d(x, y)
64+
matrices.translate(0.0, 0.0, 1.0)
65+
mc.gameRenderer.mapRenderer.draw(matrices, context.vertexConsumers, id, state, true, 240)
66+
}
67+
}
5168

52-
texture.bind()
53-
texture.update(buffer, 128, 128)
69+
override fun getHeight(): Int {
70+
return if (FilledMapItem.getMapState(stack, mc.world) != null) 100
71+
else 0
72+
}
5473

55-
texture.draw(base, scale)
74+
override fun getWidth(textRenderer: TextRenderer) = 72
5675
}
5776
}

0 commit comments

Comments
 (0)