Moves load and loadPublic to their own files, and removes some redundancy
This commit is contained in:
@@ -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!");
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user