Load user data from sql
This commit is contained in:
parent
f6dd31ba33
commit
5cc798c482
|
|
@ -4,14 +4,15 @@ import com.alttd.essentia.EssentiaPlugin;
|
|||
import com.alttd.essentia.configuration.Config;
|
||||
import com.alttd.essentia.storage.StorageProvider;
|
||||
import com.alttd.essentia.user.EssentiaUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
public class SQLStorageProvider extends StorageProvider {
|
||||
|
||||
|
|
@ -34,6 +35,10 @@ public class SQLStorageProvider extends StorageProvider {
|
|||
// TODO -- create table
|
||||
String userTable = "CREATE TABLE IF NOT EXISTS users(" +
|
||||
"uuid VARCHAR(36) NOT NULL, " +
|
||||
"DeathLocation mediumtext DEFAULT NULL, " +
|
||||
"BackLocation mediumtext DEFAULT NULL, " +
|
||||
"TeleportToggled tinyint(1) DEFAULT NULL, " +
|
||||
"HomesLocation mediumtext DEFAULT NULL, "+
|
||||
"PRIMARY KEY (uuid)" +
|
||||
")";
|
||||
addDatabaseQuery(new DatabaseQuery(userTable), false);
|
||||
|
|
@ -129,7 +134,11 @@ public class SQLStorageProvider extends StorageProvider {
|
|||
return null; // user is not in the db
|
||||
}
|
||||
return new EssentiaUser.Builder()
|
||||
.uuid(UUID.fromString(resultSet.getString("id")))
|
||||
.uuid(uuid)
|
||||
.backLocation(locationStringToLocation(resultSet.getString("BackLocation")))
|
||||
.deathLocation(locationStringToLocation(resultSet.getString("DeathLocation")))
|
||||
.homes(locationMapStringToLocationMap(resultSet.getString("locationMapToString")))
|
||||
.allowTeleports(resultSet.getBoolean("TeleportToggled"))
|
||||
.build();
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
|
@ -140,6 +149,7 @@ public class SQLStorageProvider extends StorageProvider {
|
|||
|
||||
@Override
|
||||
public void save(@NotNull EssentiaUser user) throws Exception {
|
||||
// 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, ... ?
|
||||
String sql = "INSERT INTO users" +
|
||||
|
|
@ -171,4 +181,53 @@ public class SQLStorageProvider extends StorageProvider {
|
|||
);
|
||||
}
|
||||
|
||||
private String locationToString(Location location) {
|
||||
if (location == null)
|
||||
return "";
|
||||
|
||||
String wordName = location.getWorld().getName();
|
||||
double x = location.getX();
|
||||
double y = location.getY();
|
||||
double z = location.getZ();
|
||||
float pitch = location.getPitch();
|
||||
float yaw = location.getYaw();
|
||||
return wordName + ":" + x + ":" + y + ":" + z + ":" + pitch + ":" + yaw;
|
||||
}
|
||||
|
||||
private Location locationStringToLocation(String locationString) {
|
||||
if (locationString == null || locationString.isEmpty())
|
||||
return null;
|
||||
String[] split = locationString.split(":");
|
||||
|
||||
// should prob have some error catching
|
||||
World wordName = Bukkit.getWorld(split[0]);
|
||||
double x = Double.parseDouble(split[1]);
|
||||
double y = Double.parseDouble(split[2]);
|
||||
double z = Double.parseDouble(split[3]);
|
||||
float pitch = Float.parseFloat(split[4]);
|
||||
float yaw = Float.parseFloat(split[5]);
|
||||
return new Location(wordName, x, y, z, pitch, yaw);
|
||||
}
|
||||
|
||||
private String locationMapToString(Map<String, Location> map) {
|
||||
// Todo -- can this be better?
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (Map.Entry<String, Location> entry : map.entrySet()) {
|
||||
if (!stringBuilder.isEmpty())
|
||||
stringBuilder.append(";");
|
||||
|
||||
stringBuilder.append(entry.getKey()).append("%").append(locationToString(entry.getValue()));
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
private Map<String, Location> locationMapStringToLocationMap(String locationMapString) {
|
||||
Map<String, Location> locationMap = new HashMap<>();
|
||||
String[] entries = locationMapString.split(";");
|
||||
for (String entry : entries) {
|
||||
String[] data = entry.split("%");
|
||||
locationMap.put(data[0], locationStringToLocation(data[1]));
|
||||
}
|
||||
return locationMap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user