Removes redundancy between give and givepublic
This commit is contained in:
parent
8520a4818b
commit
b021cc3471
@ -21,18 +21,22 @@ public class CommandGive implements CommandExecutor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return giveBook(sender, args, false, "player");
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean giveBook(CommandSender sender, String[] args, boolean givePublic, String folder) {
|
||||||
if (args.length == 1 || args.length > 4) {
|
if (args.length == 1 || args.length > 4) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Incorrect number of arguments for this command!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, false));
|
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, false));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,34 +59,34 @@ public class CommandGive implements CommandExecutor {
|
|||||||
//Load books available to the player
|
//Load books available to the player
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(bookIdentifier);
|
Integer.parseInt(bookIdentifier);
|
||||||
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, true));
|
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, givePublic, true));
|
||||||
} catch (NumberFormatException ignored) {}
|
} catch (NumberFormatException ignored) {}
|
||||||
|
|
||||||
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
|
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
|
||||||
if (receivingPlayer == null) {
|
if (receivingPlayer == null) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Player not found!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Player not found!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Receiving player must have space in their inventory to receive books!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
||||||
try {
|
try {
|
||||||
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, "player", Integer.parseInt(copies));
|
ItemStack newBook = booksWithoutBorders.loadBook(sender, bookToLoad, isSigned, folder, Integer.parseInt(copies));
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
receivingPlayer.getInventory().addItem(newBook);
|
receivingPlayer.getInventory().addItem(newBook);
|
||||||
BooksWithoutBorders.sendSuccessMessage(player, "Book sent!");
|
BooksWithoutBorders.sendSuccessMessage(sender, "Book sent!");
|
||||||
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
|
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Book failed to load!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Book failed to load!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Invalid number of book copies specified!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Invalid number of book copies specified!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,84 +4,19 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
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
|
* Command executor for the give public command
|
||||||
*/
|
*/
|
||||||
public class CommandGivePublic implements CommandExecutor {
|
public class CommandGivePublic extends CommandGive implements CommandExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders;
|
|
||||||
|
|
||||||
public CommandGivePublic(BooksWithoutBorders booksWithoutBorders) {
|
public CommandGivePublic(BooksWithoutBorders booksWithoutBorders) {
|
||||||
this.booksWithoutBorders = booksWithoutBorders;
|
super(booksWithoutBorders);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
if (sender instanceof Player) {
|
return giveBook(sender, args, true, "public");
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ commands:
|
|||||||
permission: bookswithoutborders.give
|
permission: bookswithoutborders.give
|
||||||
givepublic:
|
givepublic:
|
||||||
description: Gives a public book to a player
|
description: Gives a public book to a player
|
||||||
usage: /<command>
|
usage: /<command> <file name or number> <playername> [# of copies (num)] [signed (true/false)]
|
||||||
permission: bookswithoutborders.givepublic
|
permission: bookswithoutborders.givepublic
|
||||||
decrypt:
|
decrypt:
|
||||||
description: Decrypts the held book
|
description: Decrypts the held book
|
||||||
|
Loading…
Reference in New Issue
Block a user