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
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
This commit is contained in:
@@ -82,6 +82,25 @@ public class CommandDecrypt implements TabExecutor {
|
|||||||
* @return <p>True if successful</p>
|
* @return <p>True if successful</p>
|
||||||
*/
|
*/
|
||||||
private boolean adminDecrypt(@NotNull Player player, @NotNull BookMeta bookMetadata) {
|
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();
|
StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter();
|
||||||
File encryptedDirectory = new File(BooksWithoutBorders.getConfiguration().getEncryptedBookPath());
|
File encryptedDirectory = new File(BooksWithoutBorders.getConfiguration().getEncryptedBookPath());
|
||||||
String[] encryptedFiles = encryptedDirectory.list();
|
String[] encryptedFiles = encryptedDirectory.list();
|
||||||
@@ -93,18 +112,15 @@ public class CommandDecrypt implements TabExecutor {
|
|||||||
//Get the "encryption key" from the filename
|
//Get the "encryption key" from the filename
|
||||||
String key = "";
|
String key = "";
|
||||||
for (String encryptedFile : encryptedFiles) {
|
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("]"));
|
key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]"));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!key.equalsIgnoreCase("")) {
|
if (!key.isBlank()) {
|
||||||
//Decrypt the book
|
//Decrypt the book
|
||||||
ItemStack book = EncryptionHelper.loadEncryptedBook(player, key, false, true);
|
ItemStack book = EncryptionHelper.loadEncryptedBookLegacy(player, key, false);
|
||||||
if (book == null) {
|
|
||||||
book = EncryptionHelper.loadEncryptedBookLegacy(player, key, false);
|
|
||||||
}
|
|
||||||
if (book != null) {
|
if (book != null) {
|
||||||
InventoryHelper.setHeldWrittenBook(player, book);
|
InventoryHelper.setHeldWrittenBook(player, book);
|
||||||
stringFormatter.displaySuccessMessage(player, Translatable.SUCCESS_AUTO_DECRYPTED);
|
stringFormatter.displaySuccessMessage(player, Translatable.SUCCESS_AUTO_DECRYPTED);
|
||||||
|
@@ -11,7 +11,9 @@ public enum StaticMessage {
|
|||||||
BOOK_FOLDER_CREATE_FAILED("Unable to create necessary folders"),
|
BOOK_FOLDER_CREATE_FAILED("Unable to create necessary folders"),
|
||||||
COMMAND_NOT_REGISTERED("Command {command} has not been registered!"),
|
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_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;
|
private final @NotNull String messageString;
|
||||||
|
|
||||||
|
@@ -138,7 +138,8 @@ public final class BookToFromTextHelper {
|
|||||||
* @return <p>Metadata for the loaded book</p>
|
* @return <p>Metadata for the loaded book</p>
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@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;
|
BookMeta meta;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.utility;
|
|||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.StaticMessage;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.AES;
|
import net.knarcraft.bookswithoutborders.encryption.AES;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.AESConfiguration;
|
import net.knarcraft.bookswithoutborders.encryption.AESConfiguration;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
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.Magic;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.OneTimePad;
|
import net.knarcraft.bookswithoutborders.encryption.OneTimePad;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher;
|
import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher;
|
||||||
|
import net.knarcraft.knarlib.formatting.StringFormatter;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -267,12 +269,8 @@ public final class EncryptionHelper {
|
|||||||
|
|
||||||
File file = new File(path + fileName + ".yml");
|
File file = new File(path + fileName + ".yml");
|
||||||
if (!file.isFile()) {
|
if (!file.isFile()) {
|
||||||
file = new File(path + fileName + ".txt");
|
BooksWithoutBorders.sendErrorMessage(player, "Book not found!");
|
||||||
|
return null;
|
||||||
if (!file.isFile()) {
|
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Book not found!");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
bookMetadata = BookToFromTextHelper.encryptedBookFromYml(file, bookMetadata, key, forceDecrypt);
|
bookMetadata = BookToFromTextHelper.encryptedBookFromYml(file, bookMetadata, key, forceDecrypt);
|
||||||
@@ -286,15 +284,7 @@ public final class EncryptionHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (deleteEncryptedFile) {
|
if (deleteEncryptedFile) {
|
||||||
Logger logger = BooksWithoutBorders.getInstance().getLogger();
|
deleteEncryptedFile(file);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);
|
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);
|
||||||
@@ -338,29 +328,21 @@ public final class EncryptionHelper {
|
|||||||
BooksWithoutBorders.sendErrorMessage(player, "Incorrect decryption key!");
|
BooksWithoutBorders.sendErrorMessage(player, "Incorrect decryption key!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
try {
|
|
||||||
bookMetadata = BookToFromTextHelper.bookFromFile(file, bookMetadata);
|
try {
|
||||||
if (bookMetadata == null) {
|
bookMetadata = BookToFromTextHelper.bookFromFile(file, bookMetadata);
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!");
|
if (bookMetadata == null) {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!");
|
BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "Decryption failed!");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deleteEncryptedFile) {
|
if (deleteEncryptedFile) {
|
||||||
Logger logger = BooksWithoutBorders.getInstance().getLogger();
|
deleteEncryptedFile(file);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);
|
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);
|
||||||
@@ -401,6 +383,23 @@ public final class EncryptionHelper {
|
|||||||
return data;
|
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
|
* Saves an encrypted book to be decryptable for the given group
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user