Skip to content

Commit 386b04a

Browse files
committed
pass 2
1 parent 08887a6 commit 386b04a

File tree

14 files changed

+204
-78
lines changed

14 files changed

+204
-78
lines changed

src/main/java/mcp/mobius/waila/access/ClientAccessor.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import mcp.mobius.waila.api.ICommonAccessor;
55
import mcp.mobius.waila.api.IDataReader;
66
import mcp.mobius.waila.api.IEntityAccessor;
7-
import mcp.mobius.waila.api.IPluginInfo;
87
import net.minecraft.core.BlockPos;
98
import net.minecraft.core.Direction;
109
import net.minecraft.core.registries.BuiltInRegistries;
@@ -46,22 +45,6 @@ public enum ClientAccessor implements ICommonAccessor, IBlockAccessor, IEntityAc
4645
private double rayCastMaxDistance;
4746
private float frameTime;
4847

49-
private @Nullable IPluginInfo plugin;
50-
private @Nullable Class<?> provider;
51-
52-
public void setOrigin(@Nullable IPluginInfo plugin, @Nullable Class<?> provider) {
53-
this.plugin = plugin;
54-
this.provider = provider;
55-
}
56-
57-
public @Nullable IPluginInfo getPlugin() {
58-
return plugin;
59-
}
60-
61-
public @Nullable Class<?> getProvider() {
62-
return provider;
63-
}
64-
6548
@Override
6649
public Level getWorld() {
6750
return this.world;

src/main/java/mcp/mobius/waila/command/ClientCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ protected final void register(ArgumentBuilderBuilder<S> command) {
161161

162162
.then(literal("inspect"))
163163
.executes(context -> {
164-
Minecraft.getInstance().schedule(InspectorScreen::open);
164+
var client = Minecraft.getInstance();
165+
client.schedule(() -> client.setScreen(new InspectorScreen()));
165166
return 1;
166167
})
167168
.pop("inspect")

src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mcp.mobius.waila.gui.hud;
22

33
import java.util.Objects;
4+
import java.util.function.Function;
45

56
import lol.bai.badpackets.api.PacketSender;
67
import mcp.mobius.waila.Waila;
@@ -13,6 +14,7 @@
1314
import mcp.mobius.waila.config.PluginConfig;
1415
import mcp.mobius.waila.network.play.c2s.BlockDataRequestPlayC2SPacket;
1516
import mcp.mobius.waila.network.play.c2s.EntityDataRequestPlayC2SPacket;
17+
import mcp.mobius.waila.registry.PluginAware;
1618
import mcp.mobius.waila.registry.Registrar;
1719
import mcp.mobius.waila.util.ExceptionUtil;
1820
import net.minecraft.client.Minecraft;
@@ -25,6 +27,8 @@
2527

2628
public class ComponentHandler {
2729

30+
public static @Nullable Function<PluginAware<?>, Line.Wrapper> wrapperFactory;
31+
2832
public static void requestBlockData(ClientAccessor accessor) {
2933
var registrar = Registrar.get();
3034
var block = accessor.getBlock();
@@ -68,7 +72,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
6872
for (var entry : providers) {
6973
var pa = entry.instance();
7074
var provider = pa.instance();
71-
accessor.setOrigin(pa.origin(), provider.getClass());
75+
if (wrapperFactory != null) tooltip.wrapper = wrapperFactory.apply(pa);
7276
try {
7377
switch (position) {
7478
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
@@ -78,7 +82,7 @@ private static void handleBlock(ClientAccessor accessor, Tooltip tooltip, Object
7882
} catch (Throwable e) {
7983
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
8084
}
81-
accessor.setOrigin(null, null);
85+
if (wrapperFactory != null) tooltip.wrapper = null;
8286
}
8387
}
8488

@@ -113,7 +117,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
113117
for (var entry : providers) {
114118
var pa = entry.instance();
115119
var provider = pa.instance();
116-
accessor.setOrigin(pa.origin(), provider.getClass());
120+
if (wrapperFactory != null) tooltip.wrapper = wrapperFactory.apply(pa);
117121
try {
118122
switch (position) {
119123
case HEAD -> provider.appendHead(tooltip, accessor, PluginConfig.CLIENT);
@@ -123,7 +127,7 @@ public static void gatherEntity(Entity entity, ClientAccessor accessor, Tooltip
123127
} catch (Throwable e) {
124128
ExceptionUtil.dump(e, provider.getClass().toString(), tooltip);
125129
}
126-
accessor.setOrigin(null, null);
130+
if (wrapperFactory != null) tooltip.wrapper = null;
127131
}
128132
}
129133

@@ -138,6 +142,7 @@ public static ITooltipComponent getIcon(HitResult target) {
138142
var pa = provider.instance();
139143
var icon = pa.instance().getIcon(data, config);
140144
if (icon != null) {
145+
if (wrapperFactory != null) icon = wrapperFactory.apply(pa).wrap(null, icon);
141146
return icon;
142147
}
143148
}
@@ -152,6 +157,7 @@ public static ITooltipComponent getIcon(HitResult target) {
152157
var pa = provider.instance();
153158
var icon = pa.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
154159
if (icon != null) {
160+
if (wrapperFactory != null) icon = wrapperFactory.apply(pa).wrap(null, icon);
155161
result = icon;
156162
priority = provider.priority();
157163
break;
@@ -166,6 +172,7 @@ public static ITooltipComponent getIcon(HitResult target) {
166172
var pa = provider.instance();
167173
var icon = pa.instance().getIcon(ClientAccessor.INSTANCE, PluginConfig.CLIENT);
168174
if (icon != null) {
175+
if (wrapperFactory != null) icon = wrapperFactory.apply(pa).wrap(null, icon);
169176
result = icon;
170177
break;
171178
}

src/main/java/mcp/mobius/waila/gui/hud/ComponentRenderer.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,40 @@
1111
import net.minecraft.client.gui.GuiGraphics;
1212
import net.minecraft.client.renderer.RenderType;
1313
import net.minecraft.util.Mth;
14+
import org.jetbrains.annotations.Nullable;
1415

15-
public interface ComponentRenderer {
16+
public abstract class ComponentRenderer {
1617

17-
Random RANDOM = new Random();
18+
private static final Random RANDOM = new Random();
1819

19-
void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta);
20+
public abstract void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta);
2021

21-
ComponentRenderer DEFAULT = (ctx, component, x, y, cw, ch, delta) -> {
22-
component.render(ctx, x, y, delta);
22+
private static @Nullable ComponentRenderer current = null;
2323

24-
if (WailaClient.showComponentBounds) {
24+
public static ComponentRenderer get() {
25+
if (current == null) current = Default.INSTANCE;
26+
return current;
27+
}
28+
29+
public static void set(@Nullable ComponentRenderer value) {
30+
if (value == null) value = Default.INSTANCE;
31+
current = value;
32+
}
33+
34+
public static class Default extends ComponentRenderer {
35+
36+
public static final Default INSTANCE = new Default();
37+
38+
@Override
39+
public void render(GuiGraphics ctx, ITooltipComponent component, int x, int y, int cw, int ch, DeltaTracker delta) {
40+
component.render(ctx, x, y, delta);
41+
42+
if (WailaClient.showComponentBounds) {
43+
renderBounds(ctx, x, y, cw, ch, 1f);
44+
}
45+
}
46+
47+
public static void renderBounds(GuiGraphics ctx, int x, int y, int cw, int ch, float v) {
2548
ctx.pose().pushPose();
2649
var scale = (float) Minecraft.getInstance().getWindow().getGuiScale();
2750
ctx.pose().scale(1 / scale, 1 / scale, 1);
@@ -31,12 +54,13 @@ public interface ComponentRenderer {
3154
var by = Mth.floor(y * scale + 0.5);
3255
var bw = Mth.floor(cw * scale + 0.5);
3356
var bh = Mth.floor(ch * scale + 0.5);
34-
var color = (0xFF << 24) + Mth.hsvToRgb(RANDOM.nextFloat(), RANDOM.nextFloat(), 1f);
57+
var color = (0xFF << 24) + Mth.hsvToRgb(RANDOM.nextFloat(), RANDOM.nextFloat(), v);
3558
DisplayUtil.renderRectBorder(ctx.pose().last().pose(), buf, bx, by, bw, bh, 1, color, color);
3659

3760
ctx.pose().popPose();
3861
ctx.flush();
3962
}
40-
};
63+
64+
}
4165

4266
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package mcp.mobius.waila.gui.hud;
2+
3+
import mcp.mobius.waila.api.IPluginInfo;
4+
import mcp.mobius.waila.api.ITooltipComponent;
5+
import net.minecraft.client.DeltaTracker;
6+
import net.minecraft.client.gui.GuiGraphics;
7+
import net.minecraft.resources.ResourceLocation;
8+
9+
@SuppressWarnings("deprecation")
10+
public class InspectComponent implements ITooltipComponent {
11+
12+
public final ITooltipComponent actual;
13+
public final IPluginInfo plugin;
14+
public final Class<?> provider;
15+
public final ResourceLocation tag;
16+
17+
public InspectComponent(ITooltipComponent actual, IPluginInfo plugin, Class<?> provider, ResourceLocation tag) {
18+
this.actual = actual;
19+
this.plugin = plugin;
20+
this.provider = provider;
21+
this.tag = tag;
22+
}
23+
24+
@Override
25+
public int getWidth() {
26+
return actual.getWidth();
27+
}
28+
29+
@Override
30+
public int getHeight() {
31+
return actual.getHeight();
32+
}
33+
34+
@Override
35+
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
36+
actual.render(ctx, x, y, delta);
37+
}
38+
39+
public static class Growing extends InspectComponent implements HorizontalGrowing {
40+
41+
public final HorizontalGrowing actual;
42+
43+
public Growing(HorizontalGrowing actual, IPluginInfo plugin, Class<?> provider, ResourceLocation tag) {
44+
super(actual, plugin, provider, tag);
45+
this.actual = actual;
46+
}
47+
48+
@Override
49+
public int getMinimalWidth() {
50+
return actual.getMinimalWidth();
51+
}
52+
53+
@Override
54+
public void setGrownWidth(int grownWidth) {
55+
actual.setGrownWidth(grownWidth);
56+
}
57+
58+
}
59+
60+
}

src/main/java/mcp/mobius/waila/gui/hud/Line.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
public class Line implements ITooltipLine {
2121

22-
@Nullable
23-
public final ResourceLocation tag;
22+
public final @Nullable ResourceLocation tag;
2423
public final List<ITooltipComponent> components = new ArrayList<>();
2524
public final Object2IntOpenHashMap<ITooltipComponent> widths = new Object2IntOpenHashMap<>();
2625
public final Object2IntMap<ITooltipComponent> heights = new Object2IntOpenHashMap<>();
2726

27+
public Wrapper wrapper = (t, c) -> c;
28+
2829
private int fixedWidth = -1;
2930
private int width = -1;
3031
private int height = -1;
@@ -38,6 +39,7 @@ public Line(@Nullable ResourceLocation tag) {
3839

3940
@Override
4041
public Line with(ITooltipComponent component) {
42+
component = wrapper.wrap(tag, component);
4143
components.add(component);
4244
if (component instanceof HorizontalGrowing growing) {
4345
growingWeight += growing.getWeight();
@@ -154,4 +156,10 @@ public void render(ComponentRenderer renderer, GuiGraphics ctx, int x, int y, De
154156
}
155157
}
156158

159+
public interface Wrapper {
160+
161+
ITooltipComponent wrap(ResourceLocation tag, ITooltipComponent component);
162+
163+
}
164+
157165
}

src/main/java/mcp/mobius/waila/gui/hud/Tooltip.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import mcp.mobius.waila.api.ITooltip;
77
import mcp.mobius.waila.api.ITooltipLine;
88
import net.minecraft.resources.ResourceLocation;
9+
import org.jetbrains.annotations.Nullable;
910

1011
public class Tooltip extends ObjectArrayList<Line> implements ITooltip {
1112

1213
private final Object2IntMap<ResourceLocation> tags = new Object2IntOpenHashMap<>();
14+
public @Nullable Line.Wrapper wrapper;
1315

1416
public void setLine(ResourceLocation tag, Line line) {
1517
if (tags.containsKey(tag)) {
@@ -27,19 +29,23 @@ public int getLineCount() {
2729

2830
@Override
2931
public ITooltipLine getLine(int index) {
30-
return get(index);
32+
var line = get(index);
33+
if (wrapper != null) line.wrapper = wrapper;
34+
return line;
3135
}
3236

3337
@Override
3438
public ITooltipLine addLine() {
3539
var line = new Line(null);
40+
if (wrapper != null) line.wrapper = wrapper;
3641
add(line);
3742
return line;
3843
}
3944

4045
@Override
4146
public ITooltipLine setLine(ResourceLocation tag) {
4247
var line = new Line(tag);
48+
if (wrapper != null) line.wrapper = wrapper;
4349
setLine(tag, line);
4450
return line;
4551
}

src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public static void add(Line line) {
8383
}
8484

8585
for (var component : line.components) {
86+
if (component instanceof InspectComponent wrapper) {
87+
component = wrapper.actual;
88+
}
89+
8690
if (component instanceof PairComponent pair) {
8791
colonOffset = Math.max(pair.key.getWidth(), colonOffset);
8892
break;
@@ -103,9 +107,10 @@ public static Rectangle endBuild() {
103107
for (var entry : Registrar.get().eventListeners.get(Object.class)) {
104108
var pa = entry.instance();
105109
var listener = pa.instance();
106-
accessor.setOrigin(pa.origin(), listener.getClass());
110+
var wrapperFactory = ComponentHandler.wrapperFactory;
111+
if (wrapperFactory != null) TOOLTIP.wrapper = wrapperFactory.apply(pa);
107112
listener.onHandleTooltip(TOOLTIP, accessor, PluginConfig.CLIENT);
108-
accessor.setOrigin(null, null);
113+
if (wrapperFactory != null) TOOLTIP.wrapper = null;
109114
}
110115
}
111116

@@ -186,13 +191,13 @@ public static void resetState() {
186191
state = null;
187192
}
188193

189-
public static void render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
194+
public static void render(GuiGraphics ctx, DeltaTracker delta) {
190195
try (var ignored = ProfilerUtil.profile("wthit:render")) {
191-
_render(renderer, ctx, delta);
196+
_render(ctx, delta);
192197
}
193198
}
194199

195-
private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
200+
private static void _render(GuiGraphics ctx, DeltaTracker delta) {
196201
var client = Minecraft.getInstance();
197202

198203
if (WailaClient.showFps) {
@@ -211,7 +216,7 @@ private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTr
211216
// TODO: Figure out why opacity not working properly
212217
//noinspection ConstantValue
213218
if (true) {
214-
renderUncached(renderer, ctx, delta);
219+
renderUncached(ctx, delta);
215220
return;
216221
}
217222

@@ -235,7 +240,7 @@ private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTr
235240
client.getMainRenderTarget().unbindWrite();
236241
framebuffer.clear();
237242
framebuffer.bindWrite(true);
238-
renderUncached(renderer, ctx, delta);
243+
renderUncached(ctx, delta);
239244
framebuffer.unbindWrite();
240245
client.getMainRenderTarget().bindWrite(true);
241246
lastFrame = now;
@@ -262,13 +267,14 @@ private static void _render(ComponentRenderer renderer, GuiGraphics ctx, DeltaTr
262267
RenderSystem.disableBlend();
263268
}
264269

265-
private static void renderUncached(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
270+
private static void renderUncached(GuiGraphics ctx, DeltaTracker delta) {
266271
try (var ignored = ProfilerUtil.profile("wthit:render_uncached")) {
267-
_renderUncached(renderer, ctx, delta);
272+
_renderUncached(ctx, delta);
268273
}
269274
}
270275

271-
private static void _renderUncached(ComponentRenderer renderer, GuiGraphics ctx, DeltaTracker delta) {
276+
private static void _renderUncached(GuiGraphics ctx, DeltaTracker delta) {
277+
var renderer = ComponentRenderer.get();
272278
var scale = state.getScale();
273279

274280
ctx.pose().pushPose();

0 commit comments

Comments
 (0)