feat(storage): fallback on local storage

This commit is contained in:
ineanto 2023-12-14 01:35:18 +01:00
parent cb98fd2671
commit 48ca85028b
4 changed files with 25 additions and 12 deletions

View file

@ -6,7 +6,7 @@ plugins {
} }
group = "xyz.ineanto" group = "xyz.ineanto"
version = "1.0.7-RC1" version = "1.0.8-RC1"
val shadowImplementation: Configuration by configurations.creating val shadowImplementation: Configuration by configurations.creating
configurations["implementation"].extendsFrom(shadowImplementation) configurations["implementation"].extendsFrom(shadowImplementation)

View file

@ -16,6 +16,8 @@ import xyz.ineanto.nicko.i18n.LocaleFileManager;
import xyz.ineanto.nicko.mojang.MojangAPI; import xyz.ineanto.nicko.mojang.MojangAPI;
import xyz.ineanto.nicko.placeholder.NickoExpansion; import xyz.ineanto.nicko.placeholder.NickoExpansion;
import xyz.ineanto.nicko.storage.PlayerDataStore; import xyz.ineanto.nicko.storage.PlayerDataStore;
import xyz.ineanto.nicko.storage.json.JSONStorage;
import xyz.ineanto.nicko.storage.map.MapCache;
import xyz.ineanto.nicko.storage.name.PlayerNameStore; import xyz.ineanto.nicko.storage.name.PlayerNameStore;
import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.gui.structure.Structure;
import xyz.xenondevs.invui.item.builder.ItemBuilder; import xyz.xenondevs.invui.item.builder.ItemBuilder;
@ -73,14 +75,18 @@ public class NickoBukkit extends JavaPlugin {
getLogger().info("Loading persistence..."); getLogger().info("Loading persistence...");
if (!dataStore.getStorage().getProvider().init()) { if (!dataStore.getStorage().getProvider().init()) {
dataStore.getStorage().setError(true); getLogger().severe("Couldn't connect to distant persistence, falling back on local persistence.");
getLogger().severe("Failed to open persistence, data will NOT be saved!"); final JSONStorage storage = new JSONStorage();
storage.getProvider().init();
dataStore.setStorage(storage);
} }
getLogger().info("Loading cache..."); getLogger().info("Loading cache...");
if (!dataStore.getCache().getProvider().init()) { if (!dataStore.getCache().getProvider().init()) {
dataStore.getCache().setError(true); getLogger().severe("Couldn't connect to distant cache, falling back on local cache.");
getLogger().severe("Failed to open cache, data will NOT be saved!"); final MapCache cache = new MapCache();
cache.getProvider().init();
dataStore.setCache(cache);
} }
if (!unitTesting) { if (!unitTesting) {

View file

@ -19,10 +19,11 @@ import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
public class PlayerDataStore { public class PlayerDataStore {
private final Storage storage;
private final Cache cache;
private final MojangAPI mojangAPI; private final MojangAPI mojangAPI;
private Storage storage;
private Cache cache;
public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) { public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) {
this.mojangAPI = mojangAPI; this.mojangAPI = mojangAPI;
this.storage = configuration.getSqlConfiguration().isEnabled() ? this.storage = configuration.getSqlConfiguration().isEnabled() ?
@ -81,7 +82,7 @@ public class PlayerDataStore {
if (!cache.isCached(player.getUniqueId())) return ActionResult.error(I18NDict.Error.CACHE); if (!cache.isCached(player.getUniqueId())) return ActionResult.error(I18NDict.Error.CACHE);
final Optional<NickoProfile> cachedProfile = cache.retrieve(player.getUniqueId()); final Optional<NickoProfile> cachedProfile = cache.retrieve(player.getUniqueId());
if (!cachedProfile.isPresent()) return ActionResult.error(I18NDict.Error.CACHE); if (cachedProfile.isEmpty()) return ActionResult.error(I18NDict.Error.CACHE);
cache.delete(player.getUniqueId()); cache.delete(player.getUniqueId());
return storage.store(player.getUniqueId(), cachedProfile.get()); return storage.store(player.getUniqueId(), cachedProfile.get());
@ -91,7 +92,15 @@ public class PlayerDataStore {
return storage; return storage;
} }
public void setStorage(Storage storage) {
this.storage = storage;
}
public Cache getCache() { public Cache getCache() {
return cache; return cache;
} }
public void setCache(Cache cache) {
this.cache = cache;
}
} }

View file

@ -3,10 +3,7 @@ package xyz.ineanto.nicko.test.storage;
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.ineanto.nicko.NickoBukkit; import xyz.ineanto.nicko.NickoBukkit;
import xyz.ineanto.nicko.appearance.ActionResult; import xyz.ineanto.nicko.appearance.ActionResult;
import xyz.ineanto.nicko.config.Configuration; import xyz.ineanto.nicko.config.Configuration;
@ -19,6 +16,7 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@Disabled
public class BrokenSQLTest { public class BrokenSQLTest {
private static NickoBukkit plugin; private static NickoBukkit plugin;
private static PlayerMock player; private static PlayerMock player;