Developed Initiation from WorldSystemDatabase

added initiation functionality for the replacement to the dependence config.
This commit is contained in:
Daniel 2022-09-15 19:06:07 -04:00
parent 4b8b90d6e8
commit c2c8dd2e5c
30 changed files with 241 additions and 7 deletions

View File

@ -142,6 +142,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
</project>

View File

View File

@ -0,0 +1 @@
{"players":{"BlankUUID":{"playerWorlds":[]}}}

View File

@ -0,0 +1 @@
{"players":{"BlankUUID":{"playerWorlds":[]}}}

View File

@ -0,0 +1 @@
{"players":{"BlankUUID":{"playerWorlds":[]}}}

View File

@ -1,4 +1,81 @@
package de.butzlabben.world.data;
import com.google.gson.Gson;
import de.butzlabben.world.data.objects.PlayerData;
import de.butzlabben.world.data.objects.WorldSystemData;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class WorldDatabase {
//This is Set to Static to allow for only one to prevent Data to be not saved;
private static WorldSystemData wsDataBase;
private String dbFilePath;
public WorldDatabase(String databaseFilePath) {
this.dbFilePath = databaseFilePath;
this.wsDataBase = getWorldSystemDatabase();
}
public int getPlayerCount() {
return wsDataBase.getPlayers();
}
public void addPlayer(String playerUUID) {
wsDataBase.addplayer(playerUUID);
saveWSData();
}
private WorldSystemData getWorldSystemDatabase() {
File wsDataFile = new File(this.dbFilePath);
Gson gson = new Gson();
WorldSystemData newDatabase = null;
if (!wsDataFile.exists()) {
newDatabase = new WorldSystemData();
saveWSData();
return newDatabase;
}
try {
Reader reader = Files.newBufferedReader(Paths.get(this.dbFilePath));
newDatabase = gson.fromJson(reader, WorldSystemData.class);
reader.close();
} catch (IOException e) {
//Log Warning
}
if (newDatabase == null) {
newDatabase = new WorldSystemData();
}
return newDatabase;
}
private void saveWSData() {
Gson gson = new Gson();
File dataFile = new File(this.dbFilePath);
if (!dataFile.exists()) {
try {
dataFile.createNewFile();
} catch (IOException e) {
//Should Never Run
throw new RuntimeException(e);
}
}
try {
FileWriter writer = new FileWriter(dataFile);
gson.toJson(wsDataBase, writer);
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,27 @@
package de.butzlabben.world.data.objects;
import java.util.ArrayList;
import java.util.List;
public class PlayerData {
//TODO Write Tests
public List<PlayerWorld> playerWorlds;
public PlayerData() {
playerWorlds = new ArrayList<PlayerWorld>();
}
public void addWorld(PlayerWorld world) {
playerWorlds.add(world);
}
public int getWorldCount() {
return playerWorlds.size();
}
public PlayerWorld getWorldAt(int index) {
return playerWorlds.get(index);
}
}

View File

@ -0,0 +1,11 @@
package de.butzlabben.world.data.objects;
public class PlayerWorld {
public int worldNumber;
public long lastLoaded;
public PlayerWorld(String OWNER, String OWNERname, int worldNumber) {
this.worldNumber = worldNumber;
this.lastLoaded = 0;
}
}

View File

@ -0,0 +1,41 @@
package de.butzlabben.world.data.objects;
import de.butzlabben.WorldSystem;
import de.butzlabben.world.data.WorldDatabase;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WorldSystemData {
public Map<String, PlayerData> players;
public WorldSystemData() {
players = new HashMap<String, PlayerData>();
}
public Boolean addplayer(String uuid) {
if (players.get(uuid) == null) {
players.put(uuid, new PlayerData());
return true;
}
return false;
}
public void addWorldToPlayer(String uuid, PlayerWorld world) {
if (players.get(uuid) != null) {
players.get(uuid).addWorld(world);
}
}
public int getPlayers() {
return players.size();
}
}

View File

@ -1,4 +1,80 @@
package de.butzlabben.world.data;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import static org.junit.jupiter.api.Assertions.*;
public class TestWorldDatabase {
/**
* Before the Test this function cleans out the Working Directory
* This allows us to view the results of our prevous tests as needed
* but then allows us to run the tests Fresh.
*/
@BeforeAll
static void CleanLastTest() {
File workingDir = new File("src\\TestFiles\\workingDir\\");
for (File file : workingDir.listFiles()) {
file.delete();
}
}
/**
* This Tests the Basis Initaliaztion of the DataBase from nothing
*/
@Test
public void testDatabaseInitalizationFromNoFile() {
final String path = "src\\TestFiles\\workingDir\\dataBaseInitTestFromNoFile.json";
WorldDatabase wb = new WorldDatabase(path);
assertEquals(0, wb.getPlayerCount());
}
/**
* This Test Creates the new Object and Verifies that is can save data Properly;
* @throws FileNotFoundException
*/
@Test
public void testDatabaseInitalizationCreateValidFile() throws FileNotFoundException {
final String path = "src\\TestFiles\\workingDir\\dataBaseInitTestCreateValidFile.json";
WorldDatabase wb = new WorldDatabase(path);
wb.addPlayer("BlankUUID");
String input = new Scanner(new File(path)).nextLine();
assertEquals("{\"players\":{\"BlankUUID\":{\"playerWorlds\":[]}}}", input);
}
/**
* This Test to see if it can create to database when loading from a blank File
*/
@Test
public void testDatabaseInitalizationWithExistingEmptyFile() {
final String path = "src\\TestFiles\\ExistingEmptyFileInit.json";
WorldDatabase wb = new WorldDatabase(path);
assertEquals(0, wb.getPlayerCount());
}
/**
* This Test to see if it can create to database when loading from a populated File
*/
@Test
public void testDatabaseInitalizationWithPopulatedEmptyFile() throws FileNotFoundException {
final String path = "src\\TestFiles\\ExistingPopulatedFileInit.json";
WorldDatabase wb = new WorldDatabase(path);
String input = new Scanner(new File(path)).nextLine();
assertEquals("{\"players\":{\"BlankUUID\":{\"playerWorlds\":[]}}}", input);
assertEquals(1, wb.getPlayerCount());
}
}

View File

@ -1,7 +0,0 @@
name: 'WorldSystem'
version: '2.4.20'
main: de.bulzlabben.world.WorldSystem
description: The one world per player solution you have always dreamed of.
api-version: '1.17'