Makes some changes to formatting and un-signing
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

Makes the unsign command convert formatting codes to human editable ones
Makes the format command work on unsigned books
This commit is contained in:
2025-08-14 19:08:21 +02:00
parent 888287b447
commit f05a15586a
3 changed files with 35 additions and 7 deletions

View File

@@ -49,6 +49,14 @@ Books without Borders has got your back!
- Note that if real encryption is enabled, migrating the books will store them unencrypted (like they used to before - Note that if real encryption is enabled, migrating the books will store them unencrypted (like they used to before
real encryption was implemented) afterward. real encryption was implemented) afterward.
### Book formatting
- Formatting codes are automatically turned back into `&` codes after un-signing a book.
- `/formatbook` can be used on an unsigned book to preview formatting, but note that RGB colors will show up as
incorrect colors. That's just how that works. You must sign the book to see the real result.
- `/formatbook` can be used on a signed book (if `Format_Book_After_Signing` is disabled) in order to make any color or
formatting codes in the book display as intended.
### Commands: ### Commands:
An in-game description of available commands is available through the /bwb command. An in-game description of available commands is available through the /bwb command.

View File

@@ -30,15 +30,12 @@ public class CommandFormat implements TabExecutor {
return false; return false;
} }
if (InventoryHelper.notHoldingOneWrittenBookCheck(player, ItemStack heldBook = InventoryHelper.getHeldBook(player);
stringFormatter.replacePlaceholder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK, "{action}", if (heldBook == null) {
stringFormatter.getUnFormattedColoredMessage(Translatable.ACTION_FORMAT)), stringFormatter.displayErrorMessage(sender, Translatable.ERROR_NOT_HOLDING_ANY_BOOK);
stringFormatter.replacePlaceholder(Translatable.ERROR_ONLY_ONE_BOOK, "{action}",
stringFormatter.getUnFormattedColoredMessage(Translatable.ACTION_FORMAT)))) {
return false; return false;
} }
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
BookMeta meta = (BookMeta) heldBook.getItemMeta(); BookMeta meta = (BookMeta) heldBook.getItemMeta();
if (meta == null) { if (meta == null) {

View File

@@ -68,6 +68,8 @@ public class CommandUnSign implements TabExecutor {
if (book == null) { if (book == null) {
return; return;
} }
reverseColorCodes(book);
InventoryHelper.replaceHeldItem(player, book, mainHand); InventoryHelper.replaceHeldItem(player, book, mainHand);
} }
@@ -78,4 +80,25 @@ public class CommandUnSign implements TabExecutor {
return new ArrayList<>(); return new ArrayList<>();
} }
/**
* Reverses colors of a previously formatted book
*
* @param book <p>The book to reverse colors of</p>
*/
private void reverseColorCodes(@NotNull ItemStack book) {
try {
BookMeta meta = (BookMeta) book.getItemMeta();
if (meta != null) {
List<String> newPages = new ArrayList<>(meta.getPages().size());
for (String page : meta.getPages()) {
newPages.add(page.replaceAll("§", "&"));
}
meta.setPages(newPages);
book.setItemMeta(meta);
}
} catch (NullPointerException ignored) {
}
}
} }