ClientBundleTooltip .java

net.minecraft.client.gui.screens.inventory.tooltip.ClientBundleTooltip (class): members: PROGRESSBAR_BORDER_SPRITE, BUNDLE_EMPTY_DESCRIPTION, SLOT_MARGIN.

net.minecraft.client.gui.screens.inventory.tooltipclass228 linesclient9.3 KB

Implements

API Surface (4)

int getHeight(final Font font)
int getWidth(final Font font)
boolean showTooltipWithItemInHand()
void extractImage(final Font font, final int x, final int y, final int w, final int h, final GuiGraphicsExtractor graphics)

Fields & Constants (25)

PROGRESSBAR_BORDER_SPRITE
PROGRESSBAR_FILL_SPRITE
PROGRESSBAR_FULL_SPRITE
SLOT_HIGHLIGHT_BACK_SPRITE
SLOT_HIGHLIGHT_FRONT_SPRITE
SLOT_BACKGROUND_SPRITE
SLOT_MARGIN
SLOT_SIZE
GRID_WIDTH
PROGRESSBAR_HEIGHT
PROGRESSBAR_WIDTH
PROGRESSBAR_BORDER
PROGRESSBAR_FILL_MAX
PROGRESSBAR_MARGIN_Y
BUNDLE_FULL_TEXT
BUNDLE_EMPTY_TEXT
BUNDLE_EMPTY_DESCRIPTION
contents
graphics
slotNumber
drawX
drawY
shownItems
slotIndex
font

String literals (9)

"container/bundle/bundle_progressbar_border"
"container/bundle/bundle_progressbar_fill"
"container/bundle/bundle_progressbar_full"
"container/bundle/slot_highlight_back"
"container/bundle/slot_highlight_front"
"container/bundle/slot_background"
"item.minecraft.bundle.full"
"item.minecraft.bundle.empty"
"item.minecraft.bundle.empty.description"

Full Source

ClientBundleTooltip.java228 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.inventory.tooltip;
2
3
import com.mojang.serialization.DataResult;
4
import java.util.List;
5
import net.fabricmc.api.EnvType;
6
import net.fabricmc.api.Environment;
7
import net.minecraft.client.gui.Font;
8
import net.minecraft.client.gui.GuiGraphicsExtractor;
9
import net.minecraft.client.renderer.RenderPipelines;
10
import net.minecraft.core.component.DataComponents;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.resources.Identifier;
13
import net.minecraft.util.Mth;
14
import net.minecraft.world.item.ItemInstance;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.item.ItemStackTemplate;
17
import net.minecraft.world.item.component.BundleContents;
18
import org.apache.commons.lang3.math.Fraction;
19
import org.jspecify.annotations.Nullable;
20
21
@Environment(EnvType.CLIENT)
22
public class ClientBundleTooltip implements ClientTooltipComponent {
23
	private static final Identifier PROGRESSBAR_BORDER_SPRITE = Identifier.withDefaultNamespace("container/bundle/bundle_progressbar_border");
24
	private static final Identifier PROGRESSBAR_FILL_SPRITE = Identifier.withDefaultNamespace("container/bundle/bundle_progressbar_fill");
25
	private static final Identifier PROGRESSBAR_FULL_SPRITE = Identifier.withDefaultNamespace("container/bundle/bundle_progressbar_full");
26
	private static final Identifier SLOT_HIGHLIGHT_BACK_SPRITE = Identifier.withDefaultNamespace("container/bundle/slot_highlight_back");
27
	private static final Identifier SLOT_HIGHLIGHT_FRONT_SPRITE = Identifier.withDefaultNamespace("container/bundle/slot_highlight_front");
28
	private static final Identifier SLOT_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("container/bundle/slot_background");
29
	private static final int SLOT_MARGIN = 4;
30
	private static final int SLOT_SIZE = 24;
31
	private static final int GRID_WIDTH = 96;
32
	private static final int PROGRESSBAR_HEIGHT = 13;
33
	private static final int PROGRESSBAR_WIDTH = 96;
34
	private static final int PROGRESSBAR_BORDER = 1;
35
	private static final int PROGRESSBAR_FILL_MAX = 94;
36
	private static final int PROGRESSBAR_MARGIN_Y = 4;
37
	private static final Component BUNDLE_FULL_TEXT = Component.translatable("item.minecraft.bundle.full");
38
	private static final Component BUNDLE_EMPTY_TEXT = Component.translatable("item.minecraft.bundle.empty");
39
	private static final Component BUNDLE_EMPTY_DESCRIPTION = Component.translatable("item.minecraft.bundle.empty.description");
40
	private final BundleContents contents;
41
42
	public ClientBundleTooltip(final BundleContents contents) {
43
		this.contents = contents;
44
	}
45
46
	@Override
47
	public int getHeight(final Font font) {
48
		return this.contents.isEmpty() ? getEmptyBundleBackgroundHeight(font) : this.backgroundHeight();
49
	}
50
51
	@Override
52
	public int getWidth(final Font font) {
53
		return 96;
54
	}
55
56
	@Override
57
	public boolean showTooltipWithItemInHand() {
58
		return true;
59
	}
60
61
	private static int getEmptyBundleBackgroundHeight(final Font font) {
62
		return getEmptyBundleDescriptionTextHeight(font) + 13 + 8;
63
	}
64
65
	private int backgroundHeight() {
66
		return this.itemGridHeight() + 13 + 8;
67
	}
68
69
	private int itemGridHeight() {
70
		return this.gridSizeY() * 24;
71
	}
72
73
	private static int getContentXOffset(final int tooltipWidth) {
74
		return (tooltipWidth - 96) / 2;
75
	}
76
77
	private int gridSizeY() {
78
		return Mth.positiveCeilDiv(this.slotCount(), 4);
79
	}
80
81
	private int slotCount() {
82
		return Math.min(12, this.contents.size());
83
	}
84
85
	@Override
86
	public void extractImage(final Font font, final int x, final int y, final int w, final int h, final GuiGraphicsExtractor graphics) {
87
		DataResult<Fraction> weight = this.contents.weight();
88
		if (!weight.isError()) {
89
			if (this.contents.isEmpty()) {
90
				extractEmptyBundleTooltip(font, x, y, w, h, graphics);
91
			} else {
92
				this.extractBundleWithItemsTooltip(font, x, y, w, h, graphics, weight.getOrThrow());
93
			}
94
		}
95
	}
96
97
	private static void extractEmptyBundleTooltip(final Font font, final int x, final int y, final int w, final int h, final GuiGraphicsExtractor graphics) {
98
		int left = x + getContentXOffset(w);
99
		extractEmptyBundleDescriptionText(left, y, font, graphics);
100
		extractProgressbar(left, y + getEmptyBundleDescriptionTextHeight(font) + 4, font, graphics, Fraction.ZERO);
101
	}
102
103
	private void extractBundleWithItemsTooltip(
104
		final Font font, final int x, final int y, final int w, final int h, final GuiGraphicsExtractor graphics, final Fraction weight
105
	) {
106
		boolean isOverflowing = this.contents.size() > 12;
107
		List<ItemStackTemplate> shownItems = this.getShownItems(this.contents.getNumberOfItemsToShow());
108
		int xStartPos = x + getContentXOffset(w) + 96;
109
		int yStartPos = y + this.gridSizeY() * 24;
110
		int slotNumber = 1;
111
112
		for (int rowNumber = 1; rowNumber <= this.gridSizeY(); rowNumber++) {
113
			for (int columnNumber = 1; columnNumber <= 4; columnNumber++) {
114
				int drawX = xStartPos - columnNumber * 24;
115
				int drawY = yStartPos - rowNumber * 24;
116
				if (shouldRenderSurplusText(isOverflowing, columnNumber, rowNumber)) {
117
					extractCount(drawX, drawY, this.getAmountOfHiddenItems(shownItems), font, graphics);
118
				} else if (shouldRenderItemSlot(shownItems, slotNumber)) {
119
					this.extractSlot(slotNumber, drawX, drawY, shownItems, slotNumber, font, graphics);
120
					slotNumber++;
121
				}
122
			}
123
		}
124
125
		this.extractSelectedItemTooltip(font, graphics, x, y, w);
126
		extractProgressbar(x + getContentXOffset(w), y + this.itemGridHeight() + 4, font, graphics, weight);
127
	}
128
129
	private List<ItemStackTemplate> getShownItems(final int amountOfItemsToShow) {
130
		int lastToDisplay = Math.min(this.contents.size(), amountOfItemsToShow);
131
		return this.contents.items().subList(0, lastToDisplay);
132
	}
133
134
	private static boolean shouldRenderSurplusText(final boolean isOverflowing, final int column, final int row) {
135
		return isOverflowing && column * row == 1;
136
	}
137
138
	private static boolean shouldRenderItemSlot(final List<? extends ItemInstance> shownItems, final int slotNumber) {
139
		return shownItems.size() >= slotNumber;
140
	}
141
142
	private int getAmountOfHiddenItems(final List<ItemStackTemplate> shownItems) {
143
		return this.contents.items().stream().skip(shownItems.size()).mapToInt(ItemInstance::count).sum();
144
	}
145
146
	private void extractSlot(
147
		final int slotNumber,
148
		final int drawX,
149
		final int drawY,
150
		final List<ItemStackTemplate> shownItems,
151
		final int slotIndex,
152
		final Font font,
153
		final GuiGraphicsExtractor graphics
154
	) {
155
		int itemVisualOrderIndex = shownItems.size() - slotNumber;
156
		boolean hasHighlight = itemVisualOrderIndex == this.contents.getSelectedItemIndex();
157
		ItemStack item = shownItems.get(itemVisualOrderIndex).create();
158
		if (hasHighlight) {
159
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_HIGHLIGHT_BACK_SPRITE, drawX, drawY, 24, 24);
160
		} else {
161
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_BACKGROUND_SPRITE, drawX, drawY, 24, 24);
162
		}
163
164
		graphics.item(item, drawX + 4, drawY + 4, slotIndex);
165
		graphics.itemDecorations(font, item, drawX + 4, drawY + 4);
166
		if (hasHighlight) {
167
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_HIGHLIGHT_FRONT_SPRITE, drawX, drawY, 24, 24);
168
		}
169
	}
170
171
	private static void extractCount(final int drawX, final int drawY, final int hiddenItemCount, final Font font, final GuiGraphicsExtractor graphics) {
172
		graphics.centeredText(font, "+" + hiddenItemCount, drawX + 12, drawY + 10, -1);
173
	}
174
175
	private void extractSelectedItemTooltip(final Font font, final GuiGraphicsExtractor graphics, final int x, final int y, final int w) {
176
		ItemStackTemplate selectedItem = this.contents.getSelectedItem();
177
		if (selectedItem != null) {
178
			ItemStack itemStack = selectedItem.create();
179
			Component selectedItemName = itemStack.getStyledHoverName();
180
			int textWidth = font.width(selectedItemName.getVisualOrderText());
181
			int centerTooltip = x + w / 2 - 12;
182
			ClientTooltipComponent selectedItemNameTooltip = ClientTooltipComponent.create(selectedItemName.getVisualOrderText());
183
			graphics.tooltip(
184
				font,
185
				List.of(selectedItemNameTooltip),
186
				centerTooltip - textWidth / 2,
187
				y - 15,
188
				DefaultTooltipPositioner.INSTANCE,
189
				itemStack.get(DataComponents.TOOLTIP_STYLE),
190
				false
191
			);
192
		}
193
	}
194
195
	private static void extractProgressbar(final int x, final int y, final Font font, final GuiGraphicsExtractor graphics, final Fraction weight) {
196
		graphics.blitSprite(RenderPipelines.GUI_TEXTURED, getProgressBarTexture(weight), x + 1, y, getProgressBarFill(weight), 13);
197
		graphics.blitSprite(RenderPipelines.GUI_TEXTURED, PROGRESSBAR_BORDER_SPRITE, x, y, 96, 13);
198
		Component progressBarFillText = getProgressBarFillText(weight);
199
		if (progressBarFillText != null) {
200
			graphics.centeredText(font, progressBarFillText, x + 48, y + 3, -1);
201
		}
202
	}
203
204
	private static void extractEmptyBundleDescriptionText(final int x, final int y, final Font font, final GuiGraphicsExtractor graphics) {
205
		graphics.textWithWordWrap(font, BUNDLE_EMPTY_DESCRIPTION, x, y, 96, -5592406);
206
	}
207
208
	private static int getEmptyBundleDescriptionTextHeight(final Font font) {
209
		return font.split(BUNDLE_EMPTY_DESCRIPTION, 96).size() * 9;
210
	}
211
212
	private static int getProgressBarFill(final Fraction weight) {
213
		return Mth.clamp(Mth.mulAndTruncate(weight, 94), 0, 94);
214
	}
215
216
	private static Identifier getProgressBarTexture(final Fraction weight) {
217
		return weight.compareTo(Fraction.ONE) >= 0 ? PROGRESSBAR_FULL_SPRITE : PROGRESSBAR_FILL_SPRITE;
218
	}
219
220
	private static @Nullable Component getProgressBarFillText(final Fraction weight) {
221
		if (weight.compareTo(Fraction.ZERO) == 0) {
222
			return BUNDLE_EMPTY_TEXT;
223
		} else {
224
			return weight.compareTo(Fraction.ONE) >= 0 ? BUNDLE_FULL_TEXT : null;
225
		}
226
	}
227
}
228

Decompiled Minecraft 26.3 source of ClientBundleTooltip (net/minecraft/client/gui/screens/inventory/tooltip/ClientBundleTooltip.java) with Mojang official mappings, stored verbatim on the site.