-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathJavascriptExpansionCommands.java
More file actions
294 lines (242 loc) · 11.9 KB
/
JavascriptExpansionCommands.java
File metadata and controls
294 lines (242 loc) · 11.9 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
*
* 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 com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
public class JavascriptExpansionCommands extends Command {
private final JavascriptExpansion expansion;
private final String PERMISSION = "placeholderapi.js.admin";
private final String command;
public JavascriptExpansionCommands(JavascriptExpansion expansion) {
super("jsexpansion");
command = getName();
this.expansion = expansion;
this.setDescription("Javascript expansion commands");
this.setUsage("/" + command + " <args>");
this.setPermission(PERMISSION);
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (!sender.hasPermission(PERMISSION)) {
return Collections.emptyList();
}
final List<String> commands = new ArrayList<>(Arrays.asList("list", "parse", "reload"));
final List<String> completion = new ArrayList<>();
if (expansion.getGithubScriptManager() != null) {
commands.add(0, "git");
}
if (args.length == 1) {
return StringUtil.copyPartialMatches(args[0], commands, completion);
}
if (args[0].equalsIgnoreCase("git")) {
if (expansion.getGithubScriptManager() == null) {
return Collections.emptyList();
}
if (args.length == 2) {
return StringUtil.copyPartialMatches(args[1], Arrays.asList("download", "enable", "info", "list", "refresh"), completion);
}
if (args.length == 3 && args[1].equalsIgnoreCase("download")) {
if (expansion.getGithubScriptManager().getAvailableScripts() == null) {
return Collections.emptyList();
}
return StringUtil.copyPartialMatches(args[2], expansion.getGithubScriptManager().getAvailableScripts().stream().map(GithubScript::getName).collect(Collectors.toList()), completion);
}
}
return Collections.emptyList();
}
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (!sender.hasPermission(PERMISSION)) {
msg(sender, "&cYou don't have permission to do that!");
return true;
}
if (args.length == 0) {
msg(sender,
"&eJavascript expansion &7v: &f" + expansion.getVersion(),
"&eCreated by: &f" + expansion.getAuthor(),
"&eWiki: &fhttps://github.com/PlaceholderAPI/Javascript-Expansion/wiki",
"&r",
"&e/" + command + " reload &7- &fReload your javascripts without reloading PlaceholderAPI.",
"&e/" + command + " list &7- &fList loaded script identifiers.",
"&e/" + command + " parse [me/player] [code] &7- &fTest JavaScript code in chat."
);
if (expansion.getGithubScriptManager() != null) {
msg(sender,
"&e/" + command + " git refresh &7- &fRefresh available Github scripts",
"&e/" + command + " git download [name] &7- &fDownload a script from the js expansion github.",
"&e/" + command + " git list &7- &fList available scripts in the js expansion github.",
"&e/" + command + " git info [name] &7- &fGet the description and url of a specific script."
);
}
return true;
}
switch (args[0].toLowerCase()) {
case "git": {
if (expansion.getGithubScriptManager() == null) {
msg(sender, "&cThis feature is disabled in the PlaceholderAPI config.");
return true;
}
if (args.length < 2) {
msg(sender, "&cIncorrect usage! Type '&f/" + command + "&c' for more help.");
return true;
}
final GithubScriptManager manager = expansion.getGithubScriptManager();
switch (args[1].toLowerCase()) {
case "refresh": {
expansion.getGithubScriptManager().fetch();
msg(sender, "&aFetching available scripts... Check back in a sec!");
return true;
}
case "list": {
final List<GithubScript> availableScripts = manager.getAvailableScripts();
final Set<String> scripts = availableScripts.stream().map(GithubScript::getName).collect(Collectors.toSet());
msg(sender, availableScripts.size() + " &escript" + plural(availableScripts.size()) + " available on Github.", String.join(", ", scripts));
return true;
}
case "info": {
if (args.length < 3) {
msg(sender, "&cIncorrect usage! &f/" + command + " git info [name]");
return true;
}
final GithubScript script = manager.getScript(args[2]);
if (script == null) {
msg(sender, "&cThe script &f" + args[2] + " &cdoes not exist!");
return true;
}
msg(sender,
"&eName: &f" + script.getName(),
"&eVersion: &f" + script.getVersion(),
"&eDescription: &f" + script.getDescription(),
"&eAuthor: &f" + script.getAuthor(),
"&eSource URL: &f" + script.getUrl()
);
return true;
}
case "download": {
if (args.length < 3) {
msg(sender, "&cIncorrect usage! &f/" + command + " git download [name]");
return true;
}
final GithubScript script = manager.getScript(args[2]);
if (script == null) {
msg(sender, "&cThe script &f" + args[2] + " &cdoes not exist!");
return true;
}
if (new File(expansion.getGithubScriptManager().getJavascriptsFolder(), script.getName() + ".js").exists()) {
msg(sender, "&cCould not download " + script.getName() + " because a file with the same name already exist in the javascripts folder.");
return true;
}
manager.downloadScript(script);
msg(sender, "&aDownload started. &eCheck the scripts folder in a moment...");
return true;
}
case "enabled":
if (args.length < 3) {
msg(sender, "&cIncorrect usage! &f/" + command + " git enabled [true/false]");
return true;
}
final boolean enabled = Boolean.parseBoolean(args[2]);
final PlaceholderAPIPlugin papi = expansion.getPlaceholderAPI();
papi.getConfig().set("expansions." + this.getName() + ".github_script_downloads", enabled);
papi.saveConfig();
papi.reloadConfig();
if (!enabled) {
if (expansion.getGithubScriptManager() != null) {
expansion.getGithubScriptManager().clear();
expansion.setGithubScriptManager(null);
}
} else {
if (expansion.getGithubScriptManager() == null) {
expansion.setGithubScriptManager(new GithubScriptManager(expansion));
}
expansion.getGithubScriptManager().fetch();
}
msg(sender, "&6Git script downloads set to: &e" + enabled);
return true;
default: {
msg(sender, "&cIncorrect usage! Type '&f/" + command + "&c' for more help.");
return true;
}
}
}
case "list": {
final List<String> loaded = expansion.getLoadedIdentifiers();
msg(sender,
loaded.size() + " &7script" + plural(loaded.size()) + " loaded.",
String.join(", ", loaded)
);
return true;
}
case "parse": {
if (args.length < 3) {
msg(sender, "&cIncorrect usage! &f/" + command + " parse [me/player] [code]");
return true;
}
final String script = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
final JavascriptPlaceholder placeholder = new JavascriptPlaceholder(expansion.getGlobalEngine(), "parse-command", String.join(" ", script), true);
if ("me".equalsIgnoreCase(args[1])) {
if (!(sender instanceof Player)) {
msg(sender, "&cOnly players can run this command!");
return true;
}
sender.sendMessage(placeholder.evaluate((Player) sender));
return true;
}
final OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
if (!player.hasPlayedBefore() || player.getName() == null) {
msg(sender, "&cUnknown player " + args[1]);
return true;
}
sender.sendMessage(placeholder.evaluate(player));
return true;
}
case "reload": {
msg(sender, "&aJavascriptExpansion reloading...");
final int scripts = expansion.reloadScripts();
msg(sender, scripts + " &7script" + plural(scripts) + " loaded");
return true;
}
default: {
return true;
}
}
}
private String plural(final int amount) {
return amount > 1 ? "s" : "";
}
public void msg(CommandSender sender, String... text) {
if (text == null) {
return;
}
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Arrays.stream(text).filter(Objects::nonNull).collect(Collectors.joining("\n"))));
}
}