Skip to content

Commit

Permalink
Helper for running code with copied local variables (#7255)
Browse files Browse the repository at this point in the history
helper method for temporarily copying local variables
  • Loading branch information
sovdeeth authored Dec 15, 2024
1 parent 88ae3dd commit 1aef9b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
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

0 comments on commit 1aef9b0

Please sign in to comment.