Skip to content

Commit

Permalink
SecTransferCookieRetrieve - cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed Jan 3, 2025
1 parent dca20de commit 6ca4bf8
Showing 1 changed file with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"\t\tstore cookie \"%uuid of player%-transfer\" with key \"transfer\" on player",
"\t\ttransfer player to arg-1",
"",
"# Connect event is recommended over join event",
"# This way if you have to kick the player it's done before they join",
"on connect:",
"\t# only do a cookie check if player was transferred",
"\tif player is transferred:",
Expand All @@ -48,15 +50,15 @@ public class SecTransferCookieRetrieve extends Section {

static {
if (Skript.methodExists(Player.class, "retrieveCookie", NamespacedKey.class)) {
Skript.registerSection(SecTransferCookieRetrieve.class,
"retrieve cookie with key %namespacedkey/string% [from %player%]");
Skript.registerSection(SecTransferCookieRetrieve.class,
"retrieve cookie with key %namespacedkey/string% [from %player%]");
}
}

private Expression<?> key;
private Expression<Player> player;

@SuppressWarnings({"NullableProblems", "unchecked"})
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult, SectionNode sectionNode, List<TriggerItem> triggerItems) {
this.key = exprs[0];
Expand All @@ -70,7 +72,6 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
return true;
}

@SuppressWarnings("NullableProblems")
@Override
protected @Nullable TriggerItem walk(Event event) {
TriggerItem next = getNext();
Expand All @@ -92,31 +93,23 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

player.retrieveCookie(key).thenAccept(bytes -> {
Delay.addDelayedEvent(event); // Delay event to make sure kick effect still works
ExprTransferCookie.setLastTransferCookie(new String(bytes));
// Walk the section
walkNext(localVars, event, first);
ExprTransferCookie.setLastTransferCookie(bytes != null ? new String(bytes) : null);
// re-set local variables
if (localVars != null) Variables.setLocalVariables(event, localVars);

if (this.first != null) {
// Walk the section if there is one
TriggerItem.walk(this.first, event);
}

// remove local vars as we're now done
Variables.removeLocals(event);
// Clear cookie data before moving on
ExprTransferCookie.setLastTransferCookie(null);
}).exceptionally(throwable -> {
// Walk the section even if no cookie was found
walkNext(localVars, event, first);
return null;
});
return null;
}

private static void walkNext(Object localVars, Event event, @Nullable TriggerItem triggerItem) {
// re-set local variables
if (localVars != null) Variables.setLocalVariables(event, localVars);

if (triggerItem != null) {
TriggerItem.walk(triggerItem, event);
}

// remove local vars as we're now done
Variables.removeLocals(event);
}

@Override
public @NotNull String toString(Event e, boolean d) {
return String.format("retrieve cookie with key %s from %s",
Expand Down

0 comments on commit 6ca4bf8

Please sign in to comment.