From a7c284ade2812c126e1c2db423ea0544ec452559 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 9 Aug 2022 17:21:55 +0200 Subject: [PATCH] Adds an option for only allowing un-signing by the author --- .../command/CommandCopy.java | 26 ++------ .../command/CommandDecrypt.java | 4 +- .../command/CommandSave.java | 4 +- .../command/CommandUnSign.java | 10 +++ .../config/BooksWithoutBordersConfig.java | 16 ++++- .../config/ConfigOption.java | 5 ++ .../utility/BookFormatter.java | 31 --------- .../utility/BookHelper.java | 64 +++++++++++++++++++ .../utility/EncryptionHelper.java | 6 +- src/main/resources/config.yml | 2 + src/main/resources/plugin.yml | 3 + 11 files changed, 111 insertions(+), 60 deletions(-) create mode 100644 src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java 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 book

The book to check

- * @return

True 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 List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, + @NotNull String[] args) { int argumentCount = args.length; if (argumentCount == 1) { return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getNumbers(1, 20), args[0]); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java index 354fe76..0a4c0a8 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java @@ -2,7 +2,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; -import net.knarcraft.bookswithoutborders.utility.BookFormatter; +import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.command.Command; @@ -59,7 +59,7 @@ public class CommandDecrypt implements TabExecutor { //Get the "encryption key" from the filename String key = ""; for (String encryptedFile : encryptedFiles) { - if (encryptedFile.contains(BookFormatter.getBookFile(bookMetadata, player))) { + if (encryptedFile.contains(BookHelper.getBookFile(bookMetadata, player))) { key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]")); break; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index 490aa96..ada0d5c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -3,7 +3,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.state.ItemSlot; -import net.knarcraft.bookswithoutborders.utility.BookFormatter; +import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; @@ -86,7 +86,7 @@ public class CommandSave implements TabExecutor { } //Generate book filename - String fileName = BookFormatter.getBookFile(book, player); + String fileName = BookHelper.getBookFile(book, player); //Make sure the used folders exist File file = new File(savePath); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java index 43c29c3..a6936fd 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandUnSign.java @@ -1,7 +1,9 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; import net.knarcraft.bookswithoutborders.state.ItemSlot; +import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.Material; import org.bukkit.command.Command; @@ -14,6 +16,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * Command executor for the unsign command @@ -48,6 +51,13 @@ public class CommandUnSign implements TabExecutor { //Get the old book BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand); + //Only allow the owner to un-sign the book + if (BooksWithoutBordersConfig.getAuthorOnlyUnsign() && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyUnsign")) { + if (BookHelper.isNotAuthor(player, Objects.requireNonNull(oldBook))) { + return; + } + } + //UnSign the book ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK); newBook.setItemMeta(oldBook); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/config/BooksWithoutBordersConfig.java b/src/main/java/net/knarcraft/bookswithoutborders/config/BooksWithoutBordersConfig.java index 6b24cd5..27205d7 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/config/BooksWithoutBordersConfig.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/config/BooksWithoutBordersConfig.java @@ -34,6 +34,7 @@ public class BooksWithoutBordersConfig { private static Material bookPriceType = null; private static double bookPriceQuantity; private static boolean authorOnlyCopy; + private static boolean authorOnlyUnsign; private static boolean useYml; private static boolean adminDecrypt; private static boolean formatBooks; @@ -106,6 +107,15 @@ public class BooksWithoutBordersConfig { return authorOnlyCopy; } + /** + * Gets whether only the author of a book should be able to unsign it + * + * @return

Whether 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 book

The book to get the file of

- * @param player

The player trying to do something with the book

- * @return

The 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 book

The book to get the file of

+ * @param player

The player trying to do something with the book

+ * @return

The 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 player

The player to check

+ * @param book

The book to check

+ * @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))) { + 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 newLore = new ArrayList<>(); newLore.add(ChatColor.GRAY + "[" + groupName + " encrypted]"); @@ -300,7 +300,7 @@ public final class EncryptionHelper { private static Boolean saveEncryptedBook(Player player, BookMeta bookMetaData, String key) { String path = getBookFolder() + "Encrypted" + getSlash(); - String fileName = "[" + key + "]" + BookFormatter.getBookFile(bookMetaData, player); + String fileName = "[" + key + "]" + BookHelper.getBookFile(bookMetaData, player); fileName = fixName(cleanString(fileName), false); //cancels saving if file is already encrypted diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4d87acc..af60df1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -21,6 +21,8 @@ Options: Admin_Auto_Decrypt: false # Whether to only allow the author of a book to create copies Author_Only_Copy: false + # Whether to only allow the author of a book to unsign it + Author_Only_Unsign: false # Whether to automatically format every book when it's signed Format_Book_After_Signing: false \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a955695..ce1dab9 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -111,6 +111,7 @@ permissions: bookswithoutborders.give: true bookswithoutborders.givepublic: true bookswithoutborders.bypassauthoronlycopy: true + bookswithoutborders.bypassauthoronlyunsign: true bookswithoutborders.bypassbookprice: true bookswithoutborders.setbookprice: true bookswithoutborders.use: @@ -163,6 +164,8 @@ permissions: description: Allows player to set the lore of the currently held item bookswithoutborders.bypassauthoronlycopy: description: Allows player to ignore Author_Only_Copy config setting + bookswithoutborders.bypassauthoronlyunsign: + description: Allows player to ignore Author_Only_Unsign config setting bookswithoutborders.bypassbookprice: description: Allows player to ignore Price_to_create_book config setting bookswithoutborders.setbookprice: