feat(persistence): delete
This commit is contained in:
parent
69bae936d6
commit
86e65043e1
8 changed files with 69 additions and 9 deletions
|
@ -32,6 +32,7 @@ public class LanguageCyclingItem {
|
|||
return CycleItem.withStateChangeHandler((observer, integer) -> {
|
||||
nickoProfile.setLocale(Locale.values()[integer]);
|
||||
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()) {
|
||||
final I18N i18n = new I18N(player);
|
||||
player.sendMessage(i18n.translate(I18NDict.Event.Settings.ERROR));
|
||||
|
|
|
@ -17,6 +17,8 @@ public abstract class Cache {
|
|||
|
||||
public abstract Optional<NickoProfile> retrieve(UUID uuid);
|
||||
|
||||
public abstract ActionResult delete(UUID uuid);
|
||||
|
||||
public boolean isError() {
|
||||
return error;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ public abstract class Storage {
|
|||
|
||||
public abstract Optional<NickoProfile> retrieve(UUID uuid);
|
||||
|
||||
public abstract ActionResult delete(UUID uuid);
|
||||
|
||||
public boolean isError() {
|
||||
return error;
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
if (!file.exists()) {
|
||||
return file.createNewFile();
|
||||
|
|
|
@ -41,4 +41,11 @@ public class MapCache extends Cache {
|
|||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResult delete(UUID uuid) {
|
||||
final HashMap<UUID, NickoProfile> profiles = provider.getMap();
|
||||
profiles.remove(uuid);
|
||||
return ActionResult.ok();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.GsonBuilder;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import xyz.atnrch.nicko.appearance.ActionResult;
|
||||
import xyz.atnrch.nicko.profile.NickoProfile;
|
||||
import xyz.atnrch.nicko.config.Configuration;
|
||||
import xyz.atnrch.nicko.profile.NickoProfile;
|
||||
import xyz.atnrch.nicko.storage.Cache;
|
||||
import xyz.atnrch.nicko.storage.CacheProvider;
|
||||
|
||||
|
@ -56,4 +56,12 @@ public class RedisCache extends Cache {
|
|||
return Optional.of(profile);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResult delete(UUID uuid) {
|
||||
try (Jedis jedis = provider.getJedis()) {
|
||||
jedis.del("nicko:" + uuid.toString());
|
||||
return ActionResult.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package xyz.atnrch.nicko.storage.sql;
|
||||
|
||||
import xyz.atnrch.nicko.config.Configuration;
|
||||
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.Locale;
|
||||
import xyz.atnrch.nicko.profile.NickoProfile;
|
||||
import xyz.atnrch.nicko.storage.Storage;
|
||||
|
||||
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 {
|
||||
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `bungeecord`) VALUES (?, ?, ?, ?, ?)";
|
||||
final PreparedStatement statement = connection.prepareStatement(sql);
|
||||
|
|
|
@ -3,17 +3,17 @@ package xyz.atnrch.nicko.test.storage.redis;
|
|||
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||
import be.seeseemelk.mockbukkit.ServerMock;
|
||||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.*;
|
||||
import xyz.atnrch.nicko.NickoBukkit;
|
||||
import xyz.atnrch.nicko.appearance.AppearanceManager;
|
||||
import xyz.atnrch.nicko.config.Configuration;
|
||||
import xyz.atnrch.nicko.config.DataSourceConfiguration;
|
||||
import xyz.atnrch.nicko.profile.NickoProfile;
|
||||
import xyz.atnrch.nicko.storage.PlayerDataStore;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class RedisCacheTest {
|
||||
|
@ -34,13 +34,26 @@ public class RedisCacheTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Cache Player Data")
|
||||
public void cachePlayerData() {
|
||||
@DisplayName("Cache Profile")
|
||||
public void cacheProfile() {
|
||||
final Optional<NickoProfile> optionalProfile = plugin.getDataStore().getData(player.getUniqueId());
|
||||
assertTrue(optionalProfile.isPresent());
|
||||
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
|
||||
public static void shutdown() {
|
||||
MockBukkit.unmock();
|
||||
|
|
Loading…
Reference in a new issue