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

Use FontUtil's minecraftfont constant for bookfactory #6133

Merged
merged 1 commit into from
Aug 26, 2022
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
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