9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-29 11:49:08 +00:00

Discovery interface, implement region saving

This commit is contained in:
SamB440
2019-12-19 14:57:26 +00:00
parent ad73fa7ed1
commit d537d17e23
4 changed files with 60 additions and 6 deletions

View File

@@ -1,5 +1,7 @@
package net.islandearth.rpgregions.managers.data.account;
import net.islandearth.rpgregions.managers.data.region.Discovery;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -7,13 +9,13 @@ import java.util.UUID;
public class RPGRegionsAccount {
private UUID uuid;
private List<String> discoveredRegions = new ArrayList<>();
private List<Discovery> discoveredRegions = new ArrayList<>();
public RPGRegionsAccount(UUID uuid, List<String> discoveredRegions) {
public RPGRegionsAccount(UUID uuid, List<Discovery> discoveredRegions) {
this.discoveredRegions = discoveredRegions;
}
public List<String> getDiscoveredRegions() {
public List<Discovery> getDiscoveredRegions() {
return discoveredRegions;
}
}

View File

@@ -0,0 +1,9 @@
package net.islandearth.rpgregions.managers.data.region;
import java.util.Date;
public interface Discovery {
Date getDate();
String getRegion();
}

View File

@@ -0,0 +1,24 @@
package net.islandearth.rpgregions.managers.data.region;
import java.util.Date;
public class WorldDiscovery implements Discovery {
private Date date;
private String region;
public WorldDiscovery(Date date, String region) {
this.date = date;
this.region = region;
}
@Override
public Date getDate() {
return date;
}
@Override
public String getRegion() {
return region;
}
}

View File

@@ -5,8 +5,11 @@ import com.google.common.collect.ImmutableMap;
import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.managers.data.StorageManager;
import net.islandearth.rpgregions.managers.data.account.RPGRegionsAccount;
import net.islandearth.rpgregions.managers.data.region.Discovery;
import net.islandearth.rpgregions.managers.data.region.WorldDiscovery;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@@ -41,9 +44,9 @@ public class SqlStorage implements StorageManager {
future.complete(cachedAccounts.get(uuid));
} else {
DB.getResultsAsync("SELECT region FROM Discoveries WHERE uuid = ?", getDatabaseUuid(uuid)).whenComplete((results, error) -> {
List<String> regions = new ArrayList<>();
List<Discovery> regions = new ArrayList<>();
for (DbRow row : results) {
regions.add(row.getString("region"));
regions.add(new WorldDiscovery(Date.valueOf(row.getString("time")), row.getString("region")));
}
RPGRegionsAccount account = new RPGRegionsAccount(uuid, regions);
@@ -61,7 +64,23 @@ public class SqlStorage implements StorageManager {
@Override
public void removeCachedAccount(UUID uuid) {
//TODO save new regions
RPGRegionsAccount account = cachedAccounts.get(uuid);
List<Discovery> current = new ArrayList<>();
DB.getResultsAsync("SELECT region FROM Discoveries WHERE uuid = ?", getDatabaseUuid(uuid)).whenComplete((results, error) -> {
for (DbRow row : results) {
current.add(new WorldDiscovery(Date.valueOf(row.getString("time")), row.getString("region")));
}
});
for (Discovery region : account.getDiscoveredRegions()) {
if (!current.contains(region)) {
try {
DB.executeInsert("INSERT INTO Discoveries (uuid, region, time) VALUES (?, ?, ?)", uuid, region.getRegion(), region.getDate().toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
cachedAccounts.remove(uuid);
}
}