Fixes sorting and character indexes for filenames with color codes
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
This commit is contained in:
@@ -44,7 +44,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -124,9 +123,8 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
* @param sender <p>The sender to update books for</p>
|
||||
* @param updatePublic <p>Whether to update public books</p>
|
||||
*/
|
||||
public static void updateBooks(CommandSender sender, boolean updatePublic) {
|
||||
public static void updateBooks(@NotNull CommandSender sender, boolean updatePublic) {
|
||||
List<String> newFiles = BookFileHelper.listFiles(sender, updatePublic);
|
||||
newFiles.sort(Comparator.naturalOrder());
|
||||
if (updatePublic) {
|
||||
publicBooksList = newFiles;
|
||||
publicLetterIndex = BookFileHelper.populateLetterIndices(newFiles);
|
||||
@@ -136,6 +134,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
FileConfiguration config = this.getConfig();
|
||||
|
@@ -4,6 +4,7 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookFileHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||
@@ -144,7 +145,7 @@ public class SignEventListener implements Listener {
|
||||
player.closeInventory();
|
||||
|
||||
//Converts user supplied key into integer form
|
||||
String lineText = ChatColor.stripColor(sign.getSide(Side.FRONT).getLine(2));
|
||||
String lineText = BookFormatter.stripColor(sign.getSide(Side.FRONT).getLine(2));
|
||||
String key = EncryptionHelper.getNumberKeyFromStringKey(lineText);
|
||||
|
||||
ItemStack book = EncryptionHelper.loadEncryptedBook(player, key, false);
|
||||
@@ -163,7 +164,7 @@ public class SignEventListener implements Listener {
|
||||
*/
|
||||
private ChatColor getSignLine2Color(Sign sign) {
|
||||
String line = sign.getSide(Side.FRONT).getLine(2);
|
||||
if (!ChatColor.stripColor(line).equals(line)) {
|
||||
if (!BookFormatter.stripColor(line).equals(line)) {
|
||||
return ChatColor.getByChar(sign.getSide(Side.FRONT).getLine(2).substring(1, 2).charAt(0));
|
||||
} else {
|
||||
return null;
|
||||
@@ -291,8 +292,8 @@ public class SignEventListener implements Listener {
|
||||
boolean mainHand = hand == EquipmentSlot.HAND;
|
||||
if (heldItemType == Material.WRITTEN_BOOK) {
|
||||
player.closeInventory();
|
||||
eBook = EncryptionHelper.encryptBook(player, mainHand, ChatColor.stripColor(lines[2]),
|
||||
EncryptionStyle.getFromString(ChatColor.stripColor(lines[3])));
|
||||
eBook = EncryptionHelper.encryptBook(player, mainHand, BookFormatter.stripColor(lines[2]),
|
||||
EncryptionStyle.getFromString(BookFormatter.stripColor(lines[3])));
|
||||
if (eBook != null) {
|
||||
player.getInventory().setItem(hand, eBook);
|
||||
}
|
||||
@@ -306,7 +307,7 @@ public class SignEventListener implements Listener {
|
||||
* @param player <p>The player which clicked the sign</p>
|
||||
*/
|
||||
private void giveBook(Sign sign, Player player) {
|
||||
String fileName = ChatColor.stripColor(sign.getSide(Side.FRONT).getLine(2));
|
||||
String fileName = BookFormatter.stripColor(sign.getSide(Side.FRONT).getLine(2));
|
||||
boolean isLoadListNumber = false;
|
||||
|
||||
try {
|
||||
@@ -318,7 +319,7 @@ public class SignEventListener implements Listener {
|
||||
//Add the third line to the second line for the full filename
|
||||
String thirdLine = sign.getSide(Side.FRONT).getLine(3);
|
||||
if (!isLoadListNumber && thirdLine.length() >= 2) {
|
||||
fileName += ChatColor.stripColor(thirdLine);
|
||||
fileName += BookFormatter.stripColor(thirdLine);
|
||||
}
|
||||
|
||||
ItemStack newBook = BookLoader.loadBook(player, fileName, "true", "public");
|
||||
|
@@ -15,10 +15,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
|
||||
@@ -154,7 +156,6 @@ public final class BookFileHelper {
|
||||
int totalPages, @NotNull List<String> availableBooks,
|
||||
@NotNull Map<Character, Integer> firstInstances) {
|
||||
ComponentBuilder headerComponent = new ComponentBuilder();
|
||||
headerComponent.append("Books page " + page + " of " + totalPages).color(ChatColor.GREEN).append(" ");
|
||||
|
||||
// Display links to first page where a letter can be found
|
||||
for (int characterIndex = 0; characterIndex <= 25; characterIndex++) {
|
||||
@@ -212,6 +213,7 @@ public final class BookFileHelper {
|
||||
} else {
|
||||
nextComponent.append("Next [>]").color(ChatColor.GRAY);
|
||||
}
|
||||
nextComponent.append(" Page " + page + " of " + totalPages).color(ChatColor.GREEN);
|
||||
nextComponent.append(" ", ComponentBuilder.FormatRetention.NONE).append(
|
||||
"[Page Command]", ComponentBuilder.FormatRetention.NONE).event(new HoverEvent(
|
||||
HoverEvent.Action.SHOW_TEXT, new Text("/" + command + " page" + page))).event(
|
||||
@@ -227,10 +229,9 @@ public final class BookFileHelper {
|
||||
*/
|
||||
@NotNull
|
||||
private static String getNiceName(@NotNull String bookPath) {
|
||||
bookPath = ChatColor.translateAlternateColorCodes('&', bookPath.substring(0, bookPath.length() - 4))
|
||||
.replace("_", " ");
|
||||
bookPath = ChatColor.translateAlternateColorCodes('&', bookPath.substring(0, bookPath.length() - 4));
|
||||
String[] parts = bookPath.split(",");
|
||||
return parts[0] + ChatColor.RESET + " by " + parts[1] + ChatColor.RESET;
|
||||
return parts[0].replace("_", " ") + ChatColor.RESET + " by " + parts[1] + ChatColor.RESET;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,7 +244,7 @@ public final class BookFileHelper {
|
||||
public static Map<Character, Integer> populateLetterIndices(@NotNull List<String> books) {
|
||||
List<Character> firstCharacter = new ArrayList<>(books.size());
|
||||
for (String bookIdentifier : books) {
|
||||
firstCharacter.add(bookIdentifier.toLowerCase().charAt(0));
|
||||
firstCharacter.add(BookFormatter.stripColor(bookIdentifier).toLowerCase().charAt(0));
|
||||
}
|
||||
|
||||
Map<Character, Integer> firstEncounter = new HashMap<>();
|
||||
@@ -291,6 +292,12 @@ public final class BookFileHelper {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the book list
|
||||
Comparator<String> bookComparator = Comparator.naturalOrder();
|
||||
for (String name : fileList) {
|
||||
BooksWithoutBorders.getInstance().getLogger().log(Level.INFO, BookFormatter.stripColor(name));
|
||||
}
|
||||
fileList.sort((a, b) -> bookComparator.compare(BookFormatter.stripColor(a), BookFormatter.stripColor(b)));
|
||||
return fileList;
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.utility;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.ColorHelper;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -114,4 +115,15 @@ public final class BookFormatter {
|
||||
return bookMeta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips the color from the given input
|
||||
*
|
||||
* @param input <p>The input to strip</p>
|
||||
* @return <p>The color stripped input</p>
|
||||
*/
|
||||
@NotNull
|
||||
public static String stripColor(@NotNull String input) {
|
||||
return ColorHelper.stripColorCodes(input, ColorConversion.RGB);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user