Skip to content

Commit adda60b

Browse files
committed
Add helper for applying a font to text.
1 parent 68755cb commit adda60b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Common/src/main/java/net/darkhax/bookshelf/api/util/TextHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.minecraft.network.chat.MutableComponent;
77
import net.minecraft.network.chat.TextComponent;
88
import net.minecraft.network.chat.TranslatableComponent;
9+
import net.minecraft.resources.ResourceLocation;
910
import net.minecraft.util.StringUtil;
1011
import net.minecraft.world.entity.Entity;
1112
import net.minecraft.world.item.ItemStack;
@@ -58,4 +59,22 @@ public static <T extends Component, AT, A extends HoverEvent.Action<AT>> T setHo
5859

5960
return component;
6061
}
62+
63+
/**
64+
* Recursively applies a custom font to a text component and all of it's children components.
65+
*
66+
* @param text The text to apply the font to.
67+
* @param font The ID of the font to apply.
68+
* @return A modified text component that has had the font applied.
69+
*/
70+
public static Component applyFont(Component text, ResourceLocation font) {
71+
72+
if (text instanceof MutableComponent mutable) {
73+
74+
mutable.withStyle(style -> style.withFont(font));
75+
}
76+
77+
text.getSiblings().forEach(sib -> applyFont(sib, font));
78+
return text;
79+
}
6180
}

0 commit comments

Comments
 (0)