Finishes moving commands to separate classes

This commit is contained in:
2021-09-01 19:20:09 +02:00
parent db4f080ac8
commit 6c897b4ddf
14 changed files with 542 additions and 431 deletions

View File

@@ -1,12 +1,150 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.bookPriceQuantity;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.bookPriceType;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.commandColor;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendErrorMessage;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendSuccessMessage;
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.successColor;
/**
* Command executor for the books without borders (bwb) command
*/
public class CommandBooksWithoutBorders implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandBooksWithoutBorders(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
if (sender instanceof Player) {
showPlayerCommands(sender);
} else {
showConsoleCommands(sender);
}
return true;
}
/**
* Shows all commands available to the console
* @param sender <p>The console which sent the command</p>
*/
private void showConsoleCommands(CommandSender sender) {
sender.sendMessage(commandColor + "Use: /bwb [Command]");
sender.sendMessage(commandColor + "[] denote parameters");
sender.sendMessage(commandColor + "Commands:");
sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's config file");
sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [true/false]: " + successColor);
sendSuccessMessage(sender, "Gives the selected player a book from the public directory");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Deletes the specified");
sendSuccessMessage(sender, "file in the public directory");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
}
/**
* Shows all commands available to the sending player
* @param sender <p>The player which sent the command</p>
*/
private void showPlayerCommands(CommandSender sender) {
//Lists all commands
sender.sendMessage(commandColor + "Use: /bwb [Command]");
sender.sendMessage(commandColor + "[] denote parameters");
if (booksWithoutBorders.booksHavePrice()) {
if (bookPriceType != Material.AIR) {
sendErrorMessage(sender, "[" + (int) bookPriceQuantity + " " + bookPriceType.toString() + "(s) are required to create a book]");
} else {
sendErrorMessage(sender, "[" + BooksWithoutBorders.eco.format(bookPriceQuantity) + " is required to create a book]");
}
}
sender.sendMessage(commandColor + "Commands:");
if (sender.hasPermission("bookswithoutborders.load")) {
sender.sendMessage(commandColor + "\nLoad [file name or number] [# of copies] [true/false]:");
sendSuccessMessage(sender, "Creates a book from the specified file and gives it to the player");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
sendSuccessMessage(sender, "If true is specified the book will be signed, if false it will be");
sendSuccessMessage(sender, "unsigned");
}
if (sender.hasPermission("bookswithoutborders.loadPublic")) {
sender.sendMessage(commandColor + "loadPublic [file name or number] [# of copies] [true/false]:");
sendSuccessMessage(sender, "Same as Load, but views files in the public directory");
}
if (sender.hasPermission("bookswithoutborders.save")) {
sender.sendMessage("\n" + commandColor + "Save [true/false]: " + successColor + "Saves the book the player is");
sendSuccessMessage(sender, "holding to a text file in a private directory");
sendSuccessMessage(sender, "If true is specified, a book of the same name by the same");
sendSuccessMessage(sender, "author will be overwritten by the new book");
}
if (sender.hasPermission("bookswithoutborders.savePublic")) {
sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,");
sendSuccessMessage(sender, "but saves files in the public directory");
}
if (sender.hasPermission("bookswithoutborders.give")) {
sender.sendMessage("\n" + commandColor + "Give [file name or number] [player name] [# of copies] [true/false]:");
sendSuccessMessage(sender, "Gives the selected player a book from your personal directory");
}
if (sender.hasPermission("bookswithoutborders.givePublic")) {
sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [# of copies] [true/false]:");
sendSuccessMessage(sender, "Same as give, but uses books from the public directory");
}
if (sender.hasPermission("bookswithoutborders.delete")) {
sender.sendMessage(commandColor + "\nDelete [file name or number]: " + successColor + "Deletes the specified");
sendSuccessMessage(sender, "file in the player's directory");
sendSuccessMessage(sender, "If no file is specified, a list of available files is returned");
}
if (sender.hasPermission("bookswithoutborders.admin")) {
sender.sendMessage(commandColor + "deletePublic [file name or number]: " + successColor + "Same as Delete,");
sendSuccessMessage(sender, "but deletes files in the public directory");
sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's configuration file");
}
if (sender.hasPermission("bookswithoutborders.unsign")) {
sender.sendMessage("\n" + commandColor + "Unsign: " + successColor + "Un-signs the book the player is holding");
}
if (sender.hasPermission("bookswithoutborders.copy")) {
sender.sendMessage("\n" + commandColor + "Copy [number of copies]: " + successColor + "Copies the book the player is holding");
}
if (sender.hasPermission("bookswithoutborders.encrypt")) {
sender.sendMessage("\n" + commandColor + "Encrypt [key] [style]: " + successColor + "Encrypts the book the player is holding");
sender.sendMessage(commandColor + "[key]" + successColor + " is required and can be any phrase or number excluding spaces");
sender.sendMessage(commandColor + "[style]" + successColor + " is not required. Possible values are \"DNA\" or \"Magic\"");
}
if (sender.hasPermission("bookswithoutborders.groupEncrypt")) {
sender.sendMessage("\n" + commandColor + "groupEncrypt [group name] [key] [style]: " + successColor + "Encrypts book so that only players with the" +
"\n bookswithoutborders.decrypt." + commandColor + "[group name]" + successColor + " permission may decrypt the book by holding and left clicking the book");
}
if (sender.hasPermission("bookswithoutborders.decrypt")) {
sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding");
sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book");
}
if (sender.hasPermission("bookswithoutborders.setTitle")) {
sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding");
}
if (sender.hasPermission("bookswithoutborders.setAuthor")) {
sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding");
}
if (sender.hasPermission("bookswithoutborders.setLore")) {
sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding");
sendSuccessMessage(sender, "Insert the lore_line_separator character to force a new line\n[\"~\" by default]");
}
if (sender.hasPermission("bookswithoutborders.setBookPrice")) {
sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." +
"\nIf [Item], the item in the player's hand in the amount of [quantity] will be the price.\nIf [Eco], a Vault based economy will be used for price." +
"\nIf neither [Item/Eco] or [quantity] are specified the current price to create books will be removed.");
}
}
}

View File

@@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@@ -45,7 +46,7 @@ public class CommandCopy implements CommandExecutor {
int copies = Integer.parseInt(args[0]);
if (copies > 0) {
if (BooksWithoutBorders.authorOnlyCopy && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyCopy")) {
if (!booksWithoutBorders.isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta())))
if (!isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta())))
return false;
}
@@ -70,4 +71,22 @@ public class CommandCopy implements CommandExecutor {
return true;
}
/**
* Checks whether the given player is the author of a given book
*
* @param player <p>The player to check</p>
* @param book <p>The book to check</p>
* @return <p>True if the player is the book's author</p>
*/
private boolean isAuthor(Player player, BookMeta book) {
String author = book.getAuthor();
String playerName = InputCleaningHelper.cleanString(player.getName());
if (author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author))) {
return true;
}
BooksWithoutBorders.sendErrorMessage(player, "You must be the author of this book to use this command!");
return false;
}
}

View File

@@ -71,7 +71,7 @@ public class CommandDecrypt implements CommandExecutor {
if (!key.equalsIgnoreCase("")) {
//Decrypt the book
ItemStack book = booksWithoutBorders.eLoad(player, key, false);
ItemStack book = booksWithoutBorders.loadEncryptedBook(player, key, false);
if (book != null) {
InventoryHelper.setHeldWrittenBook(player, book);
BooksWithoutBorders.sendSuccessMessage(player, "Book auto-decrypted!");
@@ -91,7 +91,7 @@ public class CommandDecrypt implements CommandExecutor {
String key = EncryptionHelper.getNumberKeyFromStringKey(args[0]);
//Decrypt the book
ItemStack book = booksWithoutBorders.eLoad(player, key, true);
ItemStack book = booksWithoutBorders.loadEncryptedBook(player, key, true);
if (book != null) {
InventoryHelper.setHeldWrittenBook(player, book);
BooksWithoutBorders.sendSuccessMessage(player, "Book decrypted!");

View File

@@ -1,11 +1,15 @@
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;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.File;
/**
* Command executor for the delete command
*/
@@ -47,7 +51,7 @@ public class CommandDelete implements CommandExecutor {
return false;
} else {
if (!BooksWithoutBorders.loadList.get(sender.getName()).isEmpty()) {
booksWithoutBorders.deleteBook(sender, args[0], deletePublic);
performBookDeletion(sender, args[0], deletePublic);
return true;
} else {
BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!");
@@ -59,4 +63,49 @@ public class CommandDelete implements CommandExecutor {
return false;
}
/**
* Deletes the given book
*
* @param sender <p>The sender of the command to delete the book</p>
* @param fileName <p>The file name of the book</p>
* @param isPublic <p>Whether the book to delete is public or not</p>
*/
public void performBookDeletion(CommandSender sender, String fileName, Boolean isPublic) {
//If the file name is an index of the load list, load the book
try {
int loadListIndex = Integer.parseInt(fileName);
String senderName = sender.getName();
if (BooksWithoutBorders.loadList.containsKey(senderName) && loadListIndex <= BooksWithoutBorders.loadList.get(senderName).size()) {
fileName = BooksWithoutBorders.loadList.get(senderName).get(loadListIndex - 1);
}
} catch (NumberFormatException ignored) {
}
//Get the file to be deleted
File file;
if (isPublic) {
file = FileHelper.getBookFile(BooksWithoutBorders.bookFolder + fileName);
} else {
file = FileHelper.getBookFile(BooksWithoutBorders.bookFolder +
InputCleaningHelper.cleanString(sender.getName()) + BooksWithoutBorders.SLASH + fileName);
}
//Send message if no such file could be found
if (file == null) {
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect file name!");
return;
}
//Try to delete the file
try {
if (file.delete()) {
BooksWithoutBorders.sendSuccessMessage(sender, "\"" + fileName + "\" deleted successfully");
} else {
BooksWithoutBorders.sendErrorMessage(sender, "Deletion failed without an exception!");
}
} catch (Exception e) {
BooksWithoutBorders.sendErrorMessage(sender, "Deletion failed!");
}
}
}

View File

@@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@@ -68,7 +69,7 @@ public class CommandGive implements CommandExecutor {
return false;
}
String bookToLoad = booksWithoutBorders.cleanString(bookIdentifier);
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
try {
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, "player", Integer.parseInt(copies));
if (newBook != null) {

View File

@@ -1,12 +1,87 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
/**
* Command executor for the give public command
*/
public class CommandGivePublic implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandGivePublic(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
if (sender instanceof Player) {
if (!sender.hasPermission("bookswithoutborders.givePublic")) {
BooksWithoutBorders.sendErrorMessage(sender, " You don't have permission to use this command!");
return false;
}
}
if (args.length == 2 || args.length > 5) {
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
return false;
}
if (args.length == 1) {
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, true, false));
return true;
}
for (int x = 0; x < args[1].length(); x++) {
if (!Character.isDigit(args[1].charAt(x)))
break;
if (x == args[1].length() - 1)
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, true, true));
}
ItemStack newBook;
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(args[2]);
if (receivingPlayer == null) {
BooksWithoutBorders.sendErrorMessage(sender, "Player not found!");
return false;
}
if (receivingPlayer.getInventory().firstEmpty() == -1) {
BooksWithoutBorders.sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!");
return false;
}
//bwb give [book name] [player] [numCopies] [is signed]
try {
if (args.length == 5)
newBook = booksWithoutBorders.loadBook(sender, cleanString(args[1]), args[4], "public", Integer.parseInt(args[3]));
else if (args.length == 4) {
if (args[3].equalsIgnoreCase("true") || args[3].equalsIgnoreCase("false"))
newBook = booksWithoutBorders.loadBook(sender, cleanString(args[1]), args[3], "public");
else
newBook = booksWithoutBorders.loadBook(sender, cleanString(args[1]), "true", "public", Integer.parseInt(args[3]));
} else
newBook = booksWithoutBorders.loadBook(sender, cleanString(args[1]), "true", "public");
if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook);
BooksWithoutBorders.sendSuccessMessage(sender, "Book sent!");
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
return true;
} else {
BooksWithoutBorders.sendErrorMessage(sender, "Book failed to load!");
return false;
}
} catch (NumberFormatException e) {
BooksWithoutBorders.sendErrorMessage(sender, "Invalid number of book copies specified!");
return false;
}
}
}

View File

@@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@@ -71,7 +72,7 @@ public class CommandLoad implements CommandExecutor {
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, loadPublic, true));
} catch (NumberFormatException ignored) {}
String bookToLoad = booksWithoutBorders.cleanString(bookIdentifier);
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
try {
//Give the new book if it cannot be loaded
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies));

View File

@@ -0,0 +1,30 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
/**
* Command executor for the reload command
*/
public class CommandReload implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandReload(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (booksWithoutBorders.loadConfig() && booksWithoutBorders.loadExistingPlayers()) {
BooksWithoutBorders.sendSuccessMessage(sender, "BooksWithoutBorders configuration reloaded!");
} else {
BooksWithoutBorders.sendErrorMessage(sender, "Reload Failed!");
BooksWithoutBorders.sendErrorMessage(sender, "See console for details");
}
return true;
}
}

View File

@@ -3,22 +3,19 @@ package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.state.ItemSlot;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
/**
* Command executor for the unsign command
*/
public class CommandUnSign implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandUnSign(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
@@ -33,7 +30,24 @@ public class CommandUnSign implements CommandExecutor {
//Find which hand the player is using to hold the book. If holding one in each, throw an error
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, true, true);
booksWithoutBorders.unSignHeldBook(player, holdingSlot == ItemSlot.MAIN_HAND);
unSignHeldBook(player, holdingSlot == ItemSlot.MAIN_HAND);
return true;
}
/**
* Un-signs the player's currently held book
*
* @param player <p>The player holding the book</p>
* @param mainHand <p>Whether the player is holding the book in its main hand or its off hand</p>
*/
public void unSignHeldBook(Player player, boolean mainHand) {
//Get the old book
BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand);
//UnSign the book
ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK);
newBook.setItemMeta(oldBook);
InventoryHelper.replaceHeldItem(player, newBook, mainHand);
}
}

View File

@@ -11,6 +11,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Tab completer for the save command
*/
public class GiveTabCompleter implements TabCompleter {
final BooksWithoutBorders booksWithoutBorders;