diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index b4f0359..be13ea7 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -6,6 +6,7 @@ import net.knarcraft.bookswithoutborders.command.CommandDecrypt; import net.knarcraft.bookswithoutborders.command.CommandDelete; import net.knarcraft.bookswithoutborders.command.CommandDeletePublic; import net.knarcraft.bookswithoutborders.command.CommandEncrypt; +import net.knarcraft.bookswithoutborders.command.CommandFormat; import net.knarcraft.bookswithoutborders.command.CommandGive; import net.knarcraft.bookswithoutborders.command.CommandGivePublic; import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt; @@ -280,6 +281,7 @@ public class BooksWithoutBorders extends JavaPlugin { registerCommand("loadPublicBook", new CommandLoadPublic()); registerCommand("booksWithoutBorders", new CommandBooksWithoutBorders()); registerCommand("reload", new CommandReload()); + registerCommand("formatBook", new CommandFormat()); } /** @@ -299,6 +301,7 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Initializes the plugin, loading and fixing the config file + * * @return

True if successful

*/ private boolean initialize() { @@ -399,6 +402,7 @@ public class BooksWithoutBorders extends JavaPlugin { /** * Loads the config + * * @return

True if the config was loaded successfully

*/ public boolean loadConfig() { @@ -417,9 +421,6 @@ public class BooksWithoutBorders extends JavaPlugin { if (config.contains("Options.Book_for_new_players")) { firstBooks.add(config.getString("Options.Book_for_new_players")); } - if (firstBooks.isEmpty()) { - firstBooks.add(" "); - } welcomeMessage = config.getString("Options.Message_for_new_players", " "); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandFormat.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandFormat.java new file mode 100644 index 0000000..932d163 --- /dev/null +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandFormat.java @@ -0,0 +1,73 @@ +package net.knarcraft.bookswithoutborders.command; + +import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.utility.InventoryHelper; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A command for converting color codes to color formatting + */ +public class CommandFormat implements TabExecutor { + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (!(sender instanceof Player player)) { + BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!"); + return false; + } + + if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a written book to format it!", + "You cannot format two books at once!")) { + return false; + } + + ItemStack heldBook = InventoryHelper.getHeldBook(player, true); + BookMeta bookMeta = (BookMeta) heldBook.getItemMeta(); + + List formattedPages = new ArrayList<>(Objects.requireNonNull(bookMeta).getPageCount()); + for (String page : bookMeta.getPages()) { + formattedPages.add(translateAllColorCodes(page)); + } + bookMeta.setPages(formattedPages); + heldBook.setItemMeta(bookMeta); + + BooksWithoutBorders.sendSuccessMessage(sender, "Book formatted!"); + + return true; + } + + /** + * Translates all found color codes to formatting in a string + * + * @param message

The string to search for color codes

+ * @return

The message with color codes translated

+ */ + public static String translateAllColorCodes(String message) { + message = ChatColor.translateAlternateColorCodes('&', message); + Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); + Matcher matcher = pattern.matcher(message); + while (matcher.find()) { + message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); + } + return message; + } + + @Override + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + return new ArrayList<>(); + } + +}