From bc93ddcec56eb7acc42be5c2db2b042d5095281a Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 31 Aug 2021 17:09:08 +0200 Subject: [PATCH] Moves the setTitle command to its own file --- README.md | 2 +- .../BooksWithoutBorders.java | 48 ++------------ .../command/CommandCopy.java | 6 +- .../command/CommandGive.java | 2 +- .../command/CommandSetTitle.java | 64 +++++++++++++++++++ src/main/resources/plugin.yml | 4 ++ 6 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java diff --git a/README.md b/README.md index 3ac4658..5653b99 100644 --- a/README.md +++ b/README.md @@ -24,4 +24,4 @@ Ever wanted to export your book to a text file? Ever want to import it back in? * Give first time players a single book or a set of books when they join * Configurable option to require certain items or pay via Vault compatible economy to create books via command * Add lore to any item with a simple command -* Supports adding and saving color to titles, lore, and book contents \ No newline at end of file +* Supports adding and saving color to title, lore, and book contents \ No newline at end of file diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index fbce201..ed6587c 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -24,6 +24,7 @@ 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.state.EncryptionStyle; @@ -48,7 +49,6 @@ import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.BookMeta; -import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; @@ -105,6 +105,7 @@ public class BooksWithoutBorders extends JavaPlugin { registerCommand("savePublic", new CommandSavePublic(this), null); registerCommand("save", new CommandSave(this), null); registerCommand("setAuthor", new CommandSetAuthor(), null); + registerCommand("setTitle", new CommandSetTitle(), null); } /** @@ -359,7 +360,7 @@ public class BooksWithoutBorders extends JavaPlugin { sendSuccessMessage(sender, "If true is specified the book will be signed, if false it will be"); sendSuccessMessage(sender, "unsigned"); } - if (sender.hasPermission("bookswithoutborders.loadpublic")) { + if (sender.hasPermission("bookswithoutborders.loadPublic")) { sender.sendMessage(commandColor + "loadPublic [file name or number] [# of copies] [true/false]:"); sendSuccessMessage(sender, "Same as Load, but views files in the public directory"); } @@ -464,7 +465,7 @@ public class BooksWithoutBorders extends JavaPlugin { if (sender instanceof Player player) { if (args[0].equalsIgnoreCase("loadPublic")) { - if (!sender.hasPermission("bookswithoutborders.loadpublic")) { + if (!sender.hasPermission("bookswithoutborders.loadPublic")) { sendErrorMessage(sender, " You don't have permission to use this command!"); return false; } @@ -561,46 +562,11 @@ public class BooksWithoutBorders extends JavaPlugin { return false; } } - - if (args[0].equalsIgnoreCase("setTitle")) { - if (!sender.hasPermission("bookswithoutborders.settitle")) { - 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.AIR) { - sendErrorMessage(sender, "You must be holding an item to set title!"); - return false; - } - - 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); - meta = bMeta; - } else { - ItemMeta iMeta = ((Player) sender).getItemInHand().getItemMeta(); - iMeta.setDisplayName(title); - meta = iMeta; - } - - ((Player) sender).getItemInHand().setItemMeta(meta); - sendSuccessMessage(sender, "Title set to " + title + "!"); - return true; - } } if (args[0].equalsIgnoreCase("givePublic")) { if (sender instanceof Player) { - if (!sender.hasPermission("bookswithoutborders.givepublic")) { + if (!sender.hasPermission("bookswithoutborders.givePublic")) { sendErrorMessage(sender, " You don't have permission to use this command!"); return false; } @@ -661,7 +627,7 @@ public class BooksWithoutBorders extends JavaPlugin { } } - if (args[0].equalsIgnoreCase("deletepublic")) { + if (args[0].equalsIgnoreCase("deletePublic")) { if (sender instanceof Player) { if (!sender.hasPermission("bookswithoutborders.admin")) { @@ -981,7 +947,7 @@ public class BooksWithoutBorders extends JavaPlugin { book.setItemMeta(bookMetadata); book.setAmount(numCopies); - if (booksHavePrice() && !sender.hasPermission("bookswithoutborders.bypassbookprice") && + if (booksHavePrice() && !sender.hasPermission("bookswithoutborders.bypassBookPrice") && (dir.equalsIgnoreCase("public") || dir.equalsIgnoreCase("player") || dir.equalsIgnoreCase("")) && cannotPayForBookPrinting((Player) sender, numCopies)) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java index 9aee546..eab3334 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandCopy.java @@ -35,7 +35,7 @@ public class CommandCopy implements CommandExecutor { } if (args.length < 1) { - BooksWithoutBorders.sendErrorMessage(player, "You must specifiy the number of copies to be made!"); + BooksWithoutBorders.sendErrorMessage(player, "You must specify the number of copies to be made!"); return false; } @@ -44,13 +44,13 @@ public class CommandCopy implements CommandExecutor { try { int copies = Integer.parseInt(args[0]); if (copies > 0) { - if (BooksWithoutBorders.authorOnlyCopy && !player.hasPermission("bookswithoutborders.bypassauthoronlycopy")) { + if (BooksWithoutBorders.authorOnlyCopy && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyCopy")) { if (!booksWithoutBorders.isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta()))) return false; } if (booksWithoutBorders.booksHavePrice() && - !player.hasPermission("bookswithoutborders.bypassbookprice") && + !player.hasPermission("bookswithoutborders.bypassBookPrice") && booksWithoutBorders.cannotPayForBookPrinting(player, copies)) { return false; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java index df1d44c..6c93290 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGive.java @@ -70,7 +70,7 @@ public class CommandGive implements CommandExecutor { return false; } - //bwb give [bookname] [player] [numCopies] [issigned] + //bwb give [book name] [player] [numCopies] [is signed] try { if (isSigned != null && copies != null) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java new file mode 100644 index 0000000..7b0ab6f --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java @@ -0,0 +1,64 @@ +package net.knarcraft.bookswithoutborders.command; + +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.InventoryHelper; +import org.bukkit.ChatColor; +import org.bukkit.Material; +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 org.bukkit.inventory.meta.ItemMeta; + +/** + * Command executor for the set title command + */ +public class CommandSetTitle 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; + } + + ItemStack heldItem = InventoryHelper.getHeldItem(player, true); + if (heldItem.getType() == Material.AIR) { + BooksWithoutBorders.sendErrorMessage(sender, "You must be holding an item to set title!"); + return false; + } + + String title = String.join(" ", args); + title = ChatColor.translateAlternateColorCodes('&', title); + + ItemMeta itemMetadata = heldItem.getItemMeta(); + if (itemMetadata == null) { + BooksWithoutBorders.sendErrorMessage(sender, "Unable to get metadata for your held item!"); + return false; + } + + //Get and change metadata + ItemMeta newMetaData; + if (heldItem.getType() == Material.WRITTEN_BOOK) { + BookMeta bookMetadata = (BookMeta) itemMetadata; + bookMetadata.setTitle(title); + newMetaData = bookMetadata; + } else { + itemMetadata.setDisplayName(title); + newMetaData = itemMetadata; + } + + //Set the new metadata + heldItem.setItemMeta(newMetaData); + BooksWithoutBorders.sendSuccessMessage(sender, "Title set to " + title + "!"); + return true; + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a6e0919..7c764ec 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -61,6 +61,10 @@ commands: description: Sets the author of the held book usage: / permission: bookswithoutborders.setauthor + settitle: + description: Sets the title of the held book + usage: / + permission: bookswithoutborders.settitle permissions: bookswithoutborders.admin: description: Grants all permissions