Moves the delete public command to its own file

This commit is contained in:
2021-08-31 19:05:28 +02:00
parent db3679014d
commit db4f080ac8
6 changed files with 42 additions and 41 deletions

View File

@@ -19,32 +19,43 @@ public class CommandDelete implements CommandExecutor {
@Override
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!");
return false;
}
return deleteBook(sender, args, false);
}
/**
* Deletes a book
* @param sender <p>The sender trying to delete the book</p>
* @param args <p>The arguments given</p>
* @param deletePublic <p>Whether to delete a public book</p>
* @return <p>True if the book was deleted successfully</p>
*/
boolean deleteBook(CommandSender sender, String[] args, boolean deletePublic) {
//List deletable files
if (args.length == 0) {
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, false));
BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, deletePublic, false));
return true;
}
//Delete the file
if (args.length == 1) {
if (!BooksWithoutBorders.loadList.containsKey(player.getName())) {
BooksWithoutBorders.sendErrorMessage(player, "You must first use /bwb delete to create a list of delete-able files!");
if (!BooksWithoutBorders.loadList.containsKey(sender.getName())) {
BooksWithoutBorders.sendErrorMessage(sender, "You must first use /bwb delete to create a list of delete-able files!");
return false;
} else {
if (!BooksWithoutBorders.loadList.get(player.getName()).isEmpty()) {
booksWithoutBorders.deleteBook(player, args[0], false);
if (!BooksWithoutBorders.loadList.get(sender.getName()).isEmpty()) {
booksWithoutBorders.deleteBook(sender, args[0], deletePublic);
return true;
} else {
BooksWithoutBorders.sendErrorMessage(player, "No files available to delete!");
BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!");
return false;
}
}
}
BooksWithoutBorders.sendErrorMessage(player, "Incorrect number of arguments for this command!");
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
return false;
}

View File

@@ -1,12 +1,22 @@
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 CommandDeletePublic implements CommandExecutor {
/**
* Command executor for the "delete public" command
*/
public class CommandDeletePublic extends CommandDelete implements CommandExecutor {
public CommandDeletePublic(BooksWithoutBorders booksWithoutBorders) {
super(booksWithoutBorders);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
return deleteBook(sender, args, true);
}
}

View File

@@ -7,6 +7,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* Command executor for the load command
*/
public class CommandLoad implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;

View File

@@ -5,6 +5,9 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
/**
* Command executor for the load public command
*/
public class CommandLoadPublic extends CommandLoad implements CommandExecutor {
public CommandLoadPublic(BooksWithoutBorders booksWithoutBorders) {