mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancydialogs: Enhance dialog viewer management
Add viewer tracking and handle player join/quit/death events
This commit is contained in:
@@ -1 +1 @@
|
||||
0.0.12
|
||||
0.0.13
|
||||
@@ -94,7 +94,7 @@ tasks {
|
||||
minecraftVersion("1.21.7")
|
||||
|
||||
downloadPlugins {
|
||||
modrinth("fancynpcs", "2.6.0")
|
||||
modrinth("fancynpcs", "2.6.0.280")
|
||||
// hangar("ViaVersion", "5.3.2")
|
||||
// hangar("ViaBackwards", "5.3.2")
|
||||
// modrinth("multiverse-core", "4.3.11")
|
||||
|
||||
@@ -2,22 +2,39 @@ package com.fancyinnovations.fancydialogs.api;
|
||||
|
||||
import com.fancyinnovations.fancydialogs.api.data.DialogData;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public abstract class Dialog {
|
||||
|
||||
protected String id;
|
||||
protected DialogData data;
|
||||
protected Set<UUID> viewers;
|
||||
|
||||
public Dialog(String id, DialogData data) {
|
||||
this.id = id;
|
||||
this.data = data;
|
||||
this.viewers = ConcurrentHashMap.newKeySet();
|
||||
}
|
||||
|
||||
public Dialog() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the dialog for the specified player.
|
||||
*
|
||||
* @param player the player to open the dialog for
|
||||
*/
|
||||
abstract public void open(Player player);
|
||||
|
||||
/**
|
||||
* Closes the dialog for the specified player.
|
||||
*
|
||||
* @param player the player to close the dialog for
|
||||
*/
|
||||
abstract public void close(Player player);
|
||||
|
||||
public String getId() {
|
||||
@@ -27,4 +44,55 @@ public abstract class Dialog {
|
||||
public DialogData getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a set of UUIDs of players who have this dialog opened
|
||||
*/
|
||||
public Set<UUID> getViewers() {
|
||||
return Set.copyOf(viewers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the dialog is opened for a specific player by UUID.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/***
|
||||
* Checks if the dialog is opened for a specific player.
|
||||
*
|
||||
* @param player the player to check
|
||||
* @return true if the dialog is opened for the player, false otherwise
|
||||
*/
|
||||
public boolean isOpenedFor(Player player) {
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isOpenedFor(player.getUniqueId());
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public void addViewer(Player player) {
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
viewers.add(player.getUniqueId());
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public void removeViewer(Player player) {
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
viewers.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,9 @@ import com.fancyinnovations.fancydialogs.dialog.DialogImpl;
|
||||
import com.fancyinnovations.fancydialogs.fancynpcs.OpenDialogNpcAction;
|
||||
import com.fancyinnovations.fancydialogs.joinedplayerscache.JoinedPlayersCache;
|
||||
import com.fancyinnovations.fancydialogs.listener.DialogButtonClickedListener;
|
||||
import com.fancyinnovations.fancydialogs.listener.PlayerDeathListener;
|
||||
import com.fancyinnovations.fancydialogs.listener.PlayerJoinListener;
|
||||
import com.fancyinnovations.fancydialogs.listener.PlayerQuitListener;
|
||||
import com.fancyinnovations.fancydialogs.registry.DefaultDialogs;
|
||||
import com.fancyinnovations.fancydialogs.registry.DialogRegistry;
|
||||
import com.fancyinnovations.fancydialogs.storage.DialogStorage;
|
||||
@@ -207,6 +209,8 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
|
||||
|
||||
private void registerListeners() {
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerQuitListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerDeathListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new DialogButtonClickedListener(), this);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.fancyinnovations.fancydialogs.dialog;
|
||||
|
||||
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
|
||||
import com.fancyinnovations.fancydialogs.api.Dialog;
|
||||
import com.fancyinnovations.fancydialogs.api.data.DialogBodyData;
|
||||
import com.fancyinnovations.fancydialogs.api.data.DialogButton;
|
||||
@@ -170,6 +171,10 @@ public class DialogImpl extends Dialog {
|
||||
FancySitula.PACKET_FACTORY
|
||||
.createShowDialogPacket(fsDialog)
|
||||
.send(new FS_RealPlayer(player));
|
||||
|
||||
viewers.add(player.getUniqueId());
|
||||
|
||||
FancyDialogsPlugin.get().getFancyLogger().debug("Opened dialog " + id + " for player " + player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -177,6 +182,10 @@ public class DialogImpl extends Dialog {
|
||||
FancySitula.PACKET_FACTORY
|
||||
.createClearDialogPacket()
|
||||
.send(new FS_RealPlayer(player));
|
||||
|
||||
viewers.remove(player.getUniqueId());
|
||||
|
||||
FancyDialogsPlugin.get().getFancyLogger().debug("Closed dialog " + id + " for player " + player.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,6 +58,11 @@ public class CustomClickActionPacketListener {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!dialog.isOpenedFor(event.player())) {
|
||||
FancyDialogsPlugin.get().getFancyLogger().warn("Received action for dialog: " + dialogId + " but it is not opened for player: " + event.player().getName());
|
||||
return;
|
||||
}
|
||||
|
||||
DialogButton btn = dialog.getData().getButtonById(buttonId);
|
||||
if (btn == null) {
|
||||
FancyDialogsPlugin.get().getFancyLogger().warn("Received action for unknown button: " + buttonId + " in dialog: " + dialogId);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.fancyinnovations.fancydialogs.listener;
|
||||
|
||||
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
|
||||
import com.fancyinnovations.fancydialogs.api.Dialog;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
|
||||
public class PlayerDeathListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||||
for (Dialog dialog : FancyDialogsPlugin.get().getDialogRegistry().getAll()) {
|
||||
dialog.removeViewer(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,10 @@ public class PlayerJoinListener implements Listener {
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
CustomClickActionPacketListener.get().getPacketListener().inject(event.getPlayer());
|
||||
|
||||
for (Dialog dialog : FancyDialogsPlugin.get().getDialogRegistry().getAll()) {
|
||||
dialog.removeViewer(event.getPlayer());
|
||||
}
|
||||
|
||||
if (FDFeatureFlags.DISABLE_WELCOME_DIALOG.isEnabled()) {
|
||||
FancyDialogsPlugin.get().getFancyLogger().debug("Welcome dialog is disabled via feature flag");
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.fancyinnovations.fancydialogs.listener;
|
||||
|
||||
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
|
||||
import com.fancyinnovations.fancydialogs.api.Dialog;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerQuitListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
for (Dialog dialog : FancyDialogsPlugin.get().getDialogRegistry().getAll()) {
|
||||
dialog.removeViewer(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user