Fixes som nullability, and alters special character removal somewhat
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-08-02 21:35:26 +02:00
parent 2627407e6b
commit 32d31fced6
15 changed files with 137 additions and 57 deletions

View File

@@ -62,8 +62,8 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig
public class BooksWithoutBorders extends JavaPlugin {
private static ItemFactory itemFactory;
private static Map<UUID, List<String>> playerBooksList;
private static List<String> publicBooksList;
private static @NotNull Map<UUID, List<String>> playerBooksList = new HashMap<>();
private static @NotNull List<String> publicBooksList = new ArrayList<>();
private static Map<Character, Integer> publicLetterIndex;
private static Map<UUID, Map<Character, Integer>> playerLetterIndex;
private static BooksWithoutBorders booksWithoutBorders;
@@ -92,8 +92,10 @@ public class BooksWithoutBorders extends JavaPlugin {
UUID playerUUID = player.getUniqueId();
if (!playerBooksList.containsKey(playerUUID)) {
List<String> newFiles = BookFileHelper.listFiles(sender, false);
playerBooksList.put(playerUUID, newFiles);
playerLetterIndex.put(playerUUID, BookFileHelper.populateLetterIndices(newFiles));
if (newFiles != null) {
playerBooksList.put(playerUUID, newFiles);
playerLetterIndex.put(playerUUID, BookFileHelper.populateLetterIndices(newFiles));
}
}
return new ArrayList<>(playerBooksList.get(playerUUID));
} else {
@@ -125,6 +127,9 @@ public class BooksWithoutBorders extends JavaPlugin {
*/
public static void updateBooks(@NotNull CommandSender sender, boolean updatePublic) {
List<String> newFiles = BookFileHelper.listFiles(sender, updatePublic);
if (newFiles == null) {
return;
}
if (updatePublic) {
publicBooksList = newFiles;
publicLetterIndex = BookFileHelper.populateLetterIndices(newFiles);
@@ -150,8 +155,11 @@ public class BooksWithoutBorders extends JavaPlugin {
playerBooksList = new HashMap<>();
playerLetterIndex = new HashMap<>();
BooksWithoutBordersConfig.initialize(this);
publicBooksList = BookFileHelper.listFiles(consoleSender, true);
publicLetterIndex = BookFileHelper.populateLetterIndices(publicBooksList);
@Nullable List<String> files = BookFileHelper.listFiles(consoleSender, true);
if (files != null) {
publicBooksList = files;
publicLetterIndex = BookFileHelper.populateLetterIndices(files);
}
PluginManager pluginManager = this.getServer().getPluginManager();

View File

@@ -44,7 +44,7 @@ public class CommandDelete implements TabExecutor {
if (PagedBookIndex.displayPage(arguments, sender, deletePublic, command)) {
return true;
}
//Delete the file
if (arguments.length == 1) {
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic);

View File

@@ -52,7 +52,7 @@ public class CommandSave implements TabExecutor {
}
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, false, false);
if (holdingSlot != null && holdingSlot != ItemSlot.NONE) {
if (holdingSlot != ItemSlot.NONE) {
ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND);
boolean duplicate = args.length == 1 && Boolean.parseBoolean(args[0]);
saveBook(player, holdingItem, duplicate, savePublic);

View File

@@ -43,6 +43,10 @@ public class CommandSetAuthor implements TabExecutor {
boolean mainHand = heldBookSlot == ItemSlot.MAIN_HAND;
ItemStack heldBook = InventoryHelper.getHeldItem(player, mainHand);
BookMeta bookMetaData = InventoryHelper.getHeldBookMetadata(player, mainHand);
if (bookMetaData == null) {
BooksWithoutBorders.sendErrorMessage(player, "Unable to get metadata for the held book!");
return false;
}
String author = ColorHelper.translateColorCodes(String.join(" ", args), ColorConversion.RGB);
bookMetaData.setAuthor(author);

View File

@@ -26,7 +26,8 @@ import java.util.logging.Level;
public class CommandUnSign implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
if (!(sender instanceof Player player)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false;
@@ -49,9 +50,13 @@ public class CommandUnSign implements TabExecutor {
* @param player <p>The player holding the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand or its off hand</p>
*/
public void unSignHeldBook(Player player, boolean mainHand) {
public void unSignHeldBook(@NotNull Player player, boolean mainHand) {
//Get the old book
BookMeta oldMetadata = InventoryHelper.getHeldBookMetadata(player, mainHand);
if (oldMetadata == null) {
BooksWithoutBorders.sendErrorMessage(player, "Unable to get metadata from the held book!");
return;
}
ItemStack heldBook = InventoryHelper.getHeldBook(player, mainHand);
//Only allow the owner to un-sign the book
@@ -85,7 +90,8 @@ public class CommandUnSign implements TabExecutor {
}
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) {
return new ArrayList<>();
}

View File

@@ -1,5 +1,6 @@
package net.knarcraft.bookswithoutborders.gui;
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
@@ -146,7 +147,7 @@ public abstract class BookIndex {
@NotNull
protected static String getNiceName(@NotNull String bookPath) {
bookPath = ChatColor.translateAlternateColorCodes('&', bookPath.substring(0, bookPath.length() - 4));
String[] parts = bookPath.split(",");
String[] parts = bookPath.split(BooksWithoutBordersConfig.getTitleAuthorSeparator());
return parts[0].replace("_", " ") + ChatColor.RESET + " by " + parts[1] + ChatColor.RESET;
}

View File

@@ -16,6 +16,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -29,7 +30,7 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig
public class BookshelfListener implements Listener {
@EventHandler
public void onBookshelfClick(PlayerInteractEvent event) {
public void onBookshelfClick(@NotNull PlayerInteractEvent event) {
Player player = event.getPlayer();
// If left-clicking a chiseled bookshelf and sneaking, display contents
@@ -58,7 +59,8 @@ public class BookshelfListener implements Listener {
* @param bookshelfInventory <p>The inventory of the bookshelf to describe</p>
* @return <p>A textual description of the bookshelf's contents</p>
*/
private String getBookshelfDescription(ChiseledBookshelfInventory bookshelfInventory) {
@NotNull
private String getBookshelfDescription(@NotNull ChiseledBookshelfInventory bookshelfInventory) {
StringBuilder builder = new StringBuilder(getSuccessColor() + "Books in shelf:");
for (ItemStack itemStack : bookshelfInventory.getStorageContents()) {
if (itemStack == null) {
@@ -83,7 +85,8 @@ public class BookshelfListener implements Listener {
* @param itemMeta <p>The metadata for the book to describe</p>
* @return <p>The description of the book</p>
*/
private String getPlainBookDescription(ItemMeta itemMeta) {
@NotNull
private String getPlainBookDescription(@NotNull ItemMeta itemMeta) {
String name = itemMeta.getDisplayName();
if (name.isEmpty()) {
name = "Plain book";
@@ -97,7 +100,8 @@ public class BookshelfListener implements Listener {
* @param bookMeta <p>The metadata for the book to describe</p>
* @return <p>The book's description</p>
*/
private String getBookDescription(BookMeta bookMeta) {
@NotNull
private String getBookDescription(@NotNull BookMeta bookMeta) {
String title;
String author;
if (!bookMeta.hasTitle() || bookMeta.getTitle() == null) {
@@ -110,7 +114,7 @@ public class BookshelfListener implements Listener {
} else {
author = bookMeta.getAuthor();
}
return title + " by " + author;
return title + ChatColor.RESET + " by " + author;
}
/**
@@ -119,7 +123,8 @@ public class BookshelfListener implements Listener {
* @param enchantmentStorageMeta <p>The metadata for the enchanted book to describe</p>
* @return <p>The enchanted book's description</p>
*/
private String getEnchantedBookDescription(EnchantmentStorageMeta enchantmentStorageMeta) {
@NotNull
private String getEnchantedBookDescription(@NotNull EnchantmentStorageMeta enchantmentStorageMeta) {
StringBuilder builder = new StringBuilder();
builder.append("Enchanted book (");
Map<Enchantment, Integer> enchantmentMap = enchantmentStorageMeta.getStoredEnchants();
@@ -139,7 +144,8 @@ public class BookshelfListener implements Listener {
* @param enchantment <p>The enchantment to get the name of</p>
* @return <p>The prettified enchantment name</p>
*/
private String getEnchantmentName(Enchantment enchantment) {
@NotNull
private String getEnchantmentName(@NotNull Enchantment enchantment) {
// Note: While depreciated, changing this is incompatible with Paper
return uppercaseFirst(enchantment.getKey().getKey().replace("_", " "));
}
@@ -150,7 +156,8 @@ public class BookshelfListener implements Listener {
* @param input <p>The input to uppercase</p>
* @return <p>The input string with more uppercase</p>
*/
private String uppercaseFirst(String input) {
@NotNull
private String uppercaseFirst(@NotNull String input) {
String[] parts = input.split(" ");
for (int i = 0; i < parts.length; i++) {
parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1);

View File

@@ -5,6 +5,7 @@ import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
@@ -32,7 +33,7 @@ public final class BookFileHelper {
* @param possibleIndex <p>The string which might be a book index</p>
* @return <p>True if the number is a book index</p>
*/
public static boolean isBookListIndex(String possibleIndex) {
public static boolean isBookListIndex(@NotNull String possibleIndex) {
File bookDirectory = new File(getBookFolder().replaceAll("[\\\\/]$", ""));
try {
@@ -53,7 +54,7 @@ public final class BookFileHelper {
* @param bookFile <p>The path to a book</p>
* @return <p>True if the file exists and points to a book file</p>
*/
public static boolean bookFileExists(String bookFile) {
public static boolean bookFileExists(@NotNull String bookFile) {
return ((new File(bookFile).isFile() && (bookFile.endsWith(".txt") ||
bookFile.endsWith(".yml"))) || new File(bookFile + ".txt").isFile() ||
new File(bookFile + ".yml").isFile()) && !bookFile.contains("../") && !bookFile.contains("..\\");
@@ -67,7 +68,8 @@ public final class BookFileHelper {
* @param bookPath <p>The path of the book to get</p>
* @return <p>The file the path points to, or null otherwise</p>
*/
public static File getBookFile(String bookPath) {
@Nullable
public static File getBookFile(@NotNull String bookPath) {
if (!bookFileExists(bookPath)) {
return null;
}
@@ -97,7 +99,8 @@ public final class BookFileHelper {
* @param listPublic <p>Whether to list public or personal files</p>
* @return <p>A list of available files</p>
*/
public static List<String> listFiles(CommandSender sender, Boolean listPublic) {
@Nullable
public static List<String> listFiles(@NotNull CommandSender sender, @NotNull Boolean listPublic) {
File file = BookHelper.getBookDirectoryPath(listPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender);
if (file == null) {
return new ArrayList<>();
@@ -136,7 +139,8 @@ public final class BookFileHelper {
* @param searchDirectory <p>The directory to search for files</p>
* @return <p>A list of available files</p>
*/
private static List<String> listFiles(CommandSender sender, File searchDirectory) {
@Nullable
private static List<String> listFiles(@NotNull CommandSender sender, @NotNull File searchDirectory) {
List<String> fileList = new ArrayList<>();
File[] existingFiles = searchDirectory.listFiles();
@@ -146,7 +150,8 @@ public final class BookFileHelper {
}
for (File foundFile : existingFiles) {
if (!foundFile.isFile()) {
// Filter out invalid files
if (!foundFile.isFile() || foundFile.getName().contains(" ") || foundFile.getName().contains("§")) {
continue;
}
String fileName = foundFile.getName();
@@ -176,7 +181,7 @@ public final class BookFileHelper {
* @param fileName <p>The name of the file which might already exist</p>
* @return <p>The number of found duplicates</p>
*/
public static int findDuplicates(File[] foundFiles, String fileName) {
public static int findDuplicates(@NotNull File[] foundFiles, @NotNull String fileName) {
int foundDuplicates = 0;
for (File foundFile : foundFiles) {
if (foundFile.getName().matches("(\\([0-9]+\\))?" + Pattern.quote(fileName) + "(\\.yml|\\.txt)?")) {

View File

@@ -60,7 +60,7 @@ public final class BookToFromTextHelper {
bookYml.set("Lore", bookMetadata.getLore());
}
bookYml.save(path + fileName.replace("§", "&") + ".yml");
bookYml.save(path + InputCleaningHelper.cleanString(fileName) + ".yml");
}
/**
@@ -90,7 +90,7 @@ public final class BookToFromTextHelper {
* @throws IOException <p>If unable to save the book</p>
*/
public static void bookToTXT(@NotNull String folderPath, @NotNull String fileName, @NotNull BookMeta bookMetadata) throws IOException {
FileWriter fileWriter = new FileWriter(folderPath + fileName.replace("§", "&") + ".txt", StandardCharsets.UTF_8);
FileWriter fileWriter = new FileWriter(folderPath + InputCleaningHelper.cleanString(fileName) + ".txt", StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(fileWriter);
List<String> pages = bookMetadata.getPages();
@@ -164,13 +164,17 @@ public final class BookToFromTextHelper {
List<String> rawPages;
try {
rawPages = readTextFile(file);
if (rawPages == null) {
BooksWithoutBorders.getInstance().getLogger().log(Level.SEVERE, "Text file's first line was null");
return null;
}
} catch (IOException exception) {
BooksWithoutBorders.getInstance().getLogger().log(Level.SEVERE, "Unable to read text file");
return null;
}
//Parse the generation from the book data
if (rawPages != null && !rawPages.isEmpty() && rawPages.get(0).startsWith("Generation:")) {
if (!rawPages.isEmpty() && rawPages.get(0).startsWith("Generation:")) {
bookMetadata.setGeneration(BookMeta.Generation.valueOf(rawPages.get(0).split(":")[1]));
rawPages.remove(0);
}

View File

@@ -12,6 +12,7 @@ import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -32,6 +33,7 @@ public final class EconomyHelper {
*
* @return <p>An economy instance, or null if it's not initialized</p>
*/
@NotNull
public static Economy getEconomy() {
return economy;
}
@@ -67,7 +69,7 @@ public final class EconomyHelper {
* @param numCopies <p>The number of copies the player is trying to print</p>
* @return <p>True if the player cannot pay for the printing of the books</p>
*/
public static boolean cannotPayForBookPrinting(Player player, int numCopies) {
public static boolean cannotPayForBookPrinting(@NotNull Player player, int numCopies) {
//BookPriceQuantity: How many items are required to pay for each book
//BookPriceType: Which item is used to pay for the books. AIR = use economy
Material bookCurrency = BooksWithoutBordersConfig.getBookPriceType();
@@ -100,7 +102,7 @@ public final class EconomyHelper {
* @param itemCost <p>The number of writable books to pay</p>
* @return <p>True if the payment was successful</p>
*/
private static boolean takeWritableBookPayment(Player player, int itemCost) {
private static boolean takeWritableBookPayment(@NotNull Player player, int itemCost) {
List<ItemStack> books = getPlayersEmptyBooks(player);
if (countItems(books) < itemCost) {
BooksWithoutBorders.sendErrorMessage(player, itemCost + " empty " + Material.WRITABLE_BOOK +
@@ -131,7 +133,7 @@ public final class EconomyHelper {
* @param items <p>The items to count</p>
* @return <p>The total number of items</p>
*/
private static int countItems(List<ItemStack> items) {
private static int countItems(@NotNull List<ItemStack> items) {
int totalItems = 0;
for (ItemStack itemStack : items) {
totalItems += itemStack.getAmount();
@@ -145,7 +147,8 @@ public final class EconomyHelper {
* @param player <p>The player to get books for</p>
* @return <p>The empty books in the player's inventory</p>
*/
private static List<ItemStack> getPlayersEmptyBooks(Player player) {
@NotNull
private static List<ItemStack> getPlayersEmptyBooks(@NotNull Player player) {
List<ItemStack> validBooks = new ArrayList<>();
for (ItemStack itemStack : player.getInventory().getContents()) {
if (itemStack == null || itemStack.getType() != Material.WRITABLE_BOOK) {
@@ -168,7 +171,7 @@ public final class EconomyHelper {
* @param numCopies <p>The number of books the player is printing</p>
* @return <p>True if the player had the money and it has been withdrawn</p>
*/
private static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) {
private static boolean payForBookPrintingEconomy(@NotNull Player player, double cost, int numCopies) {
if ((economy.getBalance(player) - cost) >= 0) {
economy.withdrawPlayer(player, cost);
BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " +
@@ -188,7 +191,7 @@ public final class EconomyHelper {
* @param player <p>The player which needs to pay</p>
* @param itemCost <p>The number of items to pay</p>
*/
private static void payForBookPrintingItem(Player player, int itemCost) {
private static void payForBookPrintingItem(@NotNull Player player, int itemCost) {
PlayerInventory playerInventory = player.getInventory();
int clearedAmount = 0;

View File

@@ -11,6 +11,8 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
@@ -37,7 +39,8 @@ public final class EncryptionHelper {
* @param key <p>The key to transform</p>
* @return <p>The numbers representing the key's characters</p>
*/
public static String getNumberKeyFromStringKey(String key) {
@NotNull
public static String getNumberKeyFromStringKey(@NotNull String key) {
StringBuilder integerKey = new StringBuilder();
for (int x = 0; x < key.length(); x++) {
integerKey.append(Character.getNumericValue(Character.codePointAt(key, x)));
@@ -54,7 +57,9 @@ public final class EncryptionHelper {
* @param player <p>The player trying to encrypt a book</p>
* @return <p>The pages of the book in encrypted form</p>
*/
public static List<String> encryptBookPages(BookMeta book, EncryptionStyle style, String integerKey, Player player) {
@Nullable
public static List<String> encryptBookPages(@NotNull BookMeta book, @NotNull EncryptionStyle style,
@NotNull String integerKey, @NotNull Player player) {
List<String> encryptedPages = new ArrayList<>();
//Scramble the book's contents
if (style == EncryptionStyle.DNA) {
@@ -86,7 +91,9 @@ public final class EncryptionHelper {
* @param style <p>The encryption style to use</p>
* @return <p>An encrypted version of the book</p>
*/
public static ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style) {
@Nullable
public static ItemStack encryptBook(@NotNull Player player, boolean mainHand, @NotNull String key,
@NotNull EncryptionStyle style) {
return encryptBook(player, mainHand, key, style, "");
}
@@ -100,11 +107,17 @@ public final class EncryptionHelper {
* @param groupName <p>The name of the group to encrypt for, or "" otherwise</p>
* @return <p>An encrypted version of the book</p>
*/
public static ItemStack encryptBook(Player player, boolean mainHand, String key, EncryptionStyle style, String groupName) {
@Nullable
public static ItemStack encryptBook(Player player, boolean mainHand, @NotNull String key,
@NotNull EncryptionStyle style, @NotNull String groupName) {
//converts user supplied key into integer form
String integerKey = EncryptionHelper.getNumberKeyFromStringKey(key);
BookMeta book = InventoryHelper.getHeldBookMetadata(player, mainHand);
if (book == null) {
BooksWithoutBorders.sendErrorMessage(player, "Unable to get metadata from the held book!");
return null;
}
if (!book.hasPages()) {
BooksWithoutBorders.sendErrorMessage(player, "Book is empty!");
@@ -143,7 +156,9 @@ public final class EncryptionHelper {
* @param newMetadata <p>The new metadata of the book</p>
* @return <p>An encrypted version of the book</p>
*/
private static ItemStack createEncryptedBook(BookMeta book, List<String> newPages, Player player, BookMeta newMetadata) {
@NotNull
private static ItemStack createEncryptedBook(@NotNull BookMeta book, @NotNull List<String> newPages,
@NotNull Player player, @NotNull BookMeta newMetadata) {
//Create the encrypted book
ItemStack encryptedBook = new ItemStack(Material.WRITTEN_BOOK);
book.setPages(newPages);
@@ -166,7 +181,9 @@ public final class EncryptionHelper {
* @param integerKey <p>The key used to encrypt the book</p>
* @return <p>The new metadata for the book, or null if it could not be saved</p>
*/
private static BookMeta saveBookPlaintext(String groupName, Player player, BookMeta book, String integerKey) {
@Nullable
private static BookMeta saveBookPlaintext(@NotNull String groupName, @NotNull Player player,
@NotNull BookMeta book, @NotNull String integerKey) {
BookMeta newMetadata = book;
boolean wasSaved;
if (groupName.trim().isEmpty()) {
@@ -190,7 +207,8 @@ public final class EncryptionHelper {
* @param deleteEncryptedFile <p>Whether to delete the plaintext file after decryption is finished</p>
* @return <p>The loaded book, or null if no book could be loaded</p>
*/
public static ItemStack loadEncryptedBook(Player player, String key, boolean deleteEncryptedFile) {
@Nullable
public static ItemStack loadEncryptedBook(@NotNull Player player, @NotNull String key, boolean deleteEncryptedFile) {
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta();
String path = getBookFolder() + "Encrypted" + getSlash();
@@ -246,7 +264,9 @@ public final class EncryptionHelper {
* @param groupName <p>The group which should be able to decrypt the book</p>
* @return <p>The new encrypted metadata for the book, or null if encryption failed</p>
*/
private static BookMeta saveEncryptedBookForGroup(Player player, BookMeta bookMetadata, String groupName) {
@Nullable
private static BookMeta saveEncryptedBookForGroup(@NotNull Player player, @NotNull BookMeta bookMetadata,
@NotNull String groupName) {
String path = getBookFolder() + "Encrypted" + getSlash() + cleanString(groupName) + getSlash();
File dirTest = new File(path);
//Creates group dir
@@ -297,7 +317,8 @@ public final class EncryptionHelper {
* @param key <p>The key to use for encryption</p>
* @return <p>The new encrypted metadata for the book, or null if encryption failed</p>
*/
private static Boolean saveEncryptedBook(Player player, BookMeta bookMetaData, String key) {
@NotNull
private static Boolean saveEncryptedBook(@NotNull Player player, @NotNull BookMeta bookMetaData, @NotNull String key) {
String path = getBookFolder() + "Encrypted" + getSlash();
String fileName = "[" + key + "]" + BookHelper.getBookFile(bookMetaData, player, true);

View File

@@ -22,7 +22,8 @@ public final class InputCleaningHelper {
* @param list <p>The list to clean</p>
* @return <p>A clean list containing all relevant values</p>
*/
public static List<String> cleanList(List<String> list) {
@NotNull
public static List<String> cleanList(@NotNull List<String> list) {
List<String> resultList = new ArrayList<>(list);
resultList.removeIf((item) -> item == null || item.trim().isEmpty());
return resultList;
@@ -34,7 +35,8 @@ public final class InputCleaningHelper {
* @param fileName <p>The file name to clean</p>
* @return <p>The cleaned file name</p>
*/
public static String cleanString(String fileName) {
@NotNull
public static String cleanString(@NotNull String fileName) {
fileName = fileName.replace("/", "");
fileName = fileName.replace("\\", "");
fileName = fileName.replace("*", "");
@@ -44,6 +46,7 @@ public final class InputCleaningHelper {
fileName = fileName.replace(">", "");
fileName = fileName.replace("?", "");
fileName = fileName.replace("\"", "");
fileName = fileName.replace("§", "&");
return fileName;
}
@@ -54,7 +57,8 @@ public final class InputCleaningHelper {
* @param isLoading <p>Whether loading from a file as opposed to saving to a file</p>
* @return <p>The fixed name</p>
*/
public static String fixName(String fileName, Boolean isLoading) {
@NotNull
public static String fixName(@NotNull String fileName, @NotNull Boolean isLoading) {
if (isLoading) {
fileName = fileName.replace("_", " ");
} else {

View File

@@ -1,5 +1,7 @@
package net.knarcraft.bookswithoutborders.utility;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -40,6 +42,7 @@ public final class IntegerToRomanConverter {
* @param number <p>The number to convert</p>
* @return <p>The roman representation of the number</p>
*/
@NotNull
public static String getRomanNumber(int number) {
StringBuilder output = new StringBuilder();
int remainder = number;

View File

@@ -7,6 +7,8 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The inventory helper mainly helps with getting and setting books
@@ -23,7 +25,8 @@ public final class InventoryHelper {
* @param signedBook <p>Whether to check for signed or unsigned books</p>
* @return <p>The book the player is holding</p>
*/
public static ItemStack getHeldBook(Player player, boolean signedBook) {
@NotNull
public static ItemStack getHeldBook(@NotNull Player player, boolean signedBook) {
ItemSlot heldSlot = getHeldSlotBook(player, false, false, true, signedBook);
if (heldSlot == ItemSlot.MAIN_HAND) {
return getHeldItem(player, true);
@@ -40,7 +43,7 @@ public final class InventoryHelper {
* @param player <p>The player holding the book</p>
* @param newBook <p>The new book the player should hold</p>
*/
public static void setHeldWrittenBook(Player player, ItemStack newBook) {
public static void setHeldWrittenBook(@NotNull Player player, @NotNull ItemStack newBook) {
ItemSlot itemSlot = getHeldSlotBook(player, false, false, true, true);
if (itemSlot == ItemSlot.MAIN_HAND) {
replaceHeldItem(player, newBook, true);
@@ -59,7 +62,8 @@ public final class InventoryHelper {
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
* @return <p>False if the player is holding exactly one book</p>
*/
public static boolean notHoldingOneWritableBookCheck(Player player, String noBookMessage, String twoBooksMessage) {
public static boolean notHoldingOneWritableBookCheck(@NotNull Player player, @NotNull String noBookMessage,
@NotNull String twoBooksMessage) {
BookHoldingState holdingState = getBookHoldingState(player);
if (holdingState == BookHoldingState.NONE || holdingState == BookHoldingState.SIGNED_BOTH_HANDS ||
@@ -84,7 +88,8 @@ public final class InventoryHelper {
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
* @return <p>False if the player is holding exactly one book</p>
*/
public static boolean notHoldingOneWrittenBookCheck(Player player, String noBookMessage, String twoBooksMessage) {
public static boolean notHoldingOneWrittenBookCheck(@NotNull Player player, @NotNull String noBookMessage,
@NotNull String twoBooksMessage) {
BookHoldingState holdingState = getBookHoldingState(player);
if (holdingState == BookHoldingState.NONE || holdingState == BookHoldingState.UNSIGNED_BOTH_HANDS ||
@@ -111,7 +116,8 @@ public final class InventoryHelper {
* @param writtenBook <p>Whether to search for written or unwritten books, if it's relevant</p>
* @return <p>The slot of the player's held book</p>
*/
public static ItemSlot getHeldSlotBook(Player player, boolean handMatters, boolean mainHand,
@NotNull
public static ItemSlot getHeldSlotBook(@NotNull Player player, boolean handMatters, boolean mainHand,
boolean typeMatters, boolean writtenBook) {
BookHoldingState state = getBookHoldingState(player);
ItemStack mainHandItem = getHeldItem(player, true);
@@ -168,7 +174,8 @@ public final class InventoryHelper {
* @param player <p>The player to check</p>
* @return <p>The state of the player's book holding</p>
*/
private static BookHoldingState getBookHoldingState(Player player) {
@NotNull
private static BookHoldingState getBookHoldingState(@NotNull Player player) {
ItemStack mainHandItem = getHeldItem(player, true);
ItemStack offHandItem = getHeldItem(player, false);
@@ -205,7 +212,8 @@ public final class InventoryHelper {
* @param mainHand <p>Whether to get information about a book in the player's main hand or off hand</p>
* @return <p>Information about the held book</p>
*/
public static BookMeta getHeldBookMetadata(Player player, boolean mainHand) {
@Nullable
public static BookMeta getHeldBookMetadata(@NotNull Player player, boolean mainHand) {
return (BookMeta) getHeldItem(player, mainHand).getItemMeta();
}
@@ -216,7 +224,8 @@ public final class InventoryHelper {
* @param mainHand <p>Whether to get the item in the player's main hand or off hand</p>
* @return <p>The item the player is holding in the given hand</p>
*/
public static ItemStack getHeldItem(Player player, boolean mainHand) {
@NotNull
public static ItemStack getHeldItem(@NotNull Player player, boolean mainHand) {
if (mainHand) {
return player.getInventory().getItemInMainHand();
} else {
@@ -231,7 +240,7 @@ public final class InventoryHelper {
* @param newBook <p>The new book the player should hold</p>
* @param mainHand <p>Whether to replace the item in the player's main hand or off hand</p>
*/
public static void replaceHeldItem(Player player, ItemStack newBook, boolean mainHand) {
public static void replaceHeldItem(@NotNull Player player, @NotNull ItemStack newBook, boolean mainHand) {
if (mainHand) {
player.getInventory().setItemInMainHand(newBook);
} else {

View File

@@ -1,5 +1,7 @@
package net.knarcraft.bookswithoutborders.utility;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -16,6 +18,7 @@ public final class TabCompletionTypeHelper {
*
* @return <p>A list of booleans</p>
*/
@NotNull
public static List<String> getBooleans() {
List<String> booleans = new ArrayList<>();
booleans.add("true");
@@ -30,6 +33,7 @@ public final class TabCompletionTypeHelper {
* @param end <p>The end number</p>
* @return <p>A list of numbers</p>
*/
@NotNull
public static List<String> getNumbers(int start, int end) {
List<String> numbers = new ArrayList<>();
for (int i = start; i <= end; i++) {
@@ -45,6 +49,7 @@ public final class TabCompletionTypeHelper {
* @param end <p>The end number</p>
* @return <p>A list of booleans and numbers</p>
*/
@NotNull
public static List<String> getBooleansAndNumbers(int start, int end) {
List<String> booleansAndNumbers = new ArrayList<>();
List<String> booleans = getBooleans();