diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java index cef0612..d022d5d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBordersListener.java @@ -1,8 +1,8 @@ package net.knarcraft.bookswithoutborders; import java.io.File; -import java.util.ArrayList; import java.util.List; +import java.util.Objects; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -76,13 +76,13 @@ public class BooksWithoutBordersListener implements Listener { } @EventHandler - public void onHold(PlayerItemHeldEvent e) { - if (e.isCancelled()) { + public void onHold(PlayerItemHeldEvent event) { + if (event.isCancelled()) { return; } - Player player = e.getPlayer(); - int selectedSlot = e.getNewSlot(); + Player player = event.getPlayer(); + int selectedSlot = event.getNewSlot(); PlayerInventory playerInventory = player.getInventory(); ItemStack selectedItem = playerInventory.getItem(selectedSlot); @@ -101,8 +101,8 @@ public class BooksWithoutBordersListener implements Listener { } @EventHandler - public void onPlayerJoin(PlayerJoinEvent e) { - Player player = e.getPlayer(); + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); //Handle new players if (!BooksWithoutBorders.bwb.hasPlayedBefore(player.getName())) { @@ -127,69 +127,14 @@ public class BooksWithoutBordersListener implements Listener { } } - /** - * Gives a book to a player joining for the first time - * - * @param bookName
The name of the book to give
- * @param playerThe player which just joined
- * @param sendMessageWhether to send a message to the joining player
- * @returnTrue if a message has yet to be sent
- */ - private boolean giveBookToNewPlayer(String bookName, Player player, boolean sendMessage) { - if (!bookName.trim().isEmpty()) { - - //Handles loadList numbers - try { - Integer.parseInt(bookName); - //Load public books for the player - ListThe player to update
- * @param itemMetadataInformation about the held book
- * @param mainHandWhether to update the book in the player's main hand
- */ - private void updateBookInHand(Player player, ItemMeta itemMetadata, boolean mainHand) { - PlayerInventory playerInventory = player.getInventory(); - ItemStack updatedBook = updateBook(player, (BookMeta) itemMetadata); - if (updatedBook != null) { - if (mainHand) { - playerInventory.setItemInMainHand(updatedBook); - } else { - playerInventory.setItemInOffHand(updatedBook); - } - } - } - @EventHandler - public void onSignChange(SignChangeEvent e) { - if (e.isCancelled()) { + public void onSignChange(SignChangeEvent event) { + if (event.isCancelled()) { return; } - String[] lines = e.getLines(); - Player player = e.getPlayer(); + String[] lines = event.getLines(); + Player player = event.getPlayer(); //Check if creating a Books Without Borders Sign and if the player has permission if (!lines[0].equalsIgnoreCase("[BwB]") || !player.hasPermission("bookswithoutborders.signs")) { @@ -197,71 +142,29 @@ public class BooksWithoutBordersListener implements Listener { } //Mark the sign as active - e.setLine(0, ChatColor.DARK_GREEN + "[BwB]"); + event.setLine(0, ChatColor.DARK_GREEN + "[BwB]"); - if ((lines[1].equalsIgnoreCase("[Encrypt]") || lines[1].equalsIgnoreCase("[Decrypt]") || - lines[1].equalsIgnoreCase("[Give]")) && lines[2].trim().length() > 0) { - - //Mark the second line as valid - e.setLine(1, ChatColor.DARK_BLUE + lines[1]); - - //Mark valid encryption/decryption sign - if (lines[1].equalsIgnoreCase(ChatColor.DARK_BLUE + "[Encrypt]") || - lines[1].equalsIgnoreCase(ChatColor.DARK_BLUE + "[Decrypt]")) { - e.setLine(2, ChatColor.MAGIC + lines[2]); - e.setLine(3, ChatColor.DARK_BLUE + lines[3]); - } - - //Generate book giving sign - if (lines[1].equalsIgnoreCase(ChatColor.DARK_BLUE + "[Give]")) { - if (lines[2].length() > 13 || lines[3].length() > 13) { - player.sendMessage(ChatColor.RED + "[Give] signs' 3rd and 4th lines must be 13 characters or less!"); - e.setLine(2, ChatColor.DARK_RED + lines[2]); - e.setLine(3, ChatColor.DARK_RED + lines[3]); - return; - } - - //Tests if a full file name has been supplied - if ((new File(bookFolder + lines[2] + lines[3]).isFile()) || - (new File(bookFolder + lines[2] + lines[3] + ".txt").isFile()) || - (new File(bookFolder + lines[2] + lines[3] + ".yml").isFile())) { - e.setLine(2, ChatColor.DARK_GREEN + lines[2]); - e.setLine(3, ChatColor.DARK_GREEN + lines[3]); - } else { - //Tests if a load list number has been supplied - File dirFile = new File(bookFolderPath); - for (int x = 0; x < lines[2].length(); x++) { - if (!Character.isDigit(lines[2].charAt(x))) { - e.setLine(2, ChatColor.DARK_RED + lines[2]); - e.setLine(3, ChatColor.DARK_RED + lines[3]); - break; - } - if (x == lines[2].length() - 1) { - ListThe event caused by changing the sign
+ * @param linesThe lines on the sign
+ * @param playerThe player which edited the sign
+ */ + private void generateGiveSign(SignChangeEvent event, String[] lines, Player player) { + if (lines[2].length() > 13 || lines[3].length() > 13) { + player.sendMessage(ChatColor.RED + "[Give] signs' 3rd and 4th lines must be 13 characters or less!"); + markGiveSignValidity(event, false); + return; + } + + //Tests if a full file name has been supplied and points to an actual file + String signFile = bookFolder + lines[2] + lines[3]; + if (bookFileExists(signFile)) { + markGiveSignValidity(event, true); + } else { + if (isBookListIndex(lines[2])) { + ListThe string which might be a book index
+ * @returnTrue if the number is a book index
+ */ + private boolean isBookListIndex(String possibleIndex) { + File bookDirectory = new File(bookFolderPath); + + try { + //Tests if a load list number has been supplied + int parsedLine = Integer.parseInt(possibleIndex); + File[] foundFiles = bookDirectory.listFiles(File::isFile); + + return parsedLine >= 0 && parsedLine <= Objects.requireNonNull(foundFiles).length; + } catch (NumberFormatException ignored) {} + + return false; + } + + /** + * Checks if a file path points to an actual book + * @param bookFileThe path to a book
+ * @returnTrue if the file exists and points to a book file
+ */ + private boolean bookFileExists(String bookFile) { + return ((new File(bookFile).isFile() && (bookFile.toLowerCase().endsWith(".txt") || + bookFile.toLowerCase().endsWith(".yml"))) || new File(bookFile + ".txt").isFile() || + new File(bookFile + ".yml").isFile()) && !bookFile.contains("../") && !bookFile.contains("..\\"); + } + + /** + * Marks a give sign as valid or invalid + * @param eventThe event causing the creation of the give sign
+ * @param isValidWhether the created sign is valid
+ */ + private void markGiveSignValidity(SignChangeEvent event, boolean isValid) { + String[] lines = event.getLines(); + if (isValid) { + event.setLine(2, ChatColor.DARK_GREEN + lines[2]); + event.setLine(3, ChatColor.DARK_GREEN + lines[3]); + } else { + event.setLine(2, ChatColor.DARK_RED + lines[2]); + event.setLine(3, ChatColor.DARK_RED + lines[3]); + } + } + + /** + * Gives a book to a player joining for the first time + * + * @param bookNameThe name of the book to give
+ * @param playerThe player which just joined
+ * @param sendMessageWhether to send a message to the joining player
+ * @returnTrue if a message has yet to be sent
+ */ + private boolean giveBookToNewPlayer(String bookName, Player player, boolean sendMessage) { + if (!bookName.trim().isEmpty()) { + + //Handles loadList numbers + if (isBookListIndex(bookName)) { + ListThe player to update
+ * @param itemMetadataInformation about the held book
+ * @param mainHandWhether to update the book in the player's main hand
+ */ + private void updateBookInHand(Player player, ItemMeta itemMetadata, boolean mainHand) { + PlayerInventory playerInventory = player.getInventory(); + ItemStack updatedBook = updateBook(player, (BookMeta) itemMetadata); + if (updatedBook != null) { + if (mainHand) { + playerInventory.setItemInMainHand(updatedBook); + } else { + playerInventory.setItemInOffHand(updatedBook); + } + } + } + /** * Decrypts a book decryptable for a given group *