diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java index aa600f4..0e8eb7d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java @@ -2,8 +2,8 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; +import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.EconomyHelper; -import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; import org.bukkit.command.Command; @@ -41,12 +41,13 @@ public class CommandCopy implements TabExecutor { } ItemStack heldBook = InventoryHelper.getHeldBook(player, true); + //TODO: Rewrite this so the resulting book becomes a COPY, or COPY OF COPY try { int copies = Integer.parseInt(args[0]); if (copies > 0) { if (BooksWithoutBordersConfig.getAuthorOnlyCopy() && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyCopy")) { - if (!isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta()))) { + if (BookHelper.isNotAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta()))) { return false; } } @@ -68,26 +69,9 @@ public class CommandCopy implements TabExecutor { return false; } - /** - * Checks whether the given player is the author of a given book - * - * @param player
The player to check
- * @param bookThe book to check
- * @returnTrue if the player is the book's author
- */ - private boolean isAuthor(Player player, BookMeta book) { - String author = book.getAuthor(); - String playerName = InputCleaningHelper.cleanString(player.getName()); - if (author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author))) { - return true; - } - - BooksWithoutBorders.sendErrorMessage(player, "You must be the author of this book to use this command!"); - return false; - } - @Override - public ListWhether only the book author can unsign it
+ */ + public static boolean getAuthorOnlyUnsign() { + return authorOnlyUnsign; + } + /** * Gets whether to use YML, not TXT, for saving books * @@ -257,10 +267,12 @@ public class BooksWithoutBordersConfig { config.set(ConfigOption.PRICE_QUANTITY.getConfigNode(), bookPriceQuantity); config.set(ConfigOption.ADMIN_AUTO_DECRYPT.getConfigNode(), adminDecrypt); config.set(ConfigOption.AUTHOR_ONLY_COPY.getConfigNode(), authorOnlyCopy); + config.set(ConfigOption.AUTHOR_ONLY_UNSIGN.getConfigNode(), authorOnlyUnsign); //Handles old book and quill settings if (config.contains("Options.Require_book_and_quill_to_create_book")) { - sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\""); + sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting " + + "\"Require_book_and_quill_to_create_book\""); sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings"); if (config.getBoolean("Options.Require_book_and_quill_to_create_book")) { @@ -296,6 +308,8 @@ public class BooksWithoutBordersConfig { (Boolean) ConfigOption.ADMIN_AUTO_DECRYPT.getDefaultValue()); authorOnlyCopy = config.getBoolean(ConfigOption.AUTHOR_ONLY_COPY.getConfigNode(), (Boolean) ConfigOption.AUTHOR_ONLY_COPY.getDefaultValue()); + authorOnlyUnsign = config.getBoolean(ConfigOption.AUTHOR_ONLY_UNSIGN.getConfigNode(), + (Boolean) ConfigOption.AUTHOR_ONLY_UNSIGN.getDefaultValue()); firstBooks = config.getStringList(ConfigOption.BOOKS_FOR_NEW_PLAYERS.getConfigNode()); welcomeMessage = config.getString(ConfigOption.MESSAGE_FOR_NEW_PLAYERS.getConfigNode(), (String) ConfigOption.MESSAGE_FOR_NEW_PLAYERS.getDefaultValue()); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/config/ConfigOption.java b/src/main/java/net/knarcraft/bookswithoutborders/config/ConfigOption.java index bce30a2..ef00ecc 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/config/ConfigOption.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/config/ConfigOption.java @@ -55,6 +55,11 @@ public enum ConfigOption { */ AUTHOR_ONLY_COPY("Options.Author_Only_Copy", false), + /** + * Whether only the book author should be able to unsign a book + */ + AUTHOR_ONLY_UNSIGN("Author_Only_Unsign", false), + /** * Whether to automatically format every signed book */ diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java index 96402e5..78e6449 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookFormatter.java @@ -1,8 +1,6 @@ package net.knarcraft.bookswithoutborders.utility; -import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.md_5.bungee.api.ChatColor; -import org.bukkit.entity.Player; import org.bukkit.inventory.meta.BookMeta; import java.util.ArrayList; @@ -11,9 +9,6 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; -import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; - /** * A class for formatting text to fit books */ @@ -22,32 +17,6 @@ public final class BookFormatter { private BookFormatter() { } - /** - * Gets the file name of the given book - * - * @param bookThe book to get the file of
- * @param playerThe player trying to do something with the book
- * @returnThe book file
- */ - public static String getBookFile(BookMeta book, Player player) { - String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator(); - String bookName; - if (!book.hasTitle()) { - bookName = book.getTitle(); - } else { - bookName = "Untitled"; - } - - String authorName; - if (book.hasAuthor()) { - authorName = book.getAuthor(); - } else { - authorName = player.getName(); - } - - return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false); - } - /** * Formats the last page of a set of pages * diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java new file mode 100644 index 0000000..abb8cea --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java @@ -0,0 +1,64 @@ +package net.knarcraft.bookswithoutborders.utility; + +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; +import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.BookMeta; + +import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; +import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; + +/** + * Helper class for getting abstract book information + */ +public final class BookHelper { + + private BookHelper() { + + } + + /** + * Gets the file name of the given book + * + * @param bookThe book to get the file of
+ * @param playerThe player trying to do something with the book
+ * @returnThe book file
+ */ + public static String getBookFile(BookMeta book, Player player) { + String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator(); + String bookName; + if (!book.hasTitle()) { + bookName = book.getTitle(); + } else { + bookName = "Untitled"; + } + + String authorName; + if (book.hasAuthor()) { + authorName = book.getAuthor(); + } else { + authorName = player.getName(); + } + + return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false); + } + + /** + * Checks whether the given player is the author of a given book + * + * @param playerThe player to check
+ * @param bookThe book to check
+ * @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))) { + return false; + } + + BooksWithoutBorders.sendErrorMessage(player, "You must be the author of this book to use this command!"); + return true; + } + +} diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java index 10ab780..9eb84d5 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java @@ -198,7 +198,7 @@ public final class EncryptionHelper { return null; } - String fileName = "[" + key + "]" + BookFormatter.getBookFile(bookMetadata, player); + String fileName = "[" + key + "]" + BookHelper.getBookFile(bookMetadata, player); fileName = fixName(cleanString(fileName), false); File file = new File(path + fileName + ".yml"); @@ -261,7 +261,7 @@ public final class EncryptionHelper { } } //Generate file name - String fileName = BookFormatter.getBookFile(bookMetadata, player); + String fileName = BookHelper.getBookFile(bookMetadata, player); List