Fix no vars if execute is last line & fix code cleanup

This commit is contained in:
Govindas 2022-12-08 19:58:06 +02:00
parent f92b16a09f
commit eb89699ed5

View File

@ -60,7 +60,8 @@ public class EffExecuteStatement extends Effect {
private VariableString var; private VariableString var;
private boolean isLocal; private boolean isLocal;
private boolean isList; private boolean isList;
private boolean isSync; private boolean quickly;
private boolean isSync = false;
private void continueScriptExecution(Event e, Object populatedVariables) { private void continueScriptExecution(Event e, Object populatedVariables) {
lastError = null; lastError = null;
@ -82,19 +83,10 @@ public class EffExecuteStatement extends Effect {
String baseVariable = var != null ? var.toString(e).toLowerCase(Locale.ENGLISH) : null; String baseVariable = var != null ? var.toString(e).toLowerCase(Locale.ENGLISH) : null;
//if data source isn't set //if data source isn't set
if (ds == null) return; if (ds == null) return;
//if current thread is not main thread, then make this query to not have delays
boolean sync = !Bukkit.isPrimaryThread();
if (sync) {
isSync = true;
} else if (isSync) {
sync = true;
}
Object locals = Variables.removeLocals(e); Object locals = Variables.removeLocals(e);
//execute SQL statement //execute SQL statement
if (!sync) { if (Bukkit.isPrimaryThread()) {
CompletableFuture<Object> sql = CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool); CompletableFuture<Object> sql = CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
sql.whenComplete((res, err) -> { sql.whenComplete((res, err) -> {
if (err != null) { if (err != null) {
@ -105,12 +97,12 @@ public class EffExecuteStatement extends Effect {
if (res instanceof String) { if (res instanceof String) {
lastError = (String) res; lastError = (String) res;
} }
if (getNext() != null) {
//if local variables are present //if local variables are present
//bring back local variables //bring back local variables
//populate SQL data into variables //populate SQL data into variables
if (!quickly) {
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> { Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
if (locals != null) { if (locals != null && getNext() != null) {
Variables.setLocalVariables(e, locals); Variables.setLocalVariables(e, locals);
} }
if (!(res instanceof String)) { if (!(res instanceof String)) {
@ -120,21 +112,31 @@ public class EffExecuteStatement extends Effect {
//the line below is required to prevent memory leaks //the line below is required to prevent memory leaks
Variables.removeLocals(e); Variables.removeLocals(e);
}); });
} else {
if (locals != null && getNext() != null) {
Variables.setLocalVariables(e, locals);
}
if (!(res instanceof String)) {
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
}
TriggerItem.walk(getNext(), e);
//the line below is required to prevent memory leaks
Variables.removeLocals(e);
} }
}); });
// sync executed SQL query, same as above, just sync // sync executed SQL query, same as above, just sync
} else { } else {
isSync = true;
Object resources = executeStatement(ds, baseVariable, query); Object resources = executeStatement(ds, baseVariable, query);
//handle last error syntax data //handle last error syntax data
lastError = null; lastError = null;
if (resources instanceof String) { if (resources instanceof String) {
lastError = (String) resources; lastError = (String) resources;
} }
if (getNext() != null) {
//if local variables are present //if local variables are present
//bring back local variables //bring back local variables
//populate SQL data into variables //populate SQL data into variables
if (locals != null) { if (locals != null && getNext() != null) {
Variables.setLocalVariables(e, locals); Variables.setLocalVariables(e, locals);
} }
if (!(resources instanceof String)) { if (!(resources instanceof String)) {
@ -144,12 +146,11 @@ public class EffExecuteStatement extends Effect {
Variables.removeLocals(e); Variables.removeLocals(e);
} }
} }
}
@Override @Override
protected TriggerItem walk(Event e) { protected TriggerItem walk(Event e) {
debug(e, true); debug(e, true);
if (!isSync) { if (!quickly || !isSync) {
Delay.addDelayedEvent(e); Delay.addDelayedEvent(e);
} }
execute(e); execute(e);
@ -335,7 +336,7 @@ public class EffExecuteStatement extends Effect {
} }
dataSource = (Expression<HikariDataSource>) exprs[1]; dataSource = (Expression<HikariDataSource>) exprs[1];
Expression<?> expr = exprs[2]; Expression<?> expr = exprs[2];
isSync = matchedPattern == 1; quickly = matchedPattern == 1;
if (expr instanceof Variable) { if (expr instanceof Variable) {
Variable<?> varExpr = (Variable<?>) expr; Variable<?> varExpr = (Variable<?>) expr;
var = varExpr.getName(); var = varExpr.getName();