Moves the setAuthor command to its own file

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

View File

@ -21,6 +21,7 @@ import net.knarcraft.bookswithoutborders.command.CommandGive;
import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt; import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt;
import net.knarcraft.bookswithoutborders.command.CommandSave; import net.knarcraft.bookswithoutborders.command.CommandSave;
import net.knarcraft.bookswithoutborders.command.CommandSavePublic; import net.knarcraft.bookswithoutborders.command.CommandSavePublic;
import net.knarcraft.bookswithoutborders.command.CommandSetAuthor;
import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice; import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice;
import net.knarcraft.bookswithoutborders.command.CommandSetLore; import net.knarcraft.bookswithoutborders.command.CommandSetLore;
import net.knarcraft.bookswithoutborders.command.CommandUnSign; import net.knarcraft.bookswithoutborders.command.CommandUnSign;
@ -35,9 +36,11 @@ import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -90,62 +93,34 @@ public class BooksWithoutBorders extends JavaPlugin {
this.getPluginLoader().disablePlugin(this); this.getPluginLoader().disablePlugin(this);
} }
PluginCommand giveCommand = this.getCommand("give"); registerCommand("give", new CommandGive(this), new GiveTabCompleter(this));
if (giveCommand != null) { registerCommand("decrypt", new CommandDecrypt(this), null);
giveCommand.setExecutor(new CommandGive(this)); registerCommand("groupEncrypt", new CommandGroupEncrypt(this), null);
giveCommand.setTabCompleter(new GiveTabCompleter(this)); registerCommand("delete", new CommandDelete(this), null);
registerCommand("copy", new CommandCopy(this), null);
registerCommand("unSign", new CommandUnSign(this), null);
registerCommand("encrypt", new CommandEncrypt(this), null);
registerCommand("setBookPrice", new CommandSetBookPrice(this), null);
registerCommand("setLore", new CommandSetLore(), null);
registerCommand("savePublic", new CommandSavePublic(this), null);
registerCommand("save", new CommandSave(this), null);
registerCommand("setAuthor", new CommandSetAuthor(), null);
}
/**
* Registers a command
* @param commandName <p>The name of the command to register</p>
* @param executor <p>The executor to register for the command</p>
*/
private void registerCommand(String commandName, CommandExecutor executor, TabCompleter tabCompleter) {
PluginCommand pluginCommand = this.getCommand(commandName);
if (pluginCommand != null) {
pluginCommand.setExecutor(executor);
if (tabCompleter != null) {
pluginCommand.setTabCompleter(tabCompleter);
}
} else { } else {
sendErrorMessage(consoleSender, "Unable to register give command"); sendErrorMessage(consoleSender, "Failed to register command " + commandName);
}
PluginCommand decryptCommand = this.getCommand("decrypt");
if (decryptCommand != null) {
decryptCommand.setExecutor(new CommandDecrypt(this));
}
PluginCommand groupEncryptCommand = this.getCommand("groupencrypt");
if (groupEncryptCommand != null) {
groupEncryptCommand.setExecutor(new CommandGroupEncrypt(this));
}
PluginCommand deleteCommand = this.getCommand("delete");
if (deleteCommand != null) {
deleteCommand.setExecutor(new CommandDelete(this));
}
PluginCommand copyCommand = this.getCommand("copy");
if (copyCommand != null) {
copyCommand.setExecutor(new CommandCopy(this));
}
PluginCommand unSignCommand = this.getCommand("unsign");
if (unSignCommand != null) {
unSignCommand.setExecutor(new CommandUnSign(this));
}
PluginCommand encryptCommand = this.getCommand("encrypt");
if (encryptCommand != null) {
encryptCommand.setExecutor(new CommandEncrypt(this));
}
PluginCommand setBookPriceCommand = this.getCommand("setbookprice");
if (setBookPriceCommand != null) {
setBookPriceCommand.setExecutor(new CommandSetBookPrice(this));
}
PluginCommand setLoreCommand = this.getCommand("setlore");
if (setLoreCommand != null) {
setLoreCommand.setExecutor(new CommandSetLore());
}
PluginCommand savePublicCommand = this.getCommand("savepublic");
if (savePublicCommand != null) {
savePublicCommand.setExecutor(new CommandSavePublic(this));
}
PluginCommand saveCommand = this.getCommand("save");
if (saveCommand != null) {
saveCommand.setExecutor(new CommandSave(this));
} }
} }
@ -394,7 +369,7 @@ public class BooksWithoutBorders extends JavaPlugin {
sendSuccessMessage(sender, "If true is specified, a book of the same name by the same"); sendSuccessMessage(sender, "If true is specified, a book of the same name by the same");
sendSuccessMessage(sender, "author will be overwritten by the new book"); sendSuccessMessage(sender, "author will be overwritten by the new book");
} }
if (sender.hasPermission("bookswithoutborders.savepublic")) { if (sender.hasPermission("bookswithoutborders.savePublic")) {
sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,"); sender.sendMessage(commandColor + "savePublic [true/false]: " + successColor + "Same as Save,");
sendSuccessMessage(sender, "but saves files in the public directory"); sendSuccessMessage(sender, "but saves files in the public directory");
} }
@ -402,7 +377,7 @@ public class BooksWithoutBorders extends JavaPlugin {
sender.sendMessage("\n" + commandColor + "Give [file name or number] [player name] [# of copies] [true/false]:"); sender.sendMessage("\n" + commandColor + "Give [file name or number] [player name] [# of copies] [true/false]:");
sendSuccessMessage(sender, "Gives the selected player a book from your personal directory"); sendSuccessMessage(sender, "Gives the selected player a book from your personal directory");
} }
if (sender.hasPermission("bookswithoutborders.givepublic")) { if (sender.hasPermission("bookswithoutborders.givePublic")) {
sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [# of copies] [true/false]:"); sender.sendMessage(commandColor + "givePublic [file name or number] [player name] [# of copies] [true/false]:");
sendSuccessMessage(sender, "Same as give, but uses books from the public directory"); sendSuccessMessage(sender, "Same as give, but uses books from the public directory");
} }
@ -427,7 +402,7 @@ public class BooksWithoutBorders extends JavaPlugin {
sender.sendMessage(commandColor + "[key]" + successColor + " is required and can be any phrase or number excluding spaces"); sender.sendMessage(commandColor + "[key]" + successColor + " is required and can be any phrase or number excluding spaces");
sender.sendMessage(commandColor + "[style]" + successColor + " is not required. Possible values are \"DNA\" or \"Magic\""); sender.sendMessage(commandColor + "[style]" + successColor + " is not required. Possible values are \"DNA\" or \"Magic\"");
} }
if (sender.hasPermission("bookswithoutborders.groupencrypt")) { if (sender.hasPermission("bookswithoutborders.groupEncrypt")) {
sender.sendMessage("\n" + commandColor + "groupEncrypt [group name] [key] [style]: " + successColor + "Encrypts book so that only players with the" + sender.sendMessage("\n" + commandColor + "groupEncrypt [group name] [key] [style]: " + successColor + "Encrypts book so that only players with the" +
"\n bookswithoutborders.decrypt." + commandColor + "[group name]" + successColor + " permission may decrypt the book by holding and left clicking the book"); "\n bookswithoutborders.decrypt." + commandColor + "[group name]" + successColor + " permission may decrypt the book by holding and left clicking the book");
} }
@ -435,17 +410,17 @@ public class BooksWithoutBorders extends JavaPlugin {
sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding"); sender.sendMessage("\n" + commandColor + "Decrypt [key]: " + successColor + "Decrypts the book the player is holding");
sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book"); sender.sendMessage(commandColor + "[key]" + successColor + " is required and MUST be IDENTICAL to the key used to encrypt held book");
} }
if (sender.hasPermission("bookswithoutborders.settitle")) { if (sender.hasPermission("bookswithoutborders.setTitle")) {
sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding"); sender.sendMessage("\n" + commandColor + "setTitle [title]: " + successColor + "Sets the title of the book/item the player is holding");
} }
if (sender.hasPermission("bookswithoutborders.setauthor")) { if (sender.hasPermission("bookswithoutborders.setAuthor")) {
sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding"); sender.sendMessage("\n" + commandColor + "setAuthor [author]: " + successColor + "Sets the author of the book the player is holding");
} }
if (sender.hasPermission("bookswithoutborders.setlore")) { if (sender.hasPermission("bookswithoutborders.setLore")) {
sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding"); sender.sendMessage("\n" + commandColor + "setLore [lore]: " + successColor + "Sets the lore of the item the player is holding");
sendSuccessMessage(sender, "Insert the lore_line_separator character to force a new line\n[\"~\" by default]"); sendSuccessMessage(sender, "Insert the lore_line_separator character to force a new line\n[\"~\" by default]");
} }
if (sender.hasPermission("bookswithoutborders.setbookprice")) if (sender.hasPermission("bookswithoutborders.setBookPrice"))
sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." + sender.sendMessage("\n" + commandColor + "setBookPrice [Item/Eco] [quantity]: " + successColor + "Sets the per-book-price to create a book via commands." +
"\nIf [Item], the item in the player's hand in the amount of [quantity] will be the price.\nIf [Eco], a Vault based economy will be used for price." + "\nIf [Item], the item in the player's hand in the amount of [quantity] will be the price.\nIf [Eco], a Vault based economy will be used for price." +
"\nIf neither [Item/Eco] or [quantity] are specified the current price to create books will be removed."); "\nIf neither [Item/Eco] or [quantity] are specified the current price to create books will be removed.");
@ -603,23 +578,17 @@ public class BooksWithoutBorders extends JavaPlugin {
return false; return false;
} }
StringBuilder title = new StringBuilder(); String title = String.join(" ", args);
for (int x = 1; x < args.length; x++) { title = ChatColor.translateAlternateColorCodes('&', title);
if (x == 1)
title.append(args[x]);
else
title.append(" ").append(args[x]);
}
title = new StringBuilder(ChatColor.translateAlternateColorCodes('&', title.toString()));
ItemMeta meta; ItemMeta meta;
if (((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK) { if (((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK) {
BookMeta bMeta = (BookMeta) ((Player) sender).getItemInHand().getItemMeta(); BookMeta bMeta = (BookMeta) ((Player) sender).getItemInHand().getItemMeta();
bMeta.setTitle(title.toString()); bMeta.setTitle(title);
meta = bMeta; meta = bMeta;
} else { } else {
ItemMeta iMeta = ((Player) sender).getItemInHand().getItemMeta(); ItemMeta iMeta = ((Player) sender).getItemInHand().getItemMeta();
iMeta.setDisplayName(title.toString()); iMeta.setDisplayName(title);
meta = iMeta; meta = iMeta;
} }
@ -627,36 +596,6 @@ public class BooksWithoutBorders extends JavaPlugin {
sendSuccessMessage(sender, "Title set to " + title + "!"); sendSuccessMessage(sender, "Title set to " + title + "!");
return true; return true;
} }
if (args[0].equalsIgnoreCase("setAuthor")) {
if (!sender.hasPermission("bookswithoutborders.setauthor")) {
sendErrorMessage(sender, " You don't have permission to use this command!");
return false;
}
if (args.length < 2) {
sendErrorMessage(sender, "Too few command arguments!");
return false;
}
if (!(((Player) sender).getItemInHand().getType() == Material.WRITTEN_BOOK)) {
sendErrorMessage(sender, "You must be holding a written book to set author!");
return false;
}
BookMeta meta = (BookMeta) ((Player) sender).getItemInHand().getItemMeta();
StringBuilder author = new StringBuilder();
for (int x = 1; x < args.length; x++) {
if (x == 1)
author.append(args[x]);
else
author.append(" ").append(args[x]);
}
meta.setAuthor(author.toString());
((Player) sender).getItemInHand().setItemMeta(meta);
sendSuccessMessage(sender, "Book author set to " + author + "!");
return true;
}
} }
if (args[0].equalsIgnoreCase("givePublic")) { if (args[0].equalsIgnoreCase("givePublic")) {

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 //Treat all arguments as lore input
StringBuilder rawLore = new StringBuilder(); String rawLore = String.join(" ", args);
rawLore.append(args[0]);
for (int x = 1; x < args.length; x++) {
rawLore.append(" ").append(args[x]);
}
//Format lore //Format lore
rawLore = new StringBuilder(ChatColor.translateAlternateColorCodes('&', rawLore.toString())); rawLore = ChatColor.translateAlternateColorCodes('&', rawLore);
String[] loreParts = rawLore.toString().split(BooksWithoutBorders.loreSeparator); String[] loreParts = rawLore.split(BooksWithoutBorders.loreSeparator);
List<String> newLore = new ArrayList<>(Arrays.asList(loreParts)); List<String> newLore = new ArrayList<>(Arrays.asList(loreParts));
//Update lore //Update lore

View File

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

View File

@ -51,12 +51,16 @@ commands:
permission: bookswithoutborders.setlore permission: bookswithoutborders.setlore
savepublic: savepublic:
description: Saves the held book to the public books folder description: Saves the held book to the public books folder
usage: /<command> [duplicate (true/false)] usage: /<command> [overwrite (true/false)]
permission: bookswithoutborders.savepublic permission: bookswithoutborders.savepublic
save: save:
description: Saves the held book to the holding player's folder description: Saves the held book to the holding player's folder
usage: /<command> usage: /<command> [overwrite (true/false)]
permission: bookswithoutborders.save permission: bookswithoutborders.save
setauthor:
description: Sets the author of the held book
usage: /<command> <author>
permission: bookswithoutborders.setauthor
permissions: permissions:
bookswithoutborders.admin: bookswithoutborders.admin:
description: Grants all permissions description: Grants all permissions