Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deleted temporary variables and update gradle and gradle dependencies #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@
import org.apache.tools.ant.filters.ReplaceTokens

group 'com.btk5h'
version '1.2.3'
version '1.2.5'

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'
sourceCompatibility = 1.8

repositories {
mavenCentral()
maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
url 'https://papermc.io/repo/repository/maven-public/'
}
maven {
url 'https://oss.sonatype.org/content/groups/public/'
}
maven {
url 'http://maven.njol.ch/repo/'
url 'http://jitpack.io/'
}
}

Expand All @@ -52,6 +50,7 @@ processResources {
}

dependencies {
compile 'org.spigotmc:spigot-api:1.11-R0.1-SNAPSHOT'
compile 'ch.njol:skript:2.2-SNAPSHOT'
compileOnly 'org.eclipse.jdt:org.eclipse.jdt.annotation:2.0.0'
compileOnly 'com.destroystokyo.paper:paper-api:1.16.4-R0.1-SNAPSHOT'
compileOnly 'com.github.SkriptLang:Skript:2.5.1'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
59 changes: 42 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,26 @@
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.concurrent.ConcurrentHashMap;
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 +58,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 +98,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);
ConcurrentHashMap<Event, Object> localVariables =
(ConcurrentHashMap<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 +152,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 +185,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 +243,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 +255,7 @@ private HttpResponse sendRequest(Event e) {
conn.disconnect();
}
}

Variables.removeLocals(e);
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/btk5h/reqn/skript/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public boolean mustSyncDeserialization() {
}

@Override
public boolean canBeInstantiated(Class<? extends HttpResponse> c) {
public boolean canBeInstantiated() {
return false;
}
}));
Expand Down