|
26 | 26 |
|
27 | 27 | import javax.annotation.Nullable;
|
28 | 28 | import java.io.File;
|
29 |
| -import java.io.IOException; |
30 | 29 | import java.nio.file.Files;
|
31 | 30 | import java.nio.file.Path;
|
32 | 31 | import java.util.ArrayList;
|
33 | 32 | import java.util.List;
|
| 33 | +import java.util.stream.Stream; |
34 | 34 |
|
35 | 35 | public class SkriptCommandTabCompleter implements TabCompleter {
|
36 |
| - |
| 36 | + |
37 | 37 | @Override
|
38 | 38 | @Nullable
|
39 | 39 | public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
40 | 40 | ArrayList<String> options = new ArrayList<>();
|
41 | 41 |
|
42 |
| - if (!command.getName().equalsIgnoreCase("skript")) { |
| 42 | + if (!command.getName().equalsIgnoreCase("skript")) |
43 | 43 | return null;
|
44 |
| - } |
45 | 44 |
|
46 | 45 | if (args[0].equalsIgnoreCase("update") && args.length == 2) {
|
47 | 46 | options.add("check");
|
48 | 47 | options.add("changes");
|
49 | 48 | options.add("download");
|
50 |
| - } else if (args[0].matches("(?i)(reload|disable|enable)") && args.length == 2) { |
51 |
| - File scripts = new File(Skript.getInstance().getDataFolder(), Skript.SCRIPTSFOLDER); |
52 |
| - String scriptArg = StringUtils.join(args, " ", 1, args.length); |
| 49 | + } else if (args[0].matches("(?i)(reload|disable|enable)") && args.length >= 2) { |
| 50 | + File scripts = Skript.getInstance().getScriptsFolder(); |
| 51 | + String scriptsPathString = scripts.toPath().toString(); |
| 52 | + int scriptsPathLength = scriptsPathString.length(); |
| 53 | + |
| 54 | + String scriptArg = StringUtils.join(args, " ", 1, args.length); |
53 | 55 | String fs = File.separator;
|
54 |
| - |
55 |
| - try { |
56 |
| - // Live update, this will get all old and new (even not loaded) scripts |
57 |
| - Files.walk(scripts.toPath()) |
58 |
| - .map(Path::toFile) |
59 |
| - .filter(f -> (!f.isDirectory() && StringUtils.endsWithIgnoreCase(f.getName(), ".sk")) || |
60 |
| - f.isDirectory()) // filter folders and skript files only |
61 |
| - .filter(f -> { // Filtration for enable, disable and reload |
62 |
| - if (args[0].equalsIgnoreCase("enable")) |
63 |
| - return f.getName().startsWith("-"); |
64 |
| - else // reload & disable both accepts only non-hyphened files and not hidden folders |
65 |
| - return !f.getAbsolutePath().matches(".*?(\\\\-|/-|^-).*") && (f.isDirectory() && |
66 |
| - !f.getAbsolutePath().matches(".*?(\\\\\\.|/\\.|^\\.).*") || !f.isDirectory()); |
67 |
| - }) |
68 |
| - .filter(f -> { // Autocomplete incomplete script name arg |
69 |
| - return scriptArg.length() > 0 ? f.getName().startsWith(scriptArg) : true; |
70 |
| - }) |
| 56 | + |
| 57 | + boolean enable = args[0].equalsIgnoreCase("enable"); |
| 58 | + |
| 59 | + // Live update, this will get all old and new (even not loaded) scripts |
| 60 | + // TODO Find a better way for caching, it isn't exactly ideal to be calling this method constantly |
| 61 | + try (Stream<Path> files = Files.walk(scripts.toPath())) { |
| 62 | + files.map(Path::toFile) |
71 | 63 | .forEach(f -> {
|
72 |
| - if (!f.toString().equals(scripts.toString())) |
73 |
| - options.add(f.toString() |
74 |
| - .replace(scripts.toPath() + (!f.isDirectory() && f.getParentFile().toPath().toString() |
75 |
| - .equals(scripts.toPath().toString()) ? fs : ""), "") // Extract file short path, and remove '/' from the beginning of files in root only |
76 |
| - + (f.isDirectory() && f.toString().length() > 0 ? fs : "")); // add File.separator at the end of directories |
77 |
| - }); |
78 |
| - |
79 |
| - // TODO handle file permissions |
80 |
| - } catch (IOException e) { |
81 |
| - e.printStackTrace(); |
| 64 | + if (!(enable ? ScriptLoader.getDisabledScriptsFilter() : ScriptLoader.getLoadedScriptsFilter()).accept(f)) |
| 65 | + return; |
| 66 | + |
| 67 | + String fileString = f.toString().substring(scriptsPathLength); |
| 68 | + if (fileString.isEmpty()) |
| 69 | + return; |
| 70 | + |
| 71 | + if (f.isDirectory()) { |
| 72 | + fileString = fileString + fs; // Add file separator at the end of directories |
| 73 | + } else if (f.getParentFile().toPath().toString().equals(scriptsPathString)) { |
| 74 | + fileString = fileString.substring(1); // Remove file separator from the beginning of files or directories in root only |
| 75 | + if (fileString.isEmpty()) |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Make sure the user's argument matches with the file's name or beginning of file path |
| 80 | + if (scriptArg.length() > 0 && !f.getName().startsWith(scriptArg) && !fileString.startsWith(scriptArg)) |
| 81 | + return; |
| 82 | + |
| 83 | + // Trim off previous arguments if needed |
| 84 | + if (args.length > 2 && fileString.length() >= scriptArg.length()) |
| 85 | + fileString = fileString.substring(scriptArg.lastIndexOf(" ") + 1); |
| 86 | + |
| 87 | + // Just in case |
| 88 | + if (fileString.isEmpty()) |
| 89 | + return; |
| 90 | + |
| 91 | + options.add(fileString); |
| 92 | + }); |
| 93 | + } catch (Exception e) { |
| 94 | + //noinspection ThrowableNotThrown |
| 95 | + Skript.exception(e, "An error occurred while trying to update the list of disabled scripts!"); |
82 | 96 | }
|
83 | 97 |
|
84 | 98 | // These will be added even if there are incomplete script arg
|
85 |
| - options.add("all"); |
86 |
| - if (args[0].equalsIgnoreCase("reload")) { |
87 |
| - options.add("config"); |
88 |
| - options.add("aliases"); |
89 |
| - options.add("scripts"); |
| 99 | + if (args.length == 2) { |
| 100 | + options.add("all"); |
| 101 | + if (args[0].equalsIgnoreCase("reload")) { |
| 102 | + options.add("config"); |
| 103 | + options.add("aliases"); |
| 104 | + options.add("scripts"); |
| 105 | + } |
90 | 106 | }
|
| 107 | + |
91 | 108 | } else if (args.length == 1) {
|
92 | 109 | options.add("help");
|
93 | 110 | options.add("reload");
|
94 | 111 | options.add("enable");
|
95 | 112 | options.add("disable");
|
96 | 113 | options.add("update");
|
97 | 114 | options.add("info");
|
98 |
| - if (new File(Skript.getInstance().getDataFolder() + "/doc-templates").exists()) { |
| 115 | + if (new File(Skript.getInstance().getDataFolder() + "/doc-templates").exists()) |
99 | 116 | options.add("gen-docs");
|
100 |
| - } |
101 |
| - if (TestMode.DEV_MODE) { |
| 117 | + if (TestMode.DEV_MODE) |
102 | 118 | options.add("test");
|
103 |
| - } |
104 | 119 | }
|
105 | 120 |
|
106 | 121 | return options;
|
107 | 122 | }
|
108 |
| - |
| 123 | + |
109 | 124 | }
|
0 commit comments