From e0a7a9dcc2e8f66835dddfc36c463e128c0dd324 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 26 Aug 2025 11:44:01 +0200 Subject: [PATCH] Adds an argument for setting a book's display name instead of title #13 --- README.md | 2 +- .../command/CommandSetTitle.java | 26 ++++++++++++++----- .../config/translation/Translatable.java | 17 +++++++----- src/main/resources/plugin.yml | 3 ++- src/main/resources/strings.yml | 1 + 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 26a1069..3f3ec96 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ An in-game description of available commands is available through the /bwb comma | /setbookprice | bwbprice | \ \ | bookswithoutborders.setbookprice | Sets the per-book price to create a book via commands. If "Item", the item in the player's hand in the amount of \ will be the price. If "Eco", a Vault based economy will be used for price. If neither \ nor \ are specified, the current price to create books will be removed. | | /setBookshelfData | bwbshelfdata | \ \text> \[more text] | bookswithoutborders.editbookshelf | Sets the name/lore for a bookshelf which is shown when peeking at its contents. | | /setlore | bwblore | \ | bookswithoutborders.setlore | Sets the lore of the item the player is holding. Insert the lore_line_separator character to force a new line ("~" by default) | -| /settitle | bwbtitle | \ | bookswithoutborders.settitle | Sets the title of the book/item the player is holding | +| /settitle | bwbtitle | \<title> \[title] ... \[setDisplayName (true/false)] | bookswithoutborders.settitle | Sets the title of the book or the display name of the item the player is holding. Add a true at the end (`/settitle some title true`) to set a book's display name instead of its title. | | /unsignbook | bwbunsign | None | bookswithoutborders.unsign | Un-signs the book the player is holding | ### Permissions: diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java index e978d99..9701cef 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSetTitle.java @@ -1,10 +1,9 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.config.translation.Translatable; +import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import net.knarcraft.knarlib.formatting.FormatBuilder; -import net.knarcraft.knarlib.property.ColorConversion; -import net.knarcraft.knarlib.util.ColorHelper; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -42,8 +41,15 @@ public class CommandSetTitle implements TabExecutor { return false; } - String title = String.join(" ", arguments).strip(); - title = ColorHelper.translateColorCodes(title, ColorConversion.RGB); + boolean setDisplayName = false; + String title; + if (InputCleaningHelper.isBoolean(arguments[arguments.length - 1])) { + setDisplayName = Boolean.parseBoolean(arguments[arguments.length - 1]); + title = InputCleaningHelper.mergeArguments(arguments, 1); + } else { + title = String.join(" ", arguments); + } + title = new FormatBuilder(title).color().build(); ItemMeta itemMetadata = heldItem.getItemMeta(); if (itemMetadata == null) { @@ -53,7 +59,7 @@ public class CommandSetTitle implements TabExecutor { //Get and change metadata ItemMeta newMetaData; - if (heldItem.getType() == Material.WRITTEN_BOOK) { + if (heldItem.getType() == Material.WRITTEN_BOOK && !setDisplayName) { if (title.length() > 32) { new FormatBuilder(Translatable.ERROR_TITLE_LENGTH).error(player); return false; @@ -68,7 +74,12 @@ public class CommandSetTitle implements TabExecutor { //Set the new metadata heldItem.setItemMeta(newMetaData); - new FormatBuilder(Translatable.SUCCESS_TITLE_SET).replace("{title}", title).success(player); + + if (heldItem.getType() == Material.WRITTEN_BOOK && !setDisplayName) { + new FormatBuilder(Translatable.SUCCESS_TITLE_SET).replace("{title}", title).success(player); + } else { + new FormatBuilder(Translatable.SUCCESS_DISPLAY_NAME_SET).replace("{displayName}", title).success(player); + } return true; } @@ -77,6 +88,9 @@ public class CommandSetTitle implements TabExecutor { @NotNull String[] arguments) { List<String> options = new ArrayList<>(); options.add("<new title>"); + if (arguments.length > 1) { + options.addAll(List.of("true", "false")); + } return options; } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/config/translation/Translatable.java b/src/main/java/net/knarcraft/bookswithoutborders/config/translation/Translatable.java index 40a24a0..0b5365d 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/config/translation/Translatable.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/config/translation/Translatable.java @@ -79,20 +79,20 @@ public enum Translatable implements TranslatableMessage { SUCCESS_LORE_SET, /** - * The success message displayed when an item's title is successfully set + * The success message displayed when a book's title is successfully set */ SUCCESS_TITLE_SET, + /** + * The success message displayed when an item's display name is successfully set + */ + SUCCESS_DISPLAY_NAME_SET, + /** * The success message displayed when books have been successfully migrated */ SUCCESS_MIGRATED, - /** - * The error to display when the console attempts to run a player-only command - */ - ERROR_PLAYER_ONLY, - /** * The success message displayed when a book is successfully encrypted */ @@ -108,6 +108,11 @@ public enum Translatable implements TranslatableMessage { */ NEUTRAL_ATTEMPTING_LEGACY_DECRYPTION, + /** + * The error to display when the console attempts to run a player-only command + */ + ERROR_PLAYER_ONLY, + /** * The error displayed when running a relevant command without holding a written book */ diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ac2b36e..bc4a354 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -164,10 +164,11 @@ commands: Note that this behaves differently for signed books compared to other items. For signed books, the book's actual name in the book's metadata is changed, and that name is limited to 32 characters. For other items, the display name is set, which has no relevant limit. + Add the argument true after typing in the title to set the display name of a book instead of the title. Color and formatting codes (#000000, &a, &4, &n, &#ffffff) are supported. aliases: - bwbtitle - usage: /<command> <title> + usage: /<command> <title> [title] ... [setDisplayName (true/false)] permission: bookswithoutborders.settitle loadbook: description: | diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index 73ed587..88ba082 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -27,6 +27,7 @@ en: SUCCESS_GENERATION_CHANGED: "Book generation successfully changed!" SUCCESS_LORE_SET: "Added lore to item!" SUCCESS_TITLE_SET: "Title set to {title}!" + SUCCESS_DISPLAY_NAME_SET: "Display name set to {displayName}!" SUCCESS_MIGRATED: "Successfully migrated all books" SUCCESS_ENCRYPTED: "Book encrypted!" SUCCESS_UNSIGNED: "Book un-signed"