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