148 lines
6.3 KiB
Java
148 lines
6.3 KiB
Java
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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
|
@NotNull String[] args) {
|
|
if (!sender.hasPermission("blacksmith.admin")) {
|
|
return new ArrayList<>();
|
|
}
|
|
|
|
//Arguments: <setting> [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<String> 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 <p>The arguments given by the user</p>
|
|
* @return <p>Null if not writing a spaced message</p>
|
|
*/
|
|
private List<String> 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 <p>The global setting to get tab-completions for</p>
|
|
* @param args <p>The arguments given by the user</p>
|
|
* @return <p>The tab-completions to show to the user</p>
|
|
*/
|
|
private List<String> 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 <p>The name of the used command</p>
|
|
* @param commandValue <p>The command value used to filter tab-completions</p>
|
|
* @return <p>Some valid options for the command's argument</p>
|
|
*/
|
|
private List<String> 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 <p>The global setting to get tab-completions for</p>
|
|
* @param commandValue <p>The command value used to filter between available tab-completions</p>
|
|
* @return <p>The available tab-completions</p>
|
|
*/
|
|
private List<String> getCompletions(GlobalSetting globalSetting, String commandValue) {
|
|
List<String> 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;
|
|
}
|
|
|
|
}
|