mirror of
https://github.com/PlaceholderAPI/Javascript-Expansion.git
synced 2025-05-23 18:42:44 +00:00
Ability to download verified .js examples from the js expansion github repo
This commit is contained in:
parent
8327f7e671
commit
f1c65e7ee3
@ -20,7 +20,10 @@
|
||||
*/
|
||||
package com.extendedclip.papi.expansion.javascript;
|
||||
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScript;
|
||||
import com.extendedclip.papi.expansion.javascript.cloud.GithubScriptManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -35,6 +38,7 @@ import me.clip.placeholderapi.expansion.Cacheable;
|
||||
import me.clip.placeholderapi.expansion.Configurable;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -47,19 +51,16 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
private ScriptEngine globalEngine = null;
|
||||
|
||||
private JavascriptPlaceholdersConfig config;
|
||||
|
||||
private final Set<JavascriptPlaceholder> scripts = new HashSet<>();
|
||||
|
||||
private final String VERSION = getClass().getPackage().getImplementationVersion();
|
||||
|
||||
private static JavascriptExpansion instance;
|
||||
private boolean debug = false;
|
||||
private GithubScriptManager githubScripts = null;
|
||||
|
||||
public JavascriptExpansion() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private boolean debug = false;
|
||||
|
||||
/*
|
||||
* I am just testing the waters here because there is no command system for expansions...
|
||||
*/
|
||||
@ -88,6 +89,9 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
msg(p, "&r");
|
||||
msg(p, "&7/papijsp reload &7- &fReload your javascripts without reloading PlaceholderAPI");
|
||||
msg(p, "&7/papijsp list &7- &fList loaded script identifiers.");
|
||||
msg(p, "&7/papijsp git download <name> &7- &fDownload a script from the js expansion github.");
|
||||
msg(p, "&7/papijsp git list &7- &fList available scripts in the js expansion github.");
|
||||
msg(p, "&7/papijsp git info (name) &7- &fGet the description and url of a specific script.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -105,11 +109,68 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
return;
|
||||
}
|
||||
|
||||
event.getPlayer().sendMessage("&cIncorrect usage &7- &f/papijsp");
|
||||
if (msg.equals("/papijsp git list")) {
|
||||
msg(p, GithubScript.values().length + " &7script"
|
||||
+ (GithubScript.values().length == 1 ? "" : "s") + " available on Github.");
|
||||
msg(p, String.join(", ", GithubScript.getAllScriptNames()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.startsWith("/papijsp git info ")) {
|
||||
|
||||
if (this.githubScripts == null) {
|
||||
msg(p, "This feature is disabled in the PAPI config!");
|
||||
return;
|
||||
}
|
||||
|
||||
msg = msg.replace("/papijsp git info ", "");
|
||||
|
||||
GithubScript script = GithubScript.getScript(msg);
|
||||
|
||||
if (script == null) {
|
||||
msg(p, "&cThe script &7" + msg + " &cdoes not exist!");
|
||||
return;
|
||||
}
|
||||
|
||||
msg(p, "&7Name: &f" + script.getName(),
|
||||
"&7Version: &f" + script.getVersion(),
|
||||
"&7Description: &f" + script.getDescription(),
|
||||
"&7Url: &f" + script.getUrl());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.startsWith("/papijsp git download ")) {
|
||||
|
||||
if (this.githubScripts == null) {
|
||||
msg(p, "This feature is disabled in the PAPI config!");
|
||||
return;
|
||||
}
|
||||
|
||||
msg = msg.replace("/papijsp git download ", "");
|
||||
|
||||
GithubScript script = GithubScript.getScript(msg);
|
||||
|
||||
if (script == null) {
|
||||
msg(p, "&cThe script &7" + msg + " &cdoes not exist!");
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(getPlaceholderAPI(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
githubScripts.downloadScript(script);
|
||||
}
|
||||
});
|
||||
msg(p, "&aDownload initiated... Check the scripts folder in a moment...");
|
||||
return;
|
||||
}
|
||||
|
||||
msg(p, "&cIncorrect usage &7- &f/papijsp");
|
||||
}
|
||||
|
||||
public void msg(Player p, String text) {
|
||||
p.sendMessage(ChatColor.translateAlternateColorCodes('&', text));
|
||||
public void msg(Player p, String... text) {
|
||||
Arrays.stream(text).forEach(line -> p.sendMessage(ChatColor.translateAlternateColorCodes('&', line)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -161,6 +222,9 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
System.out.println("names: " + factory.getNames());
|
||||
}
|
||||
}
|
||||
if ((Boolean) get("github_script_downloads", false)) {
|
||||
githubScripts = new GithubScriptManager(this);
|
||||
}
|
||||
return super.register();
|
||||
}
|
||||
|
||||
@ -238,11 +302,16 @@ public class JavascriptExpansion extends PlaceholderExpansion implements Cacheab
|
||||
return globalEngine;
|
||||
}
|
||||
|
||||
public JavascriptPlaceholdersConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getDefaults() {
|
||||
Map<String, Object> def = new HashMap<String, Object>();
|
||||
def.put("engine", "javascript");
|
||||
def.put("debug", false);
|
||||
def.put("github_script_downloads", false);
|
||||
return def;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.extendedclip.papi.expansion.javascript.cloud;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum GithubScript {
|
||||
HAS_PERMISSION("has_permission",
|
||||
"1.0.0",
|
||||
"aBooDyy",
|
||||
"Check if player have \"permission.test\" permission and send a custom message.",
|
||||
"https://raw.githubusercontent.com/PlaceholderAPI/Javascript-Expansion/master/scripts/has_permission.js"),
|
||||
;
|
||||
|
||||
private String name, version, author, description, url;
|
||||
|
||||
private GithubScript(String name, String version, String author, String description, String url) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
this.author = author;
|
||||
this.description = description;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public static List<String> getAllScriptNames() {
|
||||
return Arrays.stream(values()).map(GithubScript::getName).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static GithubScript getScript(String name) {
|
||||
return Arrays.stream(values()).filter(s -> { return s.getName().equalsIgnoreCase(name); } ).findFirst().orElse(null);
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.extendedclip.papi.expansion.javascript.cloud;
|
||||
|
||||
import com.extendedclip.papi.expansion.javascript.JavascriptExpansion;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GithubScriptManager {
|
||||
|
||||
private JavascriptExpansion expansion;
|
||||
private String path;
|
||||
|
||||
public GithubScriptManager(JavascriptExpansion expansion) {
|
||||
this.expansion = expansion;
|
||||
path = expansion.getPlaceholderAPI().getDataFolder()
|
||||
+ File.separator
|
||||
+ "javascripts"
|
||||
+ File.separator;
|
||||
}
|
||||
|
||||
public void downloadScript(GithubScript script) {
|
||||
List<String> contents = read(script.getUrl());
|
||||
if (contents == null || contents.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
File f = new File(path, script.getName() + ".js");
|
||||
try (PrintStream out = new PrintStream(new FileOutputStream(f))) {
|
||||
contents.forEach(l -> out.println(l));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
expansion.getConfig().load().set(script.getName() + ".file", script.getName() +".js");
|
||||
expansion.getConfig().save();
|
||||
}
|
||||
|
||||
private List<String> read(String url) {
|
||||
|
||||
List<String> lines = new ArrayList<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(new URL(url).openStream()))) {
|
||||
|
||||
String inputLine;
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
lines.add(inputLine);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user