Skip to content

Instantly share code, notes, and snippets.

@Blueyescat
Last active May 24, 2024 16:49
Show Gist options
  • Save Blueyescat/03d1a26d832ce80e36b9f959027473fa to your computer and use it in GitHub Desktop.
Save Blueyescat/03d1a26d832ce80e36b9f959027473fa to your computer and use it in GitHub Desktop.
import org.bukkit.event.Event;
import ch.njol.skript.Skript;
import ch.njol.skript.effects.Delay;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.variables.Variables;
/**
* This is *not* similar to Skript's AsyncEffect class.
* This just provides some utility for local variable stuff.
* Only the delay stuff works if the Variables methods aren't available.
*
* @author Blueyescat <https://github.com/Blueyescat>
*/
public abstract class AsyncEffectUtility extends Effect {
private static boolean CAN_MANAGE_LOCAL_VARS = Skript.methodExists(Variables.class, "removeLocals", Event.class) &&
Skript.methodExists(Variables.class, "setLocalVariables", Event.class, Object.class);
private Object localVars;
@Override
protected TriggerItem walk(Event e) {
debug(e, true);
Delay.addDelayedEvent(e);
execute(e);
return null;
}
protected void removeAndSaveLocals(Event e) {
if (CAN_MANAGE_LOCAL_VARS)
localVars = Variables.removeLocals(e);
}
protected void putLocalsBack(Event e) {
if (CAN_MANAGE_LOCAL_VARS && localVars != null)
Variables.setLocalVariables(e, localVars);
}
protected boolean shouldContinue() {
return getNext() != null;
}
protected void continueTrigger(Event e) {
TriggerItem.walk(getNext(), e);
}
protected void continueTriggerAndRemoveLocals(Event e) {
continueTrigger(e);
if (CAN_MANAGE_LOCAL_VARS)
Variables.removeLocals(e);
}
protected void putLocalsBackAndContinueTrigger(Event e) {
if (CAN_MANAGE_LOCAL_VARS)
putLocalsBack(e);
continueTrigger(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment