feat(persistence): delete

This commit is contained in:
ineanto 2023-07-14 13:39:20 +02:00
parent 69bae936d6
commit 86e65043e1
8 changed files with 69 additions and 9 deletions

View file

@ -32,6 +32,7 @@ public class LanguageCyclingItem {
return CycleItem.withStateChangeHandler((observer, integer) -> { return CycleItem.withStateChangeHandler((observer, integer) -> {
nickoProfile.setLocale(Locale.values()[integer]); nickoProfile.setLocale(Locale.values()[integer]);
observer.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 0.707107f); // 0.707107 ~= C observer.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 0.707107f); // 0.707107 ~= C
// TODO (Ineanto, 7/14/23): This checks a 2nd time for the profile.
if (dataStore.updateCache(player.getUniqueId(), nickoProfile).isError()) { if (dataStore.updateCache(player.getUniqueId(), nickoProfile).isError()) {
final I18N i18n = new I18N(player); final I18N i18n = new I18N(player);
player.sendMessage(i18n.translate(I18NDict.Event.Settings.ERROR)); player.sendMessage(i18n.translate(I18NDict.Event.Settings.ERROR));

View file

@ -17,6 +17,8 @@ public abstract class Cache {
public abstract Optional<NickoProfile> retrieve(UUID uuid); public abstract Optional<NickoProfile> retrieve(UUID uuid);
public abstract ActionResult delete(UUID uuid);
public boolean isError() { public boolean isError() {
return error; return error;
} }

View file

@ -17,6 +17,8 @@ public abstract class Storage {
public abstract Optional<NickoProfile> retrieve(UUID uuid); public abstract Optional<NickoProfile> retrieve(UUID uuid);
public abstract ActionResult delete(UUID uuid);
public boolean isError() { public boolean isError() {
return error; return error;
} }

View file

@ -74,6 +74,16 @@ public class JSONStorage extends Storage {
} }
} }
@Override
public ActionResult delete(UUID uuid) {
final File directory = new File(NickoBukkit.getInstance().getDataFolder() + "/players/");
final File file = new File(directory, uuid.toString() + ".json");
if (file.delete() || !file.exists()) {
return ActionResult.ok();
}
return ActionResult.error(I18NDict.Error.JSON_ERROR);
}
private boolean checkFileExists(File file) throws IOException { private boolean checkFileExists(File file) throws IOException {
if (!file.exists()) { if (!file.exists()) {
return file.createNewFile(); return file.createNewFile();

View file

@ -41,4 +41,11 @@ public class MapCache extends Cache {
} }
return Optional.empty(); return Optional.empty();
} }
@Override
public ActionResult delete(UUID uuid) {
final HashMap<UUID, NickoProfile> profiles = provider.getMap();
profiles.remove(uuid);
return ActionResult.ok();
}
} }

View file

@ -4,8 +4,8 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import xyz.atnrch.nicko.appearance.ActionResult; import xyz.atnrch.nicko.appearance.ActionResult;
import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.config.Configuration; import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.storage.Cache; import xyz.atnrch.nicko.storage.Cache;
import xyz.atnrch.nicko.storage.CacheProvider; import xyz.atnrch.nicko.storage.CacheProvider;
@ -56,4 +56,12 @@ public class RedisCache extends Cache {
return Optional.of(profile); return Optional.of(profile);
} }
} }
@Override
public ActionResult delete(UUID uuid) {
try (Jedis jedis = provider.getJedis()) {
jedis.del("nicko:" + uuid.toString());
return ActionResult.ok();
}
}
} }

View file

@ -1,10 +1,10 @@
package xyz.atnrch.nicko.storage.sql; package xyz.atnrch.nicko.storage.sql;
import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.appearance.ActionResult; import xyz.atnrch.nicko.appearance.ActionResult;
import xyz.atnrch.nicko.profile.NickoProfile; import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.i18n.I18NDict; import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.Locale; import xyz.atnrch.nicko.i18n.Locale;
import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.storage.Storage; import xyz.atnrch.nicko.storage.Storage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -101,6 +101,23 @@ public class SQLStorage extends Storage {
} }
} }
@Override
public ActionResult delete(UUID uuid) {
final Connection connection = getProvider().getConnection();
if (connection == null) return ActionResult.error(I18NDict.Error.SQL_ERROR);
try {
final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?";
final PreparedStatement statement = connection.prepareStatement(sql);
statement.setBinaryStream(1, uuidToBin(uuid));
statement.executeUpdate();
return ActionResult.ok();
} catch (SQLException e) {
logger.warning("Couldn't fetch profile: " + e.getMessage());
return ActionResult.error(I18NDict.Error.SQL_ERROR);
}
}
private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException { private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException {
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `bungeecord`) VALUES (?, ?, ?, ?, ?)"; final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `bungeecord`) VALUES (?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);

View file

@ -3,17 +3,17 @@ package xyz.atnrch.nicko.test.storage.redis;
import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock; import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.entity.PlayerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import xyz.atnrch.nicko.NickoBukkit; import xyz.atnrch.nicko.NickoBukkit;
import xyz.atnrch.nicko.appearance.AppearanceManager;
import xyz.atnrch.nicko.config.Configuration; import xyz.atnrch.nicko.config.Configuration;
import xyz.atnrch.nicko.config.DataSourceConfiguration; import xyz.atnrch.nicko.config.DataSourceConfiguration;
import xyz.atnrch.nicko.profile.NickoProfile; import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.storage.PlayerDataStore;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class RedisCacheTest { public class RedisCacheTest {
@ -34,13 +34,26 @@ public class RedisCacheTest {
} }
@Test @Test
@DisplayName("Cache Player Data") @DisplayName("Cache Profile")
public void cachePlayerData() { public void cacheProfile() {
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
assertTrue(optionalProfile.isPresent()); assertTrue(optionalProfile.isPresent());
assertTrue(plugin.getDataStore().getCache().isCached(player.getUniqueId())); assertTrue(plugin.getDataStore().getCache().isCached(player.getUniqueId()));
} }
@Test
@DisplayName("Update Cache Profile")
public void updatePlayerCache() {
final PlayerDataStore dataStore = plugin.getDataStore();
final AppearanceManager appearanceManager = AppearanceManager.get(player);
appearanceManager.setName("Notch");
dataStore.updateCache(player.getUniqueId(), appearanceManager.getProfile());
final Optional<NickoProfile> retrieve = dataStore.getCache().retrieve(player.getUniqueId());
assertTrue(retrieve.isPresent());
final NickoProfile retrieved = retrieve.get();
assertEquals(retrieved.getName(), "Notch");
}
@AfterAll @AfterAll
public static void shutdown() { public static void shutdown() {
MockBukkit.unmock(); MockBukkit.unmock();