From b954a682c2f73152c28a7ce5da303ff20e1da0cb Mon Sep 17 00:00:00 2001 From: aabssmc Date: Mon, 27 May 2024 19:50:50 -0700 Subject: [PATCH] Add EffDownloadFile --- src/main/java/lol/aabss/skhttp/SkHttp.java | 2 + .../elements/effects/EffDownloadFile.java | 87 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/main/java/lol/aabss/skhttp/elements/effects/EffDownloadFile.java diff --git a/src/main/java/lol/aabss/skhttp/SkHttp.java b/src/main/java/lol/aabss/skhttp/SkHttp.java index 557860a..92f001a 100644 --- a/src/main/java/lol/aabss/skhttp/SkHttp.java +++ b/src/main/java/lol/aabss/skhttp/SkHttp.java @@ -10,6 +10,7 @@ public final class SkHttp extends JavaPlugin { public static HttpResponse LAST_RESPONSE; + public static SkHttp instance; @Override public void onEnable() { @@ -19,6 +20,7 @@ public void onEnable() { Skript.registerAddon(this) .loadClasses("lol.aabss.skhttp", "elements"); metrics.addCustomChart(new Metrics.SimplePie("skript_version", () -> Skript.getVersion().toString())); + instance = this; getLogger().info("SkHttp load success"); } catch (IOException e) { throw new RuntimeException(e); diff --git a/src/main/java/lol/aabss/skhttp/elements/effects/EffDownloadFile.java b/src/main/java/lol/aabss/skhttp/elements/effects/EffDownloadFile.java new file mode 100644 index 0000000..7be9770 --- /dev/null +++ b/src/main/java/lol/aabss/skhttp/elements/effects/EffDownloadFile.java @@ -0,0 +1,87 @@ +package lol.aabss.skhttp.elements.effects; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Effect; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.util.Kleenean; +import org.bukkit.Bukkit; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +import static lol.aabss.skhttp.SkHttp.instance; + +@Name("Download file from URL") +@Description({ + "Downloads all data from a website and will be stored as a file with any given extension", + "Will be stored in the data folder." +}) +@Examples({ + "download file from \"https://i.imgur.com/h8iRx75.png\" with name \"image.png\"" +}) +@Since("1.2") +public class EffDownloadFile extends Effect { + + static { + Skript.registerEffect(EffDownloadFile.class, + "download file from %httpresponse/httprequest/string% (named|with name) %string%" + ); + } + + private Expression url; + private Expression name; + + @Override + protected void execute(Event e) { + if (name == null || url == null){ + return; + } + String name = this.name.getSingle(e); + Object urlString = this.url.getSingle(e); + if (name != null && urlString != null) { + URL url; + InputStream in; + try { + if (urlString instanceof HttpResponse) { + url = ((HttpResponse) urlString).uri().toURL(); + } else if (urlString instanceof HttpRequest) { + url = ((HttpRequest) urlString).uri().toURL(); + } else if (urlString instanceof String) { + url = new URL((String) urlString); + } else { + return; + } + in = url.openStream(); + Files.copy(in, new File(instance.getDataFolder().getAbsolutePath(), name).toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + + @Override + public @NotNull String toString(@Nullable Event e, boolean debug) { + return "download file from url"; + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + url = (Expression) exprs[0]; + name = (Expression) exprs[1]; + return true; + } +}