Skip to content

Commit

Permalink
Merge pull request #6133 from TownyAdvanced/bookfactory-warrior
Browse files Browse the repository at this point in the history
Use FontUtil's minecraftfont constant for bookfactory
  • Loading branch information
LlmDl authored Aug 26, 2022
2 parents 48f310f + 45ab3d2 commit 9a651be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/com/palmergames/bukkit/util/BookFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
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;

/**
* @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.
Expand Down Expand Up @@ -58,19 +59,14 @@ public static ItemStack makeBook(String title, String author, List<String> pages

/**
* A method to feed raw text out of which lines for pages.
*
* <p>
* Heavily inspired by
* https://www.spigotmc.org/threads/book-multipage-wrapping-text.383001/#post-3474541
* <a href="https://www.spigotmc.org/threads/book-multipage-wrapping-text.383001/#post-3474541">https://www.spigotmc.org/threads/book-multipage-wrapping-text.383001/#post-3474541</a>
*
* @param rawText
* @param rawText The raw text
* @return lines as a List of Strings.
*/
private static List<String> 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<String> lines = new ArrayList<>();
Expand Down Expand Up @@ -100,15 +96,15 @@ private static List<String> 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) == ' ')
spaces++;
}

// 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
Expand Down
5 changes: 4 additions & 1 deletion src/com/palmergames/bukkit/util/FontUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9a651be

Please sign in to comment.