Moves load and loadPublic to their own files, and removes some redundancy

This commit is contained in:
Kristian Knarvik 2021-08-31 18:45:18 +02:00
parent bc93ddcec5
commit db3679014d
5 changed files with 119 additions and 152 deletions

View File

@ -13,20 +13,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import net.knarcraft.bookswithoutborders.command.CommandCopy;
import net.knarcraft.bookswithoutborders.command.CommandDecrypt;
import net.knarcraft.bookswithoutborders.command.CommandDelete;
import net.knarcraft.bookswithoutborders.command.CommandEncrypt;
import net.knarcraft.bookswithoutborders.command.CommandGive;
import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt;
import net.knarcraft.bookswithoutborders.command.CommandSave;
import net.knarcraft.bookswithoutborders.command.CommandSavePublic;
import net.knarcraft.bookswithoutborders.command.CommandSetAuthor;
import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice;
import net.knarcraft.bookswithoutborders.command.CommandSetLore;
import net.knarcraft.bookswithoutborders.command.CommandSetTitle;
import net.knarcraft.bookswithoutborders.command.CommandUnSign;
import net.knarcraft.bookswithoutborders.command.GiveTabCompleter;
import net.knarcraft.bookswithoutborders.command.*;
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
@ -106,6 +93,8 @@ public class BooksWithoutBorders extends JavaPlugin {
registerCommand("save", new CommandSave(this), null);
registerCommand("setAuthor", new CommandSetAuthor(), null);
registerCommand("setTitle", new CommandSetTitle(), null);
registerCommand("load", new CommandLoad(this), null);
registerCommand("loadPublic", new CommandLoadPublic(this), null);
}
/**
@ -393,10 +382,12 @@ public class BooksWithoutBorders extends JavaPlugin {
sender.sendMessage(commandColor + "\nReload:" + successColor + " Reloads BwB's configuration file");
}
if (sender.hasPermission("bookswithoutborders.unsign"))
if (sender.hasPermission("bookswithoutborders.unsign")) {
sender.sendMessage("\n" + commandColor + "Unsign: " + successColor + "Un-signs the book the player is holding");
if (sender.hasPermission("bookswithoutborders.copy"))
}
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");
@ -421,10 +412,11 @@ public class BooksWithoutBorders extends JavaPlugin {
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"))
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.");
}
return true;
}
@ -461,109 +453,6 @@ public class BooksWithoutBorders extends JavaPlugin {
return true;
}
//Player only commands
if (sender instanceof Player player) {
if (args[0].equalsIgnoreCase("loadPublic")) {
if (!sender.hasPermission("bookswithoutborders.loadPublic")) {
sendErrorMessage(sender, " You don't have permission to use this command!");
return false;
}
if (((Player) sender).getInventory().firstEmpty() == -1) {
sendErrorMessage(sender, "You must have space in your inventory to load books!");
return false;
}
if (args.length == 1) {
loadList.put(sender.getName(), 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)
loadList.put(sender.getName(), listFiles(sender, true, true));
}
ItemStack newBook;
try {
if (args.length == 4)
newBook = loadBook(sender, cleanString(args[1]), args[3], "public", Integer.parseInt(args[2]));
else if (args.length == 3) {
if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("false"))
newBook = loadBook(sender, cleanString(args[1]), args[2], "public");
else
newBook = loadBook(sender, cleanString(args[1]), "true", "public", Integer.parseInt(args[2]));
} else
newBook = loadBook(sender, cleanString(args[1]), "true", "public");
if (newBook != null) {
((Player) sender).getInventory().addItem(newBook);
sendSuccessMessage(sender, "Book created!");
return true;
} else {
sendErrorMessage(sender, "Book failed to load!");
return false;
}
} catch (NumberFormatException e) {
sendErrorMessage(sender, "Invalid number of book copies specified!");
return false;
}
}
if (args[0].equalsIgnoreCase("load")) {
if (!sender.hasPermission("bookswithoutborders.load")) {
sendErrorMessage(sender, " You don't have permission to use this command!");
return false;
}
if (((Player) sender).getInventory().firstEmpty() == -1) {
sendErrorMessage(sender, "You must have space in your inventory to load books!");
return false;
}
if (args.length == 1) {
loadList.put(sender.getName(), listFiles(sender, false, 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)
loadList.put(sender.getName(), listFiles(sender, false, true));
}
ItemStack newBook;
try {
if (args.length == 4)
newBook = loadBook(sender, cleanString(args[1]), args[3], "player", Integer.parseInt(args[2]));
else if (args.length == 3) {
if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("false"))
newBook = loadBook(sender, cleanString(args[1]), args[2], "player");
else
newBook = loadBook(sender, cleanString(args[1]), "true", "player", Integer.parseInt(args[2]));
} else
newBook = loadBook(sender, cleanString(args[1]), "true", "player");
if (newBook != null) {
((Player) sender).getInventory().addItem(newBook);
sendSuccessMessage(sender, "Book created!");
return true;
} else {
sendErrorMessage(sender, "Book failed to load!");
return false;
}
} catch (NumberFormatException e) {
sendErrorMessage(sender, "Invalid number of book copies specified!");
return false;
}
}
}
if (args[0].equalsIgnoreCase("givePublic")) {
if (sender instanceof Player) {
if (!sender.hasPermission("bookswithoutborders.givePublic")) {

View File

@ -35,30 +35,28 @@ public class CommandGive implements CommandExecutor {
return true;
}
//Organize and parse input
String bookIdentifier = args[0];
String receivingPlayerName = args[1];
String copies = null;
String isSigned = null;
String copiesOrIsSigned = null;
String copies = "1";
String isSigned = "true";
if (args.length == 4) {
copies = args[2];
isSigned = args[3];
} else if (args.length == 3) {
copiesOrIsSigned = args[2];
}
for (int x = 0; x < bookIdentifier.length(); x++) {
if (!Character.isDigit(bookIdentifier.charAt(x))) {
break;
}
if (x == bookIdentifier.length() - 1) {
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, true));
if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("false")) {
isSigned = args[2];
} else {
copies = args[2];
}
}
ItemStack newBook;
//Load books available to the player
try {
Integer.parseInt(bookIdentifier);
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, true));
} catch (NumberFormatException ignored) {}
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
if (receivingPlayer == null) {
BooksWithoutBorders.sendErrorMessage(player, "Player not found!");
@ -70,21 +68,9 @@ public class CommandGive implements CommandExecutor {
return false;
}
//bwb give [book name] [player] [numCopies] [is signed]
String bookToLoad = booksWithoutBorders.cleanString(bookIdentifier);
try {
if (isSigned != null && copies != null) {
newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), isSigned, "player", Integer.parseInt(copies));
} else if (copiesOrIsSigned != null) {
if (copiesOrIsSigned.equalsIgnoreCase("true") || copiesOrIsSigned.equalsIgnoreCase("false")) {
newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), copiesOrIsSigned, "player");
} else {
newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), "true", "player", Integer.parseInt(copiesOrIsSigned));
}
} else {
newBook = booksWithoutBorders.loadBook(player, booksWithoutBorders.cleanString(bookIdentifier), "true", "player");
}
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, "player", Integer.parseInt(copies));
if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook);
BooksWithoutBorders.sendSuccessMessage(player, "Book sent!");

View File

@ -1,12 +1,89 @@
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;
public class CommandLoad implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandLoad(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
return loadBook(sender, args, "player", false);
}
/**
* Loads a stored book
* @param sender <p>The sender asking to load the book</p>
* @param args <p>The arguments given</p>
* @param directory <p>The directory to load from (public/player)</p>
* @param loadPublic <p>Whether to list public files as loadable</p>
* @return <p>True if the book was loaded successfully</p>
*/
boolean loadBook(CommandSender sender, String[] args, String directory, boolean loadPublic) {
if (!(sender instanceof Player player)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false;
}
if (player.getInventory().firstEmpty() == -1) {
BooksWithoutBorders.sendErrorMessage(player, "You must have space in your inventory to load books!");
return false;
}
int argumentCount = args.length;
//Show books available to the player
if (argumentCount == 0) {
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(player, loadPublic, false));
return true;
}
//Organize and parse input
String bookIdentifier = args[0];
String copies = "1";
String isSigned = "true";
if (args.length == 3) {
copies = args[1];
isSigned = args[2];
} else if (args.length == 2) {
if (args[1].equalsIgnoreCase("true") || args[1].equalsIgnoreCase("false")) {
isSigned = args[1];
} else {
copies = args[1];
}
}
//Load books available to the player
try {
Integer.parseInt(bookIdentifier);
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, loadPublic, true));
} catch (NumberFormatException ignored) {}
String bookToLoad = booksWithoutBorders.cleanString(bookIdentifier);
try {
//Give the new book if it cannot be loaded
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies));
if (newBook != null) {
player.getInventory().addItem(newBook);
BooksWithoutBorders.sendSuccessMessage(player, "Book created!");
return true;
} else {
BooksWithoutBorders.sendErrorMessage(player, "Book failed to load!");
return false;
}
} catch (NumberFormatException e) {
BooksWithoutBorders.sendErrorMessage(player, "Invalid number of book copies specified!");
return false;
}
}
}

View File

@ -1,12 +1,19 @@
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;
public class CommandLoadPublic implements CommandExecutor {
public class CommandLoadPublic extends CommandLoad implements CommandExecutor {
public CommandLoadPublic(BooksWithoutBorders booksWithoutBorders) {
super(booksWithoutBorders);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
return loadBook(sender, args, "public", true);
}
}

View File

@ -65,6 +65,14 @@ commands:
description: Sets the title of the held book
usage: /<command> <title>
permission: bookswithoutborders.settitle
load:
description: Loads a previously saved book from the holding player's folder
usage: /<command> <file name or number> [copies] [is signed (true/false)]
permission: bookswithoutborders.load
loadpublic:
description: Loads a previously saved book from the public books folder
usage: /<command> <file name or number> [copies] [is signed (true/false)]
permission: bookswithoutborders.loadpublic
permissions:
bookswithoutborders.admin:
description: Grants all permissions