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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.cyr1en.promptpaper.screen.confirmation;

import dev.cyr1en.promptpaper.config.sub.DialogConfig;
import dev.cyr1en.promptpaper.screen.dialog.DialogLifecycle;
import dev.cyr1en.promptui.ComponentUtil;
import io.papermc.paper.dialog.Dialog;
import io.papermc.paper.registry.data.dialog.ActionButton;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class ConfirmationDialogView implements ConfirmationView {
private final Component declineLabel;
private final Component declineTooltip;
private final DialogLauncher launcher;
private final Duration responseLifetime;

private Consumer<ConfirmationOutcome> callback;
private Consumer<Throwable> failureCallback;
Expand Down Expand Up @@ -108,6 +110,30 @@ public ConfirmationDialogView(
Component declineLabel,
Component declineTooltip,
DialogLauncher launcher) {
this(
plugin,
player,
title,
body,
confirmLabel,
confirmTooltip,
declineLabel,
declineTooltip,
launcher,
ClickCallback.DEFAULT_LIFETIME);
}

ConfirmationDialogView(
Plugin plugin,
Player player,
Component title,
Component body,
Component confirmLabel,
Component confirmTooltip,
Component declineLabel,
Component declineTooltip,
DialogLauncher launcher,
Duration responseLifetime) {
this.plugin = Objects.requireNonNull(plugin, "plugin must not be null");
this.player = Objects.requireNonNull(player, "player must not be null");
this.title = title != null ? title : DEFAULT_TITLE;
Expand All @@ -117,6 +143,7 @@ public ConfirmationDialogView(
this.declineLabel = declineLabel != null ? declineLabel : DEFAULT_DECLINE_LABEL;
this.declineTooltip = declineTooltip;
this.launcher = launcher;
this.responseLifetime = responseLifetime;
}

public ConfirmationDialogView(Plugin plugin, Player player, String title, String body) {
Expand Down Expand Up @@ -365,19 +392,29 @@ private class NativeDialogLauncher implements DialogLauncher {

@Override
public void show(Player targetPlayer, Runnable onConfirm, Runnable onDecline) {
var options = ClickCallback.Options.builder().uses(1).lifetime(Duration.ofMinutes(5)).build();
var options = DialogLifecycle.clickOptions(responseLifetime);

var confirmBtnBuilder =
ActionButton.builder(confirmLabel)
.action(DialogAction.customClick((view, audience) -> onConfirm.run(), options));
.action(
DialogAction.customClick(
(view, audience) -> {
if (DialogLifecycle.isOwner(targetPlayer, audience)) onConfirm.run();
},
options));
if (confirmTooltip != null && !confirmTooltip.equals(Component.empty())) {
confirmBtnBuilder.tooltip(confirmTooltip);
}
var confirmBtn = confirmBtnBuilder.build();

var declineBtnBuilder =
ActionButton.builder(declineLabel)
.action(DialogAction.customClick((view, audience) -> onDecline.run(), options));
.action(
DialogAction.customClick(
(view, audience) -> {
if (DialogLifecycle.isOwner(targetPlayer, audience)) onDecline.run();
},
options));
if (declineTooltip != null && !declineTooltip.equals(Component.empty())) {
declineBtnBuilder.tooltip(declineTooltip);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ConfirmationGuiView implements ConfirmationView, Listener {
private Inventory inventory;

private boolean open;
private boolean programmaticClose;
private boolean closeScheduled;
private boolean listenerRegistered;

public ConfirmationGuiView(Plugin plugin, Player player) {
Expand All @@ -68,9 +68,8 @@ public ConfirmationGuiView(Plugin plugin, Player player, ConfirmationGuiConfig c

@Override
public synchronized void open(Consumer<ConfirmationOutcome> callback) {
if (open) return;
if (open || closeScheduled) return;
this.callback = Objects.requireNonNull(callback, "callback must not be null");
this.programmaticClose = false;

try {
this.holder = new ConfirmationGuiHolder(this);
Expand Down Expand Up @@ -129,13 +128,8 @@ public synchronized void open(Consumer<ConfirmationOutcome> callback) {

@Override
public synchronized void close() {
if (!open) return;
this.open = false;
this.programmaticClose = true;
this.callback = null;
unregisterListener();

player.getScheduler().run(plugin, task -> player.closeInventory(), null);
finishOnNextTick(null);
}

@Override
Expand All @@ -148,40 +142,41 @@ public synchronized void onOpenFailure(Consumer<Throwable> failureCallback) {
this.failureCallback = failureCallback;
}

private void handleConfirm() {
Consumer<ConfirmationOutcome> cb = null;
synchronized (this) {
if (!open) return;
this.open = false;
this.programmaticClose = true;
unregisterListener();
cb = this.callback;
this.callback = null;
}

player.getScheduler().run(plugin, task -> player.closeInventory(), null);

if (cb != null) {
cb.accept(ConfirmationOutcome.confirmed());
private synchronized void finishOnNextTick(ConfirmationOutcome outcome) {
if (!open) return;
open = false;
closeScheduled = true;

var task =
player
.getScheduler()
.runDelayed(
plugin,
scheduled -> {
Consumer<ConfirmationOutcome> cb;
synchronized (this) {
try {
if (isMatchingInventory(player.getOpenInventory(), null, player)) {
player.closeInventory();
}
cb = callback;
} finally {
clearPendingClose();
}
}
if (cb != null && outcome != null) cb.accept(outcome);
},
this::clearPendingClose,
1L);
if (task == null) {
clearPendingClose();
}
}

private void handleDecline() {
Consumer<ConfirmationOutcome> cb = null;
synchronized (this) {
if (!open) return;
this.open = false;
this.programmaticClose = true;
unregisterListener();
cb = this.callback;
this.callback = null;
}

player.getScheduler().run(plugin, task -> player.closeInventory(), null);

if (cb != null) {
cb.accept(ConfirmationOutcome.declined());
}
private synchronized void clearPendingClose() {
closeScheduled = false;
callback = null;
unregisterListener();
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
Expand All @@ -202,9 +197,9 @@ public void onInventoryClick(InventoryClickEvent event) {
int slot = event.getSlot();
if (event.getClick() == ClickType.LEFT || event.getClick() == ClickType.RIGHT) {
if (slot == config.confirmSlot()) {
handleConfirm();
finishOnNextTick(ConfirmationOutcome.confirmed());
} else if (slot == config.declineSlot()) {
handleDecline();
finishOnNextTick(ConfirmationOutcome.declined());
}
}
}
Expand All @@ -223,10 +218,7 @@ public void onInventoryClose(InventoryCloseEvent event) {

Consumer<ConfirmationOutcome> cb = null;
synchronized (this) {
if (!open || programmaticClose) {
unregisterListener();
return;
}
if (!open) return;
this.open = false;
unregisterListener();
cb = this.callback;
Expand All @@ -245,11 +237,11 @@ public void onPlayerQuit(PlayerQuitEvent event) {
Consumer<ConfirmationOutcome> cb = null;
synchronized (this) {
if (!open) {
this.callback = null;
unregisterListener();
return;
}
this.open = false;
this.programmaticClose = true;
unregisterListener();
cb = this.callback;
this.callback = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ConfirmationPromptScreen implements InputScreen {
private final boolean declineAsCancel;
private final Runnable onOpen;
private final boolean valueMode;
private final Integer timeoutSeconds;

private Consumer<ScreenResult> callback;
private Consumer<Throwable> openFailureCallback;
Expand Down Expand Up @@ -80,6 +81,17 @@ public ConfirmationPromptScreen(
boolean declineAsCancel,
Runnable onOpen,
boolean valueMode) {
this(fallbackChain, confirmValue, declineValue, declineAsCancel, onOpen, valueMode, null);
}

ConfirmationPromptScreen(
List<ConfirmationView> fallbackChain,
String confirmValue,
String declineValue,
boolean declineAsCancel,
Runnable onOpen,
boolean valueMode,
Integer timeoutSeconds) {
Objects.requireNonNull(fallbackChain, "fallbackChain must not be null");
if (fallbackChain.isEmpty()) {
throw new IllegalArgumentException("fallbackChain must not be empty");
Expand All @@ -93,6 +105,11 @@ public ConfirmationPromptScreen(
this.declineAsCancel = declineAsCancel;
this.onOpen = onOpen;
this.valueMode = valueMode;
this.timeoutSeconds = timeoutSeconds;
}

public Integer timeoutSeconds() {
return timeoutSeconds;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.cyr1en.promptpaper.screen.confirmation;

import dev.cyr1en.promptcore.ConfirmationMode;
import dev.cyr1en.promptcore.PromptTag;
import dev.cyr1en.promptcore.session.PromptSession;
import dev.cyr1en.promptpaper.CommandPrompter;
import dev.cyr1en.promptpaper.factory.MaterialMapper;
import dev.cyr1en.promptpaper.preset.ConfirmationPrompt;
import dev.cyr1en.promptpaper.screen.dialog.DialogLifecycle;
import dev.cyr1en.promptui.ComponentUtil;
import java.time.Duration;
import java.util.List;
import org.bukkit.entity.Player;

Expand All @@ -26,6 +28,25 @@ public static ConfirmationPromptScreen create(
? screenConfig.defaultMode()
: ConfirmationMode.GUI;
var mode = confirmation.mode() != null ? confirmation.mode() : defaultMode;
var sessionOpt =
plugin.getEngine() != null
? plugin.getEngine().getSession(player)
: java.util.Optional.<PromptSession>empty();
var config = plugin.getConfigLoader().getConfig();
int timeoutSeconds =
sessionOpt
.flatMap(PromptSession::currentPrompt)
.map(PromptTag::timeout)
.orElseGet(
() -> {
if (confirmation.behavior() != null
&& confirmation.behavior().timeout() != null) {
return confirmation.behavior().timeout();
}
if (confirmation.timeout() != null) return confirmation.timeout();
return config != null ? config.promptTimeout() : 60;
});
var responseLifetime = DialogLifecycle.responseLifetime(timeoutSeconds);

var dialogTitle =
confirmation.title() != null
Expand All @@ -50,7 +71,16 @@ public static ConfirmationPromptScreen create(
: ComponentUtil.mini("Cancel"));
var dialogView =
new ConfirmationDialogView(
plugin, player, dialogTitle, dialogBody, dialogConfirm, dialogCancel);
plugin,
player,
dialogTitle,
dialogBody,
dialogConfirm,
null,
dialogCancel,
null,
null,
responseLifetime);

ConfirmationGuiConfig guiConfig;
if (screenConfig != null) {
Expand Down Expand Up @@ -129,25 +159,9 @@ public static ConfirmationPromptScreen create(
? ComponentUtil.mini(screenConfig.defaultCancelLabel())
: ConfirmationChatView.DEFAULT_CANCEL_LABEL);

var sessionOpt =
plugin.getEngine() != null
? plugin.getEngine().getSession(player)
: java.util.Optional.<dev.cyr1en.promptcore.session.PromptSession>empty();
long incarnation =
sessionOpt.map(dev.cyr1en.promptcore.session.PromptSession::incarnation).orElse(0L);
long generation =
sessionOpt.map(dev.cyr1en.promptcore.session.PromptSession::generation).orElse(0L);
int promptIndex =
sessionOpt.map(dev.cyr1en.promptcore.session.PromptSession::currentIndex).orElse(0);
int timeoutSecs;
if (confirmation.timeout() != null) {
timeoutSecs = confirmation.timeout();
} else if (plugin.getConfigLoader() != null && plugin.getConfigLoader().getConfig() != null) {
timeoutSecs = plugin.getConfigLoader().getConfig().promptTimeout();
} else {
timeoutSecs = 60;
}
long effectiveTtl = timeoutSecs > 0 ? Math.min(timeoutSecs, 3600) : 3600;
long incarnation = sessionOpt.map(PromptSession::incarnation).orElse(0L);
long generation = sessionOpt.map(PromptSession::generation).orElse(0L);
int promptIndex = sessionOpt.map(PromptSession::currentIndex).orElse(0);

var registry =
plugin.getNonceRegistry() != null ? plugin.getNonceRegistry() : new NonceResponseRegistry();
Expand All @@ -162,7 +176,7 @@ public static ConfirmationPromptScreen create(
incarnation,
generation,
promptIndex,
Duration.ofSeconds(effectiveTtl));
responseLifetime);

List<ConfirmationView> fallbackChain =
switch (mode) {
Expand Down Expand Up @@ -204,6 +218,12 @@ public static ConfirmationPromptScreen create(
String confirmValue = valueMode ? "true" : ConfirmationPromptScreen.DEFAULT_CONFIRM_VALUE;
String declineValue = valueMode ? "false" : ConfirmationPromptScreen.DEFAULT_DECLINE_VALUE;
return new ConfirmationPromptScreen(
fallbackChain, confirmValue, declineValue, !valueMode, soundAction, valueMode);
fallbackChain,
confirmValue,
declineValue,
!valueMode,
soundAction,
valueMode,
timeoutSeconds);
}
}
Loading
Loading