From efa37217c286464b33a58e6f1f7916a2e77913a3 Mon Sep 17 00:00:00 2001 From: Govindass Date: Mon, 18 May 2020 13:20:01 +0300 Subject: [PATCH] Add synchronous support on current thread --- build.gradle | 3 +- .../skriptdb/skript/EffExecuteStatement.java | 63 ++++++++++++------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/build.gradle b/build.gradle index 097aebd..0fdc2c1 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,7 @@ task buildReadme(type: Javadoc) { options.addStringOption('file', 'README.md') options.addStringOption('markdown', '-quiet') } + task fatJar(type: Jar) { manifest { attributes 'Implementation-Title': 'Gradle Jar File Example', @@ -52,4 +53,4 @@ task fatJar(type: Jar) { baseName = project.name + '-all' from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } with jar -} +} \ No newline at end of file diff --git a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java index 2adc842..390ec2a 100644 --- a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java +++ b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java @@ -34,7 +34,7 @@ import java.util.concurrent.Executors; * variable in the form `{test::::}` * * @name Execute Statement - * @pattern execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) + * @pattern [synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) * [the] [var[iable]] %-objects%] * @example execute "select * from table" in {sql} and store the result in {output::*} * @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*} @@ -44,6 +44,7 @@ public class EffExecuteStatement extends Delay { static { Skript.registerEffect(EffExecuteStatement.class, "execute %string% (in|on) %datasource% " + + "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]", "synchronously execute %string% (in|on) %datasource% " + "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]"); } @@ -58,7 +59,17 @@ public class EffExecuteStatement extends Delay { private boolean isLocal; private boolean isList; private Map doLater = new HashMap<>(); + private boolean isSync; + private void continueScriptExecution(Event e, String res) { + lastError = res; + + if (getNext() != null) { + doLater.forEach((name, value) -> setVariable(e, name, value)); + doLater.clear(); + TriggerItem.walk(getNext(), e); + } + } @Override protected void execute(Event e) { DataSource ds = dataSource.getSingle(e); @@ -67,35 +78,41 @@ public class EffExecuteStatement extends Delay { if (ds == null) return; + if (isSync) { + String result = executeStatement(ds, baseVariable, query); + continueScriptExecution(e, result); + } else { + Object locals = Variables.removeLocals(e); + CompletableFuture sql = + CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool); - Object locals = Variables.removeLocals(e); - CompletableFuture sql = - CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool); - - sql.whenComplete((res, err) -> { - if (err != null) { - err.printStackTrace(); - } - - Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> { - lastError = res; - - if (getNext() != null) { - if (locals != null) - Variables.setLocalVariables(e, locals); - doLater.forEach((name, value) -> setVariable(e, name, value)); - doLater.clear(); - TriggerItem.walk(getNext(), e); - Variables.removeLocals(e); + sql.whenComplete((res, err) -> { + if (err != null) { + err.printStackTrace(); } + + Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> { + lastError = res; + + if (getNext() != null) { + if (locals != null) + Variables.setLocalVariables(e, locals); + doLater.forEach((name, value) -> setVariable(e, name, value)); + doLater.clear(); + TriggerItem.walk(getNext(), e); + Variables.removeLocals(e); + } + }); }); - }); + } } @Override protected TriggerItem walk(Event e) { debug(e, true); - Delay.addDelayedEvent(e); + if (!isSync) { + Delay.addDelayedEvent(e); + } execute(e); return null; } @@ -260,6 +277,8 @@ public class EffExecuteStatement extends Delay { } dataSource = (Expression) exprs[1]; Expression expr = exprs[2]; + System.out.println(matchedPattern); + isSync = matchedPattern == 1; if (expr instanceof Variable) { Variable varExpr = (Variable) expr; var = varExpr.getName();