Moves setlore and setbookprice command code to their own classes
This commit is contained in:
@ -11,6 +11,9 @@ import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Command executor for the copy command
|
||||
*/
|
||||
public class CommandCopy implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
@ -6,6 +6,9 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Command executor for the delete command
|
||||
*/
|
||||
public class CommandDelete implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
@ -12,6 +12,9 @@ import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Command executor for the encrypt command
|
||||
*/
|
||||
public class CommandEncrypt implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
@ -1,12 +1,130 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
import org.bukkit.Material;
|
||||
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 set book price command
|
||||
*/
|
||||
public class CommandSetBookPrice implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandSetBookPrice(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
//Clear the current price
|
||||
if (args.length == 0) {
|
||||
return clearItemPrice(sender);
|
||||
}
|
||||
|
||||
//Warn about missing arguments
|
||||
if (args.length < 2) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "[Item/Eco] and [quantity] must be specified!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Warn about invalid argument
|
||||
if (!args[0].equalsIgnoreCase("Item") && !args[0].equalsIgnoreCase("Eco")) {
|
||||
BooksWithoutBorders. sendErrorMessage(sender, "Price type must be \"Item\" or \"Eco\"!");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
double price = Double.parseDouble(args[1]);
|
||||
if (price <= 0) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "[quantity] must be greater than 0!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("Item")) {
|
||||
return setItemPrice(sender, price);
|
||||
} else if (args[0].equalsIgnoreCase("Eco")) {
|
||||
return setEconomyPrice(sender, price);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "[quantity] must be a number!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the book price
|
||||
* @param sender <p>The sender of the command</p>
|
||||
* @return <p>True if the price was changed successfully</p>
|
||||
*/
|
||||
private boolean clearItemPrice(CommandSender sender) {
|
||||
BooksWithoutBorders.bookPriceType = null;
|
||||
BooksWithoutBorders.bookPriceQuantity = 0;
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Item type name");
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", BooksWithoutBorders.bookPriceQuantity);
|
||||
booksWithoutBorders.saveConfig();
|
||||
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Price to create books removed!");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the book price to use items, and updates book price
|
||||
* @param sender <p>The sender of the command</p>
|
||||
* @param price <p>The new price</p>
|
||||
* @return <p>True if the price was changed successfully</p>
|
||||
*/
|
||||
private boolean setItemPrice(CommandSender sender, double price) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "[Item] price can only be used by a player!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack heldItem = InventoryHelper.getHeldItem(player, true);
|
||||
if (heldItem.getType() == Material.AIR) {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "Must be holding an item to set an [Item] price!");
|
||||
return false;
|
||||
}
|
||||
|
||||
BooksWithoutBorders.bookPriceType = heldItem.getType();
|
||||
BooksWithoutBorders.bookPriceQuantity = price;
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type",
|
||||
BooksWithoutBorders.bookPriceType.toString());
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity",
|
||||
BooksWithoutBorders.bookPriceQuantity);
|
||||
booksWithoutBorders.saveConfig();
|
||||
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Book creation price set to " +
|
||||
(int) BooksWithoutBorders.bookPriceQuantity + " " + BooksWithoutBorders.bookPriceType.toString() + "(s)!");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the book price to use economy, and updates book price
|
||||
* @param sender <p>The sender of the command</p>
|
||||
* @param price <p>The new price</p>
|
||||
* @return <p>True if the price was changed successfully</p>
|
||||
*/
|
||||
private boolean setEconomyPrice(CommandSender sender, double price) {
|
||||
if (booksWithoutBorders.setupEconomy()) {
|
||||
BooksWithoutBorders.bookPriceQuantity = price;
|
||||
BooksWithoutBorders.bookPriceType = Material.AIR;
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Economy");
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity",
|
||||
BooksWithoutBorders.bookPriceQuantity);
|
||||
booksWithoutBorders.saveConfig();
|
||||
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Book creation price set to " +
|
||||
BooksWithoutBorders.eco.format(BooksWithoutBorders.bookPriceQuantity) + "!");
|
||||
return true;
|
||||
} else {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "BooksWithoutBorders failed to hook into Vault! Book price not set!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
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.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Command executor for the set lore command
|
||||
*/
|
||||
public class CommandSetLore implements CommandExecutor {
|
||||
|
||||
@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) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Missing a command argument!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack heldItem = InventoryHelper.getHeldItem(player, true);
|
||||
if (heldItem.getType() == Material.AIR) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Must be holding an item to set lore!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Treat all arguments as lore input
|
||||
StringBuilder rawLore = new StringBuilder();
|
||||
rawLore.append(args[0]);
|
||||
for (int x = 1; x < args.length; x++) {
|
||||
rawLore.append(" ").append(args[x]);
|
||||
}
|
||||
|
||||
//Format lore
|
||||
rawLore = new StringBuilder(ChatColor.translateAlternateColorCodes('&', rawLore.toString()));
|
||||
String[] loreParts = rawLore.toString().split(BooksWithoutBorders.loreSeparator);
|
||||
List<String> newLore = new ArrayList<>(Arrays.asList(loreParts));
|
||||
|
||||
//Update lore
|
||||
ItemMeta meta = heldItem.getItemMeta();
|
||||
if (meta == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Could not get metadata from the held item!");
|
||||
return false;
|
||||
}
|
||||
meta.setLore(newLore);
|
||||
heldItem.setItemMeta(meta);
|
||||
BooksWithoutBorders.sendSuccessMessage(player, "Added lore to item!");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user