Marks encrypted books to prevent dual encryption #15
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:
@@ -7,6 +7,7 @@ import net.knarcraft.bookswithoutborders.config.translation.Translatable;
|
|||||||
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
||||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EncryptedBookUtil;
|
import net.knarcraft.bookswithoutborders.utility.EncryptedBookUtil;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.EncryptionUtil;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
|
import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
|
||||||
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
||||||
import net.knarcraft.knarlib.util.TabCompletionHelper;
|
import net.knarcraft.knarlib.util.TabCompletionHelper;
|
||||||
@@ -95,6 +96,11 @@ public class CommandEncrypt implements TabExecutor {
|
|||||||
new FormatBuilder(Translatable.ERROR_ENCRYPT_EMPTY).error(player);
|
new FormatBuilder(Translatable.ERROR_ENCRYPT_EMPTY).error(player);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (EncryptionUtil.isEncrypted(bookMetadata)) {
|
||||||
|
new FormatBuilder(Translatable.ERROR_ENCRYPTED_ALREADY).error(player);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return bookMetadata;
|
return bookMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,6 +121,7 @@ public class CommandEncrypt implements TabExecutor {
|
|||||||
encryptionStyle, group, preventAdminDecryption);
|
encryptionStyle, group, preventAdminDecryption);
|
||||||
|
|
||||||
if (encryptedBook != null) {
|
if (encryptedBook != null) {
|
||||||
|
EncryptionUtil.markEncrypted(encryptedBook);
|
||||||
InventoryUtil.setHeldWrittenBook(player, encryptedBook);
|
InventoryUtil.setHeldWrittenBook(player, encryptedBook);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.command;
|
|||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.config.translation.Translatable;
|
import net.knarcraft.bookswithoutborders.config.translation.Translatable;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.encryption.EncryptionStyle;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.EncryptionUtil;
|
||||||
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@@ -34,9 +35,8 @@ public class CommandGroupEncrypt extends CommandEncrypt implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Check if book is already group encrypted
|
//Check if book is already group encrypted
|
||||||
List<String> lore = bookMetadata.getLore();
|
if (EncryptionUtil.isEncrypted(bookMetadata)) {
|
||||||
if (bookMetadata.hasLore() && lore != null && lore.get(0).contains(" encrypted]")) {
|
new FormatBuilder(Translatable.ERROR_ENCRYPTED_ALREADY).error(sender);
|
||||||
new FormatBuilder(Translatable.ERROR_GROUP_ENCRYPTED_ALREADY).error(sender);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -281,7 +281,7 @@ public enum Translatable implements TranslatableMessage {
|
|||||||
/**
|
/**
|
||||||
* The error displayed when trying to attempt a group encrypted book twice
|
* The error displayed when trying to attempt a group encrypted book twice
|
||||||
*/
|
*/
|
||||||
ERROR_GROUP_ENCRYPTED_ALREADY,
|
ERROR_ENCRYPTED_ALREADY,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The error displayed when a book fails to be loaded
|
* The error displayed when a book fails to be loaded
|
||||||
|
@@ -12,6 +12,8 @@ import net.knarcraft.bookswithoutborders.state.SignType;
|
|||||||
import net.knarcraft.bookswithoutborders.utility.BookFileUtil;
|
import net.knarcraft.bookswithoutborders.utility.BookFileUtil;
|
||||||
import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil;
|
import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EncryptedBookUtil;
|
import net.knarcraft.bookswithoutborders.utility.EncryptedBookUtil;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.EncryptionUtil;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.InventoryUtil;
|
||||||
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
import net.knarcraft.knarlib.formatting.FormatBuilder;
|
||||||
import net.knarcraft.knarlib.util.SignHelper;
|
import net.knarcraft.knarlib.util.SignHelper;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
@@ -29,6 +31,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
|
|||||||
import org.bukkit.inventory.EquipmentSlot;
|
import org.bukkit.inventory.EquipmentSlot;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -173,11 +176,18 @@ public class SignEventListener implements Listener {
|
|||||||
if (heldItemType != Material.WRITTEN_BOOK) {
|
if (heldItemType != Material.WRITTEN_BOOK) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
BookMeta meta = InventoryUtil.getHeldBookMetadata(player, ItemSlot.fromEquipmentSlot(hand));
|
||||||
|
if (meta != null && EncryptionUtil.isEncrypted(meta)) {
|
||||||
|
new FormatBuilder(Translatable.ERROR_ENCRYPTED_ALREADY).error(player);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
player.closeInventory();
|
player.closeInventory();
|
||||||
ItemStack eBook = EncryptedBookUtil.encryptBook(player, ItemSlot.fromEquipmentSlot(hand), getPassword(lines),
|
ItemStack encryptedBook = EncryptedBookUtil.encryptBook(player, ItemSlot.fromEquipmentSlot(hand), getPassword(lines),
|
||||||
getEncryptionStyle(lines), false);
|
getEncryptionStyle(lines), false);
|
||||||
if (eBook != null) {
|
if (encryptedBook != null) {
|
||||||
player.getInventory().setItem(hand, eBook);
|
EncryptionUtil.markEncrypted(encryptedBook);
|
||||||
|
player.getInventory().setItem(hand, encryptedBook);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -114,7 +114,7 @@ public final class EncryptedBookUtil {
|
|||||||
* @return <p>An encrypted version of the book</p>
|
* @return <p>An encrypted version of the book</p>
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static ItemStack encryptBook(Player player, @NotNull ItemSlot itemSlot, @NotNull String key,
|
public static ItemStack encryptBook(@NotNull Player player, @NotNull ItemSlot itemSlot, @NotNull String key,
|
||||||
@NotNull EncryptionStyle style, @NotNull String groupName,
|
@NotNull EncryptionStyle style, @NotNull String groupName,
|
||||||
boolean preventAdminDecrypt) {
|
boolean preventAdminDecrypt) {
|
||||||
BookMeta book = InventoryUtil.getHeldBookMetadata(player, itemSlot);
|
BookMeta book = InventoryUtil.getHeldBookMetadata(player, itemSlot);
|
||||||
|
@@ -1,11 +1,16 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A utility class for encryption-related tasks
|
* A utility class for encryption-related tasks
|
||||||
@@ -100,4 +105,42 @@ public final class EncryptionUtil {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a lore line marking the book as encrypted
|
||||||
|
*
|
||||||
|
* @param itemStack <p>The book to mark as encrypted</p>
|
||||||
|
*/
|
||||||
|
public static void markEncrypted(@NotNull ItemStack itemStack) {
|
||||||
|
BookMeta meta = (BookMeta) itemStack.getItemMeta();
|
||||||
|
if (meta == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> lore = meta.getLore();
|
||||||
|
if (lore == null) {
|
||||||
|
lore = new ArrayList<>();
|
||||||
|
}
|
||||||
|
lore.add(0, ChatColor.GRAY + "[Encrypted]");
|
||||||
|
meta.setLore(lore);
|
||||||
|
itemStack.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the given item stack is an encrypted book
|
||||||
|
*
|
||||||
|
* @param meta <p>The meta of the book to check</p>
|
||||||
|
* @return <p>True if the item is marked as an encrypted book</p>
|
||||||
|
*/
|
||||||
|
public static boolean isEncrypted(@NotNull BookMeta meta) {
|
||||||
|
if (!meta.hasLore()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> lore = meta.getLore();
|
||||||
|
if (lore == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return lore.get(0).equals(ChatColor.GRAY + "[Encrypted]") || lore.get(0).contains(" encrypted]");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -72,7 +72,7 @@ en:
|
|||||||
ERROR_OUT_OF_RANGE_BOOK_PAGE: "The given page index is out of bounds!"
|
ERROR_OUT_OF_RANGE_BOOK_PAGE: "The given page index is out of bounds!"
|
||||||
ERROR_NO_BOOK_PAGE: "You must supply a page index"
|
ERROR_NO_BOOK_PAGE: "You must supply a page index"
|
||||||
ERROR_GROUP_ENCRYPT_ARGUMENTS_MISSING: "You must specify a group name and key to encrypt a book!"
|
ERROR_GROUP_ENCRYPT_ARGUMENTS_MISSING: "You must specify a group name and key to encrypt a book!"
|
||||||
ERROR_GROUP_ENCRYPTED_ALREADY: "Book is already group encrypted!"
|
ERROR_ENCRYPTED_ALREADY: "Book is already encrypted!"
|
||||||
ERROR_LOAD_FAILED: "Book failed to load!"
|
ERROR_LOAD_FAILED: "Book failed to load!"
|
||||||
ERROR_RELOAD_FAILED: |
|
ERROR_RELOAD_FAILED: |
|
||||||
Reload Failed!
|
Reload Failed!
|
||||||
|
Reference in New Issue
Block a user