diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java index 80c1bf0..bdef43d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDecrypt.java @@ -82,6 +82,25 @@ public class CommandDecrypt implements TabExecutor { * @return
True if successful
*/ private boolean adminDecrypt(@NotNull Player player, @NotNull BookMeta bookMetadata) { + StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter(); + ItemStack decrypted = EncryptionHelper.loadEncryptedBook(player, "", false, true); + if (decrypted != null) { + InventoryHelper.setHeldWrittenBook(player, decrypted); + stringFormatter.displaySuccessMessage(player, Translatable.SUCCESS_AUTO_DECRYPTED); + return true; + } else { + return adminDecryptLegacy(player, bookMetadata); + } + } + + /** + * Admin decrypts a legacy book + * + * @param playerThe player performing the admin decryption
+ * @param bookMetadataThe metadata of the player's held encrypted book
+ * @returnTrue if successfully decrypted
+ */ + private boolean adminDecryptLegacy(@NotNull Player player, @NotNull BookMeta bookMetadata) { StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter(); File encryptedDirectory = new File(BooksWithoutBorders.getConfiguration().getEncryptedBookPath()); String[] encryptedFiles = encryptedDirectory.list(); @@ -93,18 +112,15 @@ public class CommandDecrypt implements TabExecutor { //Get the "encryption key" from the filename String key = ""; for (String encryptedFile : encryptedFiles) { - if (encryptedFile.contains(BookHelper.getBookFile(bookMetadata, player, true))) { + if (encryptedFile.contains(BookHelper.getBookFile(bookMetadata, player, true).replace(" ", "_"))) { key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]")); break; } } - if (!key.equalsIgnoreCase("")) { + if (!key.isBlank()) { //Decrypt the book - ItemStack book = EncryptionHelper.loadEncryptedBook(player, key, false, true); - if (book == null) { - book = EncryptionHelper.loadEncryptedBookLegacy(player, key, false); - } + ItemStack book = EncryptionHelper.loadEncryptedBookLegacy(player, key, false); if (book != null) { InventoryHelper.setHeldWrittenBook(player, book); stringFormatter.displaySuccessMessage(player, Translatable.SUCCESS_AUTO_DECRYPTED); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java b/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java index f163acc..3ac8080 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/config/StaticMessage.java @@ -11,7 +11,9 @@ public enum StaticMessage { BOOK_FOLDER_CREATE_FAILED("Unable to create necessary folders"), COMMAND_NOT_REGISTERED("Command {command} has not been registered!"), EXCEPTION_VAULT_NOT_AVAILABLE("Vault is unavailable, but book price is set to economy. Unsetting book cost!"), - EXCEPTION_VAULT_PRICE_NOT_CHANGED("BooksWithoutBorders failed to hook into Vault! Book price not set!"); + EXCEPTION_VAULT_PRICE_NOT_CHANGED("BooksWithoutBorders failed to hook into Vault! Book price not set!"), + EXCEPTION_ENCRYPTED_FILE_DELETE_FAILED("Book encryption data failed to delete upon decryption!\nFile location: {path}"), + ; private final @NotNull String messageString; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java index f422855..417565a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookToFromTextHelper.java @@ -138,7 +138,8 @@ public final class BookToFromTextHelper { * @returnMetadata for the loaded book
*/ @Nullable - public static BookMeta encryptedBookFromYml(@NotNull File file, @NotNull BookMeta bookMetadata, @NotNull String userKey, boolean forceDecrypt) { + public static BookMeta encryptedBookFromYml(@NotNull File file, @NotNull BookMeta bookMetadata, + @NotNull String userKey, boolean forceDecrypt) { BookMeta meta; try { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java index 8c02728..0ba12b9 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/EncryptionHelper.java @@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; +import net.knarcraft.bookswithoutborders.config.StaticMessage; import net.knarcraft.bookswithoutborders.encryption.AES; import net.knarcraft.bookswithoutborders.encryption.AESConfiguration; import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle; @@ -10,6 +11,7 @@ import net.knarcraft.bookswithoutborders.encryption.GenenCrypt; import net.knarcraft.bookswithoutborders.encryption.Magic; import net.knarcraft.bookswithoutborders.encryption.OneTimePad; import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -267,12 +269,8 @@ public final class EncryptionHelper { File file = new File(path + fileName + ".yml"); if (!file.isFile()) { - file = new File(path + fileName + ".txt"); - - if (!file.isFile()) { - BooksWithoutBorders.sendErrorMessage(player, "Book not found!"); - return null; - } + BooksWithoutBorders.sendErrorMessage(player, "Book not found!"); + return null; } else { try { bookMetadata = BookToFromTextHelper.encryptedBookFromYml(file, bookMetadata, key, forceDecrypt); @@ -286,15 +284,7 @@ public final class EncryptionHelper { } if (deleteEncryptedFile) { - Logger logger = BooksWithoutBorders.getInstance().getLogger(); - try { - if (!file.delete()) { - logger.log(Level.SEVERE, "Book encryption data failed to delete upon decryption!\n" + - "File location:" + file.getPath()); - } - } catch (Exception e) { - logger.log(Level.SEVERE, "Book encryption data failed to delete upon decryption!\nFile location:" + file.getPath()); - } + deleteEncryptedFile(file); } ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK); @@ -338,29 +328,21 @@ public final class EncryptionHelper { BooksWithoutBorders.sendErrorMessage(player, "Incorrect decryption key!"); return null; } - } else { - try { - bookMetadata = BookToFromTextHelper.bookFromFile(file, bookMetadata); - if (bookMetadata == null) { - BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!"); - return null; - } - } catch (Exception e) { + } + + try { + bookMetadata = BookToFromTextHelper.bookFromFile(file, bookMetadata); + if (bookMetadata == null) { BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!"); return null; } + } catch (Exception e) { + BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!"); + return null; } if (deleteEncryptedFile) { - Logger logger = BooksWithoutBorders.getInstance().getLogger(); - try { - if (!file.delete()) { - logger.log(Level.SEVERE, "Book encryption data failed to delete upon decryption!\n" + - "File location:" + file.getPath()); - } - } catch (Exception e) { - logger.log(Level.SEVERE, "Book encryption data failed to delete upon decryption!\nFile location:" + file.getPath()); - } + deleteEncryptedFile(file); } ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK); @@ -401,6 +383,23 @@ public final class EncryptionHelper { return data; } + /** + * Attempts to delete the encryption file after a book has been decrypted + * + * @param fileThe file to delete
+ */ + private static void deleteEncryptedFile(@NotNull File file) { + Logger logger = BooksWithoutBorders.getInstance().getLogger(); + String errorMessage = StaticMessage.EXCEPTION_ENCRYPTED_FILE_DELETE_FAILED.toString(); + try { + if (!file.delete()) { + logger.log(Level.SEVERE, StringFormatter.replacePlaceholder(errorMessage, "{path}", file.getPath())); + } + } catch (Exception exception) { + logger.log(Level.SEVERE, StringFormatter.replacePlaceholder(errorMessage, "{path}", file.getPath())); + } + } + /** * Saves an encrypted book to be decryptable for the given group *