diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 8fed087..9d6bb0a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -32,6 +32,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.command.TabCompleter; +import org.bukkit.configuration.Configuration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; @@ -49,28 +50,25 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; -import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getErrorColor; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSuccessColor; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; -import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; public class BooksWithoutBorders extends JavaPlugin { - private static int bookDuplicateLimit; + public static int bookDuplicateLimit; //The separating string between the book title and the book author public static String titleAuthorSeparator; public static String loreSeparator; public static List firstBooks; public static String welcomeMessage; - private static List existingPlayers; + public static List existingPlayers; public static Economy eco; public static Material bookPriceType = null; public static double bookPriceQuantity; @@ -102,29 +100,33 @@ public class BooksWithoutBorders extends JavaPlugin { registerCommands(); } + public static BooksWithoutBorders getInstance() { + return booksWithoutBorders; + } + /** * Registers all commands used by this plugin */ private void registerCommands() { - registerCommand("give", new CommandGive(this), new GiveTabCompleter(this)); + registerCommand("give", new CommandGive(), new GiveTabCompleter()); registerCommand("decrypt", new CommandDecrypt(), null); registerCommand("groupEncrypt", new CommandGroupEncrypt(), null); - registerCommand("delete", new CommandDelete(this), null); - registerCommand("deletePublic", new CommandDeletePublic(this), null); - registerCommand("copy", new CommandCopy(this), null); + registerCommand("delete", new CommandDelete(), null); + registerCommand("deletePublic", new CommandDeletePublic(), null); + registerCommand("copy", new CommandCopy(), null); registerCommand("unSign", new CommandUnSign(), null); registerCommand("encrypt", new CommandEncrypt(), null); - registerCommand("setBookPrice", new CommandSetBookPrice(this), null); + registerCommand("setBookPrice", new CommandSetBookPrice(), null); registerCommand("setLore", new CommandSetLore(), null); - registerCommand("savePublic", new CommandSavePublic(this), null); - registerCommand("save", new CommandSave(this), null); + registerCommand("savePublic", new CommandSavePublic(), null); + registerCommand("save", new CommandSave(), null); registerCommand("setAuthor", new CommandSetAuthor(), null); registerCommand("setTitle", new CommandSetTitle(), null); - registerCommand("load", new CommandLoad(this), null); - registerCommand("loadPublic", new CommandLoadPublic(this), null); - registerCommand("booksWithoutBorders", new CommandBooksWithoutBorders(this), null); - registerCommand("reload", new CommandReload(this), null); - registerCommand("givePublic", new CommandGivePublic(this), null); + registerCommand("load", new CommandLoad(), null); + registerCommand("loadPublic", new CommandLoadPublic(), null); + registerCommand("booksWithoutBorders", new CommandBooksWithoutBorders(), null); + registerCommand("reload", new CommandReload(), null); + registerCommand("givePublic", new CommandGivePublic(), null); } /** @@ -161,95 +163,110 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - this.getConfig().set("Options.Save_Books_in_Yaml_Format", useYml); - this.getConfig().set("Options.Max_Number_of_Duplicates", bookDuplicateLimit); - this.getConfig().set("Options.Title-Author_Separator", titleAuthorSeparator); - this.getConfig().set("Options.Lore_line_separator", loreSeparator); - this.getConfig().set("Options.Books_for_new_players", firstBooks); - this.getConfig().set("Options.Message_for_new_players", welcomeMessage); - if (bookPriceType != null) { - if (bookPriceType != Material.AIR) { - this.getConfig().set("Options.Price_to_create_book.Item_type", bookPriceType.toString()); - } else { - this.getConfig().set("Options.Price_to_create_book.Item_type", "Economy"); - } - } else { - this.getConfig().set("Options.Price_to_create_book.Item_type", "Item type name"); - } - this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); - this.getConfig().set("Options.Admin_Auto_Decrypt", adminDecrypt); - this.getConfig().set("Options.Author_Only_Copy", authorOnlyCopy); + //Save config with loaded values to fix invalid config values + saveConfigValues(); - //Handles old book and quill settings - if (this.getConfig().contains("Options.Require_book_and_quill_to_create_book")) { - sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\""); - sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings"); - - if (this.getConfig().getBoolean("Options.Require_book_and_quill_to_create_book")) { - bookPriceType = Material.WRITABLE_BOOK; - bookPriceQuantity = 1; - this.getConfig().set("Options.Price_to_create_book.Item_type", bookPriceType.toString()); - this.getConfig().set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); - } - - this.getConfig().set("Options.Require_book_and_quill_to_create_book", null); - } - - this.saveConfig(); - - File fTest = new File(getBookFolder()); - File efTest = new File(getBookFolder() + "Encrypted" + getSlash()); - - if (!fTest.exists()) { - try { - if (!fTest.mkdir()) { - sendErrorMessage(consoleSender, "Saving failed! Aborting..."); - return false; - } - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - if (!efTest.exists()) { - try { - if (!efTest.mkdir()) { - sendErrorMessage(consoleSender, "Saving failed! Aborting..."); - return false; - } - } catch (Exception e) { - e.printStackTrace(); - return false; - } + if (!testFileSaving()) { + return false; } return loadExistingPlayers(); } + private boolean testFileSaving() { + File fileTest = new File(getBookFolder()); + File encryptedFileTest = new File(getBookFolder() + "Encrypted" + getSlash()); + if (!fileTest.exists()) { + try { + if (!fileTest.mkdir()) { + sendErrorMessage(consoleSender, "Saving failed! Aborting..."); + return false; + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + if (!encryptedFileTest.exists()) { + try { + if (!encryptedFileTest.mkdir()) { + sendErrorMessage(consoleSender, "Saving failed! Aborting..."); + return false; + } + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + return true; + } + + public void saveConfigValues() { + Configuration config = this.getConfig(); + config.set("Options.Save_Books_in_Yaml_Format", useYml); + config.set("Options.Max_Number_of_Duplicates", bookDuplicateLimit); + config.set("Options.Title-Author_Separator", titleAuthorSeparator); + config.set("Options.Lore_line_separator", loreSeparator); + config.set("Options.Books_for_new_players", firstBooks); + config.set("Options.Message_for_new_players", welcomeMessage); + if (bookPriceType != null) { + if (bookPriceType != Material.AIR) { + config.set("Options.Price_to_create_book.Item_type", bookPriceType.toString()); + } else { + config.set("Options.Price_to_create_book.Item_type", "Economy"); + } + } else { + config.set("Options.Price_to_create_book.Item_type", "Item type name"); + } + config.set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); + config.set("Options.Admin_Auto_Decrypt", adminDecrypt); + config.set("Options.Author_Only_Copy", authorOnlyCopy); + + //Handles old book and quill settings + if (config.contains("Options.Require_book_and_quill_to_create_book")) { + sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\""); + sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings"); + + if (config.getBoolean("Options.Require_book_and_quill_to_create_book")) { + bookPriceType = Material.WRITABLE_BOOK; + bookPriceQuantity = 1; + config.set("Options.Price_to_create_book.Item_type", bookPriceType.toString()); + config.set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity); + } + + config.set("Options.Require_book_and_quill_to_create_book", null); + } + this.saveConfig(); + } + public boolean loadConfig() { this.reloadConfig(); + Configuration config = this.getConfig(); try { - useYml = this.getConfig().getBoolean("Options.Save_Books_in_Yaml_Format", true); - bookDuplicateLimit = this.getConfig().getInt("Options.Max_Number_of_Duplicates", 5); - titleAuthorSeparator = this.getConfig().getString("Options.Title-Author_Separator", ","); - loreSeparator = this.getConfig().getString("Options.Lore_line_separator", "~"); - adminDecrypt = this.getConfig().getBoolean("Options.Admin_Auto_Decrypt", false); - authorOnlyCopy = this.getConfig().getBoolean("Options.Author_Only_Copy", false); + useYml = config.getBoolean("Options.Save_Books_in_Yaml_Format", true); + bookDuplicateLimit = config.getInt("Options.Max_Number_of_Duplicates", 5); + titleAuthorSeparator = config.getString("Options.Title-Author_Separator", ","); + loreSeparator = config.getString("Options.Lore_line_separator", "~"); + adminDecrypt = config.getBoolean("Options.Admin_Auto_Decrypt", false); + authorOnlyCopy = config.getBoolean("Options.Author_Only_Copy", false); - firstBooks = this.getConfig().getStringList("Options.Books_for_new_players"); - if (this.getConfig().contains("Options.Book_for_new_players")) - firstBooks.add(this.getConfig().getString("Options.Book_for_new_players")); - if (firstBooks.isEmpty()) + //Set books to give new players + firstBooks = config.getStringList("Options.Books_for_new_players"); + if (config.contains("Options.Book_for_new_players")) { + firstBooks.add(config.getString("Options.Book_for_new_players")); + } + if (firstBooks.isEmpty()) { firstBooks.add(" "); + } - welcomeMessage = this.getConfig().getString("Options.Message_for_new_players", " "); + welcomeMessage = config.getString("Options.Message_for_new_players", " "); //Converts string into material - String sMaterial = this.getConfig().getString("Options.Price_to_create_book.Item_type", " "); + String sMaterial = config.getString("Options.Price_to_create_book.Item_type", " "); if (sMaterial.equalsIgnoreCase("Economy")) { - if (setupEconomy()) + if (setupEconomy()) { bookPriceType = Material.AIR; - else { + } else { sendErrorMessage(consoleSender, "BooksWithoutBorders failed to hook into Vault! Book price not set!"); bookPriceType = null; } @@ -259,7 +276,7 @@ public class BooksWithoutBorders extends JavaPlugin { bookPriceType = m; } } - bookPriceQuantity = this.getConfig().getDouble("Options.Price_to_create_book.Required_quantity", 0); + bookPriceQuantity = config.getDouble("Options.Price_to_create_book.Required_quantity", 0); //makes sure titleAuthorSeparator is a valid value titleAuthorSeparator = cleanString(titleAuthorSeparator); @@ -267,7 +284,7 @@ public class BooksWithoutBorders extends JavaPlugin { sendErrorMessage(consoleSender, "Title-Author_Separator is set to an invalid value!"); sendErrorMessage(consoleSender, "Reverting to default value of \",\""); titleAuthorSeparator = ","; - this.getConfig().set("Options.Title-Author_Separator", titleAuthorSeparator); + config.set("Options.Title-Author_Separator", titleAuthorSeparator); } } catch (Exception e) { sendErrorMessage(consoleSender, "Warning! Config.yml failed to load!"); @@ -334,7 +351,7 @@ public class BooksWithoutBorders extends JavaPlugin { return true; } - private void addExistingPlayer(String playerName) { + public void addExistingPlayer(String playerName) { existingPlayers.add(playerName); try { @@ -346,103 +363,6 @@ public class BooksWithoutBorders extends JavaPlugin { } } - public boolean hasPlayedBefore(String playerName) { - if (!existingPlayers.contains(playerName)) { - addExistingPlayer(playerName); - return false; - } - - return true; - } - - /** - * Saves a book to a file - * - * @param player

The player holding the book

- * @param heldBook

The book held

- * @param overwrite

Whether to overwrite any existing books

- * @param saveToPublicFolder

Whether to save the book to the public folder instead of the player folder

- */ - public void saveBook(Player player, ItemStack heldBook, boolean overwrite, boolean saveToPublicFolder) { - BookMeta book = (BookMeta) heldBook.getItemMeta(); - if (book == null) { - sendErrorMessage(player, "Unable to get metadata for your held book!"); - return; - } - - String savePath; - if (saveToPublicFolder) { - savePath = getBookFolder(); - } else { - savePath = getBookFolder() + cleanString(player.getName()) + getSlash(); - } - - //Generate book filename - String fileName; - if (!book.hasTitle()) { - fileName = "Untitled," + player.getName(); - } else { - fileName = book.getTitle() + titleAuthorSeparator + book.getAuthor(); - } - fileName = cleanString(fileName); - fileName = fixName(fileName, false); - - //Make sure the used folders exist - File file = new File(savePath); - if (!file.exists() && !file.mkdir()) { - sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!"); - return; - } - File[] foundFiles = file.listFiles(); - if (foundFiles == null) { - sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!"); - return; - } - - sendErrorMessage(player, String.valueOf(overwrite)); - sendErrorMessage(player, fileName); - sendErrorMessage(player, Arrays.toString(foundFiles)); - - //Find any duplicates of the book - int foundDuplicates = FileHelper.findDuplicates(foundFiles, fileName); - - //Deal with duplicates - if (foundDuplicates > 0) { - //TODO: Decide if this makes sense or needs to be changed - //Skip duplicate book - if (!fileName.contains("Untitled") && !overwrite) { - sendErrorMessage(player, "Book is already saved!"); - sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!"); - return; - } - - //Skip if duplicate limit is reached - if (foundDuplicates > bookDuplicateLimit) { - sendErrorMessage(player, "Maximum amount of " + fileName + " duplicates reached!"); - sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!"); - return; - } - - //Alter duplicated filename - if (fileName.contains("Untitled") && !overwrite) { - fileName = "(" + foundDuplicates + ")" + fileName; - } - } - - try { - if (useYml) { - BookToFromTextHelper.bookToYml(savePath, fileName, book); - } else { - BookToFromTextHelper.bookToTXT(savePath, fileName, book); - } - - sendSuccessMessage(player, "Book Saved as \"" + fileName + "\""); - } catch (IOException e) { - e.printStackTrace(); - } - - } - public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String dir) { return loadBook(sender, fileName, isSigned, dir, 1); } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java index b80a2a5..182334e 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandBooksWithoutBorders.java @@ -19,11 +19,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS */ public class CommandBooksWithoutBorders implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandBooksWithoutBorders(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java index f92856e..8ac3921 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java @@ -17,11 +17,7 @@ import java.util.Objects; */ public class CommandCopy implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandCopy(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index 55c26f1..12f4cac 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -18,11 +18,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS */ public class CommandDelete implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandDelete(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDeletePublic.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDeletePublic.java index dd01aba..3aa8012 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDeletePublic.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDeletePublic.java @@ -1,6 +1,5 @@ package net.knarcraft.bookswithoutborders.command; -import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender; */ public class CommandDeletePublic extends CommandDelete implements CommandExecutor { - public CommandDeletePublic(BooksWithoutBorders booksWithoutBorders) { - super(booksWithoutBorders); - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return deleteBook(sender, args, true); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index 556d71c..27e0b5c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -13,11 +13,7 @@ import org.bukkit.inventory.ItemStack; */ public class CommandGive implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandGive(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGivePublic.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGivePublic.java index cce89e7..50eda97 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGivePublic.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGivePublic.java @@ -1,6 +1,5 @@ package net.knarcraft.bookswithoutborders.command; -import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender; */ public class CommandGivePublic extends CommandGive implements CommandExecutor { - public CommandGivePublic(BooksWithoutBorders booksWithoutBorders) { - super(booksWithoutBorders); - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return giveBook(sender, args, true, "public"); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java index d4880d3..6b63fea 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java @@ -13,11 +13,7 @@ import org.bukkit.inventory.ItemStack; */ public class CommandLoad implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandLoad(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java index 9f8ebc4..41ce1df 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java @@ -1,6 +1,5 @@ package net.knarcraft.bookswithoutborders.command; -import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender; */ public class CommandLoadPublic extends CommandLoad implements CommandExecutor { - public CommandLoadPublic(BooksWithoutBorders booksWithoutBorders) { - super(booksWithoutBorders); - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return loadBook(sender, args, "public", true); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandReload.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandReload.java index a4757b4..1fb2269 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandReload.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandReload.java @@ -10,11 +10,7 @@ import org.bukkit.command.CommandSender; */ public class CommandReload implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandReload(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index 2af27f2..554c84a 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -2,24 +2,33 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.state.ItemSlot; +import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; +import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +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; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getErrorColor; +import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash; +import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; +import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; /** * Command executor for the save command */ public class CommandSave implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandSave(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return saveHeldBook(sender, args, false); @@ -43,11 +52,99 @@ public class CommandSave implements CommandExecutor { if (holdingSlot != ItemSlot.NONE) { ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND); boolean duplicate = args.length == 1 && Boolean.parseBoolean(args[0]); - booksWithoutBorders.saveBook(player, holdingItem, duplicate, savePublic); + saveBook(player, holdingItem, duplicate, savePublic); return true; } else { BooksWithoutBorders.sendErrorMessage(sender, "You must be holding a written book or book and quill to save it!"); return false; } } + + /** + * Saves a book to a file + * + * @param player

The player holding the book

+ * @param heldBook

The book held

+ * @param overwrite

Whether to overwrite any existing books

+ * @param saveToPublicFolder

Whether to save the book to the public folder instead of the player folder

+ */ + public void saveBook(Player player, ItemStack heldBook, boolean overwrite, boolean saveToPublicFolder) { + BookMeta book = (BookMeta) heldBook.getItemMeta(); + if (book == null) { + BooksWithoutBorders.sendErrorMessage(player, "Unable to get metadata for your held book!"); + return; + } + + String savePath; + if (saveToPublicFolder) { + savePath = getBookFolder(); + } else { + savePath = getBookFolder() + cleanString(player.getName()) + getSlash(); + } + + //Generate book filename + String fileName; + if (!book.hasTitle()) { + fileName = "Untitled," + player.getName(); + } else { + fileName = book.getTitle() + titleAuthorSeparator + book.getAuthor(); + } + fileName = cleanString(fileName); + fileName = fixName(fileName, false); + + //Make sure the used folders exist + File file = new File(savePath); + if (!file.exists() && !file.mkdir()) { + BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!"); + return; + } + File[] foundFiles = file.listFiles(); + if (foundFiles == null) { + BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!"); + return; + } + + BooksWithoutBorders.sendErrorMessage(player, String.valueOf(overwrite)); + BooksWithoutBorders.sendErrorMessage(player, fileName); + BooksWithoutBorders.sendErrorMessage(player, Arrays.toString(foundFiles)); + + //Find any duplicates of the book + int foundDuplicates = FileHelper.findDuplicates(foundFiles, fileName); + + //Deal with duplicates + if (foundDuplicates > 0) { + //TODO: Decide if this makes sense or needs to be changed + //Skip duplicate book + if (!fileName.contains("Untitled") && !overwrite) { + BooksWithoutBorders.sendErrorMessage(player, "Book is already saved!"); + BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!"); + return; + } + + //Skip if duplicate limit is reached + if (foundDuplicates > BooksWithoutBorders.bookDuplicateLimit) { + BooksWithoutBorders.sendErrorMessage(player, "Maximum amount of " + fileName + " duplicates reached!"); + BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!"); + return; + } + + //Alter duplicated filename + if (fileName.contains("Untitled") && !overwrite) { + fileName = "(" + foundDuplicates + ")" + fileName; + } + } + + try { + if (BooksWithoutBorders.useYml) { + BookToFromTextHelper.bookToYml(savePath, fileName, book); + } else { + BookToFromTextHelper.bookToTXT(savePath, fileName, book); + } + + BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + "\""); + } catch (IOException e) { + e.printStackTrace(); + } + + } } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSavePublic.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSavePublic.java index 0eb7995..1b24240 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSavePublic.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSavePublic.java @@ -1,6 +1,5 @@ package net.knarcraft.bookswithoutborders.command; -import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender; */ public class CommandSavePublic extends CommandSave implements CommandExecutor { - public CommandSavePublic(BooksWithoutBorders booksWithoutBorders) { - super(booksWithoutBorders); - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return saveHeldBook(sender, args, true); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java index 96bc897..af366ba 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetBookPrice.java @@ -14,11 +14,7 @@ import org.bukkit.inventory.ItemStack; */ public class CommandSetBookPrice implements CommandExecutor { - private final BooksWithoutBorders booksWithoutBorders; - - public CommandSetBookPrice(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java b/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java index 31db6b0..f6324fe 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/GiveTabCompleter.java @@ -16,11 +16,7 @@ import java.util.List; */ public class GiveTabCompleter implements TabCompleter { - final BooksWithoutBorders booksWithoutBorders; - - public GiveTabCompleter(BooksWithoutBorders booksWithoutBorders) { - this.booksWithoutBorders = booksWithoutBorders; - } + private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @Override public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java index e888404..af6131e 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java @@ -55,7 +55,7 @@ public class PlayerEventListener implements Listener { Player player = event.getPlayer(); //Handle new players - if (!BooksWithoutBorders.booksWithoutBorders.hasPlayedBefore(player.getName())) { + if (!hasPlayedBefore(player.getName())) { boolean sendMessage = true; //Gives new players necessary books @@ -173,4 +173,18 @@ public class PlayerEventListener implements Listener { return null; } + /** + * Checks whether a given player has played on the server before + * @param playerName

The player to check

+ * @return

True if the player has played before

+ */ + private boolean hasPlayedBefore(String playerName) { + if (!BooksWithoutBorders.existingPlayers.contains(playerName)) { + BooksWithoutBorders.booksWithoutBorders.addExistingPlayer(playerName); + return false; + } + + return true; + } + }