Adds an argument for setting a book's display name instead of title #13
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-08-26 11:44:01 +02:00
parent 36f031c6f9
commit e0a7a9dcc2
5 changed files with 35 additions and 14 deletions

View File

@@ -88,7 +88,7 @@ An in-game description of available commands is available through the /bwb comma
| /setbookprice | bwbprice | \<item/eco> \<quantity> | 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 \<quantity> will be the price. If "Eco", a Vault based economy will be used for price. If neither \<Item/Eco> nor \<quantity> are specified, the current price to create books will be removed. |
| /setBookshelfData | bwbshelfdata | \<delete/name/lore> \text> \[more text] | bookswithoutborders.editbookshelf | Sets the name/lore for a bookshelf which is shown when peeking at its contents. |
| /setlore | bwblore | \<new lore> | 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 | \<title> | 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:

View File

@@ -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;
}

View File

@@ -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
*/

View File

@@ -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: |

View File

@@ -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"