refactor(redis): removed debug message

This commit is contained in:
ineanto 2023-07-14 10:11:48 +02:00
parent 7c70ebf9b6
commit f795d42564
3 changed files with 39 additions and 21 deletions

View file

@ -1,4 +1,4 @@
package xyz.atnrch.nicko.appearance;
package xyz.atnrch.nicko.profile;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.*;
@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import xyz.atnrch.nicko.NickoBukkit;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.Locale;
import xyz.atnrch.nicko.mojang.MojangAPI;
import xyz.atnrch.nicko.mojang.MojangSkin;
import xyz.atnrch.nicko.storage.PlayerDataStore;
@ -23,29 +24,32 @@ import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
public class AppearanceManager {
public class ProfileManager {
private final NickoProfile profile;
private final Player player;
private final UUID uuid;
private final NickoBukkit instance = NickoBukkit.getInstance();
private final PlayerDataStore dataStore = instance.getDataStore();
private final PlayerNameStore nameStore = instance.getNameStore();
private AppearanceManager(UUID uuid) {
private ProfileManager(UUID uuid) {
this.player = Bukkit.getPlayer(uuid);
this.uuid = uuid;
this.profile = dataStore.getData(uuid).orElse(NickoProfile.EMPTY_PROFILE.clone());
}
private AppearanceManager(String name) {
private ProfileManager(String name) {
this.player = null;
this.uuid = null;
this.profile = dataStore.getOfflineData(name).orElse(NickoProfile.EMPTY_PROFILE.clone());
}
public static AppearanceManager get(Player player) {
return new AppearanceManager(player.getUniqueId());
public static ProfileManager get(Player player) {
return new ProfileManager(player.getUniqueId());
}
public static AppearanceManager get(String name) {
return new AppearanceManager(name);
public static ProfileManager get(String name) {
return new ProfileManager(name);
}
public boolean hasData() {
@ -54,6 +58,7 @@ public class AppearanceManager {
public void setSkin(String skin) {
profile.setSkin(skin);
dataStore.getCache().cache(uuid, profile);
}
public String getSkin() {
@ -66,12 +71,22 @@ public class AppearanceManager {
public void setName(String name) {
profile.setName(name);
dataStore.getCache().cache(uuid, profile);
}
public String getName() {
return profile.getName();
}
public void setLocale(Locale locale) {
profile.setLocale(locale);
dataStore.getCache().cache(uuid, profile);
}
public Locale getLocale() {
return profile.getLocale();
}
public NickoProfile getProfile() {
return profile;
}
@ -79,6 +94,7 @@ public class AppearanceManager {
public void setNameAndSkin(String name, String skin) {
this.profile.setName(name);
this.profile.setSkin(skin);
dataStore.getCache().cache(uuid, profile);
}
public ActionResult reset() {

View file

@ -3,9 +3,9 @@ package xyz.atnrch.nicko.storage.redis;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import redis.clients.jedis.Jedis;
import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.appearance.ActionResult;
import xyz.atnrch.nicko.appearance.NickoProfile;
import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.storage.Cache;
import xyz.atnrch.nicko.storage.CacheProvider;
@ -21,7 +21,6 @@ public class RedisCache extends Cache {
private RedisCacheProvider provider;
public RedisCache(Configuration configuration) {
System.out.println("Loaded REDIS CACHE");
this.configuration = configuration;
}
@ -35,23 +34,26 @@ public class RedisCache extends Cache {
@Override
public ActionResult cache(UUID uuid, NickoProfile profile) {
final Jedis jedis = provider.getJedis();
jedis.set("nicko:" + uuid.toString(), gson.toJson(profile));
return ActionResult.ok();
try (Jedis jedis = provider.getJedis()) {
jedis.set("nicko:" + uuid.toString(), gson.toJson(profile));
return ActionResult.ok();
}
}
@Override
public boolean isCached(UUID uuid) {
final Jedis jedis = provider.getJedis();
return jedis.exists("nicko:" + uuid.toString());
try (Jedis jedis = provider.getJedis()) {
return jedis.exists("nicko:" + uuid.toString());
}
}
@Override
public Optional<NickoProfile> retrieve(UUID uuid) {
final Jedis jedis = provider.getJedis();
// 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);
return Optional.of(profile);
try (Jedis jedis = provider.getJedis()) {
// 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);
return Optional.of(profile);
}
}
}