Load homes from SQL

This commit is contained in:
Len 2024-07-28 15:10:14 +02:00
parent a7f14eb573
commit e2a32c1ebd

View File

@ -137,7 +137,7 @@ public class SQLStorageProvider extends StorageProvider {
.uuid(uuid)
.backLocation(locationStringToLocation(resultSet.getString("BackLocation")))
.deathLocation(locationStringToLocation(resultSet.getString("DeathLocation")))
.homes(locationMapStringToLocationMap(resultSet.getString("locationMapToString")))
.homes(loadHomes(uuid))
.userSettings(new EssentiaUserSettings
.Builder()
//.allowTeleports(resultSet.getBoolean("TeleportToggled")) // FIXME
@ -150,8 +150,33 @@ public class SQLStorageProvider extends StorageProvider {
return null;
}
private Map<String, Home> loadHomes(UUID uuid) {
String sql = "SELECT * FROM homes WHERE uuid = ?";
Map<String, Home> locationMap = new HashMap<>();
DatabaseQuery databaseQuery = new DatabaseQuery(sql, ps -> ps.setString(1, uuid.toString()));
try (ResultSet resultSet = databaseQuery.executeQuery(getDatabaseConnection().get())) {
if (!resultSet.next()) {
return locationMap;
}
String homeName = resultSet.getString("Name");
while (resultSet.next()) {
locationMap.put(
homeName,
new EssentiaHome(
homeName,
locationStringToLocation(resultSet.getString("HomeLocation"))
)
);
}
} catch (SQLException e) {
// catch this nicely
}
return locationMap;
}
@Override
public void save(@NotNull EssentiaUser user) throws Exception {
// TODO - convert to json object and save that in sql!
// Todo - use reflection to go over the fields to save?
// might not be the best way if new fields are added...
// split into multiple tables - users, userdata, userhomes, ... ?
@ -169,6 +194,11 @@ public class SQLStorageProvider extends StorageProvider {
}
}), true
);
saveHomes(user);
}
private void saveHomes(EssentiaUser essentiaUser) {
// TODO
}
@Override
@ -224,13 +254,4 @@ public class SQLStorageProvider extends StorageProvider {
return stringBuilder.toString();
}
private Map<String, Home> locationMapStringToLocationMap(String locationMapString) {
Map<String, Home> locationMap = new HashMap<>();
String[] entries = locationMapString.split(";");
for (String entry : entries) {
String[] data = entry.split("%");
locationMap.put(data[0], new EssentiaHome(data[0], locationStringToLocation(data[1])));
}
return locationMap;
}
}