From 248d3be8689fc1c1eb58014abeed602e54a816f7 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 8 Sep 2021 16:34:40 +0200 Subject: [PATCH] Adds tab completion to the encrypt and group encrypt commands --- .../command/CommandEncrypt.java | 43 ++++++++++++++++++- .../command/CommandGroupEncrypt.java | 9 +++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java index d346aa5..098af57 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandEncrypt.java @@ -5,16 +5,19 @@ import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; +import java.util.ArrayList; +import java.util.List; + /** * Command executor for the encrypt command */ -public class CommandEncrypt implements CommandExecutor { +public class CommandEncrypt implements TabExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -90,4 +93,40 @@ public class CommandEncrypt implements CommandExecutor { } } + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + return doTabCompletion(args, false); + } + + /** + * Gets a list of string for tab completions + * @param args

The arguments given

+ * @param groupEncrypt

Whether to auto-complete for group encryption

+ * @return

The strings to auto-complete

+ */ + protected List doTabCompletion(String[] args, boolean groupEncrypt) { + int argumentsCount = args.length; + + List encryptionStyles = new ArrayList<>(); + encryptionStyles.add("dna"); + encryptionStyles.add("substitution"); + + if (argumentsCount == 1) { + List info = new ArrayList<>(); + info.add(""); + return info; + } else if (argumentsCount == 2) { + if (groupEncrypt) { + List info = new ArrayList<>(); + info.add(""); + return info; + } else { + return encryptionStyles; + } + } else if (argumentsCount == 3 && groupEncrypt) { + return encryptionStyles; + } + return new ArrayList<>(); + } + } diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java index ad3852c..2ea6734 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandGroupEncrypt.java @@ -3,8 +3,8 @@ package net.knarcraft.bookswithoutborders.command; import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.inventory.meta.BookMeta; @@ -13,7 +13,7 @@ import java.util.List; /** * Command executor for the group encrypt command */ -public class CommandGroupEncrypt extends CommandEncrypt implements CommandExecutor { +public class CommandGroupEncrypt extends CommandEncrypt implements TabExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -35,4 +35,9 @@ public class CommandGroupEncrypt extends CommandEncrypt implements CommandExecut return encryptBook(encryptionStyle, (Player) sender, args[1], args[0]); } + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + return doTabCompletion(args, true); + } + }