diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java index d17ee3f..2d7cb2d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java @@ -3,12 +3,14 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.state.BookDirectory; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.io.File; +import java.util.UUID; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; @@ -23,6 +25,24 @@ public final class BookHelper { } + /** + * Converts the author of a book from UUID if necessary + * + * @param author

The author string

+ * @return

The author string, converted if it was a UUID

+ */ + public static String authorFromUUID(String author) { + try { + UUID authorID = UUID.fromString(author); + Player player = Bukkit.getPlayer(authorID); + if (player != null) { + author = player.getName(); + } + } catch (IllegalArgumentException ignored) { + } + return author; + } + /** * Gets the file path of the selected book directory * @@ -106,10 +126,11 @@ public final class BookHelper { } String authorName; - if (book.hasAuthor()) { - authorName = book.getAuthor(); + if (!book.hasAuthor() || isAuthor(player.getName(), book.getAuthor())) { + //Store as unique id to account for name changes + authorName = player.getUniqueId().toString(); } else { - authorName = player.getName(); + authorName = book.getAuthor(); } return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false); @@ -123,14 +144,25 @@ public final class BookHelper { * @return

True if the player is not the book's author

*/ public static boolean isNotAuthor(Player player, BookMeta book) { - String author = book.getAuthor(); - String playerName = InputCleaningHelper.cleanString(player.getName()); - if (author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author))) { + if (isAuthor(player.getName(), book.getAuthor())) { return false; + } else { + BooksWithoutBorders.sendErrorMessage(player, + "You must be the author of this book to use this command!"); + return true; } + } - BooksWithoutBorders.sendErrorMessage(player, "You must be the author of this book to use this command!"); - return true; + /** + * Gets whether the given player name is equal to the given book author + * + * @param playerName

The player name to check

+ * @param author

The author to check

+ * @return

True if the player is the author

+ */ + private static boolean isAuthor(String playerName, String author) { + playerName = InputCleaningHelper.cleanString(playerName); + return author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author)); } } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java index 262eaf1..5eb3069 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java @@ -14,6 +14,7 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; +import static net.knarcraft.bookswithoutborders.utility.BookHelper.authorFromUUID; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; /** @@ -113,7 +114,7 @@ public final class BookToFromTextHelper { bookMetadata.setGeneration(BookMeta.Generation.valueOf(bookYml.getString("Generation", "ORIGINAL"))); bookMetadata.setTitle(bookYml.getString("Title", "Untitled")); - bookMetadata.setAuthor(bookYml.getString("Author", "Unknown")); + bookMetadata.setAuthor(authorFromUUID(bookYml.getString("Author", "Unknown"))); bookMetadata.setPages(bookYml.getStringList("Pages")); bookMetadata.setLore(bookYml.getStringList("Lore")); } catch (IllegalArgumentException e) { @@ -169,7 +170,7 @@ public final class BookToFromTextHelper { List pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages)); //Update the metadata of the book with its new values - bookMetadata.setAuthor(author); + bookMetadata.setAuthor(authorFromUUID(author)); bookMetadata.setTitle(title); bookMetadata.setPages(pages); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java index 29366e6..efd2099 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.state.BookDirectory; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -149,8 +150,20 @@ public final class FileHelper { } for (File foundFile : existingFiles) { - if (foundFile.isFile()) { - fileList.add(foundFile.getName()); + if (!foundFile.isFile()) { + continue; + } + String fileName = foundFile.getName(); + String separator = BooksWithoutBordersConfig.getTitleAuthorSeparator(); + if (fileName.contains(separator)) { + //Convert the UUID into a username if necessary + String[] data = fileName.split(separator); + String extension = data[1].substring(data[1].length() - 4); + String userName = data[1].substring(0, data[1].length() - 4); + data[1] = BookHelper.authorFromUUID(userName) + extension; + fileList.add(String.join(separator, data)); + } else { + fileList.add(fileName); } }