Cleans the addTitlePage command a bit

This commit is contained in:
2025-09-03 03:24:04 +02:00
parent 94e7ea5567
commit 5eab950048
9 changed files with 50 additions and 40 deletions

View File

@@ -33,21 +33,20 @@ public class CommandAddTitlePage implements TabExecutor {
return false;
}
// Note: There are two different book checks as an book is accepted normally, but a written book is required for
// adding an author title page at the beginning.
ItemStack heldBook = InventoryUtil.getHeldBook(player);
if (heldBook == null) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_ANY_BOOK).error(sender);
return false;
return true;
}
int index;
if (arguments.length < 1) {
String noBookError = new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}",
Translatable.ACTION_ADD_TITLE_AUTHOR_PAGE).build();
String twoBooksError = new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}",
Translatable.ACTION_ADD_TITLE_AUTHOR_PAGE).build();
if (InventoryUtil.notHoldingOneWrittenBookCheck(player, noBookError, twoBooksError)) {
return false;
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_ADD_TITLE_AUTHOR_PAGE),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_ADD_TITLE_AUTHOR_PAGE))) {
return true;
}
index = 0;
@@ -61,7 +60,6 @@ public class CommandAddTitlePage implements TabExecutor {
}
String title = null;
if (arguments.length > 1) {
// Get all arguments as a space-separated string
StringBuilder builder = new StringBuilder(arguments[1]);
@@ -74,15 +72,29 @@ public class CommandAddTitlePage implements TabExecutor {
BookMeta bookMeta = (BookMeta) heldBook.getItemMeta();
if (bookMeta == null) {
new FormatBuilder(Translatable.ERROR_METADATA_MISSING).error(sender);
return false;
return true;
}
List<String> pages = new ArrayList<>(bookMeta.getPages());
if (index < 0) {
new FormatBuilder(Translatable.ERROR_INVALID_BOOK_PAGE).error(sender);
return false;
}
addBookPage(heldBook, bookMeta, index, title);
new FormatBuilder(Translatable.SUCCESS_TITLE_PAGE_ADDED).success(sender);
return true;
}
/**
* Adds the specified book page
*
* @param heldBook <p>The book to update</p>
* @param bookMeta <p>The metadata of the book to alter</p>
* @param index <p>The index to add the page at</p>
* @param title <p>The title of the new page</p>
*/
private void addBookPage(@NotNull ItemStack heldBook, @NotNull BookMeta bookMeta, int index,
@Nullable String title) {
List<String> pages = new ArrayList<>(bookMeta.getPages());
if (title == null && heldBook.getType() == Material.WRITTEN_BOOK) {
// Add a page with the book title and book author
String loreSeparator = BooksWithoutBorders.getConfiguration().getLoreSeparator();
@@ -113,8 +125,6 @@ public class CommandAddTitlePage implements TabExecutor {
}
bookMeta.setPages(pages);
heldBook.setItemMeta(bookMeta);
new FormatBuilder(Translatable.SUCCESS_TITLE_PAGE_ADDED).success(sender);
return true;
}
/**
@@ -123,16 +133,16 @@ public class CommandAddTitlePage implements TabExecutor {
* @param input <p>The input to format</p>
* @return <p>The formatted input</p>
*/
@NotNull
private String formatTitle(@NotNull String input) {
String loreSeparator = BooksWithoutBorders.getConfiguration().getLoreSeparator();
if (input.contains(loreSeparator)) {
String[] parts = input.split(loreSeparator);
StringBuilder output = new StringBuilder();
output.append(new FormatBuilder(Formatting.NEUTRAL_TITLE_PAGE_HEADER_FORMAT).replace("{header}", parts[0]).color().build());
FormatBuilder builder = new FormatBuilder(Formatting.NEUTRAL_TITLE_PAGE_HEADER_FORMAT).replace("{header}", parts[0]);
for (int i = 1; i < parts.length; i++) {
output.append(new FormatBuilder(Formatting.NEUTRAL_TITLE_PAGE_TEXT_FORMAT).replace("{text}", parts[i]).color().build());
builder.append(Formatting.NEUTRAL_TITLE_PAGE_TEXT_FORMAT).replace("{text}", parts[i]);
}
return output.toString();
return builder.color().build();
} else {
return new FormatBuilder(Formatting.NEUTRAL_TITLE_PAGE_HEADER_FORMAT).replace("{header}", input).color().build();
}

View File

@@ -31,8 +31,8 @@ public class CommandClear implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWritableBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITABLE_BOOK).replace("{action}", Translatable.ACTION_CLEAR).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_CLEAR).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITABLE_BOOK).replace("{action}", Translatable.ACTION_CLEAR),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_CLEAR))) {
return false;
}

View File

@@ -36,8 +36,8 @@ public class CommandCopy implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_COPY).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_COPY).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_COPY),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_COPY))) {
return false;
}

View File

@@ -34,8 +34,8 @@ public class CommandDecrypt implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_DECRYPT).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_DECRYPT).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_DECRYPT),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_DECRYPT))) {
return false;
}

View File

@@ -69,8 +69,8 @@ public class CommandEncrypt implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_ENCRYPT).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_ENCRYPT).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_ENCRYPT),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_ENCRYPT))) {
return null;
}

View File

@@ -38,8 +38,8 @@ public class CommandSetAuthor implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_SET_AUTHOR).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_SET_AUTHOR).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_SET_AUTHOR),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_SET_AUTHOR))) {
return false;
}

View File

@@ -29,8 +29,8 @@ public class CommandSetGeneration implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_CHANGE_GENERATION).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_CHANGE_GENERATION).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_CHANGE_GENERATION),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_CHANGE_GENERATION))) {
return false;
}

View File

@@ -34,8 +34,8 @@ public class CommandUnSign implements TabExecutor {
}
if (InventoryUtil.notHoldingOneWrittenBookCheck(player,
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_UNSIGN).build(),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_UNSIGN).build())) {
new FormatBuilder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK).replace("{action}", Translatable.ACTION_UNSIGN),
new FormatBuilder(Translatable.ERROR_ONLY_ONE_BOOK).replace("{action}", Translatable.ACTION_UNSIGN))) {
return false;
}

View File

@@ -77,18 +77,18 @@ public final class InventoryUtil {
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
* @return <p>False if the player is holding exactly one book</p>
*/
public static boolean notHoldingOneWritableBookCheck(@NotNull Player player, @NotNull String noBookMessage,
@NotNull String twoBooksMessage) {
public static boolean notHoldingOneWritableBookCheck(@NotNull Player player, @NotNull FormatBuilder noBookMessage,
@NotNull FormatBuilder twoBooksMessage) {
BookHoldingState holdingState = getBookHoldingState(player);
if (holdingState == BookHoldingState.NONE || holdingState == BookHoldingState.SIGNED_BOTH_HANDS ||
holdingState == BookHoldingState.SIGNED_MAIN_HAND || holdingState == BookHoldingState.SIGNED_OFF_HAND) {
new FormatBuilder(noBookMessage).error(player);
noBookMessage.error(player);
return true;
}
if (holdingState == BookHoldingState.UNSIGNED_BOTH_HANDS) {
new FormatBuilder(twoBooksMessage).error(player);
twoBooksMessage.error(player);
return true;
}
@@ -103,18 +103,18 @@ public final class InventoryUtil {
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
* @return <p>False if the player is holding exactly one book</p>
*/
public static boolean notHoldingOneWrittenBookCheck(@NotNull Player player, @NotNull String noBookMessage,
@NotNull String twoBooksMessage) {
public static boolean notHoldingOneWrittenBookCheck(@NotNull Player player, @NotNull FormatBuilder noBookMessage,
@NotNull FormatBuilder twoBooksMessage) {
BookHoldingState holdingState = getBookHoldingState(player);
if (holdingState == BookHoldingState.NONE || holdingState == BookHoldingState.UNSIGNED_BOTH_HANDS ||
holdingState == BookHoldingState.UNSIGNED_MAIN_HAND || holdingState == BookHoldingState.UNSIGNED_OFF_HAND) {
new FormatBuilder(noBookMessage).error(player);
noBookMessage.error(player);
return true;
}
if (holdingState == BookHoldingState.SIGNED_BOTH_HANDS) {
new FormatBuilder(twoBooksMessage).error(player);
twoBooksMessage.error(player);
return true;
}