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
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void open() {

playerExecutor.execute(
() -> {
if (terminalDelivered.get()) return;
// Advisory & provider state check after hop
if (detached.get() || !handle.isActive()) {
if (terminalDelivered.compareAndSet(false, true)) {
Expand Down Expand Up @@ -166,6 +167,7 @@ public void open() {
}
delegateRef.set(delegate);
bindDelegateCallbacks();
if (terminalDelivered.get() || !verifier.verify(snapshot)) return;
} catch (Throwable t) {
if (terminalDelivered.compareAndSet(false, true)) {
deliverError(t);
Expand All @@ -183,17 +185,17 @@ public void open() {
}

InputScreen finalDelegate = delegate;
boolean opened =
handle.runIfActive(
() -> {
try {
finalDelegate.open();
} catch (Throwable t) {
if (terminalDelivered.compareAndSet(false, true)) {
deliverError(t);
}
}
});
boolean opened;
try {
opened =
handle.runIfActive(
() -> {
if (!detached.get() && !terminalDelivered.get()) finalDelegate.open();
});
} catch (Throwable t) {
if (terminalDelivered.compareAndSet(false, true)) deliverError(t);
return;
}

if (!opened) {
if (terminalDelivered.compareAndSet(false, true)) {
Expand Down Expand Up @@ -315,48 +317,40 @@ public boolean isTerminalDelivered() {
}

private void bindDelegateCallbacks() {
if (detached.get() || !handle.isActive()) {
return;
}
InputScreen delegate = delegateRef.get();
if (delegate != null && delegateCallbacksRegistered.compareAndSet(false, true)) {
boolean bound =
handle.runIfActive(
() -> {
try {
delegate.onResult(this::handleDelegateResult);
} catch (Throwable t) {
handleRegistrationError(t);
return;
}
try {
delegate.onOpenFailure(this::handleDelegateOpenFailure);
} catch (Throwable t) {
handleRegistrationError(t);
}
});
if (!bound) {
delegateCallbacksRegistered.set(false);
}
}
if (delegate == null || detached.get() || delegateCallbacksRegistered.get()) return;
handle.runIfActive(
() -> {
if (detached.get() || !delegateCallbacksRegistered.compareAndSet(false, true)) return;
try {
delegate.onResult(this::handleDelegateResult);
delegate.onOpenFailure(this::handleDelegateOpenFailure);
} catch (Throwable t) {
handleRegistrationError(t);
}
});
}

private void handleRegistrationError(Throwable t) {
playerExecutor.execute(
if (terminalDelivered.compareAndSet(false, true)) {
executeAfterInvocation(
() -> {
if (!detached.get() && handle.isActive()) deliverError(t);
});
}
}

private void executeAfterInvocation(Runnable task) {
CustomScreenToken.afterInvocation(
() -> {
if (!detached.get() && terminalDelivered.compareAndSet(false, true)) {
deliverError(t);
if (!detached.get() && handle.isActive()) {
playerExecutor.execute(task, this::handleRetirement);
}
},
this::handleRetirement);
});
}

private void handleDelegateResult(ScreenResult result) {
if (detached.get() || !handle.isActive()) {
return;
}

playerExecutor.execute(
executeAfterInvocation(
() -> {
if (detached.get() || !handle.isActive()) {
return;
Expand All @@ -378,16 +372,11 @@ private void handleDelegateResult(ScreenResult result) {
// Contained
}
}
},
this::handleRetirement);
});
}

private void handleDelegateOpenFailure(Throwable error) {
if (detached.get() || !handle.isActive()) {
return;
}

playerExecutor.execute(
executeAfterInvocation(
() -> {
if (detached.get() || !handle.isActive()) {
return;
Expand All @@ -403,8 +392,7 @@ private void handleDelegateOpenFailure(Throwable error) {

deliverError(
error != null ? error : new IllegalStateException("Open failure with null error"));
},
this::handleRetirement);
});
}

private void deliverError(Throwable error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.cyr1en.promptpaper.custom;

import dev.cyr1en.promptui.api.PromptScreenFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -15,6 +17,8 @@
*/
public final class CustomScreenToken implements CustomScreenHandle {

private static final ThreadLocal<List<Runnable>> PENDING_CALLBACKS = new ThreadLocal<>();

private final Object gate = new Object();
private final long providerId;
private final String key;
Expand Down Expand Up @@ -70,33 +74,44 @@ public PromptScreenFactory factory() {

@Override
public boolean runIfActive(Runnable action) {
synchronized (gate) {
if (state != ProviderState.ACTIVE) {
return false;
}
action.run();
return true;
}
return callIfActive(
() -> {
action.run();
return true;
})
.isPresent();
}

static void afterInvocation(Runnable callback) {
var callbacks = PENDING_CALLBACKS.get();
if (callbacks == null) callback.run();
else callbacks.add(callback);
}

@Override
public <T> Optional<T> callIfActive(Supplier<T> action) {
synchronized (gate) {
if (state != ProviderState.ACTIVE) {
return Optional.empty();
var callbacks = PENDING_CALLBACKS.get();
boolean outermost = callbacks == null;
if (outermost) {
callbacks = new ArrayList<>();
PENDING_CALLBACKS.set(callbacks);
}
try {
synchronized (gate) {
return state == ProviderState.ACTIVE ? Optional.ofNullable(action.get()) : Optional.empty();
}
} finally {
if (outermost) {
// A host callback can open another provider's screen on the same region.
PENDING_CALLBACKS.remove();
callbacks.forEach(Runnable::run);
}
return Optional.ofNullable(action.get());
}
}

@Override
public <T> Optional<T> invokeFactory(Function<PromptScreenFactory, T> action) {
synchronized (gate) {
if (state != ProviderState.ACTIVE || factory == null) {
return Optional.empty();
}
return Optional.ofNullable(action.apply(factory));
}
return callIfActive(() -> action.apply(factory));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ static Map<Material, BedrockAnvilAppearance> selected(CommandPrompter plugin) th
var materials = EnumSet.of(Material.PAPER);
addMaterial(materials, config.anvilItem());
addMaterial(materials, config.anvilCancelItem());
addMaterial(materials, config.searchAnvilItem());
addMaterial(materials, config.cancelItem());
for (var prompt : plugin.getPresetRegistry().snapshot().prompts().values()) {
if (prompt instanceof AnvilPrompt anvil) {
addMaterial(materials, anvil.leftButton().buttonIcon());
Expand Down
Loading
Loading