Adds clearBook command #7 and improves README

This commit is contained in:
2022-11-04 13:01:56 +01:00
parent 0fdfd81579
commit f005b8f8e5
6 changed files with 156 additions and 81 deletions

View File

@@ -1,6 +1,7 @@
package net.knarcraft.bookswithoutborders;
import net.knarcraft.bookswithoutborders.command.CommandBooksWithoutBorders;
import net.knarcraft.bookswithoutborders.command.CommandClear;
import net.knarcraft.bookswithoutborders.command.CommandCopy;
import net.knarcraft.bookswithoutborders.command.CommandDecrypt;
import net.knarcraft.bookswithoutborders.command.CommandDelete;
@@ -173,6 +174,7 @@ public class BooksWithoutBorders extends JavaPlugin {
registerCommand("reload", new CommandReload());
registerCommand("formatBook", new CommandFormat());
registerCommand("setBookGeneration", new CommandSetGeneration());
registerCommand("clearBook", new CommandClear());
}
/**

View File

@@ -0,0 +1,48 @@
package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* Command executor for the clear command
*/
public class CommandClear implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
if (!(sender instanceof Player player)) {
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
return false;
}
InventoryHelper.notHoldingOneWritableBookCheck(player, "You must be holding a writable book to copy it!",
"You cannot copy two books at once!");
//Clear the player's held book
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
BookMeta bookMeta = (BookMeta) heldBook;
bookMeta.setPages("");
heldBook.setItemMeta(bookMeta);
return true;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
return new ArrayList<>();
}
}

View File

@@ -103,7 +103,7 @@ public final class EconomyHelper {
private static boolean takeWritableBookPayment(Player player, int itemCost) {
List<ItemStack> books = getPlayersEmptyBooks(player);
if (countItems(books) < itemCost) {
BooksWithoutBorders.sendErrorMessage(player, itemCost + " " + Material.WRITABLE_BOOK +
BooksWithoutBorders.sendErrorMessage(player, itemCost + " empty " + Material.WRITABLE_BOOK +
"(s) are required for this command!");
return false;
} else {

View File

@@ -51,6 +51,31 @@ public final class InventoryHelper {
}
}
/**
* Performs checks to validate that a player contains exactly one unwritten book
*
* @param player <p>The player to validate</p>
* @param noBookMessage <p>The message to display if the player is not holding a book</p>
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
* @return <p>False if the player is holding exactly one book</p>
*/
public static boolean notHoldingOneWritableBookCheck(Player player, String noBookMessage, String twoBooksMessage) {
BookHoldingState holdingState = getBookHoldingState(player);
if (holdingState == BookHoldingState.NONE || holdingState == BookHoldingState.SIGNED_BOTH_HANDS ||
holdingState == BookHoldingState.SIGNED_MAIN_HAND || holdingState == BookHoldingState.SIGNED_OFF_HAND) {
BooksWithoutBorders.sendErrorMessage(player, noBookMessage);
return true;
}
if (holdingState == BookHoldingState.UNSIGNED_BOTH_HANDS) {
BooksWithoutBorders.sendErrorMessage(player, twoBooksMessage);
return true;
}
return false;
}
/**
* Performs checks to validate that a player contains exactly one written book
*

View File

@@ -45,6 +45,10 @@ commands:
description: Copies the book the player is holding
usage: /<command> <# of copies>
permission: bookswithoutborders.copy
clearbook:
description: Removes all text from the book the player is holding
usage: /<command>
permission: bookswithoutborders.clear
unsignbook:
description: Un-signs the book the player is holding
usage: /<command>
@@ -130,6 +134,7 @@ permissions:
bookswithoutborders.alterbooks:
description: Allows player to change books' data such as lore/title/author/generation/formatting and unsigning books
children:
bookswithoutborders.clear: true
bookswithoutborders.unsign: true
bookswithoutborders.settitle: true
bookswithoutborders.setauthor: true
@@ -145,9 +150,11 @@ permissions:
bookswithoutborders.delete:
description: Allows player to delete books from their personal directory
bookswithoutborders.unsign:
description: Allows player to use unsign command
description: Allows player to use the unsign command
bookswithoutborders.copy:
description: Allows player to use copy command
description: Allows player to use the copy command
bookswithoutborders.clear:
description: Allows player to use the clear command
bookswithoutborders.loadpublic:
description: Allows player to load from the public directory
bookswithoutborders.savepublic: