| 1 | package nl.timfalken.copperkeys; |
| 2 | |
| 3 | import net.kyori.adventure.text.Component; |
| 4 | import net.kyori.adventure.text.format.NamedTextColor; |
| 5 | import org.bukkit.command.Command; |
| 6 | import org.bukkit.command.CommandExecutor; |
| 7 | import org.bukkit.command.CommandSender; |
| 8 | import org.bukkit.entity.Player; |
| 9 | import org.bukkit.inventory.ItemStack; |
| 10 | |
| 11 | import java.util.UUID; |
| 12 | |
| 13 | final class KeyNameCommand implements CommandExecutor { |
| 14 | private final CopperKeysPlugin plugin; |
| 15 | |
| 16 | KeyNameCommand(CopperKeysPlugin plugin) { |
| 17 | this.plugin = plugin; |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { |
| 22 | if (!(sender instanceof Player player)) { |
| 23 | sender.sendMessage("Only players can rename keys."); |
| 24 | return true; |
| 25 | } |
| 26 | if (args.length == 0) { |
| 27 | player.sendActionBar(Component.text("Usage: /keyname <name>", NamedTextColor.RED)); |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | String name = String.join(" ", args).trim(); |
| 32 | if (name.isEmpty()) { |
| 33 | player.sendActionBar(Component.text("Usage: /keyname <name>", NamedTextColor.RED)); |
| 34 | return true; |
| 35 | } |
| 36 | if (name.length() > KeyItems.MAX_KEY_NAME_LENGTH) { |
| 37 | player.sendActionBar(Component.text( |
| 38 | "Name must be at most " + KeyItems.MAX_KEY_NAME_LENGTH + " characters", |
| 39 | NamedTextColor.RED |
| 40 | )); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | ItemStack held = player.getInventory().getItemInMainHand(); |
| 45 | if (!plugin.keyItems().isKey(held)) { |
| 46 | player.sendActionBar(Component.text("Hold a bound key to rename it", NamedTextColor.RED)); |
| 47 | return true; |
| 48 | } |
| 49 | if (plugin.keyItems().isBlank(held)) { |
| 50 | player.sendActionBar(Component.text("Blank keys cannot be renamed", NamedTextColor.RED)); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | UUID bindingId = plugin.keyItems().getBinding(held); |
| 55 | if (bindingId == null || plugin.lockStore().get(bindingId) == null) { |
| 56 | player.sendActionBar(Component.text("That key is not linked to a lock", NamedTextColor.RED)); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | plugin.renameKeyBinding(bindingId, name, player); |
| 61 | player.sendActionBar(Component.text("Key renamed to " + name, NamedTextColor.GOLD)); |
| 62 | return true; |
| 63 | } |
| 64 | } |