Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: format tooltips in the same way as other Galacticraft blocks #20

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState bloc
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, @NotNull TooltipFlag flag) {
if (Screen.hasShiftDown()) {
tooltip.addAll(DisplayUtil.wrapText(Component.translatable(this.getDescriptionId() + ".description"), 128));
tooltip.addAll(DisplayUtil.wrapText(Component.translatable(this.getDescriptionId() + ".description").withStyle(Constant.Text.GRAY_STYLE), 150));
} else {
tooltip.add(PRESS_SHIFT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,10 @@ public static String truncateDecimal(double d, int places) {
}

public static @NotNull @Unmodifiable List<Component> wrapText(@NotNull String text, int length, @Nullable Style style) {
if (text.length() <= length) {
return ImmutableList.of(Component.literal(text));
}

Minecraft minecraft = Minecraft.getInstance();
ImmutableList.Builder<Component> list = ImmutableList.builder();
StringBuilder builder = new StringBuilder();
int lineLength = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
lineLength += minecraft.font.width(String.valueOf(c));
if (Character.isWhitespace(c) && lineLength >= length) {
lineLength = 0;
list.add(Component.literal(builder.toString()).setStyle(style));
builder.delete(0, builder.length());
} else {
builder.append(c);
}
}
list.add(Component.literal(builder.toString()).setStyle(style));
minecraft.font.getSplitter().splitLines(text, length, Style.EMPTY).stream()
.forEach(formattedText -> list.add(Component.literal(formattedText.getString()).withStyle(style)));
return list.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"block.machinelib_testmod.generator": "Generator",
"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.",
"block.machinelib_testmod.mixer": "Mixer",
"block.machinelib_testmod.melter": "Melter"
"block.machinelib_testmod.mixer.description": "Combines water and lava to produce obsidian in a controlled environment.",
"block.machinelib_testmod.melter": "Melter",
"block.machinelib_testmod.melter.description": "Melts cobblestone into lava."
}