Fixes and improves book saving
Removes redundancy Fixes broken duplicate checking Moves the save function to its own class
This commit is contained in:
@@ -1,12 +1,52 @@
|
||||
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 command
|
||||
*/
|
||||
public class CommandSave implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandSave(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return false;
|
||||
return saveHeldBook(sender, args, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the player's held book if it exists
|
||||
* @param sender <p>The sender of the command</p>
|
||||
* @param args <p>The arguments given</p>
|
||||
* @param savePublic <p>Whether to save the book in the public directory or the player directory</p>
|
||||
* @return <p>True if a book was saved successfully</p>
|
||||
*/
|
||||
boolean saveHeldBook(CommandSender sender, String[] args, boolean savePublic) {
|
||||
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);
|
||||
boolean duplicate = args.length == 1 && Boolean.parseBoolean(args[0]);
|
||||
booksWithoutBorders.saveBook(player, holdingItem, duplicate, savePublic);
|
||||
return true;
|
||||
} else {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "You must be holding a written book or book and quill to save it!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,45 +1,22 @@
|
||||
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 class CommandSavePublic extends CommandSave implements CommandExecutor {
|
||||
|
||||
public CommandSavePublic(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
super(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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return saveHeldBook(sender, args, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -24,7 +24,8 @@ public class CommandSetBookPrice implements CommandExecutor {
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
//Clear the current price
|
||||
if (args.length == 0) {
|
||||
return clearItemPrice(sender);
|
||||
clearItemPrice(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
//Warn about missing arguments
|
||||
@@ -60,9 +61,8 @@ public class CommandSetBookPrice implements CommandExecutor {
|
||||
/**
|
||||
* 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) {
|
||||
private void clearItemPrice(CommandSender sender) {
|
||||
BooksWithoutBorders.bookPriceType = null;
|
||||
BooksWithoutBorders.bookPriceQuantity = 0;
|
||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Item type name");
|
||||
@@ -70,7 +70,6 @@ public class CommandSetBookPrice implements CommandExecutor {
|
||||
booksWithoutBorders.saveConfig();
|
||||
|
||||
BooksWithoutBorders.sendSuccessMessage(sender, "Price to create books removed!");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
|
||||
public class GiveTabCompleter implements TabCompleter {
|
||||
|
||||
BooksWithoutBorders booksWithoutBorders;
|
||||
final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public GiveTabCompleter(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
|
Reference in New Issue
Block a user