fancydialogs: Add dialog close timeout

This commit is contained in:
Oliver
2025-10-15 16:26:50 +02:00
parent 1b2ec76eb5
commit bef84f0366
4 changed files with 40 additions and 14 deletions

View File

@@ -1 +1 @@
0.0.26
0.0.27

View File

@@ -4,6 +4,7 @@ import com.fancyinnovations.fancydialogs.api.data.DialogData;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -12,12 +13,12 @@ public abstract class Dialog {
protected String id;
protected DialogData data;
protected Set<UUID> viewers;
protected Map<UUID, Long> viewers; // uuid, time opened
public Dialog(String id, DialogData data) {
this.id = id;
this.data = data;
this.viewers = ConcurrentHashMap.newKeySet();
this.viewers = new ConcurrentHashMap<>();
}
public Dialog() {
@@ -49,7 +50,7 @@ public abstract class Dialog {
* @return a set of UUIDs of players who have this dialog opened
*/
public Set<UUID> getViewers() {
return Set.copyOf(viewers);
return Set.copyOf(viewers.keySet());
}
/**
@@ -58,13 +59,7 @@ public abstract class Dialog {
* @param uuid of the player to check
* @return true if the dialog is opened for the player, false otherwise
*/
public boolean isOpenedFor(UUID uuid) {
if (uuid == null) {
return false;
}
return viewers.contains(uuid);
}
public abstract boolean isOpenedFor(UUID uuid);
/***
* Checks if the dialog is opened for a specific player.
@@ -85,7 +80,8 @@ public abstract class Dialog {
if (player == null) {
return;
}
viewers.add(player.getUniqueId());
viewers.put(player.getUniqueId(), System.currentTimeMillis());
}
@ApiStatus.Internal
@@ -93,6 +89,7 @@ public abstract class Dialog {
if (player == null) {
return;
}
viewers.remove(player.getUniqueId());
}
}

View File

@@ -12,6 +12,7 @@ public class FancyDialogsConfig {
private String logLevel;
private String welcomeDialogID;
private String quickActionsDialogID;
private long closeTimeout;
public void load() {
FancyDialogsPlugin.get().reloadConfig();
@@ -29,6 +30,9 @@ public class FancyDialogsConfig {
quickActionsDialogID = (String) ConfigHelper.getOrDefault(config, "quick_actions_dialog_id", "quick_actions");
config.setInlineComments("quick_actions_dialog_id", List.of("The ID of the dialog which will be shown to the player when they click on the quick actions key ('G' by default)."));
closeTimeout = (long) ConfigHelper.getOrDefault(config, "close_timeout", 1000L * 60 * 2);
config.setInlineComments("close_timeout", List.of("The time in milliseconds after which a dialog will be considered closed if the player does not respond. 0 means no timeout."));
FancyDialogsPlugin.get().saveConfig();
}
@@ -47,4 +51,8 @@ public class FancyDialogsConfig {
public String getQuickActionsDialogID() {
return quickActionsDialogID;
}
public long getCloseTimeout() {
return closeTimeout;
}
}

View File

@@ -27,6 +27,7 @@ import org.lushplugins.chatcolorhandler.parsers.ParserTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class DialogImpl extends Dialog {
@@ -177,7 +178,7 @@ public class DialogImpl extends Dialog {
.createShowDialogPacket(buildForPlayer(player))
.send(new FS_RealPlayer(player));
viewers.add(player.getUniqueId());
addViewer(player);
FancyDialogsPlugin.get().getFancyLogger().debug("Opened dialog " + id + " for player " + player.getName());
}
@@ -188,9 +189,29 @@ public class DialogImpl extends Dialog {
.createClearDialogPacket()
.send(new FS_RealPlayer(player));
viewers.remove(player.getUniqueId());
removeViewer(player);
FancyDialogsPlugin.get().getFancyLogger().debug("Closed dialog " + id + " for player " + player.getName());
}
@Override
public boolean isOpenedFor(UUID uuid) {
if (uuid == null) {
return false;
}
if (!viewers.containsKey(uuid)) {
return false;
}
long openedAt = viewers.get(uuid);
long now = System.currentTimeMillis();
if (now - openedAt > FancyDialogsPlugin.get().getFancyDialogsConfig().getCloseTimeout()) {
viewers.remove(uuid);
return false;
}
return true;
}
}