mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancydialogs: Add joined players cache
This commit is contained in:
@@ -14,6 +14,7 @@ import com.fancyinnovations.fancydialogs.config.FDFeatureFlags;
|
||||
import com.fancyinnovations.fancydialogs.config.FancyDialogsConfig;
|
||||
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.PlayerJoinListener;
|
||||
import com.fancyinnovations.fancydialogs.registry.DefaultDialogs;
|
||||
@@ -58,6 +59,7 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
|
||||
private DialogRegistry dialogRegistry;
|
||||
private DialogStorage dialogStorage;
|
||||
private ActionRegistryImpl actionRegistry;
|
||||
private JoinedPlayersCache joinedPlayersCache;
|
||||
|
||||
public FancyDialogsPlugin() {
|
||||
INSTANCE = this;
|
||||
@@ -124,6 +126,9 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
|
||||
|
||||
actionRegistry = new ActionRegistryImpl();
|
||||
|
||||
joinedPlayersCache = new JoinedPlayersCache();
|
||||
joinedPlayersCache.load();
|
||||
|
||||
fancyLogger.info("Successfully loaded FancyDialogs version %s".formatted(getDescription().getVersion()));
|
||||
}
|
||||
|
||||
@@ -257,4 +262,8 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
|
||||
public ActionRegistryImpl getActionRegistry() {
|
||||
return actionRegistry;
|
||||
}
|
||||
|
||||
public JoinedPlayersCache getJoinedPlayersCache() {
|
||||
return joinedPlayersCache;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.fancyinnovations.fancydialogs.joinedplayerscache;
|
||||
|
||||
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
|
||||
import de.oliver.jdb.JDB;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class JoinedPlayersCache {
|
||||
|
||||
private final Set<String> playersJoined;
|
||||
private final JDB jdb;
|
||||
|
||||
public JoinedPlayersCache() {
|
||||
this.playersJoined = new HashSet<>();
|
||||
this.jdb = new JDB("plugins/FancyDialogs/data");
|
||||
}
|
||||
|
||||
public void load() {
|
||||
try {
|
||||
playersJoined.clear();
|
||||
String[] data = jdb.get("joined_players", String[].class);
|
||||
if (data != null) {
|
||||
Collections.addAll(playersJoined, data);
|
||||
FancyDialogsPlugin.get().getFancyLogger().debug("Loaded joined players from file.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
FancyDialogsPlugin.get().getFancyLogger().error("Failed to load joined players cache");
|
||||
FancyDialogsPlugin.get().getFancyLogger().error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
jdb.set("joined_players", playersJoined);
|
||||
FancyDialogsPlugin.get().getFancyLogger().debug("Saved joined players cache");
|
||||
} catch (Exception e) {
|
||||
FancyDialogsPlugin.get().getFancyLogger().error("Failed to save joined players cache");
|
||||
FancyDialogsPlugin.get().getFancyLogger().error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkIfPlayerJoined(UUID playerUUID) {
|
||||
return playersJoined.contains(playerUUID.toString());
|
||||
}
|
||||
|
||||
public void addPlayer(UUID playerUUID) {
|
||||
if (playersJoined.add(playerUUID.toString())) {
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,19 +12,23 @@ public class PlayerJoinListener implements Listener {
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
CustomClickActionPacketListener.get().getPacketListener().inject(event.getPlayer());
|
||||
|
||||
boolean isNewPlayer = !event.getPlayer().hasPlayedBefore();
|
||||
boolean hasJoinedBefore = FancyDialogsPlugin.get().getJoinedPlayersCache().checkIfPlayerJoined(event.getPlayer().getUniqueId());
|
||||
if (FancyDialogsPlugin.get().getFancyDialogsConfig().getLogLevel().equalsIgnoreCase("debug")) {
|
||||
isNewPlayer = true;
|
||||
hasJoinedBefore = false;
|
||||
}
|
||||
|
||||
if (isNewPlayer) {
|
||||
if (!hasJoinedBefore) {
|
||||
String welcomeDialogID = FancyDialogsPlugin.get().getFancyDialogsConfig().getWelcomeDialogID();
|
||||
Dialog dialog = FancyDialogsPlugin.get().getDialogRegistry().get(welcomeDialogID);
|
||||
if (dialog != null) {
|
||||
dialog.open(event.getPlayer());
|
||||
} else {
|
||||
FancyDialogsPlugin.get().getLogger().warning("Welcome dialog with ID " + welcomeDialogID + " not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
FancyDialogsPlugin.get().getJoinedPlayersCache().addPlayer(event.getPlayer().getUniqueId());
|
||||
FancyDialogsPlugin.get().getFancyLogger().debug("Player " + event.getPlayer().getName() + " has joined for the first time and the welcome dialog has been opened.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user