Moves the save public command to its own class
This commit is contained in:
parent
bd4013e84f
commit
9d64cd787d
@ -18,6 +18,7 @@ import net.knarcraft.bookswithoutborders.command.CommandDelete;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandEncrypt;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandGive;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandSavePublic;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandSetLore;
|
||||
import net.knarcraft.bookswithoutborders.command.CommandUnSign;
|
||||
@ -135,6 +136,11 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
if (setLoreCommand != null) {
|
||||
setLoreCommand.setExecutor(new CommandSetLore());
|
||||
}
|
||||
|
||||
PluginCommand savePublicCommand = this.getCommand("savepublic");
|
||||
if (savePublicCommand != null) {
|
||||
savePublicCommand.setExecutor(new CommandSavePublic(this));
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean init() {
|
||||
@ -476,32 +482,6 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
//Player only commands
|
||||
if (sender instanceof Player player) {
|
||||
|
||||
if (args[0].equalsIgnoreCase("savePublic")) {
|
||||
if (!sender.hasPermission("bookswithoutborders.savepublic")) {
|
||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, false, false);
|
||||
if (holdingSlot != ItemSlot.NONE) {
|
||||
ItemStack holdingItem;
|
||||
if (holdingSlot == ItemSlot.MAIN_HAND) {
|
||||
holdingItem = player.getInventory().getItemInMainHand();
|
||||
} else {
|
||||
holdingItem = player.getInventory().getItemInOffHand();
|
||||
}
|
||||
if (args.length == 2) {
|
||||
saveBook(player, holdingItem, args[1], true);
|
||||
} else {
|
||||
saveBook(player, holdingItem, "false", true);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
sendErrorMessage(sender, "You must be holding a written book or book and quill to save it!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("save")) {
|
||||
if (!sender.hasPermission("bookswithoutborders.save")) {
|
||||
sendErrorMessage(sender, " You don't have permission to use this command!");
|
||||
@ -510,12 +490,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
|
||||
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, false, false);
|
||||
if (holdingSlot != ItemSlot.NONE) {
|
||||
ItemStack holdingItem;
|
||||
if (holdingSlot == ItemSlot.MAIN_HAND) {
|
||||
holdingItem = player.getInventory().getItemInMainHand();
|
||||
} else {
|
||||
holdingItem = player.getInventory().getItemInOffHand();
|
||||
}
|
||||
ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND);
|
||||
if (args.length == 2) {
|
||||
saveBook(player, holdingItem, args[1], false);
|
||||
} else {
|
||||
@ -943,7 +918,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
return bDat;
|
||||
}
|
||||
|
||||
protected void saveBook(Player player, ItemStack heldBook, String dupe, Boolean saveToPublicFolder) {
|
||||
public void saveBook(Player player, ItemStack heldBook, String dupe, Boolean saveToPublicFolder) {
|
||||
//Notice: Could be both a signed or unsigned book
|
||||
BookMeta book = (BookMeta) heldBook.getItemMeta();
|
||||
if (book == null) {
|
||||
@ -1170,7 +1145,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
}
|
||||
|
||||
public ItemStack eLoad(Player player, String key, boolean deleteEncryptedFile) {
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player);
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||
BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta();
|
||||
String path = bookFolder + "Encrypted" + SLASH;
|
||||
|
||||
@ -1214,7 +1189,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
|
||||
ItemStack newBook = new ItemStack(Material.WRITTEN_BOOK);//Book(book.getAuthor(), book.getTitle(), pages, 1, 387);
|
||||
newBook.setItemMeta(bookMetadata);
|
||||
newBook.setAmount(InventoryHelper.getHeldBook(player).getAmount());
|
||||
newBook.setAmount(InventoryHelper.getHeldBook(player, true).getAmount());
|
||||
return newBook;
|
||||
}
|
||||
|
||||
@ -1321,7 +1296,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
||||
encryptedBook.setItemMeta(book);
|
||||
|
||||
//Update item amount
|
||||
encryptedBook.setAmount(InventoryHelper.getHeldBook(player).getAmount());
|
||||
encryptedBook.setAmount(InventoryHelper.getHeldBook(player, true).getAmount());
|
||||
//Set new item metadata
|
||||
encryptedBook.setItemMeta(newMetadata);
|
||||
|
||||
|
@ -7,6 +7,7 @@ import java.util.Objects;
|
||||
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Tag;
|
||||
@ -118,8 +119,8 @@ public class BooksWithoutBordersListener implements Listener {
|
||||
}
|
||||
|
||||
//Updates any books in either hand
|
||||
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
|
||||
ItemStack offHandItem = player.getInventory().getItemInOffHand();
|
||||
ItemStack mainHandItem = InventoryHelper.getHeldItem(player, true);
|
||||
ItemStack offHandItem = InventoryHelper.getHeldItem(player, false);
|
||||
if (mainHandItem.getType() == Material.WRITTEN_BOOK) {
|
||||
ItemMeta itemMetadata = mainHandItem.getItemMeta();
|
||||
updateBookInHand(player, itemMetadata, true);
|
||||
|
@ -39,7 +39,7 @@ public class CommandCopy implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player);
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||
|
||||
try {
|
||||
int copies = Integer.parseInt(args[0]);
|
||||
|
@ -36,7 +36,7 @@ public class CommandDecrypt implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack heldItem = InventoryHelper.getHeldBook(player);
|
||||
ItemStack heldItem = InventoryHelper.getHeldBook(player, true);
|
||||
BookMeta bookMetadata = (BookMeta) heldItem.getItemMeta();
|
||||
if (bookMetadata == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Your book seems to be corrupt!");
|
||||
|
@ -44,7 +44,7 @@ public class CommandEncrypt implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player);
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||
|
||||
if (!((BookMeta) Objects.requireNonNull(heldBook.getItemMeta())).hasPages()) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Book must have contents to encrypt!");
|
||||
|
@ -45,7 +45,7 @@ public class CommandGroupEncrypt implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player);
|
||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||
BookMeta bookMetadata = (BookMeta) heldBook.getItemMeta();
|
||||
|
||||
if (bookMetadata == null) {
|
||||
|
@ -1,12 +1,45 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Command executor for the save public command
|
||||
*/
|
||||
public class CommandSavePublic implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandSavePublic(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return false;
|
||||
if (!(sender instanceof Player player)) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, false, false);
|
||||
if (holdingSlot != ItemSlot.NONE) {
|
||||
ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND);
|
||||
if (args.length == 1) {
|
||||
booksWithoutBorders.saveBook(player, holdingItem, args[0], true);
|
||||
} else {
|
||||
booksWithoutBorders.saveBook(player, holdingItem, "false", true);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "You must be holding a written book or book and quill to save it!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package net.knarcraft.bookswithoutborders.utility;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.state.BookHoldingState;
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.state.HoldingItemState;
|
||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -14,16 +13,6 @@ import org.bukkit.inventory.meta.BookMeta;
|
||||
*/
|
||||
public class InventoryHelper {
|
||||
|
||||
/**
|
||||
* Gets the book the holder is playing
|
||||
*
|
||||
* @param player <p>The player holding the book</p>
|
||||
* @return <p>The book the player is holding</p>
|
||||
*/
|
||||
public static ItemStack getHeldBook(Player player) {
|
||||
return getHeldBook(player, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the book the holder is playing
|
||||
*
|
||||
@ -34,9 +23,9 @@ public class InventoryHelper {
|
||||
public static ItemStack getHeldBook(Player player, boolean signedBook) {
|
||||
ItemSlot heldSlot = getHeldSlotBook(player, false, false, true, signedBook);
|
||||
if (heldSlot == ItemSlot.MAIN_HAND) {
|
||||
return player.getInventory().getItemInMainHand();
|
||||
return getHeldItem(player, true);
|
||||
} else if (heldSlot == ItemSlot.OFF_HAND) {
|
||||
return player.getInventory().getItemInOffHand();
|
||||
return getHeldItem(player, false);
|
||||
} else {
|
||||
throw new IllegalArgumentException("The player is not holding exactly one book.");
|
||||
}
|
||||
@ -71,18 +60,6 @@ public class InventoryHelper {
|
||||
return notHoldingOneBookCheck(player, noBookMessage, twoBooksMessage, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs checks to validate that a player contains exactly one writable
|
||||
*
|
||||
* @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 writable book</p>
|
||||
*/
|
||||
public static boolean notHoldingOneWritableBookCheck(Player player, String noBookMessage, String twoBooksMessage) {
|
||||
return notHoldingOneBookCheck(player, noBookMessage, twoBooksMessage, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs checks to validate that a player contains exactly one book
|
||||
* @param player <p>The player to validate</p>
|
||||
@ -143,8 +120,8 @@ public class InventoryHelper {
|
||||
public static ItemSlot getHeldSlotBook(Player player, boolean handMatters, boolean mainHand,
|
||||
boolean typeMatters, boolean writtenBook) {
|
||||
BookHoldingState state = getBookHoldingState(player);
|
||||
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
|
||||
ItemStack offHandItem = player.getInventory().getItemInOffHand();
|
||||
ItemStack mainHandItem = getHeldItem(player, true);
|
||||
ItemStack offHandItem = getHeldItem(player, false);
|
||||
Material requiredMaterial = writtenBook ? Material.WRITTEN_BOOK : Material.WRITABLE_BOOK;
|
||||
//Ambiguous or empty
|
||||
if (state == BookHoldingState.SIGNED_BOTH_HANDS ||
|
||||
@ -197,8 +174,8 @@ public class InventoryHelper {
|
||||
* @return <p>The state of the player's book holding</p>
|
||||
*/
|
||||
private static BookHoldingState getBookHoldingState(Player player) {
|
||||
ItemStack mainHandItem = player.getInventory().getItemInMainHand();
|
||||
ItemStack offHandItem = player.getInventory().getItemInOffHand();
|
||||
ItemStack mainHandItem = getHeldItem(player, true);
|
||||
ItemStack offHandItem = getHeldItem(player, false);
|
||||
|
||||
boolean hasSignedBookInMainHand = mainHandItem.getType() == Material.WRITTEN_BOOK;
|
||||
boolean hasUnsignedBookInMainHand = mainHandItem.getType() == Material.WRITABLE_BOOK;
|
||||
@ -226,28 +203,6 @@ public class InventoryHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state of which hands of the player contains an item type
|
||||
*
|
||||
* @param player <p>The player possibly holding the item</p>
|
||||
* @param type <p>The type of material to look for</p>
|
||||
* @return <p>The state of the player's item holding</p>
|
||||
*/
|
||||
private static HoldingItemState getHoldingItemState(Player player, Material type) {
|
||||
boolean hasBookInMainHand = player.getInventory().getItemInMainHand().getType() == type;
|
||||
boolean hasBookInOffHand = player.getInventory().getItemInOffHand().getType() == type;
|
||||
|
||||
if (hasBookInMainHand && hasBookInOffHand) {
|
||||
return HoldingItemState.BOTH_HANDS;
|
||||
} else if (hasBookInMainHand) {
|
||||
return HoldingItemState.MAIN_HAND;
|
||||
} else if (hasBookInOffHand) {
|
||||
return HoldingItemState.OFF_HAND;
|
||||
} else {
|
||||
return HoldingItemState.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets metadata about the player's held book
|
||||
*
|
||||
|
@ -49,6 +49,10 @@ commands:
|
||||
description: Sets the lore of the held item
|
||||
usage: /<command> <new lore>
|
||||
permission: bookswithoutborders.setlore
|
||||
savepublic:
|
||||
description: Saves the held book to the public books folder
|
||||
usage: /<command> [duplicate (true/false)]
|
||||
permission: bookswithoutborders.savepublic
|
||||
permissions:
|
||||
bookswithoutborders.admin:
|
||||
description: Grants all permissions
|
||||
|
Loading…
Reference in New Issue
Block a user