feat: action result on storage

This commit is contained in:
aro 2023-01-23 10:53:44 +01:00
parent d9dca0c0cc
commit 27a2ecda32
9 changed files with 40 additions and 16 deletions

View file

@ -30,5 +30,7 @@ public record I18NDict(String key) {
public static final I18NDict NAME_FAIL_MOJANG = new I18NDict("error.couldnt_get_name_from_mojang");
public static final I18NDict INVALID_USERNAME = new I18NDict("error.invalid_username");
public static final I18NDict UNEXPECTED_ERROR = new I18NDict("error.generic");
public static final I18NDict SQL_ERROR = new I18NDict("error.sql");
public static final I18NDict JSON_ERROR = new I18NDict("error.json");
}
}

View file

@ -19,7 +19,7 @@ public class PlayerDataStore {
private final HashMap<UUID, String> names = new HashMap<>();
public PlayerDataStore(NickoBukkit instance) {
this.storage = instance.getNickoConfig().isLocalStorage() ? new JSONStorage() : new SQLStorage(instance);
this.storage = instance.getNickoConfig().isLocalStorage() ? new JSONStorage(instance) : new SQLStorage(instance);
}
public void storeName(Player player) {

View file

@ -1,5 +1,6 @@
package net.artelnatif.nicko.storage;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import java.util.Optional;
@ -10,7 +11,7 @@ public abstract class Storage {
public abstract StorageProvider getProvider();
public abstract void store(UUID uuid, NickoProfile profile);
public abstract ActionResult<Void> store(UUID uuid, NickoProfile profile);
public abstract boolean isStored(UUID uuid);

View file

@ -3,7 +3,9 @@ package net.artelnatif.nicko.storage.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.storage.Storage;
import net.artelnatif.nicko.storage.StorageProvider;
@ -12,14 +14,18 @@ import java.util.Optional;
import java.util.UUID;
public class JSONStorage extends Storage {
private final NickoBukkit instance;
final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
final File directory = new File(NickoBukkit.getInstance().getDataFolder() + "/players/");
public JSONStorage(NickoBukkit instance) { this.instance = instance; }
@Override
public StorageProvider getProvider() { return new JSONStorageProvider(directory); }
@Override
public void store(UUID uuid, NickoProfile profile) {
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
final String profileToJson = gson.toJson(profile);
final File file = new File(directory, uuid.toString() + ".json");
@ -30,14 +36,16 @@ public class JSONStorage extends Storage {
writer.write(profileToJson);
}
} catch (IOException e) {
System.out.println("Could not write to file.");
throw new RuntimeException(e);
instance.getLogger().warning("Could not write to file.");
return new ActionResult<>(I18NDict.Error.JSON_ERROR);
}
}
} catch (IOException e) {
System.out.println("Could not create file.");
throw new RuntimeException(e);
instance.getLogger().warning("Could not create file.");
return new ActionResult<>(I18NDict.Error.JSON_ERROR);
}
return new ActionResult<>();
}
@Override

View file

@ -1,7 +1,9 @@
package net.artelnatif.nicko.storage.sql;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.storage.Storage;
import java.sql.Connection;
@ -22,13 +24,16 @@ public class SQLStorage extends Storage {
}
@Override
public void store(UUID uuid, NickoProfile profile) {
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
final Connection connection = getProvider().getConnection();
try {
connection.prepareStatement("");
} catch (SQLException e) {
throw new RuntimeException(e);
instance.getLogger().warning("Unable to store player.");
return new ActionResult<>(I18NDict.Error.UNEXPECTED_ERROR);
}
return new ActionResult<>();
}
@Override

View file

@ -37,7 +37,6 @@ public class SQLStorageProvider implements StorageProvider {
}
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}

View file

@ -5,6 +5,8 @@ error:
generic: "Unknown error"
invalid_username: "§cThe specified username is not a valid Minecraft username."
player_offline: "§c{0} §fis offline, please try again."
sql: "SQL Error"
json: "JSON Error"
event:
admin:
cache_clear: "§aSkin cache cleaned."

View file

@ -5,6 +5,8 @@ error:
generic: "Erreur inconnue"
invalid_username: "§cLe pseudo spécifié n''est pas un pseudo Minecraft valide."
player_offline: "§c{0} §fest hors-ligne, veuillez réessayer."
sql: "Erreur SQL"
json: "Erreur JSON"
event:
admin:
cache_clear: "§aCache des skins nettoyé."

View file

@ -5,11 +5,11 @@ import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.config.NickoConfiguration;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.Locale;
import org.junit.jupiter.api.*;
import java.util.Optional;
public class SQLStorageTest {
private static ServerMock server;
private static NickoBukkit plugin;
@ -29,12 +29,17 @@ public class SQLStorageTest {
@Test
@DisplayName("Create SQL Tables")
public void testSQLTables() {
public void createSQLTables() {
Assertions.assertFalse(plugin.getDataStore().getStorage().isError());
}
@Test
@DisplayName("Store Player Via SQL")
public void storePlayer() {
final PlayerMock playerMock = server.addPlayer();
final Optional<NickoProfile> data = plugin.getDataStore().getData(playerMock.getUniqueId());
Assertions.assertTrue(data.isPresent());
Assertions.assertNull(data.get().getSkin());
final NickoProfile profile = new NickoProfile("Notch", "Notch", Locale.ENGLISH, true);
final ActionResult<Void> storeAction = plugin.getDataStore().getStorage().store(playerMock.getUniqueId(), profile);
Assertions.assertFalse(storeAction.isError());
}
@AfterAll