Skip to content

Commit aa62e89

Browse files
authored
Structure API (#4108)
One small step for a Skripter... ONE GIANT LEAP FOR SKRIPTKIND!!!
1 parent a8df952 commit aa62e89

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4815
-2128
lines changed

src/main/java/ch/njol/skript/ScriptLoader.java

Lines changed: 560 additions & 717 deletions
Large diffs are not rendered by default.

src/main/java/ch/njol/skript/Skript.java

Lines changed: 195 additions & 125 deletions
Large diffs are not rendered by default.

src/main/java/ch/njol/skript/SkriptCommand.java

Lines changed: 272 additions & 186 deletions
Large diffs are not rendered by default.

src/main/java/ch/njol/skript/SkriptCommandTabCompleter.java

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,84 +26,99 @@
2626

2727
import javax.annotation.Nullable;
2828
import java.io.File;
29-
import java.io.IOException;
3029
import java.nio.file.Files;
3130
import java.nio.file.Path;
3231
import java.util.ArrayList;
3332
import java.util.List;
33+
import java.util.stream.Stream;
3434

3535
public class SkriptCommandTabCompleter implements TabCompleter {
36-
36+
3737
@Override
3838
@Nullable
3939
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
4040
ArrayList<String> options = new ArrayList<>();
4141

42-
if (!command.getName().equalsIgnoreCase("skript")) {
42+
if (!command.getName().equalsIgnoreCase("skript"))
4343
return null;
44-
}
4544

4645
if (args[0].equalsIgnoreCase("update") && args.length == 2) {
4746
options.add("check");
4847
options.add("changes");
4948
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);
5355
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)
7163
.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!");
8296
}
8397

8498
// 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+
}
90106
}
107+
91108
} else if (args.length == 1) {
92109
options.add("help");
93110
options.add("reload");
94111
options.add("enable");
95112
options.add("disable");
96113
options.add("update");
97114
options.add("info");
98-
if (new File(Skript.getInstance().getDataFolder() + "/doc-templates").exists()) {
115+
if (new File(Skript.getInstance().getDataFolder() + "/doc-templates").exists())
99116
options.add("gen-docs");
100-
}
101-
if (TestMode.DEV_MODE) {
117+
if (TestMode.DEV_MODE)
102118
options.add("test");
103-
}
104119
}
105120

106121
return options;
107122
}
108-
123+
109124
}

0 commit comments

Comments
 (0)