Fixes redundancy in book filename generation, and saves Generation
This commit is contained in:
parent
542cd03bdc
commit
0d4d87373c
@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
import org.bukkit.command.Command;
|
||||
@ -44,17 +45,9 @@ public class CommandDecrypt implements TabExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
String authorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
|
||||
//Warning: admin decrypt only allows decrypting files created by the same player. Not sure if intended
|
||||
if (args.length == 0 && BooksWithoutBordersConfig.getAdminDecrypt() && player.hasPermission("bookswithoutborders.admin")) {
|
||||
String path = getBookFolder() + "Encrypted" + getSlash();
|
||||
String fileName;
|
||||
if (bookMetadata.hasTitle()) {
|
||||
fileName = bookMetadata.getTitle() + authorSeparator + bookMetadata.getAuthor();
|
||||
} else {
|
||||
fileName = "Untitled" + authorSeparator + player.getName();
|
||||
}
|
||||
|
||||
File encryptedDirectory = new File(path);
|
||||
String[] encryptedFiles = encryptedDirectory.list();
|
||||
@ -66,7 +59,7 @@ public class CommandDecrypt implements TabExecutor {
|
||||
//Get the "encryption key" from the filename
|
||||
String key = "";
|
||||
for (String encryptedFile : encryptedFiles) {
|
||||
if (encryptedFile.contains(fileName)) {
|
||||
if (encryptedFile.contains(BookFormatter.getBookFile(bookMetadata, player))) {
|
||||
key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]"));
|
||||
break;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.command;
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
@ -23,8 +24,8 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig
|
||||
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getCommandColor;
|
||||
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getErrorColor;
|
||||
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getTitleAuthorSeparator;
|
||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
||||
|
||||
/**
|
||||
* Command executor for the save command
|
||||
@ -85,15 +86,7 @@ public class CommandSave implements TabExecutor {
|
||||
}
|
||||
|
||||
//Generate book filename
|
||||
String authorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
String fileName;
|
||||
if (!book.hasTitle()) {
|
||||
fileName = "Untitled" + authorSeparator + player.getName();
|
||||
} else {
|
||||
fileName = book.getTitle() + authorSeparator + book.getAuthor();
|
||||
}
|
||||
fileName = cleanString(fileName);
|
||||
fileName = fixName(fileName, false);
|
||||
String fileName = BookFormatter.getBookFile(book, player);
|
||||
|
||||
//Make sure the used folders exist
|
||||
File file = new File(savePath);
|
||||
@ -114,21 +107,24 @@ public class CommandSave implements TabExecutor {
|
||||
if (foundDuplicates > 0) {
|
||||
//TODO: Decide if this makes sense or needs to be changed
|
||||
//Skip duplicate book
|
||||
if (!fileName.contains("Untitled") && !overwrite) {
|
||||
if (!fileName.contains("Untitled" + getTitleAuthorSeparator()) && !overwrite) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Book is already saved!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " + getErrorColor() + "to overwrite!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " +
|
||||
getErrorColor() + "to overwrite!");
|
||||
return;
|
||||
}
|
||||
|
||||
//Skip if duplicate limit is reached
|
||||
if (foundDuplicates > BooksWithoutBordersConfig.getBookDuplicateLimit()) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Maximum amount of " + fileName + " duplicates reached!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " + getErrorColor() + "to overwrite!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Maximum amount of " + fileName +
|
||||
" duplicates reached!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " +
|
||||
getErrorColor() + "to overwrite!");
|
||||
return;
|
||||
}
|
||||
|
||||
//Alter duplicated filename
|
||||
if (fileName.contains("Untitled") && !overwrite) {
|
||||
if (fileName.contains("Untitled" + getTitleAuthorSeparator()) && !overwrite) {
|
||||
fileName = "(" + foundDuplicates + ")" + fileName;
|
||||
}
|
||||
}
|
||||
@ -150,7 +146,8 @@ public class CommandSave implements TabExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
|
||||
@NotNull String[] args) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package net.knarcraft.bookswithoutborders.utility;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -9,6 +11,9 @@ import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
||||
|
||||
/**
|
||||
* A class for formatting text to fit books
|
||||
*/
|
||||
@ -17,6 +22,32 @@ public final class BookFormatter {
|
||||
private BookFormatter() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the file name of the given book
|
||||
*
|
||||
* @param book <p>The book to get the file of</p>
|
||||
* @param player <p>The player trying to do something with the book</p>
|
||||
* @return <p>The book file</p>
|
||||
*/
|
||||
public static String getBookFile(BookMeta book, Player player) {
|
||||
String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
String bookName;
|
||||
if (!book.hasTitle()) {
|
||||
bookName = book.getTitle();
|
||||
} else {
|
||||
bookName = "Untitled";
|
||||
}
|
||||
|
||||
String authorName;
|
||||
if (book.hasAuthor()) {
|
||||
authorName = book.getAuthor();
|
||||
} else {
|
||||
authorName = player.getName();
|
||||
}
|
||||
|
||||
return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the last page of a set of pages
|
||||
*
|
||||
|
@ -41,6 +41,9 @@ public final class BookToFromTextHelper {
|
||||
if (bookMetadata.hasAuthor()) {
|
||||
bookYml.set("Author", bookMetadata.getAuthor());
|
||||
}
|
||||
if (bookMetadata.hasGeneration() && bookMetadata.getGeneration() != null) {
|
||||
bookYml.set("Generation", bookMetadata.getGeneration().name());
|
||||
}
|
||||
if (bookMetadata.hasPages()) {
|
||||
bookYml.set("Pages", bookMetadata.getPages());
|
||||
}
|
||||
@ -81,8 +84,15 @@ public final class BookToFromTextHelper {
|
||||
PrintWriter printWriter = new PrintWriter(fileWriter);
|
||||
List<String> pages = bookMetadata.getPages();
|
||||
|
||||
String generation;
|
||||
if (bookMetadata.hasGeneration() && bookMetadata.getGeneration() != null) {
|
||||
generation = ":" + bookMetadata.getGeneration().name();
|
||||
} else {
|
||||
generation = "";
|
||||
}
|
||||
|
||||
//Save each page of the book as a text line
|
||||
printWriter.println("[Book]");
|
||||
printWriter.println("[Book]" + generation);
|
||||
for (String page : pages) {
|
||||
printWriter.println(page);
|
||||
}
|
||||
@ -100,6 +110,7 @@ public final class BookToFromTextHelper {
|
||||
try {
|
||||
FileConfiguration bookYml = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
bookMetadata.setGeneration(BookMeta.Generation.valueOf(bookYml.getString("Generation", "ORIGINAL")));
|
||||
bookMetadata.setTitle(bookYml.getString("Title", "Untitled"));
|
||||
bookMetadata.setAuthor(bookYml.getString("Author", "Unknown"));
|
||||
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
||||
@ -123,13 +134,16 @@ public final class BookToFromTextHelper {
|
||||
String title;
|
||||
String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
|
||||
//Remove .txt extension
|
||||
fileName = fileName.substring(0, fileName.length() - 4);
|
||||
//Get title and author from the file name
|
||||
if (fileName.contains(titleAuthorSeparator)) {
|
||||
author = fileName.substring(fileName.indexOf(titleAuthorSeparator) + 1, fileName.length() - 4);
|
||||
title = fileName.substring(0, fileName.indexOf(titleAuthorSeparator));
|
||||
String[] titleAuthor = fileName.split(titleAuthorSeparator);
|
||||
title = titleAuthor[0];
|
||||
author = titleAuthor[1];
|
||||
} else {
|
||||
author = "Unknown";
|
||||
title = fileName.substring(0, fileName.length() - 4);
|
||||
title = fileName;
|
||||
}
|
||||
|
||||
//Replace underscores with spaces
|
||||
@ -144,6 +158,12 @@ public final class BookToFromTextHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
//Parse the generation from the book data
|
||||
if (rawPages != null && rawPages.size() > 0 && rawPages.get(0).startsWith("Generation:")) {
|
||||
bookMetadata.setGeneration(BookMeta.Generation.valueOf(rawPages.get(0).split(":")[1]));
|
||||
rawPages.remove(0);
|
||||
}
|
||||
|
||||
//Remove any empty pages
|
||||
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
||||
|
||||
@ -172,8 +192,11 @@ public final class BookToFromTextHelper {
|
||||
bufferedReader.close();
|
||||
return null;
|
||||
}
|
||||
if (firstLine.equalsIgnoreCase("[Book]")) {
|
||||
if (firstLine.toLowerCase().startsWith("[book]")) {
|
||||
//Read every line directly as a page, as this is a saved book
|
||||
if (firstLine.contains(":")) {
|
||||
rawPages.add("Generation:" + firstLine.split(":")[1]);
|
||||
}
|
||||
String readLine;
|
||||
do {
|
||||
readLine = bufferedReader.readLine();
|
||||
|
@ -198,12 +198,8 @@ public final class EncryptionHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
String authorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
String fileName = (!bookMetadata.hasTitle()) ? "Untitled" + authorSeparator + player.getName() :
|
||||
bookMetadata.getTitle() + authorSeparator + bookMetadata.getAuthor();
|
||||
fileName = "[" + key + "]" + fileName;
|
||||
fileName = cleanString(fileName);
|
||||
fileName = fixName(fileName, false);
|
||||
String fileName = "[" + key + "]" + BookFormatter.getBookFile(bookMetadata, player);
|
||||
fileName = fixName(cleanString(fileName), false);
|
||||
|
||||
File file = new File(path + fileName + ".yml");
|
||||
if (!file.isFile()) {
|
||||
@ -264,13 +260,8 @@ public final class EncryptionHelper {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//Creates file
|
||||
String authorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
String fileName = (!bookMetadata.hasTitle()) ? "Untitled" + authorSeparator + player.getName() :
|
||||
bookMetadata.getTitle() + authorSeparator + bookMetadata.getAuthor();
|
||||
|
||||
fileName = cleanString(fileName);
|
||||
fileName = fixName(fileName, false);
|
||||
//Generate file name
|
||||
String fileName = BookFormatter.getBookFile(bookMetadata, player);
|
||||
|
||||
List<String> newLore = new ArrayList<>();
|
||||
newLore.add(ChatColor.GRAY + "[" + groupName + " encrypted]");
|
||||
@ -283,7 +274,8 @@ public final class EncryptionHelper {
|
||||
bookMetadata.setLore(newLore);
|
||||
|
||||
//Save file
|
||||
File file = (BooksWithoutBordersConfig.getUseYml()) ? new File(path + fileName + ".yml") : new File(path + fileName + ".txt");
|
||||
File file = (BooksWithoutBordersConfig.getUseYml()) ? new File(path + fileName + ".yml") :
|
||||
new File(path + fileName + ".txt");
|
||||
if (!file.isFile()) {
|
||||
try {
|
||||
BookToFromTextHelper.bookToYml(path, fileName, bookMetadata);
|
||||
@ -307,16 +299,13 @@ public final class EncryptionHelper {
|
||||
*/
|
||||
private static Boolean saveEncryptedBook(Player player, BookMeta bookMetaData, String key) {
|
||||
String path = getBookFolder() + "Encrypted" + getSlash();
|
||||
String authorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||
String fileName = (!bookMetaData.hasTitle()) ? "Untitled" + authorSeparator + player.getName() :
|
||||
bookMetaData.getTitle() + authorSeparator + bookMetaData.getAuthor();
|
||||
|
||||
fileName = "[" + key + "]" + fileName;
|
||||
fileName = cleanString(fileName);
|
||||
fileName = fixName(fileName, false);
|
||||
String fileName = "[" + key + "]" + BookFormatter.getBookFile(bookMetaData, player);
|
||||
fileName = fixName(cleanString(fileName), false);
|
||||
|
||||
//cancels saving if file is already encrypted
|
||||
File file = (BooksWithoutBordersConfig.getUseYml()) ? new File(path + fileName + ".yml") : new File(path + fileName + ".txt");
|
||||
File file = (BooksWithoutBordersConfig.getUseYml()) ? new File(path + fileName + ".yml") :
|
||||
new File(path + fileName + ".txt");
|
||||
if (file.isFile()) {
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user