package net.knarcraft.blacksmith.command; import net.knarcraft.blacksmith.config.GlobalSetting; import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.util.InputParsingHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; import static net.knarcraft.blacksmith.util.TabCompleteValuesHelper.getTabCompletions; import static net.knarcraft.blacksmith.util.TabCompletionHelper.filterMatchingContains; /** * The tab completer for the command used for changing global configuration options */ public class BlackSmithConfigTabCompleter implements TabCompleter { @Nullable @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { if (!sender.hasPermission("blacksmith.admin")) { return new ArrayList<>(); } //Arguments: [new value/material or enchantment] [] //Prevent tab-completion when typing messages with spaces if (skipCompletionForSpacedMessage(args) != null) { return new ArrayList<>(); } if (args.length == 1) { List availableCommands = new ArrayList<>(); availableCommands.add("reload"); for (NPCSetting setting : NPCSetting.values()) { availableCommands.add(setting.getCommandName()); } for (GlobalSetting globalSetting : GlobalSetting.values()) { availableCommands.add(globalSetting.getCommandName()); } return filterMatchingContains(availableCommands, args[0]); } else if (args.length == 2) { return tabCompleteCommandValues(args[0], args[1]); } else if (args.length == 3) { //Get per-material tab completions, or return nothing if an invalid setting was specified for (GlobalSetting globalSetting : GlobalSetting.values()) { if (globalSetting.getCommandName().equalsIgnoreCase(args[0])) { return getPerTypeTabCompletions(globalSetting, args); } } return new ArrayList<>(); } return null; } /** * Checks whether to tab-complete nothing because a message containing spaces is being written * * @param args

The arguments given by the user

* @return

Null if not writing a spaced message

*/ private List skipCompletionForSpacedMessage(String[] args) { if (args.length > 2) { NPCSetting npcSetting = null; for (NPCSetting setting : NPCSetting.values()) { if (setting.getCommandName().equalsIgnoreCase(args[0])) { npcSetting = setting; break; } } if (npcSetting != null && npcSetting.getPath().startsWith("defaults.messages")) { return new ArrayList<>(); } } return null; } /** * Gets tab-completions for a selected material or enchantment * * @param globalSetting

The global setting to get tab-completions for

* @param args

The arguments given by the user

* @return

The tab-completions to show to the user

*/ private List getPerTypeTabCompletions(GlobalSetting globalSetting, String[] args) { //Display possible tab-completions only if a valid enchantment or material is provided if (((globalSetting == GlobalSetting.BASE_PRICE || globalSetting == GlobalSetting.PRICE_PER_DURABILITY_POINT) && InputParsingHelper.matchMaterial(args[1]) != null) || (globalSetting == GlobalSetting.ENCHANTMENT_COST && InputParsingHelper.matchEnchantment(args[1]) != null)) { return filterMatchingContains(getTabCompletions(globalSetting.getValueType()), args[2]); } else { return new ArrayList<>(); } } /** * Tab completes the values available for the given command * * @param commandName

The name of the used command

* @param commandValue

The command value used to filter tab-completions

* @return

Some valid options for the command's argument

*/ private List tabCompleteCommandValues(String commandName, String commandValue) { if (commandName.equalsIgnoreCase("reload")) { return new ArrayList<>(); } for (GlobalSetting globalSetting : GlobalSetting.values()) { if (globalSetting.getCommandName().equalsIgnoreCase(commandName)) { return getCompletions(globalSetting, commandValue); } } for (NPCSetting npcSetting : NPCSetting.values()) { if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) { return filterMatchingContains(getTabCompletions( npcSetting.getValueType()), commandValue); } } return null; } /** * Gets tab-completions for the given global setting and filters on the command value * * @param globalSetting

The global setting to get tab-completions for

* @param commandValue

The command value used to filter between available tab-completions

* @return

The available tab-completions

*/ private List getCompletions(GlobalSetting globalSetting, String commandValue) { List returnValues = filterMatchingContains(getTabCompletions(globalSetting.getValueType()), commandValue); if (globalSetting == GlobalSetting.BASE_PRICE || globalSetting == GlobalSetting.PRICE_PER_DURABILITY_POINT) { returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.MATERIAL), commandValue)); } else if (globalSetting == GlobalSetting.ENCHANTMENT_COST) { returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.ENCHANTMENT), commandValue)); } return returnValues; } }