Implements the scrapper global edit command
Changes setting quite a bit to avoid code duplication
This commit is contained in:
@ -1,20 +1,79 @@
|
||||
package net.knarcraft.blacksmith.command.scrapper;
|
||||
|
||||
import org.apache.commons.lang.NotImplementedException;
|
||||
import net.knarcraft.blacksmith.config.Setting;
|
||||
import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting;
|
||||
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.knarlib.util.TabCompletionHelper.filterMatchingContains;
|
||||
|
||||
/**
|
||||
* The tab completer for the command used for changing global scrapper configuration options
|
||||
*/
|
||||
public class ScrapperConfigTabCompleter implements TabCompleter {
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command,
|
||||
@NotNull String s, @NotNull String[] strings) {
|
||||
throw new NotImplementedException();
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
|
||||
@NotNull String s, @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 (args.length > 2 && skipCompletionForSpacedMessage(args[0])) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
if (args.length == 1) {
|
||||
List<String> availableCommands = new ArrayList<>();
|
||||
availableCommands.add("reload");
|
||||
for (Setting setting : ScrapperSetting.values()) {
|
||||
availableCommands.add(setting.getCommandName());
|
||||
}
|
||||
return filterMatchingContains(availableCommands, args[0]);
|
||||
} else if (args.length == 2) {
|
||||
return tabCompleteCommandValues(args[0], args[1]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether to tab-complete nothing because a message containing spaces is being written
|
||||
*
|
||||
* @param command <p>The command specified by the user</p>
|
||||
* @return <p>Null if not writing a spaced message</p>
|
||||
*/
|
||||
private boolean skipCompletionForSpacedMessage(@NotNull String command) {
|
||||
Setting scrapperSetting = ScrapperSetting.getSetting(command);
|
||||
return scrapperSetting != null && scrapperSetting.isMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(@NotNull String commandName, @NotNull String commandValue) {
|
||||
if (commandName.equalsIgnoreCase("reload")) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Setting scrapperSetting = ScrapperSetting.getSetting(commandName);
|
||||
if (scrapperSetting != null) {
|
||||
return filterMatchingContains(getTabCompletions(scrapperSetting.getValueType()), commandValue);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user