Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/lang/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Variable<T> implements Expression<T>, KeyReceiverExpression<T>, Key
private final boolean list;

private final @Nullable Variable<?> source;
private final Map<Event, String[]> cache = new WeakHashMap<>();
private final Map<Event, String[]> cache = Collections.synchronizedMap(new WeakHashMap<>());

@SuppressWarnings("unchecked")
private Variable(VariableString name, Class<? extends T>[] types, boolean local, boolean ephemeral, boolean list, @Nullable Variable<?> source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ExprFunctionCall<T> extends SimpleExpression<T> implements KeyProvi
private final FunctionReference<?> function;
private final Class<? extends T>[] returnTypes;
private final Class<T> returnType;
private final Map<Event, String[]> cache = new WeakHashMap<>();
private final Map<Event, String[]> cache = Collections.synchronizedMap(new WeakHashMap<>());

public ExprFunctionCall(FunctionReference<T> function) {
this(function, function.returnTypes);
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/ch/njol/skript/lang/function/ScriptFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class ScriptFunction<T> extends Function<T> implements ReturnHandler<T> {

private final Trigger trigger;

private boolean returnValueSet;
private T @Nullable [] returnValues;
private String @Nullable [] returnKeys;
private final ThreadLocal<Boolean> returnValueSet = ThreadLocal.withInitial(() -> false);
private final ThreadLocal<T @Nullable []> returnValues = new ThreadLocal<>();
private final ThreadLocal<String @Nullable []> returnKeys = new ThreadLocal<>();

/**
* @deprecated use {@link ScriptFunction#ScriptFunction(Signature, SectionNode)} instead.
Expand Down Expand Up @@ -85,12 +85,12 @@ public ScriptFunction(Signature<T> sign, SectionNode node) {

trigger.execute(event);
ClassInfo<T> returnType = getReturnType();
return returnType != null ? returnValues : null;
return returnType != null ? returnValues.get() : null;
}

@Override
public @NotNull String @Nullable [] returnedKeys() {
return returnKeys;
return returnKeys.get();
}

/**
Expand All @@ -99,26 +99,26 @@ public ScriptFunction(Signature<T> sign, SectionNode node) {
@Deprecated(since = "2.9.0", forRemoval = true)
@ApiStatus.Internal
public final void setReturnValue(@Nullable T[] values) {
assert !returnValueSet;
returnValueSet = true;
this.returnValues = values;
assert !returnValueSet.get();
returnValueSet.set(true);
this.returnValues.set(values);
}

@Override
public boolean resetReturnValue() {
returnValueSet = false;
returnValues = null;
returnKeys = null;
returnValueSet.remove();
returnValues.remove();
returnKeys.remove();
return true;
}

@Override
public final void returnValues(Event event, Expression<? extends T> value) {
assert !returnValueSet;
returnValueSet = true;
this.returnValues = value.getArray(event);
assert !returnValueSet.get();
returnValueSet.set(true);
this.returnValues.set(value.getArray(event));
if (KeyProviderExpression.canReturnKeys(value))
this.returnKeys = ((KeyProviderExpression<?>) value).getArrayKeys(event);
this.returnKeys.set(((KeyProviderExpression<?>) value).getArrayKeys(event));
}

@Override
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/ch/njol/skript/variables/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,23 +457,23 @@ public static Object getVariable(String name, @Nullable Event event, boolean loc

return map.getVariable(n);
} else {
// Prevent race conditions from returning variables with incorrect values
if (!changeQueue.isEmpty()) {
// Gets the last VariableChange made
VariableChange variableChange = changeQueue.stream()
.filter(change -> change.name.equals(n))
.reduce((first, second) -> second)
// Gets last value, as iteration is from head to tail,
// and adding occurs at the tail (and we want the most recently added)
.orElse(null);

if (variableChange != null) {
return variableChange.value;
}
}

try {
variablesLock.readLock().lock();
// Prevent race conditions from returning variables with incorrect values
if (!changeQueue.isEmpty()) {
// Gets the last VariableChange made
VariableChange variableChange = changeQueue.stream()
.filter(change -> change.name.equals(n))
.reduce((first, second) -> second)
// Gets last value, as iteration is from head to tail,
// and adding occurs at the tail (and we want the most recently added)
.orElse(null);

if (variableChange != null) {
return variableChange.value;
}
}

return variables.getVariable(n);
} finally {
variablesLock.readLock().unlock();
Expand Down