Skip to content

Commit

Permalink
Add EffDownloadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
aabssmc committed May 28, 2024
1 parent 3562045 commit b954a68
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/lol/aabss/skhttp/SkHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public final class SkHttp extends JavaPlugin {

public static HttpResponse<?> LAST_RESPONSE;
public static SkHttp instance;

@Override
public void onEnable() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> url;
private Expression<String> 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<Object>) exprs[0];
name = (Expression<String>) exprs[1];
return true;
}
}

0 comments on commit b954a68

Please sign in to comment.