aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/tooltip/TooltipManager.java
blob: b5f44405dd3829cd045a42a953e2deccc3cd825e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package de.hysky.skyblocker.utils.tooltip;

import de.hysky.skyblocker.skyblock.item.tooltip.MuseumTooltip;
import de.hysky.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class TooltipManager {
	private static final TooltipAdder[] adders = new TooltipAdder[]{
			new LineSmoothener(),
			new DungeonQualityTooltip(0),
			new ObtainedTooltip(3),
			new MuseumTooltip(4),
			new ColorTooltip(5),
			new MotesTooltip(0),
			new NpcPriceTooltip(1)
	};
	private static final ArrayList<TooltipAdder> currentScreenAdders = new ArrayList<>();

	private TooltipManager() {
	}

	public static void init() {
		ScreenEvents.AFTER_INIT.register((client, screen, width, height) -> {
			onScreenChange(screen);
			ScreenEvents.remove(screen).register(ignored -> currentScreenAdders.clear());
		});
	}

	private static void onScreenChange(Screen screen) {
		final String title = screen.getTitle().getString();
		for (TooltipAdder adder : adders) {
			if (adder.titlePattern == null || adder.titlePattern.matcher(title).matches()) {
				currentScreenAdders.add(adder);
			}
		}
		currentScreenAdders.sort(Comparator.comparingInt(adder -> adder.priority));
	}

	/**
	 * <p>Adds additional text from all adders that are applicable to the current screen.
	 * This method is run on each tooltip render, so don't do any heavy calculations here.</p>
	 *
	 * <p>If you want to add info to the tooltips of multiple items, consider using a switch statement with {@code focusedSlot.getIndex()}</p>
	 *
	 * @param lore        The lore of the focused item.
	 * @param focusedSlot The slot that is currently focused by the cursor.
	 * @return The lore itself after all adders have added their text.
	 * @deprecated This method is public only for the sake of the mixin. Don't call directly, not that there is any point to it.
	 */
	@Deprecated
	public static List<Text> addToTooltip(List<Text> lore, Slot focusedSlot) {
		if (!Utils.isOnSkyblock()) return lore;
		for (TooltipAdder adder : currentScreenAdders) {
			adder.addToTooltip(lore, focusedSlot);
		}
		return lore;
	}
}