Moves the setAuthor command to its own file

This commit is contained in:
2021-08-31 16:28:31 +02:00
parent 4a9c678bff
commit 7247098a54
5 changed files with 101 additions and 114 deletions

View File

@ -0,0 +1,48 @@
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;
import org.bukkit.inventory.meta.BookMeta;
/**
* Command executor for the set author command
*/
public class CommandSetAuthor 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, "Too few command arguments!");
return false;
}
if (InventoryHelper.notHoldingOneWrittenBookCheck(player,
"You must be holding a written book to set author!", "You cannot set the " +
"author of two books at once!")) {
return false;
}
ItemSlot heldBookSlot = InventoryHelper.getHeldSlotBook(player, false, false, true, true);
boolean mainHand = heldBookSlot == ItemSlot.MAIN_HAND;
ItemStack heldBook = InventoryHelper.getHeldItem(player, mainHand);
BookMeta bookMetaData = InventoryHelper.getHeldBookMetadata(player, mainHand);
String author = String.join(" ", args);
bookMetaData.setAuthor(author);
heldBook.setItemMeta(bookMetaData);
BooksWithoutBorders.sendSuccessMessage(player, "Book author set to " + author + "!");
return true;
}
}

View File

@ -39,15 +39,11 @@ public class CommandSetLore implements CommandExecutor {
}
//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]);
}
String rawLore = String.join(" ", args);
//Format lore
rawLore = new StringBuilder(ChatColor.translateAlternateColorCodes('&', rawLore.toString()));
String[] loreParts = rawLore.toString().split(BooksWithoutBorders.loreSeparator);
rawLore = ChatColor.translateAlternateColorCodes('&', rawLore);
String[] loreParts = rawLore.split(BooksWithoutBorders.loreSeparator);
List<String> newLore = new ArrayList<>(Arrays.asList(loreParts));
//Update lore

View File

@ -9,7 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* Command executor for the un-sign command
* Command executor for the unsign command
*/
public class CommandUnSign implements CommandExecutor {
@ -26,8 +26,8 @@ public class CommandUnSign implements CommandExecutor {
return false;
}
if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a signed book to un-sign it!",
"You cannot un-sign two books at once. Please un-equip one of the books you're holding!")) {
if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a signed book to unsign it!",
"You cannot unsign two books at once. Please un-equip one of the books you're holding!")) {
return false;
}