From ccae0084ea85ef577309316775a63ae58bf02b6b Mon Sep 17 00:00:00 2001
From: ineanto <pantoine.rochas@gmail.com>
Date: Tue, 29 Aug 2023 20:20:56 +0200
Subject: [PATCH] feat: delete from cache when leaving, reopen gui on changing
 language

---
 .../nicko/gui/items/settings/LanguageCyclingItem.java  |  5 +++--
 .../java/xyz/atnrch/nicko/storage/PlayerDataStore.java | 10 +++++-----
 .../xyz/atnrch/nicko/storage/redis/RedisCache.java     |  1 +
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/main/java/xyz/atnrch/nicko/gui/items/settings/LanguageCyclingItem.java b/src/main/java/xyz/atnrch/nicko/gui/items/settings/LanguageCyclingItem.java
index dc083f0..282ed0a 100644
--- a/src/main/java/xyz/atnrch/nicko/gui/items/settings/LanguageCyclingItem.java
+++ b/src/main/java/xyz/atnrch/nicko/gui/items/settings/LanguageCyclingItem.java
@@ -4,6 +4,7 @@ import org.bukkit.Material;
 import org.bukkit.Sound;
 import org.bukkit.entity.Player;
 import xyz.atnrch.nicko.NickoBukkit;
+import xyz.atnrch.nicko.gui.SettingsGUI;
 import xyz.atnrch.nicko.i18n.I18N;
 import xyz.atnrch.nicko.i18n.I18NDict;
 import xyz.atnrch.nicko.i18n.ItemTranslation;
@@ -44,10 +45,10 @@ public class LanguageCyclingItem {
                 // TODO (Ineanto, 7/14/23): This checks a 2nd time for the profile.
                 if (dataStore.updateCache(player.getUniqueId(), nickoProfile).isError()) {
                     player.sendMessage(i18n.translate(I18NDict.Event.Settings.ERROR));
-                    player.getOpenInventory().close();
                 } else {
-                    player.sendMessage("Updated language to " + nickoProfile.getLocale().getCode());
+                    new SettingsGUI(player).open();
                 }
+                player.getOpenInventory().close();
             }, localeOrdinal, providers);
         }
 
diff --git a/src/main/java/xyz/atnrch/nicko/storage/PlayerDataStore.java b/src/main/java/xyz/atnrch/nicko/storage/PlayerDataStore.java
index 1b6c0ce..0412913 100644
--- a/src/main/java/xyz/atnrch/nicko/storage/PlayerDataStore.java
+++ b/src/main/java/xyz/atnrch/nicko/storage/PlayerDataStore.java
@@ -76,12 +76,12 @@ public class PlayerDataStore {
         if (storage.isError()) return ActionResult.error(I18NDict.Error.GENERIC);
         if (cache.isError()) return ActionResult.error(I18NDict.Error.CACHE);
         if (!cache.isCached(player.getUniqueId())) return ActionResult.error(I18NDict.Error.GENERIC);
-        if (!cache.retrieve(player.getUniqueId()).isPresent())
-            return ActionResult.error(I18NDict.Error.GENERIC);
 
-        // TODO (Ineanto, 5/20/23): Remove value from cache
-        //profiles.remove(player.getUniqueId());
-        return storage.store(player.getUniqueId(), cache.retrieve(player.getUniqueId()).get());
+        final Optional<NickoProfile> cachedProfile = cache.retrieve(player.getUniqueId());
+        if (!cachedProfile.isPresent()) return ActionResult.error(I18NDict.Error.GENERIC);
+
+        cache.delete(player.getUniqueId());
+        return storage.store(player.getUniqueId(), cachedProfile.get());
     }
 
     public Storage getStorage() {
diff --git a/src/main/java/xyz/atnrch/nicko/storage/redis/RedisCache.java b/src/main/java/xyz/atnrch/nicko/storage/redis/RedisCache.java
index a8d532c..d6ad5e4 100644
--- a/src/main/java/xyz/atnrch/nicko/storage/redis/RedisCache.java
+++ b/src/main/java/xyz/atnrch/nicko/storage/redis/RedisCache.java
@@ -50,6 +50,7 @@ public class RedisCache extends Cache {
     @Override
     public Optional<NickoProfile> retrieve(UUID uuid) {
         try (Jedis jedis = provider.getJedis()) {
+            // 29/08/23: what the fuck was I talking about?
             // TODO (Ineanto, 5/20/23): Check if cached before because Jedis returns a bulk reply so this is unsafe
             final String data = jedis.get("nicko:" + uuid.toString());
             final NickoProfile profile = gson.fromJson(data, NickoProfile.class);