/
ScriptHelper.java
152 lines (127 loc) · 5.79 KB
/
ScriptHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package net.aufdemrand.denizen.scripts;
import net.aufdemrand.denizen.events.ScriptReloadEvent;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.utilities.Utilities;
public class ScriptHelper {
/*
* Reloads and retrieves information from the Denizen/scripts.yml.
*/
private static YamlConfiguration _yamlScripts = null;
public static void reloadScripts() {
String concatenated = _concatenateCoreScripts();
_yamlScripts = new YamlConfiguration();
try {
_yamlScripts.loadFromString(concatenated);
} catch (InvalidConfigurationException e) {
dB.echoError("Could not load scripts!");
e.printStackTrace();
}
ScriptRegistry._buildCoreYamlScriptContainers(getScripts());
Bukkit.getServer().getPluginManager().callEvent(new ScriptReloadEvent());
}
public static FileConfiguration _gs() {
return getScripts();
}
private static FileConfiguration getScripts() {
if (_yamlScripts == null) {
reloadScripts();
}
return _yamlScripts;
}
private static boolean HadAnError = false;
public static boolean getHadAnError() {
return HadAnError;
}
public static void resetHadAnError() {
HadAnError = false;
}
private static String _concatenateCoreScripts() {
try {
File file = new File(DenizenAPI.getCurrentInstance()
.getDataFolder() + File.separator + "scripts");
if(!file.exists()) {
dB.echoError("No script folder found, please create one.");
HadAnError = true;
return "";
}
// Get files
List<File> files = Utilities.listDScriptFiles(file, Settings.LoadScriptsInSubfolders());
if (files.size() > 0) {
StringBuilder sb = new StringBuilder();
YamlConfiguration yaml;
dB.echoDebug("Processing 'util.dscript'... ");
yaml = YamlConfiguration.loadConfiguration(DenizenAPI.getCurrentInstance().getResource("util.dscript"));
sb.append(yaml.saveToString() + "\r\n");
dB.echoDebug("Processing 'genies.dscript'... ");
yaml = YamlConfiguration.loadConfiguration(DenizenAPI.getCurrentInstance().getResource("genies.dscript"));
sb.append(yaml.saveToString() + "\r\n");
dB.echoDebug("Processing outside scripts... ");
for (FileConfiguration outsideConfig : ScriptRegistry.outside_scripts) {
try {
sb.append(outsideConfig.saveToString() + "\r\n");
} catch (Exception e) {
dB.echoError("Woah! Error parsing outside scripts!");
HadAnError = true;
}
}
for (File f : files){
String fileName = f.getAbsolutePath().substring(file.getAbsolutePath().length());
dB.echoDebug("Processing '" + fileName + "'... ");
try {
yaml = YamlConfiguration.loadConfiguration(f);
String saved = yaml.saveToString();
if (yaml != null && saved.length() > 0)
sb.append(saved + "\r\n");
else {
dB.echoError(ChatColor.RED + "Woah! Error parsing " + fileName + "! This script has been skipped. See console for YAML errors.");
HadAnError = true;
}
} catch (RuntimeException e) {
dB.echoError(ChatColor.RED + "Woah! Error parsing " + fileName + "!");
HadAnError = true;
if (dB.showStackTraces) {
dB.echoDebug("STACKTRACE follows:");
e.printStackTrace();
}
else dB.echoDebug("Use '/denizen debug -s' for the nitty-gritty.");
}
}
dB.echoApproval("All scripts loaded!");
return yamlKeysToUpperCase(sb.toString());
} else {
dB.echoError(ChatColor.RED + "Woah! No scripts in /plugins/Denizen/scripts/ to load!");
HadAnError = true;
}
} catch (Exception error) {
dB.echoError(ChatColor.RED + "Woah! No script folder found in /plugins/Denizen/scripts/");
HadAnError = true;
if (dB.showStackTraces) error.printStackTrace();
}
return "";
}
/**
* Changes YAML 'keys' to all Upper Case to de-sensitize case sensitivity when
* reading and parsing scripts.
*/
private static String yamlKeysToUpperCase(String string) {
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("(^.*?[^\\s](:\\s))", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);
while (matcher.find())
matcher.appendReplacement(sb, matcher.group().toUpperCase());
matcher.appendTail(sb);
return sb.toString();
}
}