Updates admin decryption to handle both new and old encrypted files
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-08-15 17:20:23 +02:00
parent 67a16bc604
commit b8af9b94be
4 changed files with 58 additions and 40 deletions

View File

@@ -82,6 +82,25 @@ public class CommandDecrypt implements TabExecutor {
* @return <p>True if successful</p>
*/
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 player <p>The player performing the admin decryption</p>
* @param bookMetadata <p>The metadata of the player's held encrypted book</p>
* @return <p>True if successfully decrypted</p>
*/
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);

View File

@@ -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;

View File

@@ -138,7 +138,8 @@ public final class BookToFromTextHelper {
* @return <p>Metadata for the loaded book</p>
*/
@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 {

View File

@@ -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 file <p>The file to delete</p>
*/
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
*