Stores author name as UUID if storing own books
This change basically always stores the player's own books under their UUID, thus preventing being denied access to their own books if they change their username. The UUID is converted back to the username on the fly, so it shouldn't be noticeable for the players.
This commit is contained in:
parent
40512dd771
commit
c995a4fc0f
@ -3,12 +3,14 @@ package net.knarcraft.bookswithoutborders.utility;
|
|||||||
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.BookDirectory;
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
||||||
@ -23,6 +25,24 @@ public final class BookHelper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the author of a book from UUID if necessary
|
||||||
|
*
|
||||||
|
* @param author <p>The author string</p>
|
||||||
|
* @return <p>The author string, converted if it was a UUID</p>
|
||||||
|
*/
|
||||||
|
public static String authorFromUUID(String author) {
|
||||||
|
try {
|
||||||
|
UUID authorID = UUID.fromString(author);
|
||||||
|
Player player = Bukkit.getPlayer(authorID);
|
||||||
|
if (player != null) {
|
||||||
|
author = player.getName();
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
}
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the file path of the selected book directory
|
* Gets the file path of the selected book directory
|
||||||
*
|
*
|
||||||
@ -106,10 +126,11 @@ public final class BookHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String authorName;
|
String authorName;
|
||||||
if (book.hasAuthor()) {
|
if (!book.hasAuthor() || isAuthor(player.getName(), book.getAuthor())) {
|
||||||
authorName = book.getAuthor();
|
//Store as unique id to account for name changes
|
||||||
|
authorName = player.getUniqueId().toString();
|
||||||
} else {
|
} else {
|
||||||
authorName = player.getName();
|
authorName = book.getAuthor();
|
||||||
}
|
}
|
||||||
|
|
||||||
return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false);
|
return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false);
|
||||||
@ -123,14 +144,25 @@ public final class BookHelper {
|
|||||||
* @return <p>True if the player is not the book's author</p>
|
* @return <p>True if the player is not the book's author</p>
|
||||||
*/
|
*/
|
||||||
public static boolean isNotAuthor(Player player, BookMeta book) {
|
public static boolean isNotAuthor(Player player, BookMeta book) {
|
||||||
String author = book.getAuthor();
|
if (isAuthor(player.getName(), book.getAuthor())) {
|
||||||
String playerName = InputCleaningHelper.cleanString(player.getName());
|
|
||||||
if (author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author))) {
|
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player,
|
||||||
|
"You must be the author of this book to use this command!");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "You must be the author of this book to use this command!");
|
/**
|
||||||
return true;
|
* Gets whether the given player name is equal to the given book author
|
||||||
|
*
|
||||||
|
* @param playerName <p>The player name to check</p>
|
||||||
|
* @param author <p>The author to check</p>
|
||||||
|
* @return <p>True if the player is the author</p>
|
||||||
|
*/
|
||||||
|
private static boolean isAuthor(String playerName, String author) {
|
||||||
|
playerName = InputCleaningHelper.cleanString(playerName);
|
||||||
|
return author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import java.io.PrintWriter;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static net.knarcraft.bookswithoutborders.utility.BookHelper.authorFromUUID;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,7 +114,7 @@ public final class BookToFromTextHelper {
|
|||||||
|
|
||||||
bookMetadata.setGeneration(BookMeta.Generation.valueOf(bookYml.getString("Generation", "ORIGINAL")));
|
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(authorFromUUID(bookYml.getString("Author", "Unknown")));
|
||||||
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
||||||
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@ -169,7 +170,7 @@ public final class BookToFromTextHelper {
|
|||||||
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
||||||
|
|
||||||
//Update the metadata of the book with its new values
|
//Update the metadata of the book with its new values
|
||||||
bookMetadata.setAuthor(author);
|
bookMetadata.setAuthor(authorFromUUID(author));
|
||||||
bookMetadata.setTitle(title);
|
bookMetadata.setTitle(title);
|
||||||
bookMetadata.setPages(pages);
|
bookMetadata.setPages(pages);
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -149,8 +150,20 @@ public final class FileHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (File foundFile : existingFiles) {
|
for (File foundFile : existingFiles) {
|
||||||
if (foundFile.isFile()) {
|
if (!foundFile.isFile()) {
|
||||||
fileList.add(foundFile.getName());
|
continue;
|
||||||
|
}
|
||||||
|
String fileName = foundFile.getName();
|
||||||
|
String separator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||||
|
if (fileName.contains(separator)) {
|
||||||
|
//Convert the UUID into a username if necessary
|
||||||
|
String[] data = fileName.split(separator);
|
||||||
|
String extension = data[1].substring(data[1].length() - 4);
|
||||||
|
String userName = data[1].substring(0, data[1].length() - 4);
|
||||||
|
data[1] = BookHelper.authorFromUUID(userName) + extension;
|
||||||
|
fileList.add(String.join(separator, data));
|
||||||
|
} else {
|
||||||
|
fileList.add(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user