diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 0cfbf06..6c96b85 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -60,22 +60,35 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; public class BooksWithoutBorders extends JavaPlugin { + + //Limit for duplicates of a book public static int bookDuplicateLimit; //The separating string between the book title and the book author public static String titleAuthorSeparator; + //Separator to force Lore newline public static String loreSeparator; + //Books to give players on first login public static List firstBooks; + //Message to display to new players public static String welcomeMessage; + //List of players public static List existingPlayers; + //The material used for payment public static Material bookPriceType = null; + //The number of items/currency to pay for each transaction public static double bookPriceQuantity; + //Whether only the author of a book should be allowed to copy the book public static boolean authorOnlyCopy; + //Whether to use YML files instead of TXT files for saving books public static boolean useYml; + //Whether admins should be able to decrypt group encrypted books without belonging to its group public static boolean adminDecrypt; + private static ItemFactory itemFactory; public static Map> loadList; - public static BooksWithoutBorders booksWithoutBorders; + private static BooksWithoutBorders booksWithoutBorders; public static ConsoleCommandSender consoleSender; + private static String existingPlayersFile; @Override public void onEnable() { @@ -85,6 +98,8 @@ public class BooksWithoutBorders extends JavaPlugin { firstBooks = new ArrayList<>(); BooksWithoutBordersSettings.initialize(this); + existingPlayersFile = this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt"; + PluginManager pluginManager = this.getServer().getPluginManager(); if (getSlash() != null && initialize()) { @@ -170,6 +185,10 @@ public class BooksWithoutBorders extends JavaPlugin { return loadExistingPlayers(); } + /** + * Makes sure necessary folders exist + * @return

True if necessary folders exist

+ */ private boolean testFileSaving() { File fileTest = new File(getBookFolder()); File encryptedFileTest = new File(getBookFolder() + "Encrypted" + getSlash()); @@ -293,7 +312,7 @@ public class BooksWithoutBorders extends JavaPlugin { } public boolean loadExistingPlayers() { - File fTest = new File(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt"); + File fTest = new File(existingPlayersFile); existingPlayers = new ArrayList<>(); if (!fTest.exists()) { @@ -339,7 +358,7 @@ public class BooksWithoutBorders extends JavaPlugin { existingPlayers.add(playerName); try { - PrintWriter pw = new PrintWriter(new FileWriter(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt", true)); + PrintWriter pw = new PrintWriter(new FileWriter(existingPlayersFile, true)); pw.println(playerName); pw.close(); } catch (IOException e) { @@ -467,24 +486,6 @@ public class BooksWithoutBorders extends JavaPlugin { } } - /** - * Lists available files - * - * @param sender

The command sender looking for files

- * @param listPublic

Whether to list public or personal files

- * @param silent

Whether to just return the list without printing it

- * @return

A list of available files

- */ - public List listFiles(CommandSender sender, Boolean listPublic, boolean silent) { - File file; - if (listPublic) { - file = new File(getBookFolder()); - } else { - file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash()); - } - return FileHelper.listFiles(sender, file, silent); - } - /** * Sends a success message to a command sender (player or a console) * diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index 12f4cac..58488cb 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -18,8 +18,6 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS */ public class CommandDelete implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (!(sender instanceof Player)) { @@ -41,7 +39,7 @@ public class CommandDelete implements CommandExecutor { boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) { //List deletable files if (args.length == 0) { - BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, deletePublic, false)); + BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, deletePublic, false)); return true; } //Delete the file diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index 27e0b5c..69c0743 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -32,7 +33,7 @@ public class CommandGive implements CommandExecutor { } if (args.length == 0) { - BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, false)); + BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, false)); return true; } @@ -55,7 +56,7 @@ public class CommandGive implements CommandExecutor { //Load books available to the player try { Integer.parseInt(bookIdentifier); - BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, true)); + BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, true)); } catch (NumberFormatException ignored) { } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java index 6b63fea..123a7b8 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -44,7 +45,7 @@ public class CommandLoad implements CommandExecutor { //Show books available to the player if (argumentCount == 0) { - BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(player, loadPublic, false)); + BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(player, loadPublic, false)); return true; } @@ -66,7 +67,7 @@ public class CommandLoad implements CommandExecutor { //Load books available to the player try { Integer.parseInt(bookIdentifier); - BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, loadPublic, true)); + BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, loadPublic, true)); } catch (NumberFormatException ignored) { } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index a71db4a..8385c31 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -14,7 +14,6 @@ import org.bukkit.inventory.meta.BookMeta; import java.io.File; import java.io.IOException; -import java.util.Arrays; import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.titleAuthorSeparator; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java b/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java index df80316..31209d5 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.FileHelper; import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -8,7 +9,6 @@ import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** @@ -49,7 +49,7 @@ public class GiveTabCompleter implements TabCompleter { if (argumentCount == 1) { //Return list of books - return BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, false, true)); + return BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, false, true)); } else if (argumentCount == 2) { //Return online players return playerNames; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java index af6131e..5617bce 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.listener; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.Material; @@ -24,6 +25,7 @@ import static net.knarcraft.bookswithoutborders.utility.FileHelper.isBookListInd public class PlayerEventListener implements Listener { private final String slash = getSlash(); + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @EventHandler public void onHold(PlayerItemHeldEvent event) { @@ -90,12 +92,12 @@ public class PlayerEventListener implements Listener { //Handles loadList numbers if (isBookListIndex(bookName)) { - List availableFiles = BooksWithoutBorders.booksWithoutBorders.listFiles(player, true, true); + List availableFiles = FileHelper.listFiles(player, true, true); BooksWithoutBorders.loadList.put(player.getName(), availableFiles); } //Give the book to the player if it exists - ItemStack newBook = BooksWithoutBorders.booksWithoutBorders.loadBook(player, bookName, "true", "public"); + ItemStack newBook = booksWithoutBorders.loadBook(player, bookName, "true", "public"); if (newBook != null) { player.getInventory().addItem(newBook); } @@ -103,7 +105,7 @@ public class PlayerEventListener implements Listener { //Send the player a welcome message if it exists if (!BooksWithoutBorders.welcomeMessage.trim().isEmpty() && newBook != null && sendMessage) { sendMessage = false; - BooksWithoutBorders.booksWithoutBorders.getServer().getScheduler().scheduleSyncDelayedTask(BooksWithoutBorders.booksWithoutBorders, + booksWithoutBorders.getServer().getScheduler().scheduleSyncDelayedTask(booksWithoutBorders, () -> player.sendMessage(BooksWithoutBorders.welcomeMessage), 40L); } } @@ -164,7 +166,7 @@ public class PlayerEventListener implements Listener { for (String path : possiblePaths) { File file = new File(path); if (file.isFile()) { - return BooksWithoutBorders.booksWithoutBorders.loadBook(player, fileName, "true", "player"); + return booksWithoutBorders.loadBook(player, fileName, "true", "player"); } } return null; @@ -180,7 +182,7 @@ public class PlayerEventListener implements Listener { */ private boolean hasPlayedBefore(String playerName) { if (!BooksWithoutBorders.existingPlayers.contains(playerName)) { - BooksWithoutBorders.booksWithoutBorders.addExistingPlayer(playerName); + booksWithoutBorders.addExistingPlayer(playerName); return false; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java index 780be11..cb25a5a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/SignEventListener.java @@ -184,7 +184,7 @@ public class SignEventListener implements Listener { markGiveSignValidity(event, true); } else { if (isBookListIndex(lines[2])) { - List availableFiles = BooksWithoutBorders.booksWithoutBorders.listFiles(player, true, true); + List availableFiles = FileHelper.listFiles(player, true, true); BooksWithoutBorders.loadList.put(player.getName(), availableFiles); markGiveSignValidity(event, true); return; @@ -245,7 +245,7 @@ public class SignEventListener implements Listener { return; } } - newBook = BooksWithoutBorders.booksWithoutBorders.loadBook(player, fileName, "true", groupName, heldItem.getAmount()); + newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", groupName, heldItem.getAmount()); player.getInventory().setItem(hand, newBook); player.closeInventory(); @@ -288,7 +288,7 @@ public class SignEventListener implements Listener { if (!Character.isDigit(fileName.charAt(x))) break; if (x == fileName.length() - 1) { - BooksWithoutBorders.loadList.put(player.getName(), BooksWithoutBorders.booksWithoutBorders.listFiles(player, true, true)); + BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, true, true)); isLoadListNumber = true; } } @@ -296,7 +296,7 @@ public class SignEventListener implements Listener { if (!isLoadListNumber && sign.getLine(3).length() >= 2) fileName = ChatColor.stripColor(sign.getLine(2)) + ChatColor.stripColor(sign.getLine(3)); - ItemStack newBook = BooksWithoutBorders.booksWithoutBorders.loadBook(player, fileName, "true", "public"); + ItemStack newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", "public"); if (newBook != null) { player.getInventory().addItem(newBook); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java index 42520f8..1a637b3 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java @@ -11,6 +11,8 @@ import java.util.Objects; import java.util.regex.Pattern; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; +import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; /** * Helper class for dealing with files @@ -85,6 +87,24 @@ public final class FileHelper { return null; } + /** + * Lists available files + * + * @param sender

The command sender looking for files

+ * @param listPublic

Whether to list public or personal files

+ * @param silent

Whether to just return the list without printing it

+ * @return

A list of available files

+ */ + public static List listFiles(CommandSender sender, Boolean listPublic, boolean silent) { + File file; + if (listPublic) { + file = new File(getBookFolder()); + } else { + file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash()); + } + return FileHelper.listFiles(sender, file, silent); + } + /** * Lists available files *