Starts work on making the commands more modern

Adds classes for all commands
Implements command class and tab completer for the give command
Performs more code cleanup
This commit is contained in:
2021-08-30 17:03:58 +02:00
parent 6efbd377ee
commit 504ef09192
19 changed files with 598 additions and 142 deletions

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandBooksWithoutBorders implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandCopy implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,99 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
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;
import org.bukkit.inventory.meta.BookMeta;
import java.io.File;
public class CommandDecrypt implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandDecrypt(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false;
}
if (!InventoryHelper.notHoldingOneWrittenBookCheck(player,
"You must be holding a written book to decrypt it!",
"You cannot decrypt two books at once!")) {
return false;
}
ItemStack heldItem = InventoryHelper.getHeldBook(player);
BookMeta bookMetadata = (BookMeta) heldItem.getItemMeta();
if (bookMetadata == null) {
BooksWithoutBorders.sendErrorMessage(player, "Your book seems to be corrupt!");
return false;
}
//Warning: admin decrypt only allows decrypting files created by the same player. Not sure if intended
if (args.length == 0 && BooksWithoutBorders.adminDecrypt && player.hasPermission("bookswithoutborders.admin")) {
String path = BooksWithoutBorders.bookFolder + "Encrypted" + BooksWithoutBorders.SLASH;
String fileName;
if (bookMetadata.hasTitle()) {
fileName = bookMetadata.getTitle() + BooksWithoutBorders.titleAuthorSeparator + bookMetadata.getAuthor();
} else {
fileName = "Untitled," + player.getName();
}
File encryptedDirectory = new File(path);
String[] encryptedFiles = encryptedDirectory.list();
if (encryptedFiles == null) {
return false;
}
//Get the "encryption key" from the filename
String key = "";
for (String encryptedFile : encryptedFiles) {
if (encryptedFile.contains(fileName)) {
key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]"));
break;
}
}
if (!key.equalsIgnoreCase("")) {
//Decrypt the book
ItemStack book = booksWithoutBorders.eLoad(player, key, false);
if (book != null) {
InventoryHelper.setHeldBook(player, book);
BooksWithoutBorders.sendSuccessMessage(player, "Book auto-decrypted!");
return true;
} else {
return false;
}
} else {
BooksWithoutBorders.sendErrorMessage(player, "No matching encrypted book found!");
return false;
}
} else if (args.length == 0) {
BooksWithoutBorders.sendErrorMessage(player, "No decryption password given!");
return false;
}
String key = EncryptionHelper.getNumberKeyFromStringKey(args[0]);
//Decrypt the book
ItemStack book = booksWithoutBorders.eLoad(player, key, true);
if (book != null) {
InventoryHelper.setHeldBook(player, book);
BooksWithoutBorders.sendSuccessMessage(player, "Book decrypted!");
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandDelete implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandDeletePublic implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandEncrypt implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,100 @@
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 CommandGive implements CommandExecutor {
private final BooksWithoutBorders booksWithoutBorders;
public CommandGive(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false;
}
if (args.length == 1 || args.length > 4) {
BooksWithoutBorders.sendErrorMessage(player, "Incorrect number of arguments for this command!");
return false;
}
if (args.length == 0) {
BooksWithoutBorders.loadList.put(player.getName(), booksWithoutBorders.listFiles(player, false, false));
return true;
}
String bookIdentifier = args[0];
String receivingPlayerName = args[1];
String copies = null;
String isSigned = null;
String copiesOrIsSigned = null;
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));
}
}
ItemStack newBook;
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
if (receivingPlayer == null) {
BooksWithoutBorders.sendErrorMessage(player, "Player not found!");
return false;
}
if (receivingPlayer.getInventory().firstEmpty() == -1) {
BooksWithoutBorders.sendErrorMessage(player, "Receiving player must have space in their inventory to receive books!");
return false;
}
//bwb give [bookname] [player] [numCopies] [issigned]
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");
}
if (newBook != null) {
receivingPlayer.getInventory().addItem(newBook);
BooksWithoutBorders.sendSuccessMessage(player, "Book sent!");
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
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

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandGivePublic implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandGroupEncrypt implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandLoad implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandLoadPublic implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandSave implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandSavePublic implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandSetBookPrice implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,12 @@
package net.knarcraft.bookswithoutborders.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CommandUnsign implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
}

View File

@ -0,0 +1,75 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GiveTabCompleter implements TabCompleter {
BooksWithoutBorders booksWithoutBorders;
public GiveTabCompleter(BooksWithoutBorders booksWithoutBorders) {
this.booksWithoutBorders = booksWithoutBorders;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
Server server = booksWithoutBorders.getServer();
List<String> playerNames = new ArrayList<>();
for (Player player : server.getOnlinePlayers()) {
playerNames.add(player.getName());
}
List<String> booleans = new ArrayList<>();
booleans.add("true");
booleans.add("false");
List<String> numbers = new ArrayList<>();
numbers.add("1");
numbers.add("2");
numbers.add("3");
numbers.add("4");
numbers.add("5");
List<String> boolAndNumbers = new ArrayList<>();
boolAndNumbers.addAll(booleans);
boolAndNumbers.addAll(numbers);
int argumentCount = args.length;
if (argumentCount > 2) {
//Don't continue with autocompletion if the recipient is invalid
if (server.getPlayer(args[1]) == null) {
return null;
}
}
BooksWithoutBorders.sendErrorMessage(sender, Arrays.toString(args));
BooksWithoutBorders.sendErrorMessage(sender, String.valueOf(argumentCount));
if (argumentCount == 1) {
//Return list of books
return BooksWithoutBorders.loadList.put(sender.getName(), booksWithoutBorders.listFiles(sender, false, true));
} else if (argumentCount == 2) {
//Return online players
return playerNames;
} else if (argumentCount == 3) {
//Number of copies
return boolAndNumbers;
} else if (argumentCount == 4) {
//Signed
try {
Integer.parseInt(args[2]);
return booleans;
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
}