Skip to content

Commit

Permalink
Fix deleted temporary variables (fixes btk5h#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
kulkalkul committed Dec 3, 2020
1 parent 0fbd7cc commit 7ae4609
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/main/java/com/btk5h/reqn/skript/EffRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@
import com.btk5h.reqn.Reqn;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.io.*;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.effects.Delay;
Expand All @@ -57,6 +57,7 @@
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.util.Kleenean;
import ch.njol.skript.variables.Variables;

import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -96,23 +97,39 @@ public class EffRequest extends Effect {
private Expression<String> headers;
private Expression<String> body;

private final HashMap<Event, Object> keptVariables = new HashMap<>();

@SuppressWarnings("unchecked")
@Override
protected void execute(Event e) {
CompletableFuture.supplyAsync(() -> sendRequest(e), threadPool)
.whenComplete((resp, err) -> {
if (err != null) {
err.printStackTrace();
lastResponse = null;
return;
}

Bukkit.getScheduler().runTask(Reqn.getInstance(), () -> {
lastResponse = resp;
if (getNext() != null) {
TriggerItem.walk(getNext(), e);
try {
Field localVariablesField = Variables.class.getDeclaredField("localVariables");
localVariablesField.setAccessible(true);
HashMap<Event, Object> localVariables =
(HashMap<Event, Object>) localVariablesField.get(Variables.class);
localVariablesField.setAccessible(false);

Object variablesMap = localVariables.get(e);
keptVariables.put(e, variablesMap);

CompletableFuture.supplyAsync(() -> sendRequest(e), threadPool)
.whenComplete((resp, err) -> {
if (err != null) {
err.printStackTrace();
lastResponse = null;
return;
}

Bukkit.getScheduler().runTask(Reqn.getInstance(), () -> {
lastResponse = resp;
if (getNext() != null) {
TriggerItem.walk(getNext(), e);
}
});
});
});
} catch (NoSuchFieldException | IllegalAccessException err) {
err.printStackTrace();
}
}

@Override
Expand All @@ -134,13 +151,19 @@ private void delay(Event e) {
}

private HttpResponse sendRequest(Event e) {

Object variablesMap = keptVariables.get(e);
keptVariables.remove(e);
Variables.setLocalVariables(e, variablesMap);

String method = null;
if (this.method != null) {
method = this.method.getSingle(e).toUpperCase();
}

String url = this.url.getSingle(e);
if (url == null) {
Variables.removeLocals(e);
return null;
}
url = url.replace('§', '&');
Expand All @@ -161,7 +184,7 @@ private HttpResponse sendRequest(Event e) {
conn = (HttpURLConnection) target.openConnection();

conn.setRequestProperty("User-Agent", String.format("Reqn/%s (https://github.com/btk5h/reqn)",
Reqn.getInstance().getDescription().getVersion()));
Reqn.getInstance().getDescription().getVersion()));

for (String header : headers) {
Matcher headerMatcher = HEADER.matcher(header);
Expand Down Expand Up @@ -219,6 +242,7 @@ private HttpResponse sendRequest(Event e) {
}
}

Variables.removeLocals(e);
return new HttpResponse(conn.getResponseCode(), conn.getResponseMessage(), statusLine,
responseHeaders, responseBody.toString());
} catch (MalformedURLException err) {
Expand All @@ -230,7 +254,7 @@ private HttpResponse sendRequest(Event e) {
conn.disconnect();
}
}

Variables.removeLocals(e);
return null;
}

Expand Down

0 comments on commit 7ae4609

Please sign in to comment.