Adds tab completion to the encrypt and group encrypt commands

This commit is contained in:
Kristian Knarvik 2021-09-08 16:34:40 +02:00
parent 0deeb6c36a
commit 248d3be868
2 changed files with 48 additions and 4 deletions

View File

@ -5,16 +5,19 @@ import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper; import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
import net.knarcraft.bookswithoutborders.utility.InventoryHelper; import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
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.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.BookMeta;
import java.util.ArrayList;
import java.util.List;
/** /**
* Command executor for the encrypt command * Command executor for the encrypt command
*/ */
public class CommandEncrypt implements CommandExecutor { public class CommandEncrypt implements TabExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -90,4 +93,40 @@ public class CommandEncrypt implements CommandExecutor {
} }
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
return doTabCompletion(args, false);
}
/**
* Gets a list of string for tab completions
* @param args <p>The arguments given</p>
* @param groupEncrypt <p>Whether to auto-complete for group encryption</p>
* @return <p>The strings to auto-complete</p>
*/
protected List<String> doTabCompletion(String[] args, boolean groupEncrypt) {
int argumentsCount = args.length;
List<String> encryptionStyles = new ArrayList<>();
encryptionStyles.add("dna");
encryptionStyles.add("substitution");
if (argumentsCount == 1) {
List<String> info = new ArrayList<>();
info.add("<password>");
return info;
} else if (argumentsCount == 2) {
if (groupEncrypt) {
List<String> info = new ArrayList<>();
info.add("<group>");
return info;
} else {
return encryptionStyles;
}
} else if (argumentsCount == 3 && groupEncrypt) {
return encryptionStyles;
}
return new ArrayList<>();
}
} }

View File

@ -3,8 +3,8 @@ package net.knarcraft.bookswithoutborders.command;
import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.state.EncryptionStyle; import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
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.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.BookMeta;
@ -13,7 +13,7 @@ import java.util.List;
/** /**
* Command executor for the group encrypt command * Command executor for the group encrypt command
*/ */
public class CommandGroupEncrypt extends CommandEncrypt implements CommandExecutor { public class CommandGroupEncrypt extends CommandEncrypt implements TabExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 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]); return encryptBook(encryptionStyle, (Player) sender, args[1], args[0]);
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
return doTabCompletion(args, true);
}
} }