diff --git a/src/com/palmergames/bukkit/util/BookFactory.java b/src/com/palmergames/bukkit/util/BookFactory.java index fcfe12c869..81b1f9d0dd 100644 --- a/src/com/palmergames/bukkit/util/BookFactory.java +++ b/src/com/palmergames/bukkit/util/BookFactory.java @@ -6,7 +6,6 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; -import org.bukkit.map.MinecraftFont; import net.md_5.bungee.api.ChatColor; @@ -14,6 +13,8 @@ * @author LlmDl */ public class BookFactory { + + private static final float MAX_LINE_WIDTH = FontUtil.measureWidth("LLLLLLLLLLLLLLLLLLL"); // 113 pixels. /** * Returns a book itemstack with the given title, author and rawText. @@ -58,19 +59,14 @@ public static ItemStack makeBook(String title, String author, List pages /** * A method to feed raw text out of which lines for pages. - * + *

* Heavily inspired by - * https://www.spigotmc.org/threads/book-multipage-wrapping-text.383001/#post-3474541 + * https://www.spigotmc.org/threads/book-multipage-wrapping-text.383001/#post-3474541 * - * @param rawText + * @param rawText The raw text * @return lines as a List of Strings. */ private static List getLines(String rawText) { - // Note that the only flaw with using MinecraftFont is that it can't account for - // some UTF-8 symbols, it will throw an IllegalArgumentException - final MinecraftFont font = new MinecraftFont(); - final int maxLineWidth = font.getWidth("LLLLLLLLLLLLLLLLLLL"); // 113 pixels. - // An arraylist to store all of the individual lines which are made to fit a // book's line width. List lines = new ArrayList<>(); @@ -100,7 +96,7 @@ private static List getLines(String rawText) { * believe a space is only 2 pixels wide while it is in fact 3 pixels wide. */ int spaces = 0; // Number of pixels to add to the line length test later on. - if (font.getWidth(" ") == 2) { + if (FontUtil.font.getWidth(" ") == 2) { spaces = 1; // Because one space will be added in the test. for (int i = 0; i < line.length(); ++i) if (line.charAt(i) == ' ') @@ -108,7 +104,7 @@ private static List getLines(String rawText) { } // Current line + word is too long to be one line - if (FontUtil.measureWidth(line + " " + word) + spaces > maxLineWidth) { + if (FontUtil.measureWidth(line + " " + word) + spaces > MAX_LINE_WIDTH) { // Add our current line lines.add(line + "\n"); // Set our next line to start off with this word diff --git a/src/com/palmergames/bukkit/util/FontUtil.java b/src/com/palmergames/bukkit/util/FontUtil.java index ac95467c18..d310028b3f 100644 --- a/src/com/palmergames/bukkit/util/FontUtil.java +++ b/src/com/palmergames/bukkit/util/FontUtil.java @@ -5,11 +5,14 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextDecoration; +import org.jetbrains.annotations.ApiStatus; import solar.squares.pixelwidth.DefaultCharacterWidthFunction; import solar.squares.pixelwidth.PixelWidthSource; public class FontUtil { - private static final MinecraftFont font = new MinecraftFont(); + @ApiStatus.Internal + public static final MinecraftFont font = new MinecraftFont(); + private static final PixelWidthSource widthSource = PixelWidthSource .pixelWidth(new DefaultCharacterWidthFunction() { @Override