Replaces static strings for some commands
This commit is contained in:
@@ -56,35 +56,38 @@ public class CommandEncrypt implements TabExecutor {
|
||||
@Nullable
|
||||
protected BookMeta performPreChecks(@NotNull CommandSender sender, @NotNull String[] arguments,
|
||||
int necessaryArguments, @NotNull String missingArgumentsError) {
|
||||
StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter();
|
||||
if (!(sender instanceof Player player)) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_PLAYER_ONLY);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (InventoryHelper.notHoldingOneWrittenBookCheck(player,
|
||||
"You must be holding a written book to encrypt it!",
|
||||
"You cannot encrypt two books at once!")) {
|
||||
stringFormatter.replacePlaceholder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK, "{action}",
|
||||
stringFormatter.getUnFormattedColoredMessage(Translatable.ACTION_ENCRYPT)),
|
||||
stringFormatter.replacePlaceholder(Translatable.ERROR_ONLY_ONE_BOOK, "{action}",
|
||||
stringFormatter.getUnFormattedColoredMessage(Translatable.ACTION_ENCRYPT)))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int argumentCount = arguments.length;
|
||||
if (argumentCount < necessaryArguments) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, missingArgumentsError);
|
||||
stringFormatter.displayErrorMessage(player, missingArgumentsError);
|
||||
return null;
|
||||
}
|
||||
if (argumentCount > necessaryArguments + 1) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Too many command options specified!");
|
||||
stringFormatter.displayErrorMessage(player, Translatable.ERROR_TOO_MANY_ARGUMENTS_COMMAND);
|
||||
return null;
|
||||
}
|
||||
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||
BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta();
|
||||
if (bookMetadata == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Your book seems to be corrupt!");
|
||||
stringFormatter.displayErrorMessage(player, Translatable.ERROR_METADATA_MISSING);
|
||||
return null;
|
||||
}
|
||||
if (!bookMetadata.hasPages()) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Book must have contents to encrypt!");
|
||||
stringFormatter.displayErrorMessage(player, Translatable.ERROR_ENCRYPT_EMPTY);
|
||||
return null;
|
||||
}
|
||||
return bookMetadata;
|
||||
|
@@ -1,8 +1,10 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.config.Translatable;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
import net.knarcraft.knarlib.formatting.StringFormatter;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
@@ -22,13 +24,17 @@ public class CommandFormat implements TabExecutor {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||
@NotNull String[] arguments) {
|
||||
StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter();
|
||||
if (!(sender instanceof Player player)) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_PLAYER_ONLY);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a written book to format it!",
|
||||
"You cannot format two books at once!")) {
|
||||
if (InventoryHelper.notHoldingOneWrittenBookCheck(player,
|
||||
stringFormatter.replacePlaceholder(Translatable.ERROR_NOT_HOLDING_WRITTEN_BOOK, "{action}",
|
||||
stringFormatter.getUnFormattedColoredMessage(Translatable.ACTION_FORMAT)),
|
||||
stringFormatter.replacePlaceholder(Translatable.ERROR_ONLY_ONE_BOOK, "{action}",
|
||||
stringFormatter.getUnFormattedColoredMessage(Translatable.ACTION_FORMAT)))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -36,13 +42,13 @@ public class CommandFormat implements TabExecutor {
|
||||
BookMeta meta = (BookMeta) heldBook.getItemMeta();
|
||||
|
||||
if (meta == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Unable to get metadata from the held book!");
|
||||
stringFormatter.displayErrorMessage(player, Translatable.ERROR_METADATA_MISSING);
|
||||
return false;
|
||||
}
|
||||
|
||||
heldBook.setItemMeta(BookFormatter.formatPages(meta));
|
||||
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Book formatted!");
|
||||
stringFormatter.displaySuccessMessage(sender, Translatable.SUCCESS_FORMATTED);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.config.Translatable;
|
||||
import net.knarcraft.bookswithoutborders.gui.PagedBookIndex;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.TabCompletionTypeHelper;
|
||||
import net.knarcraft.knarlib.formatting.StringFormatter;
|
||||
import net.knarcraft.knarlib.util.TabCompletionHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
@@ -55,8 +57,9 @@ public class CommandGive implements TabExecutor {
|
||||
|
||||
int argumentCount = arguments.length;
|
||||
|
||||
StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter();
|
||||
if (arguments.length == 1) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_GIVE_NO_RECIPIENT);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -66,18 +69,8 @@ public class CommandGive implements TabExecutor {
|
||||
String copies = "1";
|
||||
String isSigned = "true";
|
||||
|
||||
if (arguments.length == 4) {
|
||||
copies = arguments[2];
|
||||
isSigned = arguments[3];
|
||||
} else if (arguments.length == 3) {
|
||||
if (arguments[2].equalsIgnoreCase("true") || arguments[2].equalsIgnoreCase("false")) {
|
||||
isSigned = arguments[2];
|
||||
} else {
|
||||
copies = arguments[2];
|
||||
}
|
||||
}
|
||||
|
||||
if (argumentCount > 3 && InputCleaningHelper.isInt(arguments[argumentCount - 2]) && InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
|
||||
if (argumentCount > 3 && InputCleaningHelper.isInt(arguments[argumentCount - 2]) &&
|
||||
InputCleaningHelper.isBoolean(arguments[argumentCount - 1])) {
|
||||
receivingPlayerName = arguments[argumentCount - 3];
|
||||
isSigned = arguments[argumentCount - 1];
|
||||
copies = arguments[argumentCount - 2];
|
||||
@@ -96,16 +89,15 @@ public class CommandGive implements TabExecutor {
|
||||
}
|
||||
|
||||
//Try and find the target player
|
||||
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
|
||||
Player receivingPlayer = booksWithoutBorders.getServer().getPlayerExact(receivingPlayerName);
|
||||
if (receivingPlayer == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Player not found!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_GIVE_RECIPIENT_UNKNOWN);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Make sure the receiver is able to fit the book
|
||||
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Receiving player must have space in their inventory" +
|
||||
" to receive books!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_GIVE_RECIPIENT_FULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -119,7 +111,7 @@ public class CommandGive implements TabExecutor {
|
||||
try {
|
||||
return loadAndGiveBook(bookIdentifier, sender, receivingPlayer, isSigned, folder, copies);
|
||||
} catch (NumberFormatException e) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Invalid number of book copies specified!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_GIVE_INVALID_COPIES_AMOUNT);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -162,10 +154,13 @@ public class CommandGive implements TabExecutor {
|
||||
|
||||
if (argumentCount > 2 && InputCleaningHelper.isBoolean(arguments[argumentCount - 2])) {
|
||||
return output;
|
||||
} else if (argumentCount > 2 && server.getPlayer(arguments[argumentCount - 3]) != null && InputCleaningHelper.isInt(arguments[argumentCount - 2])) {
|
||||
output.addAll(TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]));
|
||||
} else if (argumentCount > 2 && server.getPlayer(arguments[argumentCount - 2]) != null) {
|
||||
output.addAll(TabCompletionHelper.filterMatchingStartsWith(TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), arguments[argumentCount - 1]));
|
||||
} else if (argumentCount > 2 && server.getPlayerExact(arguments[argumentCount - 3]) != null &&
|
||||
InputCleaningHelper.isInt(arguments[argumentCount - 2])) {
|
||||
output.addAll(TabCompletionHelper.filterMatchingStartsWith(
|
||||
TabCompletionTypeHelper.getBooleans(), arguments[argumentCount - 1]));
|
||||
} else if (argumentCount > 2 && server.getPlayerExact(arguments[argumentCount - 2]) != null) {
|
||||
output.addAll(TabCompletionHelper.filterMatchingStartsWith(
|
||||
TabCompletionTypeHelper.getBooleansAndNumbers(1, 3), arguments[argumentCount - 1]));
|
||||
} else {
|
||||
List<String> players = new ArrayList<>();
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
@@ -190,16 +185,17 @@ public class CommandGive implements TabExecutor {
|
||||
private boolean loadAndGiveBook(@NotNull String bookIdentifier, @NotNull CommandSender sender,
|
||||
@NotNull Player receivingPlayer, @NotNull String isSigned, @NotNull String folder,
|
||||
@NotNull String copies) throws NumberFormatException {
|
||||
StringFormatter stringFormatter = BooksWithoutBorders.getStringFormatter();
|
||||
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
||||
ItemStack newBook = BookLoader.loadBook(sender, bookToLoad, isSigned, folder, Integer.parseInt(copies));
|
||||
if (newBook != null) {
|
||||
//NOTE: As this method bypasses cost, it should also bypass the generation change
|
||||
receivingPlayer.getInventory().addItem(newBook);
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Book sent!");
|
||||
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
|
||||
stringFormatter.displaySuccessMessage(sender, Translatable.SUCCESS_GIVE_SENT);
|
||||
stringFormatter.displaySuccessMessage(receivingPlayer, Translatable.SUCCESS_GIVE_RECEIVED);
|
||||
return true;
|
||||
} else {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Book failed to load!");
|
||||
stringFormatter.displayErrorMessage(sender, Translatable.ERROR_GIVE_LOAD_FAILED);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -33,6 +33,21 @@ public enum Translatable implements TranslatableMessage {
|
||||
*/
|
||||
SUCCESS_DELETED,
|
||||
|
||||
/**
|
||||
* The success message displayed when a book is successfully formatted
|
||||
*/
|
||||
SUCCESS_FORMATTED,
|
||||
|
||||
/**
|
||||
* The success message displayed when a book is successfully sent
|
||||
*/
|
||||
SUCCESS_GIVE_SENT,
|
||||
|
||||
/**
|
||||
* The success message displayed when a book is successfully received
|
||||
*/
|
||||
SUCCESS_GIVE_RECEIVED,
|
||||
|
||||
/**
|
||||
* The error to display when the console attempts to run a player-only command
|
||||
*/
|
||||
@@ -133,6 +148,41 @@ public enum Translatable implements TranslatableMessage {
|
||||
*/
|
||||
ERROR_ENCRYPT_NO_KEY,
|
||||
|
||||
/**
|
||||
* The error displayed when exceeding the number of allowed arguments for a command
|
||||
*/
|
||||
ERROR_TOO_MANY_ARGUMENTS_COMMAND,
|
||||
|
||||
/**
|
||||
* The error displayed when trying to encrypt an empty book
|
||||
*/
|
||||
ERROR_ENCRYPT_EMPTY,
|
||||
|
||||
/**
|
||||
* The error displayed after using the give command without specifying a recipient
|
||||
*/
|
||||
ERROR_GIVE_NO_RECIPIENT,
|
||||
|
||||
/**
|
||||
* The error displayed when using the give command with an invalid recipient
|
||||
*/
|
||||
ERROR_GIVE_RECIPIENT_UNKNOWN,
|
||||
|
||||
/**
|
||||
* The error displayed when using the give command with a recipient with a full inventory
|
||||
*/
|
||||
ERROR_GIVE_RECIPIENT_FULL,
|
||||
|
||||
/**
|
||||
* The error displayed when using the give command with an invalid number of copies (unlikely to ever happen)
|
||||
*/
|
||||
ERROR_GIVE_INVALID_COPIES_AMOUNT,
|
||||
|
||||
/**
|
||||
* The error displayed when a book is not properly loaded when using the give command
|
||||
*/
|
||||
ERROR_GIVE_LOAD_FAILED,
|
||||
|
||||
/**
|
||||
* The header displayed before printing all commands
|
||||
*/
|
||||
@@ -182,6 +232,16 @@ public enum Translatable implements TranslatableMessage {
|
||||
* The translation of the decrypt action
|
||||
*/
|
||||
ACTION_DECRYPT,
|
||||
|
||||
/**
|
||||
* The translation of the encrypt action
|
||||
*/
|
||||
ACTION_ENCRYPT,
|
||||
|
||||
/**
|
||||
* The translation of the format action
|
||||
*/
|
||||
ACTION_FORMAT,
|
||||
;
|
||||
|
||||
@Override
|
||||
|
@@ -287,7 +287,7 @@ public final class BookFileHelper {
|
||||
public static String replaceAuthorWithUUID(@NotNull String fileName) {
|
||||
String userName = BookFormatter.stripColor(getBookAuthorFromPath(fileName));
|
||||
|
||||
Player player = Bukkit.getPlayer(userName);
|
||||
Player player = Bukkit.getPlayerExact(userName);
|
||||
if (player != null) {
|
||||
return userName.replace(userName, player.getUniqueId().toString());
|
||||
} else {
|
||||
|
@@ -5,10 +5,18 @@ en:
|
||||
SUCCESS_DECRYPTED: "Book decrypted!"
|
||||
SUCCESS_AUTO_DECRYPTED: "Book auto-decrypted!"
|
||||
SUCCESS_DELETED: "\"{file}\" deleted successfully"
|
||||
SUCCESS_FORMATTED: "Book formatted!"
|
||||
SUCCESS_GIVE_SENT: "Book sent!"
|
||||
SUCCESS_GIVE_RECEIVED: "Book received!"
|
||||
ERROR_PLAYER_ONLY: "This command can only be used by a player!"
|
||||
ERROR_NOT_HOLDING_WRITTEN_BOOK: "You must be holding a written book to {action} it!"
|
||||
ERROR_NOT_HOLDING_WRITABLE_BOOK: "You must be holding a writable book to {action} it!"
|
||||
ERROR_ONLY_ONE_BOOK: "You cannot {action} two books at once!"
|
||||
ACTION_COPY: "copy"
|
||||
ACTION_CLEAR: "clear"
|
||||
ACTION_DECRYPT: "decrypt"
|
||||
ACTION_ENCRYPT: "encrypt"
|
||||
ACTION_FORMAT: "format"
|
||||
ERROR_COPY_COUNT_NOT_SPECIFIED: "You must specify the number of copies to be made!"
|
||||
ERROR_COPY_NEGATIVE_AMOUNT: "Number of copies must be larger than 0!"
|
||||
ERROR_COPY_INVALID_AMOUNT: |
|
||||
@@ -27,6 +35,13 @@ en:
|
||||
ERROR_DELETE_FAILED_SILENT: "Deletion failed without an exception!"
|
||||
ERROR_DELETE_FAILED_EXCEPTION: "Deletion failed!"
|
||||
ERROR_ENCRYPT_NO_KEY: "You must specify a key to encrypt a book!"
|
||||
ERROR_ENCRYPT_EMPTY: "Book must have contents to encrypt!"
|
||||
ERROR_TOO_MANY_ARGUMENTS_COMMAND: "You have given too many arguments for this command!"
|
||||
ERROR_GIVE_NO_RECIPIENT: "You have not specified the recipient of the book!"
|
||||
ERROR_GIVE_RECIPIENT_UNKNOWN: "Player not found!"
|
||||
ERROR_GIVE_RECIPIENT_FULL: "Receiving player must have space in their inventory to receive books!"
|
||||
ERROR_GIVE_INVALID_COPIES_AMOUNT: "Invalid number of book copies specified!"
|
||||
ERROR_GIVE_LOAD_FAILED: "Book failed to load!"
|
||||
NEUTRAL_COMMANDS_HEADER: |
|
||||
&e[] denote optional parameters
|
||||
<> denote required parameters
|
||||
@@ -39,7 +54,4 @@ en:
|
||||
NEUTRAL_COMMANDS_COMMAND: "\n \n&e{usage}: &a{description}"
|
||||
NEUTRAL_COMMANDS_COMMAND_NO_PERMISSION_REQUIRED: "None"
|
||||
NEUTRAL_COMMANDS_COMMAND_PERMISSION: " &7{{permission}}"
|
||||
NEUTRAL_UNKNOWN_AUTHOR: "Unknown"
|
||||
ACTION_COPY: "copy"
|
||||
ACTION_CLEAR: "clear"
|
||||
ACTION_DECRYPT: "decrypt"
|
||||
NEUTRAL_UNKNOWN_AUTHOR: "Unknown"
|
Reference in New Issue
Block a user