mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2025-12-31 04:36:29 +00:00
Add yaml storage
This commit is contained in:
@@ -39,7 +39,7 @@ dependencies {
|
||||
implementation 'mysql:mysql-connector-java:5.1.33'
|
||||
implementation 'org.apache.commons:commons-lang3:3.6'
|
||||
compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.2-SNAPSHOT'
|
||||
compileOnly name: 'languagy-1.2.5'
|
||||
compileOnly name: 'languagy-1.2.6'
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
|
||||
Binary file not shown.
@@ -43,7 +43,7 @@ public class SqlStorage implements StorageManager {
|
||||
if (cachedAccounts.containsKey(uuid)) {
|
||||
future.complete(cachedAccounts.get(uuid));
|
||||
} else {
|
||||
DB.getResultsAsync("SELECT region FROM Discoveries WHERE uuid = ?", getDatabaseUuid(uuid)).whenComplete((results, error) -> {
|
||||
DB.getResultsAsync("SELECT region FROM Discoveries WHERE uuid = ?", getDatabaseUuid(uuid).toString()).whenComplete((results, error) -> {
|
||||
List<Discovery> regions = new ArrayList<>();
|
||||
for (DbRow row : results) {
|
||||
regions.add(new WorldDiscovery(Date.valueOf(row.getString("time")), row.getString("region")));
|
||||
@@ -75,7 +75,7 @@ public class SqlStorage implements StorageManager {
|
||||
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());
|
||||
DB.executeInsert("INSERT INTO Discoveries (uuid, region, time) VALUES (?, ?, ?)", getDatabaseUuid(uuid).toString(), region.getRegion(), region.getDate().toString());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,26 +1,83 @@
|
||||
package net.islandearth.rpgregions.managers.data.yml;
|
||||
|
||||
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.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public class YamlStorage implements StorageManager {
|
||||
|
||||
private ConcurrentMap<UUID, RPGRegionsAccount> cachedAccounts = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public CompletableFuture<RPGRegionsAccount> getAccount(UUID uuid) {
|
||||
return null;
|
||||
CompletableFuture<RPGRegionsAccount> future = new CompletableFuture<>();
|
||||
if (cachedAccounts.containsKey(uuid)) {
|
||||
future.complete(cachedAccounts.get(uuid));
|
||||
} else {
|
||||
File file = new File(JavaPlugin.getPlugin(RPGRegions.class).getDataFolder() + "/accounts/" + uuid.toString() + ".yml");
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
List<Discovery> regions = new ArrayList<>();
|
||||
for (String results : config.getStringList("Discoveries")) {
|
||||
String[] data = results.split(";");
|
||||
Date time = Date.valueOf(data[0]);
|
||||
String region = data[1];
|
||||
regions.add(new WorldDiscovery(time, region));
|
||||
}
|
||||
|
||||
RPGRegionsAccount account = new RPGRegionsAccount(uuid, regions);
|
||||
cachedAccounts.put(uuid, account);
|
||||
future.complete(account);
|
||||
}
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConcurrentMap<UUID, RPGRegionsAccount> getCachedAccounts() {
|
||||
return null;
|
||||
return (ConcurrentMap<UUID, RPGRegionsAccount>) ImmutableMap.copyOf(cachedAccounts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCachedAccount(UUID uuid) {
|
||||
RPGRegionsAccount account = cachedAccounts.get(uuid);
|
||||
File file = new File(JavaPlugin.getPlugin(RPGRegions.class).getDataFolder() + "/accounts/" + uuid.toString() + ".yml");
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
List<Discovery> current = new ArrayList<>();
|
||||
for (String results : config.getStringList("Discoveries")) {
|
||||
String[] data = results.split(";");
|
||||
Date time = Date.valueOf(data[0]);
|
||||
String region = data[1];
|
||||
current.add(new WorldDiscovery(time, region));
|
||||
}
|
||||
|
||||
List<String> newData = config.getStringList("Discoveries");
|
||||
for (Discovery region : account.getDiscoveredRegions()) {
|
||||
if (!current.contains(region)) {
|
||||
newData.add(region.getDate().toString() + ";" + region.getRegion());
|
||||
}
|
||||
}
|
||||
|
||||
config.set("Discoveries", newData);
|
||||
try {
|
||||
config.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
cachedAccounts.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user