Moves the delete public command to its own file

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

View File

@ -84,6 +84,7 @@ public class BooksWithoutBorders extends JavaPlugin {
registerCommand("decrypt", new CommandDecrypt(this), null);
registerCommand("groupEncrypt", new CommandGroupEncrypt(this), null);
registerCommand("delete", new CommandDelete(this), null);
registerCommand("deletePublic", new CommandDeletePublic(this), null);
registerCommand("copy", new CommandCopy(this), null);
registerCommand("unSign", new CommandUnSign(this), null);
registerCommand("encrypt", new CommandEncrypt(this), null);
@ -516,37 +517,6 @@ public class BooksWithoutBorders extends JavaPlugin {
}
}
if (args[0].equalsIgnoreCase("deletePublic")) {
if (sender instanceof Player) {
if (!sender.hasPermission("bookswithoutborders.admin")) {
sendErrorMessage(sender, " You don't have permission to use this command!");
return false;
}
}
//lists delete-able files
if (args.length == 1) {
loadList.put(sender.getName(), listFiles(sender, true, false));
return true;
}
//actual deletion
if (args.length == 2) {
if (!loadList.containsKey(sender.getName())) {
sendErrorMessage(sender, "You must first use /bwb deletePublic to create a list of delete-able files!");
return false;
} else {
if (!loadList.get(sender.getName()).isEmpty()) {
deleteBook((sender), args[1], true);
return true;
} else {
sendErrorMessage(sender, "No files available to delete!");
return false;
}
}
}
}
sendErrorMessage(sender, "Command not recognized! Use " + commandColor + "/BwB" + errorColor + " for more info!");
}
return false;

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) {

View File

@ -29,6 +29,10 @@ commands:
description: Deletes a book saved by the executing user
usage: /<command> <file name or number>
permission: bookswithoutborders.delete
deletepublic:
description: Deletes a public book
usage: /<command> <file name of number>
permission: bookswithoutborders.admin
copy:
description: Copies the held book
usage: /<command> <copies>