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
+ * @returnThe 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 { * @returnTrue 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 playerNameThe player name to check
+ * @param authorThe author to check
+ * @returnTrue 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