Removes the use of string values for book directories

This commit is contained in:
2025-09-01 16:06:52 +02:00
parent 4fc201276f
commit 3e45cad29a
8 changed files with 19 additions and 57 deletions

View File

@@ -4,6 +4,7 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.config.translation.GiveMessage;
import net.knarcraft.bookswithoutborders.config.translation.Translatable;
import net.knarcraft.bookswithoutborders.gui.PagedBookIndex;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil;
import net.knarcraft.bookswithoutborders.utility.InputCleaningUtil;
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeUtil;
@@ -42,7 +43,7 @@ public class CommandGive implements TabExecutor {
return false;
}
return giveBook(sender, arguments, false, "player", label);
return giveBook(sender, arguments, false, BookDirectory.PLAYER, label);
}
/**
@@ -56,7 +57,7 @@ public class CommandGive implements TabExecutor {
* @return <p>True if the book was given successfully</p>
*/
boolean giveBook(@NotNull CommandSender sender, @NotNull String[] arguments, boolean givePublic,
@NotNull String folder, @NotNull String commandName) {
@NotNull BookDirectory folder, @NotNull String commandName) {
if (PagedBookIndex.displayPage(arguments, sender, givePublic, commandName)) {
return true;
}
@@ -135,7 +136,7 @@ public class CommandGive implements TabExecutor {
* @throws NumberFormatException <p>If the number of copies given is not a number</p>
*/
private boolean parseArgumentsAndContinue(@NotNull String[] arguments, @NotNull CommandSender sender,
@NotNull String directory, boolean givePublic) {
@NotNull BookDirectory directory, boolean givePublic) {
int argumentCount = arguments.length;
//Organize and parse input
String bookIdentifier;
@@ -192,7 +193,7 @@ public class CommandGive implements TabExecutor {
* @return <p>True if the book was successfully given</p>
*/
private boolean loadAndGiveBook(@NotNull String bookIdentifier, @NotNull CommandSender sender,
@NotNull Player receivingPlayer, boolean isSigned, @NotNull String folder,
@NotNull Player receivingPlayer, boolean isSigned, @NotNull BookDirectory folder,
@NotNull String copies) throws NumberFormatException {
String bookToLoad = InputCleaningUtil.cleanString(bookIdentifier);
ItemStack newBook = BookLoaderUtil.loadBook(sender, bookToLoad, isSigned, folder, Integer.parseInt(copies));

View File

@@ -1,5 +1,6 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
@@ -16,7 +17,7 @@ public class CommandGivePublic extends CommandGive implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
return giveBook(sender, arguments, true, "public", label);
return giveBook(sender, arguments, true, BookDirectory.PUBLIC, label);
}
@Override

View File

@@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.config.translation.Translatable;
import net.knarcraft.bookswithoutborders.gui.PagedBookIndex;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil;
import net.knarcraft.bookswithoutborders.utility.InputCleaningUtil;
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeUtil;
@@ -31,7 +32,7 @@ public class CommandLoad implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
return loadBook(sender, arguments, "player", false, label);
return loadBook(sender, arguments, BookDirectory.PLAYER, false, label);
}
/**
@@ -44,7 +45,7 @@ public class CommandLoad implements TabExecutor {
* @param commandName <p>The name of the command calling this method</p>
* @return <p>True if the book was loaded successfully</p>
*/
public boolean loadBook(@NotNull CommandSender sender, @NotNull String[] arguments, @NotNull String directory,
public boolean loadBook(@NotNull CommandSender sender, @NotNull String[] arguments, @NotNull BookDirectory directory,
boolean loadPublic, @NotNull String commandName) {
if (!(sender instanceof Player player)) {
new FormatBuilder(Translatable.ERROR_PLAYER_ONLY).error(sender);
@@ -120,7 +121,7 @@ public class CommandLoad implements TabExecutor {
* @throws NumberFormatException <p>If the number of copies given is not a number</p>
*/
private boolean parseArgumentsAndContinue(@NotNull String[] arguments, @NotNull Player player,
@NotNull String directory, boolean loadPublic) {
@NotNull BookDirectory directory, boolean loadPublic) {
int argumentCount = arguments.length;
String bookIdentifier = arguments[0];
int copies = 1;
@@ -158,7 +159,7 @@ public class CommandLoad implements TabExecutor {
* @return <p>The loaded book(s)</p>
*/
private boolean loadBook(@NotNull Player player, boolean loadPublic, @NotNull String bookIdentifier,
boolean isSigned, @NotNull String directory, int copies) {
boolean isSigned, @NotNull BookDirectory directory, int copies) {
BooksWithoutBorders.updateBooks(player, loadPublic);
String bookToLoad = InputCleaningUtil.cleanString(bookIdentifier);

View File

@@ -1,5 +1,6 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@@ -15,7 +16,7 @@ public class CommandLoadPublic extends CommandLoad implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
return loadBook(sender, arguments, "public", true, label);
return loadBook(sender, arguments, BookDirectory.PUBLIC, true, label);
}
@Override

View File

@@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.listener;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.config.BwBConfig;
import net.knarcraft.bookswithoutborders.config.StaticMessage;
import net.knarcraft.bookswithoutborders.state.BookDirectory;
import net.knarcraft.bookswithoutborders.utility.BookLoaderUtil;
import net.knarcraft.bookswithoutborders.utility.InputCleaningUtil;
import net.knarcraft.knarlib.formatting.FormatBuilder;
@@ -60,7 +61,7 @@ public class PlayerEventListener implements Listener {
if (!bookName.trim().isEmpty()) {
//Give the book to the player if it exists
ItemStack newBook = BookLoaderUtil.loadBook(player, bookName, true, "public");
ItemStack newBook = BookLoaderUtil.loadBook(player, bookName, true, BookDirectory.PUBLIC);
if (newBook != null) {
player.getInventory().addItem(newBook);
}

View File

@@ -270,7 +270,7 @@ public class SignEventListener implements Listener {
fileName += InputCleaningUtil.stripColor(thirdLine);
}
ItemStack newBook = BookLoaderUtil.loadBook(player, fileName, true, "public");
ItemStack newBook = BookLoaderUtil.loadBook(player, fileName, true, BookDirectory.PUBLIC);
if (newBook != null) {
player.getInventory().addItem(newBook);

View File

@@ -1,8 +1,5 @@
package net.knarcraft.bookswithoutborders.state;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* This enum represents the different directories books can be saved in
*/
@@ -23,25 +20,6 @@ public enum BookDirectory {
*/
ENCRYPTED;
/**
* Gets the relevant book directory given a directory name
*
* @param directory <p>The directory to transform</p>
* @return <p>A book directory, or null if the given directory is empty</p>
*/
@Nullable
public static BookDirectory getFromString(@NotNull String directory) {
if (directory.equalsIgnoreCase("public")) {
return BookDirectory.PUBLIC;
} else if (directory.equalsIgnoreCase("player")) {
return BookDirectory.PLAYER;
} else if (!directory.trim().isEmpty()) {
return BookDirectory.ENCRYPTED;
} else {
return null;
}
}
/**
* Gets the common public/player directory based on the input
*

View File

@@ -19,7 +19,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* A utility class for loading books, and taking required cost
* A utility class for loading books, and taking the required cost from players
*/
public final class BookLoaderUtil {
@@ -37,31 +37,10 @@ public final class BookLoaderUtil {
*/
@Nullable
public static ItemStack loadBook(@NotNull CommandSender sender, @NotNull String fileName, boolean isSigned,
@NotNull String directory) {
@NotNull BookDirectory directory) {
return loadBook(sender, fileName, isSigned, directory, 1);
}
/**
* Loads the given book
*
* @param sender <p>The command sender trying to load the book</p>
* @param fileName <p>The index or file name of the book to load</p>
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
* @param directory <p>The directory to save the book in</p>
* @param numCopies <p>The number of copies to load</p>
* @return <p>The loaded book</p>
*/
@Nullable
public static ItemStack loadBook(@NotNull CommandSender sender, @NotNull String fileName, boolean isSigned,
@NotNull String directory, int numCopies) {
BookDirectory bookDirectory = BookDirectory.getFromString(directory);
if (bookDirectory == null) {
new FormatBuilder(Translatable.ERROR_BOOK_DIRECTORY_UNKNOWN).error(sender);
return null;
}
return loadBook(sender, fileName, isSigned, bookDirectory, numCopies);
}
/**
* Loads the given book
*