diff --git a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java index 8745a48..7664859 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/BooksWithoutBorders.java @@ -32,6 +32,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFactory; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; @@ -42,6 +43,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder; import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getErrorColor; @@ -54,7 +56,7 @@ import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig public class BooksWithoutBorders extends JavaPlugin { private static ItemFactory itemFactory; - private static Map> playerBooksList; + private static Map> playerBooksList; private static List publicBooksList; private static BooksWithoutBorders booksWithoutBorders; private static ConsoleCommandSender consoleSender; @@ -78,13 +80,15 @@ public class BooksWithoutBorders extends JavaPlugin { public static List getAvailableBooks(CommandSender sender, boolean getPublic) { if (getPublic) { return new ArrayList<>(publicBooksList); - } else { - String senderName = sender.getName(); - if (!playerBooksList.containsKey(senderName)) { + } else if (sender instanceof Player player) { + UUID playerUUID = player.getUniqueId(); + if (!playerBooksList.containsKey(playerUUID)) { List 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 newFiles = FileHelper.listFiles(sender, updatePublic); if (updatePublic) { publicBooksList = newFiles; - } else { - playerBooksList.put(sender.getName(), newFiles); + } else if (sender instanceof Player player) { + playerBooksList.put(player.getUniqueId(), newFiles); } } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index 9dd5c5e..9f3ee34 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -1,8 +1,9 @@ package net.knarcraft.bookswithoutborders.command; 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.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -14,9 +15,6 @@ import java.io.File; import java.util.ArrayList; 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 */ @@ -82,13 +80,9 @@ public class CommandDelete implements TabExecutor { } //Get the file to be deleted - File file; - if (isPublic) { - file = FileHelper.getBookFile(getBookFolder() + fileName); - } else { - file = FileHelper.getBookFile(getBookFolder() + - InputCleaningHelper.cleanString(sender.getName()) + getSlash() + fileName); - } + String bookDirectory = BookHelper.getBookDirectoryPathString( + isPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender); + File file = FileHelper.getBookFile(bookDirectory + fileName); //Send message if no such file could be found if (file == null) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java index ada0d5c..d6cd765 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandSave.java @@ -2,6 +2,7 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig; +import net.knarcraft.bookswithoutborders.state.BookDirectory; import net.knarcraft.bookswithoutborders.state.ItemSlot; import net.knarcraft.bookswithoutborders.utility.BookHelper; import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper; @@ -20,12 +21,9 @@ import java.io.IOException; import java.util.ArrayList; 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.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; /** * Command executor for the save command @@ -78,12 +76,8 @@ public class CommandSave implements TabExecutor { return; } - String savePath; - if (saveToPublicFolder) { - savePath = getBookFolder(); - } else { - savePath = getBookFolder() + cleanString(player.getName()) + getSlash(); - } + String savePath = BookHelper.getBookDirectoryPathString( + saveToPublicFolder ? BookDirectory.PUBLIC : BookDirectory.PLAYER, player); //Generate book filename String fileName = BookHelper.getBookFile(book, player); diff --git a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java index f358a7b..590454e 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/listener/PlayerEventListener.java @@ -2,6 +2,8 @@ package net.knarcraft.bookswithoutborders.listener; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; 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.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; @@ -17,16 +19,13 @@ import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.ItemMeta; import java.io.File; - -import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder; -import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash; +import java.util.logging.Level; /** * A listener for listening to player-related events such as joining or holding a book */ public class PlayerEventListener implements Listener { - private final String slash = getSlash(); private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); @EventHandler @@ -58,6 +57,16 @@ public class PlayerEventListener implements Listener { public void onPlayerJoin(PlayerJoinEvent event) { 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 if (!player.hasPlayedBefore()) { boolean sendMessage = true; @@ -151,13 +160,14 @@ public class PlayerEventListener implements Listener { 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[]{ - getBookFolder() + fileName + ".yml", - getBookFolder() + fileName + ".txt", - getBookFolder() + cleanPlayerName + slash + fileName + ".yml", - getBookFolder() + cleanPlayerName + slash + fileName + ".txt" + publicFolderPath + fileName + ".yml", + publicFolderPath + fileName + ".txt", + playerFolderPath + fileName + ".yml", + playerFolderPath + fileName + ".txt" }; for (String path : possiblePaths) { diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java index b74c1fb..d17ee3f 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookHelper.java @@ -2,10 +2,15 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; 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.inventory.ItemStack; 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.fixName; @@ -18,6 +23,39 @@ public final class BookHelper { } + /** + * Gets the file path of the selected book directory + * + * @param bookDirectory

The book directory to get (ENCRYPTED is not supported here)

+ * @param sender

The command sender trying to get the directory

+ * @return

The path of the directory, or null if not possible to get

+ */ + 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

The book directory to get (ENCRYPTED is not supported here)

+ * @param sender

The command sender trying to get the directory

+ * @return

The path of the directory, or null if not possible to get

+ */ + 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 * diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java index dbdbe45..93f76ff 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/BookLoader.java @@ -116,16 +116,15 @@ public final class BookLoader { * @return

A file or null if it does not exist

*/ private static File getFullPath(CommandSender sender, String fileName, BookDirectory bookDirectory, String directory) { - File file = null; + File file; String slash = BooksWithoutBordersConfig.getSlash(); String bookFolder = BooksWithoutBordersConfig.getBookFolder(); - if (bookDirectory == BookDirectory.PUBLIC) { - 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) { + if (bookDirectory == BookDirectory.ENCRYPTED) { file = FileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName); + } else { + file = FileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName); } + if (file == null || !file.isFile()) { BooksWithoutBorders.sendErrorMessage(sender, "Incorrect file name!"); return null; diff --git a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java index 059f1c9..29366e6 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/utility/FileHelper.java @@ -1,6 +1,7 @@ package net.knarcraft.bookswithoutborders.utility; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; +import net.knarcraft.bookswithoutborders.state.BookDirectory; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -15,8 +16,6 @@ import java.util.Objects; import java.util.regex.Pattern; 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 @@ -98,11 +97,9 @@ public final class FileHelper { * @return

A list of available files

*/ public static List listFiles(CommandSender sender, Boolean listPublic) { - File file; - if (listPublic) { - file = new File(getBookFolder()); - } else { - file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash()); + File file = BookHelper.getBookDirectoryPath(listPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender); + if (file == null) { + return new ArrayList<>(); } return FileHelper.listFiles(sender, file); }