From 35260ff54c4aa59be7afb1ef29f911f1c6abfe98 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Mon, 12 Dec 2022 18:06:39 +0100 Subject: [PATCH] PlayerNonPersistentConfig moved to Pandalib --- .../lib/paper/players/PaperOnlinePlayer.java | 27 ++++ .../players/PlayerNonPersistentConfig.java | 122 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PlayerNonPersistentConfig.java diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java index 538929e..58bd6f7 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java @@ -3,6 +3,7 @@ package fr.pandacube.lib.paper.players; import com.destroystokyo.paper.ClientOption; import com.destroystokyo.paper.ClientOption.ChatVisibility; import com.destroystokyo.paper.SkinParts; +import fr.pandacube.lib.paper.players.PlayerNonPersistentConfig.Expiration; import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; @@ -278,4 +279,30 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer } + + + + + + /* + * Player config + */ + + default String getNonPersistentConfig(String key) { + return PlayerNonPersistentConfig.getData(getUniqueId(), key); + } + + default String getNonPersistentConfig(String key, String deflt) { + return PlayerNonPersistentConfig.getData(getUniqueId(), key); + } + + default void setNonPersistentConfig(String key, String value, Expiration expiration) { + PlayerNonPersistentConfig.setData(getUniqueId(), key, value, expiration); + } + + default void unsetNonPersistentConfig(String key) { + PlayerNonPersistentConfig.unsetData(getUniqueId(), key); + } + + } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PlayerNonPersistentConfig.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PlayerNonPersistentConfig.java new file mode 100644 index 0000000..0ed1f78 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PlayerNonPersistentConfig.java @@ -0,0 +1,122 @@ +package fr.pandacube.lib.paper.players; + +import com.destroystokyo.paper.event.server.ServerTickStartEvent; +import fr.pandacube.lib.paper.PandaLibPaper; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; + +public class PlayerNonPersistentConfig { + private static final Map> data = new HashMap<>(); + + private static long tick = 0; + + static { + new ConfigListeners(); + } + + + public static void setData(UUID playerId, String key, String value, Expiration expiration) { + data.computeIfAbsent(Objects.requireNonNull(playerId, "playerId"), pp -> new HashMap<>()) + .put(Objects.requireNonNull(key, "key"), + new ConfigEntry(Objects.requireNonNull(value, "value"), + Objects.requireNonNull(expiration, "expiration") + ) + ); + } + + public static void unsetData(UUID playerId, String key) { + data.getOrDefault(Objects.requireNonNull(playerId, "playerId"), new HashMap<>()) + .remove(Objects.requireNonNull(key, "key")); + } + + public static String getData(UUID playerId, String key) { + Map playerData = data.getOrDefault(Objects.requireNonNull(playerId, "playerId"), new HashMap<>()); + ConfigEntry ce = playerData.get(Objects.requireNonNull(key, "key")); + if (ce == null) + return null; + if (!ce.expiration.valid(playerId, key)) { + playerData.remove(key); + return null; + } + return ce.value; + } + + public static boolean isDataSet(UUID playerId, String key) { + return getData(playerId, key) != null; + } + + + + + + + + + + + + + private record ConfigEntry(String value, Expiration expiration) { } + + + + + public static abstract class Expiration { + abstract boolean valid(UUID player, String key); + } + + public static class ExpiresLogout extends Expiration { + protected boolean valid(UUID player, String key) { + return Bukkit.getPlayer(player) != null; // should not be call if player reconnects because it is removed on player quit + } + } + + public static class ExpiresTick extends Expiration { + long expirationTick; + + public ExpiresTick(long expirationDelayTick) { + expirationTick = tick + expirationDelayTick; + } + + protected boolean valid(UUID player, String key) { + return tick < expirationTick; + } + } + + public static class ExpiresServerStop extends Expiration { + protected boolean valid(UUID player, String key) { + return true; + } + } + + + + + + + + private static class ConfigListeners implements Listener { + public ConfigListeners() { + Bukkit.getPluginManager().registerEvents(this, PandaLibPaper.getPlugin()); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + data.getOrDefault(event.getPlayer().getUniqueId(), new HashMap<>()) + .entrySet() + .removeIf(e -> e.getValue().expiration instanceof ExpiresLogout); + } + + @EventHandler + public void onTickStart(ServerTickStartEvent event) { + tick++; + } + } +}