Changes player folders to UUIDs to prevent problems if users change their names

This commit is contained in:
Kristian Knarvik 2022-08-10 13:44:11 +02:00
parent 70ad6390db
commit 5d340af6f2
7 changed files with 86 additions and 50 deletions

View File

@ -32,6 +32,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemFactory;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
@ -42,6 +43,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getErrorColor; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getErrorColor;
@ -54,7 +56,7 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig
public class BooksWithoutBorders extends JavaPlugin { public class BooksWithoutBorders extends JavaPlugin {
private static ItemFactory itemFactory; private static ItemFactory itemFactory;
private static Map<String, List<String>> playerBooksList; private static Map<UUID, List<String>> playerBooksList;
private static List<String> publicBooksList; private static List<String> publicBooksList;
private static BooksWithoutBorders booksWithoutBorders; private static BooksWithoutBorders booksWithoutBorders;
private static ConsoleCommandSender consoleSender; private static ConsoleCommandSender consoleSender;
@ -78,13 +80,15 @@ public class BooksWithoutBorders extends JavaPlugin {
public static List<String> getAvailableBooks(CommandSender sender, boolean getPublic) { public static List<String> getAvailableBooks(CommandSender sender, boolean getPublic) {
if (getPublic) { if (getPublic) {
return new ArrayList<>(publicBooksList); return new ArrayList<>(publicBooksList);
} else { } else if (sender instanceof Player player) {
String senderName = sender.getName(); UUID playerUUID = player.getUniqueId();
if (!playerBooksList.containsKey(senderName)) { if (!playerBooksList.containsKey(playerUUID)) {
List<String> newFiles = FileHelper.listFiles(sender, false); List<String> newFiles = FileHelper.listFiles(sender, false);
playerBooksList.put(senderName, newFiles); playerBooksList.put(playerUUID, newFiles);
} }
return playerBooksList.get(senderName); return playerBooksList.get(playerUUID);
} else {
return new ArrayList<>();
} }
} }
@ -98,8 +102,8 @@ public class BooksWithoutBorders extends JavaPlugin {
List<String> newFiles = FileHelper.listFiles(sender, updatePublic); List<String> newFiles = FileHelper.listFiles(sender, updatePublic);
if (updatePublic) { if (updatePublic) {
publicBooksList = newFiles; publicBooksList = newFiles;
} else { } else if (sender instanceof Player player) {
playerBooksList.put(sender.getName(), newFiles); playerBooksList.put(player.getUniqueId(), newFiles);
} }
} }

View File

@ -1,8 +1,9 @@
package net.knarcraft.bookswithoutborders.command; package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.utility.BookHelper;
import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -14,9 +15,6 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
/** /**
* Command executor for the delete command * Command executor for the delete command
*/ */
@ -82,13 +80,9 @@ public class CommandDelete implements TabExecutor {
} }
//Get the file to be deleted //Get the file to be deleted
File file; String bookDirectory = BookHelper.getBookDirectoryPathString(
if (isPublic) { isPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender);
file = FileHelper.getBookFile(getBookFolder() + fileName); File file = FileHelper.getBookFile(bookDirectory + fileName);
} else {
file = FileHelper.getBookFile(getBookFolder() +
InputCleaningHelper.cleanString(sender.getName()) + getSlash() + fileName);
}
//Send message if no such file could be found //Send message if no such file could be found
if (file == null) { if (file == null) {

View File

@ -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.state.BookDirectory;
import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookHelper;
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
@ -20,12 +21,9 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
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.getTitleAuthorSeparator; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getTitleAuthorSeparator;
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
/** /**
* Command executor for the save command * Command executor for the save command
@ -78,12 +76,8 @@ public class CommandSave implements TabExecutor {
return; return;
} }
String savePath; String savePath = BookHelper.getBookDirectoryPathString(
if (saveToPublicFolder) { saveToPublicFolder ? BookDirectory.PUBLIC : BookDirectory.PLAYER, player);
savePath = getBookFolder();
} else {
savePath = getBookFolder() + cleanString(player.getName()) + getSlash();
}
//Generate book filename //Generate book filename
String fileName = BookHelper.getBookFile(book, player); String fileName = BookHelper.getBookFile(book, player);

View File

@ -2,6 +2,8 @@ package net.knarcraft.bookswithoutborders.listener;
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.utility.BookHelper;
import net.knarcraft.bookswithoutborders.utility.BookLoader; import net.knarcraft.bookswithoutborders.utility.BookLoader;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
@ -17,16 +19,13 @@ import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import java.io.File; import java.io.File;
import java.util.logging.Level;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
/** /**
* A listener for listening to player-related events such as joining or holding a book * A listener for listening to player-related events such as joining or holding a book
*/ */
public class PlayerEventListener implements Listener { public class PlayerEventListener implements Listener {
private final String slash = getSlash();
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
@EventHandler @EventHandler
@ -58,6 +57,16 @@ public class PlayerEventListener implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
//If a book directory exists with this player's name, move it to this player's UUID
String bookFolder = BooksWithoutBordersConfig.getBookFolder();
File file = new File(bookFolder + InputCleaningHelper.cleanString(player.getName()));
if (file.exists()) {
if (!file.renameTo(new File(bookFolder + player.getUniqueId()))) {
BooksWithoutBorders.getInstance().getLogger().log(Level.WARNING, "Unable to migrate player book " +
"directory for player " + player.getName());
}
}
//Handle new players //Handle new players
if (!player.hasPlayedBefore()) { if (!player.hasPlayedBefore()) {
boolean sendMessage = true; boolean sendMessage = true;
@ -151,13 +160,14 @@ public class PlayerEventListener implements Listener {
fileName = oldBook.getTitle() + BooksWithoutBordersConfig.getTitleAuthorSeparator() + oldBook.getAuthor(); fileName = oldBook.getTitle() + BooksWithoutBordersConfig.getTitleAuthorSeparator() + oldBook.getAuthor();
} }
String cleanPlayerName = InputCleaningHelper.cleanString(player.getName()); String playerFolderPath = BookHelper.getBookDirectoryPathString(BookDirectory.PLAYER, player);
String publicFolderPath = BookHelper.getBookDirectoryPathString(BookDirectory.PUBLIC, player);
String[] possiblePaths = new String[]{ String[] possiblePaths = new String[]{
getBookFolder() + fileName + ".yml", publicFolderPath + fileName + ".yml",
getBookFolder() + fileName + ".txt", publicFolderPath + fileName + ".txt",
getBookFolder() + cleanPlayerName + slash + fileName + ".yml", playerFolderPath + fileName + ".yml",
getBookFolder() + cleanPlayerName + slash + fileName + ".txt" playerFolderPath + fileName + ".txt"
}; };
for (String path : possiblePaths) { for (String path : possiblePaths) {

View File

@ -2,10 +2,15 @@ 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 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 static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName; import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
@ -18,6 +23,39 @@ public final class BookHelper {
} }
/**
* Gets the file path of the selected book directory
*
* @param bookDirectory <p>The book directory to get (ENCRYPTED is not supported here)</p>
* @param sender <p>The command sender trying to get the directory</p>
* @return <p>The path of the directory, or null if not possible to get</p>
*/
public static File getBookDirectoryPath(BookDirectory bookDirectory, CommandSender sender) {
String bookFolderString = getBookDirectoryPathString(bookDirectory, sender);
if (bookFolderString == null) {
return null;
}
return new File(bookFolderString);
}
/**
* Gets the string path of the selected book directory
*
* @param bookDirectory <p>The book directory to get (ENCRYPTED is not supported here)</p>
* @param sender <p>The command sender trying to get the directory</p>
* @return <p>The path of the directory, or null if not possible to get</p>
*/
public static String getBookDirectoryPathString(BookDirectory bookDirectory, CommandSender sender) {
String folder = null;
String bookFolder = BooksWithoutBordersConfig.getBookFolder();
if (bookDirectory == BookDirectory.PUBLIC) {
folder = bookFolder;
} else if (bookDirectory == BookDirectory.PLAYER && sender instanceof Player player) {
folder = bookFolder + player.getUniqueId() + getSlash();
}
return folder;
}
/** /**
* Increases the generation of the given book, if necessary * Increases the generation of the given book, if necessary
* *

View File

@ -116,16 +116,15 @@ public final class BookLoader {
* @return <p>A file or null if it does not exist</p> * @return <p>A file or null if it does not exist</p>
*/ */
private static File getFullPath(CommandSender sender, String fileName, BookDirectory bookDirectory, String directory) { private static File getFullPath(CommandSender sender, String fileName, BookDirectory bookDirectory, String directory) {
File file = null; File file;
String slash = BooksWithoutBordersConfig.getSlash(); String slash = BooksWithoutBordersConfig.getSlash();
String bookFolder = BooksWithoutBordersConfig.getBookFolder(); String bookFolder = BooksWithoutBordersConfig.getBookFolder();
if (bookDirectory == BookDirectory.PUBLIC) { if (bookDirectory == BookDirectory.ENCRYPTED) {
file = FileHelper.getBookFile(bookFolder + fileName);
} else if (bookDirectory == BookDirectory.PLAYER) {
file = FileHelper.getBookFile(bookFolder + InputCleaningHelper.cleanString(sender.getName()) + slash + fileName);
} else if (bookDirectory == BookDirectory.ENCRYPTED) {
file = FileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName); file = FileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName);
} else {
file = FileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName);
} }
if (file == null || !file.isFile()) { if (file == null || !file.isFile()) {
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect file name!"); BooksWithoutBorders.sendErrorMessage(sender, "Incorrect file name!");
return null; return null;

View File

@ -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.state.BookDirectory;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -15,8 +16,6 @@ import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
/** /**
* Helper class for dealing with files * Helper class for dealing with files
@ -98,11 +97,9 @@ public final class FileHelper {
* @return <p>A list of available files</p> * @return <p>A list of available files</p>
*/ */
public static List<String> listFiles(CommandSender sender, Boolean listPublic) { public static List<String> listFiles(CommandSender sender, Boolean listPublic) {
File file; File file = BookHelper.getBookDirectoryPath(listPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender);
if (listPublic) { if (file == null) {
file = new File(getBookFolder()); return new ArrayList<>();
} else {
file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash());
} }
return FileHelper.listFiles(sender, file); return FileHelper.listFiles(sender, file);
} }