diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index ed6587c..fcb2ca0 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -13,20 +13,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import net.knarcraft.bookswithoutborders.command.CommandCopy; -import net.knarcraft.bookswithoutborders.command.CommandDecrypt; -import net.knarcraft.bookswithoutborders.command.CommandDelete; -import net.knarcraft.bookswithoutborders.command.CommandEncrypt; -import net.knarcraft.bookswithoutborders.command.CommandGive; -import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt; -import net.knarcraft.bookswithoutborders.command.CommandSave; -import net.knarcraft.bookswithoutborders.command.CommandSavePublic; -import net.knarcraft.bookswithoutborders.command.CommandSetAuthor; -import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice; -import net.knarcraft.bookswithoutborders.command.CommandSetLore; -import net.knarcraft.bookswithoutborders.command.CommandSetTitle; -import net.knarcraft.bookswithoutborders.command.CommandUnSign; -import net.knarcraft.bookswithoutborders.command.GiveTabCompleter; +import net.knarcraft.bookswithoutborders.command.*; import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.utility.BookFormatter; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; @@ -106,6 +93,8 @@ public class BooksWithoutBorders extends JavaPlugin { registerCommand("save", new CommandSave(this), null); registerCommand("setAuthor", new CommandSetAuthor(), null); registerCommand("setTitle", new CommandSetTitle(), null); + registerCommand("load", new CommandLoad(this), null); + registerCommand("loadPublic", new CommandLoadPublic(this), null); } /** @@ -393,10 +382,12 @@ public class BooksWithoutBorders extends JavaPlugin { sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's configuration file"); } - if (sender.hasPermission("bookswithoutborders.unsign")) + if (sender.hasPermission("bookswithoutborders.unsign")) { sender.sendMessage("\n" + commandColor + "Unsign: " + successColor + "Un-signs the book the player is holding"); - if (sender.hasPermission("bookswithoutborders.copy")) + } + if (sender.hasPermission("bookswithoutborders.copy")) { sender.sendMessage("\n" + commandColor + "Copy [number of copies]: " + successColor + "Copies the book the player is holding"); + } if (sender.hasPermission("bookswithoutborders.encrypt")) { sender.sendMessage("\n" + commandColor + "Encrypt [key] [style]: " + successColor + "Encrypts the book the player is holding"); @@ -421,10 +412,11 @@ public class BooksWithoutBorders extends JavaPlugin { sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding"); sendSuccessMessage(sender, "Insert the lore_line_separator character to force a new line\n[\"~\" by default]"); } - if (sender.hasPermission("bookswithoutborders.setBookPrice")) + if (sender.hasPermission("bookswithoutborders.setBookPrice")) { sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." + "\nIf [Item], the item in the player's hand in the amount of [quantity] will be the price.\nIf [Eco], a Vault based economy will be used for price." + "\nIf neither [Item/Eco] or [quantity] are specified the current price to create books will be removed."); + } return true; } @@ -461,109 +453,6 @@ public class BooksWithoutBorders extends JavaPlugin { return true; } - //Player only commands - if (sender instanceof Player player) { - - if (args[0].equalsIgnoreCase("loadPublic")) { - if (!sender.hasPermission("bookswithoutborders.loadPublic")) { - sendErrorMessage(sender, " You don't have permission to use this command!"); - return false; - } - - if (((Player) sender).getInventory().firstEmpty() == -1) { - sendErrorMessage(sender, "You must have space in your inventory to load books!"); - return false; - } - - if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, true, false)); - return true; - } - - for (int x = 0; x < args[1].length(); x++) { - if (!Character.isDigit(args[1].charAt(x))) - break; - if (x == args[1].length() - 1) - loadList.put(sender.getName(), listFiles(sender, true, true)); - } - - ItemStack newBook; - try { - if (args.length == 4) - newBook = loadBook(sender, cleanString(args[1]), args[3], "public", Integer.parseInt(args[2])); - else if (args.length == 3) { - if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("false")) - newBook = loadBook(sender, cleanString(args[1]), args[2], "public"); - else - newBook = loadBook(sender, cleanString(args[1]), "true", "public", Integer.parseInt(args[2])); - } else - newBook = loadBook(sender, cleanString(args[1]), "true", "public"); - - if (newBook != null) { - ((Player) sender).getInventory().addItem(newBook); - sendSuccessMessage(sender, "Book created!"); - return true; - } else { - sendErrorMessage(sender, "Book failed to load!"); - return false; - } - } catch (NumberFormatException e) { - sendErrorMessage(sender, "Invalid number of book copies specified!"); - return false; - } - } - - if (args[0].equalsIgnoreCase("load")) { - if (!sender.hasPermission("bookswithoutborders.load")) { - sendErrorMessage(sender, " You don't have permission to use this command!"); - return false; - } - - if (((Player) sender).getInventory().firstEmpty() == -1) { - sendErrorMessage(sender, "You must have space in your inventory to load books!"); - return false; - } - - if (args.length == 1) { - loadList.put(sender.getName(), listFiles(sender, false, false)); - return true; - } - - for (int x = 0; x < args[1].length(); x++) { - if (!Character.isDigit(args[1].charAt(x))) - break; - if (x == args[1].length() - 1) - loadList.put(sender.getName(), listFiles(sender, false, true)); - } - - ItemStack newBook; - try { - - if (args.length == 4) - newBook = loadBook(sender, cleanString(args[1]), args[3], "player", Integer.parseInt(args[2])); - else if (args.length == 3) { - if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("false")) - newBook = loadBook(sender, cleanString(args[1]), args[2], "player"); - else - newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[2])); - } else - newBook = loadBook(sender, cleanString(args[1]), "true", "player"); - - if (newBook != null) { - ((Player) sender).getInventory().addItem(newBook); - sendSuccessMessage(sender, "Book created!"); - return true; - } else { - sendErrorMessage(sender, "Book failed to load!"); - return false; - } - } catch (NumberFormatException e) { - sendErrorMessage(sender, "Invalid number of book copies specified!"); - return false; - } - } - } - if (args[0].equalsIgnoreCase("givePublic")) { if (sender instanceof Player) { if (!sender.hasPermission("bookswithoutborders.givePublic")) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index 6c93290..5387a8d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -35,30 +35,28 @@ public class CommandGive implements CommandExecutor { return true; } + //Organize and parse input String bookIdentifier = args[0]; String receivingPlayerName = args[1]; - - String copies = null; - String isSigned = null; - String copiesOrIsSigned = null; + String copies = "1"; + String isSigned = "true"; if (args.length == 4) { copies = args[2]; isSigned = args[3]; } else if (args.length == 3) { - copiesOrIsSigned = args[2]; - } - - - for (int x = 0; x < bookIdentifier.length(); x++) { - if (!Character.isDigit(bookIdentifier.charAt(x))) { - break; - } - if (x == bookIdentifier.length() - 1) { - BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, true)); + if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("false")) { + isSigned = args[2]; + } else { + copies = args[2]; } } - ItemStack newBook; + //Load books available to the player + try { + Integer.parseInt(bookIdentifier); + BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, true)); + } catch (NumberFormatException ignored) {} + Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName); if (receivingPlayer == null) { BooksWithoutBorders.sendErrorMessage(player, "Player not found!"); @@ -70,21 +68,9 @@ public class CommandGive implements CommandExecutor { return false; } - //bwb give [book name] [player] [numCopies] [is signed] + String bookToLoad = booksWithoutBorders.cleanString(bookIdentifier); try { - - if (isSigned != null && copies != null) { - newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), isSigned, "player", Integer.parseInt(copies)); - } else if (copiesOrIsSigned != null) { - if (copiesOrIsSigned.equalsIgnoreCase("true") || copiesOrIsSigned.equalsIgnoreCase("false")) { - newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), copiesOrIsSigned, "player"); - } else { - newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), "true", "player", Integer.parseInt(copiesOrIsSigned)); - } - } else { - newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), "true", "player"); - } - + ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, "player", Integer.parseInt(copies)); if (newBook != null) { receivingPlayer.getInventory().addItem(newBook); BooksWithoutBorders.sendSuccessMessage(player, "Book sent!"); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java index 6d01062..850ff67 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoad.java @@ -1,12 +1,89 @@ 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; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; public class CommandLoad implements CommandExecutor { + + private final BooksWithoutBorders booksWithoutBorders; + + public CommandLoad(BooksWithoutBorders booksWithoutBorders) { + this.booksWithoutBorders = booksWithoutBorders; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return false; + return loadBook(sender, args, "player", false); } + + /** + * Loads a stored book + * @param sender
The sender asking to load the book
+ * @param argsThe arguments given
+ * @param directoryThe directory to load from (public/player)
+ * @param loadPublicWhether to list public files as loadable
+ * @returnTrue if the book was loaded successfully
+ */ + boolean loadBook(CommandSender sender, String[] args, String directory, boolean loadPublic) { + if (!(sender instanceof Player player)) { + BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!"); + return false; + } + + if (player.getInventory().firstEmpty() == -1) { + BooksWithoutBorders.sendErrorMessage(player, "You must have space in your inventory to load books!"); + return false; + } + + int argumentCount = args.length; + + //Show books available to the player + if (argumentCount == 0) { + BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(player, loadPublic, false)); + return true; + } + + //Organize and parse input + String bookIdentifier = args[0]; + String copies = "1"; + String isSigned = "true"; + if (args.length == 3) { + copies = args[1]; + isSigned = args[2]; + } else if (args.length == 2) { + if (args[1].equalsIgnoreCase("true") || args[1].equalsIgnoreCase("false")) { + isSigned = args[1]; + } else { + copies = args[1]; + } + } + + //Load books available to the player + try { + Integer.parseInt(bookIdentifier); + BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, loadPublic, true)); + } catch (NumberFormatException ignored) {} + + String bookToLoad = booksWithoutBorders.cleanString(bookIdentifier); + try { + //Give the new book if it cannot be loaded + ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies)); + if (newBook != null) { + player.getInventory().addItem(newBook); + BooksWithoutBorders.sendSuccessMessage(player, "Book created!"); + return true; + } else { + BooksWithoutBorders.sendErrorMessage(player, "Book failed to load!"); + return false; + } + } catch (NumberFormatException e) { + BooksWithoutBorders.sendErrorMessage(player, "Invalid number of book copies specified!"); + return false; + } + } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java index 6f754de..62787e9 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandLoadPublic.java @@ -1,12 +1,19 @@ 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; -public class CommandLoadPublic implements CommandExecutor { +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 false; + return loadBook(sender, args, "public", true); } + } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7c764ec..2f1af1f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -65,6 +65,14 @@ commands: description: Sets the title of the held book usage: /