Moves another file loading related function to the FileHelper class

This commit is contained in:
Kristian Knarvik 2021-09-02 20:46:37 +02:00
parent 1b8965ace3
commit c39fd7d7db
9 changed files with 62 additions and 40 deletions

View File

@ -60,22 +60,35 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
public class BooksWithoutBorders extends JavaPlugin {
//Limit for duplicates of a book
public static int bookDuplicateLimit;
//The separating string between the book title and the book author
public static String titleAuthorSeparator;
//Separator to force Lore newline
public static String loreSeparator;
//Books to give players on first login
public static List<String> firstBooks;
//Message to display to new players
public static String welcomeMessage;
//List of players
public static List<String> existingPlayers;
//The material used for payment
public static Material bookPriceType = null;
//The number of items/currency to pay for each transaction
public static double bookPriceQuantity;
//Whether only the author of a book should be allowed to copy the book
public static boolean authorOnlyCopy;
//Whether to use YML files instead of TXT files for saving books
public static boolean useYml;
//Whether admins should be able to decrypt group encrypted books without belonging to its group
public static boolean adminDecrypt;
private static ItemFactory itemFactory;
public static Map<String, List<String>> loadList;
public static BooksWithoutBorders booksWithoutBorders;
private static BooksWithoutBorders booksWithoutBorders;
public static ConsoleCommandSender consoleSender;
private static String existingPlayersFile;
@Override
public void onEnable() {
@ -85,6 +98,8 @@ public class BooksWithoutBorders extends JavaPlugin {
firstBooks = new ArrayList<>();
BooksWithoutBordersSettings.initialize(this);
existingPlayersFile = this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt";
PluginManager pluginManager = this.getServer().getPluginManager();
if (getSlash() != null && initialize()) {
@ -170,6 +185,10 @@ public class BooksWithoutBorders extends JavaPlugin {
return loadExistingPlayers();
}
/**
* Makes sure necessary folders exist
* @return <p>True if necessary folders exist</p>
*/
private boolean testFileSaving() {
File fileTest = new File(getBookFolder());
File encryptedFileTest = new File(getBookFolder() + "Encrypted" + getSlash());
@ -293,7 +312,7 @@ public class BooksWithoutBorders extends JavaPlugin {
}
public boolean loadExistingPlayers() {
File fTest = new File(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt");
File fTest = new File(existingPlayersFile);
existingPlayers = new ArrayList<>();
if (!fTest.exists()) {
@ -339,7 +358,7 @@ public class BooksWithoutBorders extends JavaPlugin {
existingPlayers.add(playerName);
try {
PrintWriter pw = new PrintWriter(new FileWriter(this.getDataFolder().getAbsolutePath() + getSlash() + "Existing Players.txt", true));
PrintWriter pw = new PrintWriter(new FileWriter(existingPlayersFile, true));
pw.println(playerName);
pw.close();
} catch (IOException e) {
@ -467,24 +486,6 @@ public class BooksWithoutBorders extends JavaPlugin {
}
}
/**
* Lists available files
*
* @param sender <p>The command sender looking for files</p>
* @param listPublic <p>Whether to list public or personal files</p>
* @param silent <p>Whether to just return the list without printing it</p>
* @return <p>A list of available files</p>
*/
public List<String> listFiles(CommandSender sender, Boolean listPublic, boolean silent) {
File file;
if (listPublic) {
file = new File(getBookFolder());
} else {
file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash());
}
return FileHelper.listFiles(sender, file, silent);
}
/**
* Sends a success message to a command sender (player or a console)
*

View File

@ -18,8 +18,6 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS
*/
public class CommandDelete implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
@ -41,7 +39,7 @@ public class CommandDelete implements CommandExecutor {
boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) {
//List deletable files
if (args.length == 0) {
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, deletePublic, false));
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, deletePublic, false));
return true;
}
//Delete the file

View File

@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -32,7 +33,7 @@ public class CommandGive implements CommandExecutor {
}
if (args.length == 0) {
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, false));
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, false));
return true;
}
@ -55,7 +56,7 @@ public class CommandGive implements CommandExecutor {
//Load books available to the player
try {
Integer.parseInt(bookIdentifier);
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, true));
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, givePublic, true));
} catch (NumberFormatException ignored) {
}

View File

@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -44,7 +45,7 @@ public class CommandLoad implements CommandExecutor {
//Show books available to the player
if (argumentCount == 0) {
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(player, loadPublic, false));
BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(player, loadPublic, false));
return true;
}
@ -66,7 +67,7 @@ public class CommandLoad implements CommandExecutor {
//Load books available to the player
try {
Integer.parseInt(bookIdentifier);
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, loadPublic, true));
BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, loadPublic, true));
} catch (NumberFormatException ignored) {
}

View File

@ -14,7 +14,6 @@ import org.bukkit.inventory.meta.BookMeta;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.titleAuthorSeparator;
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;

View File

@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -8,7 +9,6 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -49,7 +49,7 @@ public class GiveTabCompleter implements TabCompleter {
if (argumentCount == 1) {
//Return list of books
return BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, false, true));
return BooksWithoutBorders.loadList.put(sender.getName(), FileHelper.listFiles(sender, false, true));
} else if (argumentCount == 2) {
//Return online players
return playerNames;

View File

@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.listener;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
import org.bukkit.Material;
@ -24,6 +25,7 @@ import static net.knarcraft.bookswithoutborders.utility.FileHelper.isBookListInd
public class PlayerEventListener implements Listener {
private final String slash = getSlash();
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
@EventHandler
public void onHold(PlayerItemHeldEvent event) {
@ -90,12 +92,12 @@ public class PlayerEventListener implements Listener {
//Handles loadList numbers
if (isBookListIndex(bookName)) {
List<String> availableFiles = BooksWithoutBorders.booksWithoutBorders.listFiles(player, true, true);
List<String> availableFiles = FileHelper.listFiles(player, true, true);
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
}
//Give the book to the player if it exists
ItemStack newBook = BooksWithoutBorders.booksWithoutBorders.loadBook(player, bookName, "true", "public");
ItemStack newBook = booksWithoutBorders.loadBook(player, bookName, "true", "public");
if (newBook != null) {
player.getInventory().addItem(newBook);
}
@ -103,7 +105,7 @@ public class PlayerEventListener implements Listener {
//Send the player a welcome message if it exists
if (!BooksWithoutBorders.welcomeMessage.trim().isEmpty() && newBook != null && sendMessage) {
sendMessage = false;
BooksWithoutBorders.booksWithoutBorders.getServer().getScheduler().scheduleSyncDelayedTask(BooksWithoutBorders.booksWithoutBorders,
booksWithoutBorders.getServer().getScheduler().scheduleSyncDelayedTask(booksWithoutBorders,
() -> player.sendMessage(BooksWithoutBorders.welcomeMessage), 40L);
}
}
@ -164,7 +166,7 @@ public class PlayerEventListener implements Listener {
for (String path : possiblePaths) {
File file = new File(path);
if (file.isFile()) {
return BooksWithoutBorders.booksWithoutBorders.loadBook(player, fileName, "true", "player");
return booksWithoutBorders.loadBook(player, fileName, "true", "player");
}
}
return null;
@ -180,7 +182,7 @@ public class PlayerEventListener implements Listener {
*/
private boolean hasPlayedBefore(String playerName) {
if (!BooksWithoutBorders.existingPlayers.contains(playerName)) {
BooksWithoutBorders.booksWithoutBorders.addExistingPlayer(playerName);
booksWithoutBorders.addExistingPlayer(playerName);
return false;
}

View File

@ -184,7 +184,7 @@ public class SignEventListener implements Listener {
markGiveSignValidity(event, true);
} else {
if (isBookListIndex(lines[2])) {
List<String> availableFiles = BooksWithoutBorders.booksWithoutBorders.listFiles(player, true, true);
List<String> availableFiles = FileHelper.listFiles(player, true, true);
BooksWithoutBorders.loadList.put(player.getName(), availableFiles);
markGiveSignValidity(event, true);
return;
@ -245,7 +245,7 @@ public class SignEventListener implements Listener {
return;
}
}
newBook = BooksWithoutBorders.booksWithoutBorders.loadBook(player, fileName, "true", groupName, heldItem.getAmount());
newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", groupName, heldItem.getAmount());
player.getInventory().setItem(hand, newBook);
player.closeInventory();
@ -288,7 +288,7 @@ public class SignEventListener implements Listener {
if (!Character.isDigit(fileName.charAt(x)))
break;
if (x == fileName.length() - 1) {
BooksWithoutBorders.loadList.put(player.getName(), BooksWithoutBorders.booksWithoutBorders.listFiles(player, true, true));
BooksWithoutBorders.loadList.put(player.getName(), FileHelper.listFiles(player, true, true));
isLoadListNumber = true;
}
}
@ -296,7 +296,7 @@ public class SignEventListener implements Listener {
if (!isLoadListNumber && sign.getLine(3).length() >= 2)
fileName = ChatColor.stripColor(sign.getLine(2)) + ChatColor.stripColor(sign.getLine(3));
ItemStack newBook = BooksWithoutBorders.booksWithoutBorders.loadBook(player, fileName, "true", "public");
ItemStack newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", "public");
if (newBook != null) {
player.getInventory().addItem(newBook);

View File

@ -11,6 +11,8 @@ import java.util.Objects;
import java.util.regex.Pattern;
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
/**
* Helper class for dealing with files
@ -85,6 +87,24 @@ public final class FileHelper {
return null;
}
/**
* Lists available files
*
* @param sender <p>The command sender looking for files</p>
* @param listPublic <p>Whether to list public or personal files</p>
* @param silent <p>Whether to just return the list without printing it</p>
* @return <p>A list of available files</p>
*/
public static List<String> listFiles(CommandSender sender, Boolean listPublic, boolean silent) {
File file;
if (listPublic) {
file = new File(getBookFolder());
} else {
file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash());
}
return FileHelper.listFiles(sender, file, silent);
}
/**
* Lists available files
*