-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathJavascriptPlaceholder.java
More file actions
198 lines (162 loc) · 6.33 KB
/
JavascriptPlaceholder.java
File metadata and controls
198 lines (162 loc) · 6.33 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
/*
*
* 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.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.logging.Level;
public class JavascriptPlaceholder {
private final String DIRECTORY = PlaceholderAPIPlugin.getInstance().getDataFolder() + "/javascripts/javascript_data";
private final ScriptEngine engine;
private final String identifier;
private final String script;
private final boolean dataPersists;
private ScriptData scriptData;
private final File dataFile;
private YamlConfiguration yaml;
public JavascriptPlaceholder(ScriptEngine engine, String identifier, String script, boolean dataPersists) {
Validate.notNull(engine, "ScriptEngine can not be null");
Validate.notNull(identifier, "Identifier can not be null");
Validate.notNull(script, "Script can not be null");
this.engine = engine;
this.identifier = identifier;
this.script = script;
this.dataPersists = dataPersists;
final File directory = new File(DIRECTORY);
if (!directory.exists()) {
directory.mkdirs();
}
scriptData = new ScriptData();
dataFile = new File(directory, identifier + "_data.yml");
engine.put("Data", scriptData);
engine.put("BukkitServer", Bukkit.getServer());
engine.put("Expansion", JavascriptExpansion.getInstance());
engine.put("Placeholder", this);
engine.put("PlaceholderAPI", PlaceholderAPI.class);
}
public String getIdentifier() {
return identifier;
}
public String getScript() {
return script;
}
public String evaluate(OfflinePlayer player, String... args) {
String exp = PlaceholderAPI.setPlaceholders(player, script);
try {
String[] arguments = null;
if (args != null && args.length > 0) {
arguments = new String[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] == null || args[i].isEmpty()) {
continue;
}
arguments[i] = PlaceholderAPI.setBracketPlaceholders(player, args[i]);
}
}
if (arguments == null) {
arguments = new String[]{};
}
engine.put("args", arguments);
if (player != null && player.isOnline()) {
engine.put("BukkitPlayer", player.getPlayer());
engine.put("Player", player.getPlayer());
}
engine.put("OfflinePlayer", player);
Object result = engine.eval(exp);
return result != null ? PlaceholderAPI.setBracketPlaceholders(player, result.toString()) : "";
} catch (ScriptException ex) {
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript] An error occurred while executing the script '" + identifier + "'", ex);
}
return "Script error! (check console)";
}
public ScriptData getData() {
// this should never be null but just in case setData(null) is called
if (scriptData == null) {
scriptData = new ScriptData();
}
return scriptData;
}
public void setData(ScriptData data) {
this.scriptData = data;
}
public boolean loadData() {
yaml = new YamlConfiguration();
dataFile.getParentFile().mkdirs();
if (!dataFile.exists()) {
try {
dataFile.createNewFile();
} catch (IOException e) {
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while creating data file for " + getIdentifier(), e);
return false;
}
}
try {
yaml.load(dataFile);
} catch (IOException | InvalidConfigurationException e) {
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while loading for " + getIdentifier(), e);
return false;
}
final Set<String> keys = yaml.getKeys(true);
if (keys.size() == 0) {
return false;
}
if (scriptData == null) {
scriptData = new ScriptData();
} else {
scriptData.clear();
}
keys.forEach(key -> scriptData.set(key, yaml.get(key)));
if (!scriptData.isEmpty()) {
this.setData(scriptData);
return true;
}
return false;
}
public boolean saveData() {
if (!dataPersists || scriptData == null || scriptData.isEmpty() || yaml == null) {
return false;
}
scriptData.getData().forEach((key, value) -> yaml.set(key, value));
try {
yaml.save(dataFile);
return true;
} catch (IOException e) {
PlaceholderAPIPlugin.getInstance().getLogger().log(Level.SEVERE, "[JavaScript Expansion] An error occurred while saving data for " + getIdentifier(), e);
return false;
}
}
public void cleanup() {
if (this.scriptData != null) {
this.scriptData.clear();
this.scriptData = null;
}
this.yaml = null;
}
}