-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathJavascriptPlaceholdersConfig.java
More file actions
221 lines (183 loc) · 8.08 KB
/
JavascriptPlaceholdersConfig.java
File metadata and controls
221 lines (183 loc) · 8.08 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
*
* Javascript-Expansion
* Copyright (C) 2020 Ryan McCarthy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package com.extendedclip.papi.expansion.javascript;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
public class JavascriptPlaceholdersConfig {
private JavascriptExpansion ex;
private PlaceholderAPIPlugin plugin;
private FileConfiguration config;
private File file;
public JavascriptPlaceholdersConfig(JavascriptExpansion ex) {
this.ex = ex;
plugin = ex.getPlaceholderAPI();
reload();
}
public void reload() {
if (file == null) {
file = new File(plugin.getDataFolder(), "javascript_placeholders.yml");
}
config = YamlConfiguration.loadConfiguration(file);
config.options().header("Javascript Expansion: " + ex.getVersion()
+ "\nThis is the main configuration file for the Javascript Expansion."
+ "\n"
+ "\nYou will define your javascript placeholders in this file."
+ "\n"
+ "\nJavascript files must be located in the:"
+ "\n /plugins/placeholderapi/javascripts/ folder"
+ "\n"
+ "\nA detailed guide on how to create your own javascript placeholders"
+ "\ncan be found here:"
+ "\nhttps://github.com/PlaceholderAPI-Expansions/Javascript-Expansion/wiki"
+ "\n"
+ "\nYour javascript placeholders will be identified by: %javascript_<identifier>%"
+ "\n"
+ "\nConfiguration format:"
+ "\n"
+ "\n<identifier>:"
+ "\n file: <name of file>.<file extension>"
+ "\n engine: (name of script engine)"
+ "\n"
+ "\n"
+ "\nExample:"
+ "\n"
+ "\n'my_placeholder':"
+ "\n file: 'my_placeholder.js'"
+ "\n engine: 'nashorn'");
if (config.getKeys(false).isEmpty()) {
config.set("example.file", "example.js");
config.set("example.engine", "nashorn");
}
save();
}
public FileConfiguration load() {
if (config == null) {
reload();
}
return config;
}
public void save() {
if ((config == null) || (file == null)) {
return;
}
try {
load().save(file);
} catch (IOException ex) {
plugin.getLogger().log(Level.SEVERE, "Could not save to " + file, ex);
}
}
public int loadPlaceholders() {
if (config == null || config.getKeys(false).isEmpty()) {
return 0;
}
final File directory = new File(plugin.getDataFolder(), "javascripts");
try {
if (!directory.exists()) {
directory.mkdirs();
plugin.getLogger().info("[JavaScript Expansion] Creating directory: " + directory.getPath());
}
} catch (SecurityException e) {
plugin.getLogger().log(Level.SEVERE, "[JavaScript Expansion] Could not create directory: " + directory.getPath(), e);
}
for (String identifier : config.getKeys(false)) {
if (!config.contains(identifier + ".file") || config.getString(identifier + ".file") == null) {
plugin.getLogger().warning("[JavaScript Expansion] Javascript placeholder: " + identifier + " does not have a file specified");
continue;
}
File scriptFile = new File(plugin.getDataFolder() + "/javascripts", config.getString(identifier + ".file"));
if (!scriptFile.exists()) {
plugin.getLogger().info("[JavaScript Expansion] " +scriptFile.getName() + " does not exist. Creating file...");
try {
scriptFile.createNewFile();
plugin.getLogger().info("[JavaScript Expansion] " + scriptFile.getName() + " created! Add your javascript to this file and use '/jsexpansion reload' to load it!");
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while creating " + scriptFile.getName(), e);
}
continue;
}
String script = getContents(scriptFile);
if (script == null || script.isEmpty()) {
plugin.getLogger().warning("[JavaScript Expansion] File: " + scriptFile.getName() + " for javascript placeholder: " + identifier + " is empty");
continue;
}
ScriptEngine engine;
if (!config.contains(identifier + ".engine")) {
engine = ex.getGlobalEngine();
} else {
try {
engine = new ScriptEngineManager().getEngineByName(config.getString(identifier + ".engine", "nashorn"));
} catch (NullPointerException e) {
plugin.getLogger().warning("[JavaScript Expansion] ScriptEngine type for javascript placeholder: " + identifier + " is invalid! Defaulting to global");
engine = ex.getGlobalEngine();
}
}
if (engine == null) {
plugin.getLogger().warning("[JavaScript Expansion] Failed to set ScriptEngine for javascript placeholder: " + identifier);
continue;
}
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(engine, identifier, script, (!config.contains(identifier + ".data_persists")) || config.getBoolean(identifier + ".data_persists"));
final boolean added = ex.addJSPlaceholder(placeholder);
if (added) {
if (placeholder.loadData()) {
plugin.getLogger().info("[JavaScript Expansion] Loaded data for javascript placeholder: " + identifier);
}
plugin.getLogger().info("[JavaScript Expansion] %javascript_" + identifier + "% has been loaded!");
} else {
plugin.getLogger().warning("[JavaScript Expansion] Javascript placeholder %javascript_" + identifier + "% is a duplicate!");
}
}
return ex.getAmountLoaded();
}
private String getContents(File file) {
final StringBuilder sb = new StringBuilder();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line == null || line.isEmpty()) {
continue;
}
line = line.trim();
/* temp fix for single line comments
* doesnt solve every case though..
* lines that start with code and may have a comment afterward still screw stuff up...
*/
if (line.startsWith("//")) {
continue;
}
sb.append(line).append(" ");
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return sb.toString();
}
}