Skip to content

Commit 35bfd53

Browse files
authoredFeb 5, 2025
fix: format tooltips in the same way as other Galacticraft blocks (#20)
* fix: format tooltips in the same way as other Galacticraft blocks by splitting into lines of at most 150 characters * refactor: moved code to handle splitting long lines to DisplayUtil.wrapText and added descriptions of varying lengths to the test machines * refactor: removed unused imports
1 parent 7dac4a3 commit 35bfd53

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed
 

‎src/main/java/dev/galacticraft/machinelib/api/block/MachineBlock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState bloc
212212
@Override
213213
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, @NotNull TooltipFlag flag) {
214214
if (Screen.hasShiftDown()) {
215-
tooltip.addAll(DisplayUtil.wrapText(Component.translatable(this.getDescriptionId() + ".description"), 128));
215+
tooltip.addAll(DisplayUtil.wrapText(Component.translatable(this.getDescriptionId() + ".description").withStyle(Constant.Text.GRAY_STYLE), 150));
216216
} else {
217217
tooltip.add(PRESS_SHIFT);
218218
}

‎src/main/java/dev/galacticraft/machinelib/client/api/util/DisplayUtil.java

+2-18
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,10 @@ public static String truncateDecimal(double d, int places) {
9191
}
9292

9393
public static @NotNull @Unmodifiable List<Component> wrapText(@NotNull String text, int length, @Nullable Style style) {
94-
if (text.length() <= length) {
95-
return ImmutableList.of(Component.literal(text));
96-
}
97-
9894
Minecraft minecraft = Minecraft.getInstance();
9995
ImmutableList.Builder<Component> list = ImmutableList.builder();
100-
StringBuilder builder = new StringBuilder();
101-
int lineLength = 0;
102-
for (int i = 0; i < text.length(); i++) {
103-
char c = text.charAt(i);
104-
lineLength += minecraft.font.width(String.valueOf(c));
105-
if (Character.isWhitespace(c) && lineLength >= length) {
106-
lineLength = 0;
107-
list.add(Component.literal(builder.toString()).setStyle(style));
108-
builder.delete(0, builder.length());
109-
} else {
110-
builder.append(c);
111-
}
112-
}
113-
list.add(Component.literal(builder.toString()).setStyle(style));
96+
minecraft.font.getSplitter().splitLines(text, length, Style.EMPTY).stream()
97+
.forEach(formattedText -> list.add(Component.literal(formattedText.getString()).withStyle(style)));
11498
return list.build();
11599
}
116100

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"block.machinelib_testmod.generator": "Generator",
3+
"block.machinelib_testmod.generator.description": "Burns the same items that can be used to power a furnace to instead produce energy which can be used to charge batteries and power other machines.",
34
"block.machinelib_testmod.mixer": "Mixer",
4-
"block.machinelib_testmod.melter": "Melter"
5+
"block.machinelib_testmod.mixer.description": "Combines water and lava to produce obsidian in a controlled environment.",
6+
"block.machinelib_testmod.melter": "Melter",
7+
"block.machinelib_testmod.melter.description": "Melts cobblestone into lava."
58
}

0 commit comments

Comments
 (0)