Adds a new /formatbook command

This commit is contained in:
Kristian Knarvik 2022-01-17 00:47:10 +01:00
parent 94589faba0
commit 7acaa9fc81
2 changed files with 77 additions and 3 deletions

View File

@ -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 <p>True if successful</p>
*/
private boolean initialize() {
@ -399,6 +402,7 @@ public class BooksWithoutBorders extends JavaPlugin {
/**
* Loads the config
*
* @return <p>True if the config was loaded successfully</p>
*/
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", " ");

View File

@ -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<String> 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 <p>The string to search for color codes</p>
* @return <p>The message with color codes translated</p>
*/
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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return new ArrayList<>();
}
}