diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 0000426..fbce201 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -21,6 +21,7 @@ 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.CommandUnSign; @@ -35,9 +36,11 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabCompleter; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -90,62 +93,34 @@ public class BooksWithoutBorders extends JavaPlugin { this.getPluginLoader().disablePlugin(this); } - PluginCommand giveCommand = this.getCommand("give"); - if (giveCommand != null) { - giveCommand.setExecutor(new CommandGive(this)); - giveCommand.setTabCompleter(new GiveTabCompleter(this)); + registerCommand("give", new CommandGive(this), new GiveTabCompleter(this)); + registerCommand("decrypt", new CommandDecrypt(this), null); + registerCommand("groupEncrypt", new CommandGroupEncrypt(this), null); + registerCommand("delete", new CommandDelete(this), null); + registerCommand("copy", new CommandCopy(this), null); + registerCommand("unSign", new CommandUnSign(this), null); + registerCommand("encrypt", new CommandEncrypt(this), null); + registerCommand("setBookPrice", new CommandSetBookPrice(this), null); + registerCommand("setLore", new CommandSetLore(), null); + registerCommand("savePublic", new CommandSavePublic(this), null); + registerCommand("save", new CommandSave(this), null); + registerCommand("setAuthor", new CommandSetAuthor(), null); + } + + /** + * Registers a command + * @param commandName
The name of the command to register
+ * @param executorThe executor to register for the command
+ */ + private void registerCommand(String commandName, CommandExecutor executor, TabCompleter tabCompleter) { + PluginCommand pluginCommand = this.getCommand(commandName); + if (pluginCommand != null) { + pluginCommand.setExecutor(executor); + if (tabCompleter != null) { + pluginCommand.setTabCompleter(tabCompleter); + } } else { - sendErrorMessage(consoleSender, "Unable to register give command"); - } - - PluginCommand decryptCommand = this.getCommand("decrypt"); - if (decryptCommand != null) { - decryptCommand.setExecutor(new CommandDecrypt(this)); - } - - PluginCommand groupEncryptCommand = this.getCommand("groupencrypt"); - if (groupEncryptCommand != null) { - groupEncryptCommand.setExecutor(new CommandGroupEncrypt(this)); - } - - PluginCommand deleteCommand = this.getCommand("delete"); - if (deleteCommand != null) { - deleteCommand.setExecutor(new CommandDelete(this)); - } - - PluginCommand copyCommand = this.getCommand("copy"); - if (copyCommand != null) { - copyCommand.setExecutor(new CommandCopy(this)); - } - - PluginCommand unSignCommand = this.getCommand("unsign"); - if (unSignCommand != null) { - unSignCommand.setExecutor(new CommandUnSign(this)); - } - - PluginCommand encryptCommand = this.getCommand("encrypt"); - if (encryptCommand != null) { - encryptCommand.setExecutor(new CommandEncrypt(this)); - } - - PluginCommand setBookPriceCommand = this.getCommand("setbookprice"); - if (setBookPriceCommand != null) { - setBookPriceCommand.setExecutor(new CommandSetBookPrice(this)); - } - - PluginCommand setLoreCommand = this.getCommand("setlore"); - if (setLoreCommand != null) { - setLoreCommand.setExecutor(new CommandSetLore()); - } - - PluginCommand savePublicCommand = this.getCommand("savepublic"); - if (savePublicCommand != null) { - savePublicCommand.setExecutor(new CommandSavePublic(this)); - } - - PluginCommand saveCommand = this.getCommand("save"); - if (saveCommand != null) { - saveCommand.setExecutor(new CommandSave(this)); + sendErrorMessage(consoleSender, "Failed to register command " + commandName); } } @@ -394,7 +369,7 @@ public class BooksWithoutBorders extends JavaPlugin { sendSuccessMessage(sender, "If true is specified, a book of the same name by the same"); sendSuccessMessage(sender, "author will be overwritten by the new book"); } - if (sender.hasPermission("bookswithoutborders.savepublic")) { + if (sender.hasPermission("bookswithoutborders.savePublic")) { sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,"); sendSuccessMessage(sender, "but saves files in the public directory"); } @@ -402,7 +377,7 @@ public class BooksWithoutBorders extends JavaPlugin { sender.sendMessage("\n" + commandColor + "Give [file name or number] [player name] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Gives the selected player a book from your personal directory"); } - if (sender.hasPermission("bookswithoutborders.givepublic")) { + if (sender.hasPermission("bookswithoutborders.givePublic")) { sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Same as give, but uses books from the public directory"); } @@ -427,7 +402,7 @@ public class BooksWithoutBorders extends JavaPlugin { sender.sendMessage(commandColor + "[key]" + successColor + " is required and can be any phrase or number excluding spaces"); sender.sendMessage(commandColor + "[style]" + successColor + " is not required. Possible values are \"DNA\" or \"Magic\""); } - if (sender.hasPermission("bookswithoutborders.groupencrypt")) { + if (sender.hasPermission("bookswithoutborders.groupEncrypt")) { sender.sendMessage("\n" + commandColor + "groupEncrypt [group name] [key] [style]: " + successColor + "Encrypts book so that only players with the" + "\n bookswithoutborders.decrypt." + commandColor + "[group name]" + successColor + " permission may decrypt the book by holding and left clicking the book"); } @@ -435,17 +410,17 @@ public class BooksWithoutBorders extends JavaPlugin { sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding"); sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book"); } - if (sender.hasPermission("bookswithoutborders.settitle")) { + if (sender.hasPermission("bookswithoutborders.setTitle")) { sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding"); } - if (sender.hasPermission("bookswithoutborders.setauthor")) { + if (sender.hasPermission("bookswithoutborders.setAuthor")) { sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding"); } - if (sender.hasPermission("bookswithoutborders.setlore")) { + if (sender.hasPermission("bookswithoutborders.setLore")) { 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."); @@ -603,23 +578,17 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } - StringBuilder title = new StringBuilder(); - for (int x = 1; x < args.length; x++) { - if (x == 1) - title.append(args[x]); - else - title.append(" ").append(args[x]); - } - title = new StringBuilder(ChatColor.translateAlternateColorCodes('&', title.toString())); + String title = String.join(" ", args); + title = ChatColor.translateAlternateColorCodes('&', title); ItemMeta meta; if (((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK) { BookMeta bMeta = (BookMeta) ((Player) sender).getItemInHand().getItemMeta(); - bMeta.setTitle(title.toString()); + bMeta.setTitle(title); meta = bMeta; } else { ItemMeta iMeta = ((Player) sender).getItemInHand().getItemMeta(); - iMeta.setDisplayName(title.toString()); + iMeta.setDisplayName(title); meta = iMeta; } @@ -627,36 +596,6 @@ public class BooksWithoutBorders extends JavaPlugin { sendSuccessMessage(sender, "Title set to " + title + "!"); return true; } - - if (args[0].equalsIgnoreCase("setAuthor")) { - if (!sender.hasPermission("bookswithoutborders.setauthor")) { - sendErrorMessage(sender, " You don't have permission to use this command!"); - return false; - } - - if (args.length < 2) { - sendErrorMessage(sender, "Too few command arguments!"); - return false; - } - - if (!(((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK)) { - sendErrorMessage(sender, "You must be holding a written book to set author!"); - return false; - } - - BookMeta meta = (BookMeta) ((Player) sender).getItemInHand().getItemMeta(); - StringBuilder author = new StringBuilder(); - for (int x = 1; x < args.length; x++) { - if (x == 1) - author.append(args[x]); - else - author.append(" ").append(args[x]); - } - meta.setAuthor(author.toString()); - ((Player) sender).getItemInHand().setItemMeta(meta); - sendSuccessMessage(sender, "Book author set to " + author + "!"); - return true; - } } if (args[0].equalsIgnoreCase("givePublic")) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java new file mode 100644 index 0000000..a8ec855 --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetAuthor.java @@ -0,0 +1,48 @@ +package net.knarcraft.bookswithoutborders.command; + +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.state.ItemSlot; +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; + +/** + * Command executor for the set author command + */ +public class CommandSetAuthor implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player player)) { + BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!"); + return false; + } + + if (args.length < 1) { + BooksWithoutBorders.sendErrorMessage(player, "Too few command arguments!"); + return false; + } + + if (InventoryHelper.notHoldingOneWrittenBookCheck(player, + "You must be holding a written book to set author!", "You cannot set the " + + "author of two books at once!")) { + return false; + } + + ItemSlot heldBookSlot = InventoryHelper.getHeldSlotBook(player, false, false, true, true); + boolean mainHand = heldBookSlot == ItemSlot.MAIN_HAND; + ItemStack heldBook = InventoryHelper.getHeldItem(player, mainHand); + BookMeta bookMetaData = InventoryHelper.getHeldBookMetadata(player, mainHand); + + String author = String.join(" ", args); + bookMetaData.setAuthor(author); + heldBook.setItemMeta(bookMetaData); + BooksWithoutBorders.sendSuccessMessage(player, "Book author set to " + author + "!"); + return true; + } + +} diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java index dfbf74d..12736d0 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetLore.java @@ -39,15 +39,11 @@ public class CommandSetLore implements CommandExecutor { } //Treat all arguments as lore input - StringBuilder rawLore = new StringBuilder(); - rawLore.append(args[0]); - for (int x = 1; x < args.length; x++) { - rawLore.append(" ").append(args[x]); - } + String rawLore = String.join(" ", args); //Format lore - rawLore = new StringBuilder(ChatColor.translateAlternateColorCodes('&', rawLore.toString())); - String[] loreParts = rawLore.toString().split(BooksWithoutBorders.loreSeparator); + rawLore = ChatColor.translateAlternateColorCodes('&', rawLore); + String[] loreParts = rawLore.split(BooksWithoutBorders.loreSeparator); List