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

Helper for running code with copied local variables #7255

Merged
merged 2 commits into from
Dec 15, 2024
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
13 changes: 3 additions & 10 deletions src/main/java/ch/njol/skript/sections/EffSecSpawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,15 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

@Override
@Nullable
@SuppressWarnings({"unchecked", "rawtypes"})
protected TriggerItem walk(Event event) {
protected @Nullable TriggerItem walk(Event event) {
lastSpawned = null;

Consumer<? extends Entity> consumer;
if (trigger != null) {
consumer = entity -> {
lastSpawned = entity;
SpawnEvent spawnEvent = new SpawnEvent(entity);
// Copy the local variables from the calling code to this section
Variables.setLocalVariables(spawnEvent, Variables.copyLocalVariables(event));
TriggerItem.walk(trigger, spawnEvent);
// And copy our (possibly modified) local variables back to the calling code
Variables.setLocalVariables(event, Variables.copyLocalVariables(spawnEvent));
// Clear spawnEvent's local variables as it won't be done automatically
Variables.removeLocals(spawnEvent);
Variables.withLocalVariables(event, spawnEvent, () -> TriggerItem.walk(trigger, spawnEvent));
};
} else {
consumer = null;
Expand All @@ -167,6 +159,7 @@ protected TriggerItem walk(Event event) {
double typeAmount = amount * type.getAmount();
for (int i = 0; i < typeAmount; i++) {
if (consumer != null) {
//noinspection unchecked,rawtypes
type.data.spawn(location, (Consumer) consumer); // lastSpawned set within Consumer
} else {
lastSpawned = type.data.spawn(location);
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/ch/njol/skript/variables/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,28 @@ public static void setLocalVariables(Event event, @Nullable Object map) {
* @param event the event to copy local variables from.
* @return the copy.
*/
@Nullable
public static Object copyLocalVariables(Event event) {
public static @Nullable Object copyLocalVariables(Event event) {
VariablesMap from = localVariables.get(event);
if (from == null)
return null;

return from.copy();
}

/**
* Copies local variables from provider to user, runs action, then copies variables back to provider.
* Removes local variables from user after action is finished.
* @param provider The originator of the local variables.
* @param user The event to copy the variables to and back from.
* @param action The code to run while the variables are copied.
*/
public static void withLocalVariables(Event provider, Event user, @NotNull Runnable action) {
Variables.setLocalVariables(user, Variables.copyLocalVariables(provider));
action.run();
Variables.setLocalVariables(provider, Variables.copyLocalVariables(user));
Variables.removeLocals(user);
}

/**
* Returns the internal value of the requested variable.
* <p>
Expand Down
Loading