This commit is contained in:
Len
2024-07-28 12:52:55 +02:00
parent 5cc798c482
commit fe83919200
43 changed files with 346 additions and 107 deletions
@@ -18,7 +18,7 @@ public class StorageManager {
return switch (type) {
case MYSQL -> new SQLStorageProvider(plugin);
case YAML -> new YamlStorageProvider(plugin, plugin.getDataFolder().getPath() + File.separator + "PlayerData");
case SQLITE -> throw new UnsupportedOperationException(); // TODO
case SQLITE -> throw new UnsupportedOperationException(); // FIXME
};
}
@@ -1,9 +1,11 @@
package com.alttd.essentia.storage;
import com.alttd.essentia.EssentiaPlugin;
import com.alttd.essentia.api.events.EssentiaUserLoadEvent;
import com.alttd.essentia.configuration.Config;
import com.alttd.essentia.user.EssentiaUser;
import com.alttd.essentia.user.User;
import com.alttd.essentia.api.user.User;
import lombok.Getter;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
@@ -12,6 +14,8 @@ import java.util.UUID;
public abstract class StorageProvider {
protected final EssentiaPlugin plugin;
@Getter
private AutoSaveTask autoSaveTask;
public StorageProvider(EssentiaPlugin plugin) {
@@ -27,7 +31,7 @@ public abstract class StorageProvider {
plugin.userManager().addUser(user);
// TODO -- UserLoadEvent?
new EssentiaUserLoadEvent(user).callEvent();
return user;
}
@@ -2,6 +2,8 @@ package com.alttd.essentia.storage.mysql;
import com.alttd.essentia.EssentiaPlugin;
import com.alttd.essentia.configuration.Config;
import com.alttd.essentia.api.model.Home;
import com.alttd.essentia.model.EssentiaHome;
import com.alttd.essentia.storage.StorageProvider;
import com.alttd.essentia.user.EssentiaUser;
import org.bukkit.Bukkit;
@@ -34,14 +36,27 @@ public class SQLStorageProvider extends StorageProvider {
private void createTables() {
// TODO -- create table
String userTable = "CREATE TABLE IF NOT EXISTS users(" +
"uuid VARCHAR(36) NOT NULL, " +
"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)" +
")";
String homeTable = "CREATE TABLE IF NOT EXISTS homes(" +
"UUID VARCHAR(36) NOT NULL, " +
"Name mediumtext DEFAULT NULL, " +
"HomeLocation mediumtext DEFAULT NULL, "+
")";
String settingsTable = "CREATE TABLE IF NOT EXISTS homes(" +
"UUID VARCHAR(36) NOT NULL, " +
"TeleportToggled tinyint(1) DEFAULT NULL, " +
"GodMode tinyint(1) DEFAULT NULL, " +
"Flying tinyint(1) DEFAULT NULL, " +
"PRIMARY KEY (uuid)" +
")";
addDatabaseQuery(new DatabaseQuery(userTable), false);
addDatabaseQuery(new DatabaseQuery(homeTable), false);
addDatabaseQuery(new DatabaseQuery(settingsTable), false);
}
public DatabaseConnection getDatabaseConnection() {
@@ -108,22 +123,6 @@ public class SQLStorageProvider extends StorageProvider {
}
}
boolean hasTable(String table) {
DatabaseConnection connection = getDatabaseConnection();
boolean match = false;
try (ResultSet rs = connection.get().getMetaData().getTables(null, null, table, null)) {
while (rs.next()) {
if (table.equalsIgnoreCase(rs.getString("TABLE_NAME"))) {
match = true;
break;
}
}
} catch (SQLException e) {
return match;
}
return match;
}
// TODO -- make this async
@Override
protected EssentiaUser load(UUID uuid) {
@@ -221,12 +220,12 @@ public class SQLStorageProvider extends StorageProvider {
return stringBuilder.toString();
}
private Map<String, Location> locationMapStringToLocationMap(String locationMapString) {
Map<String, Location> locationMap = new HashMap<>();
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], locationStringToLocation(data[1]));
locationMap.put(data[0], new EssentiaHome(data[0], locationStringToLocation(data[1])));
}
return locationMap;
}
@@ -1,6 +1,8 @@
package com.alttd.essentia.storage.yaml;
import com.alttd.essentia.EssentiaPlugin;
import com.alttd.essentia.api.model.Home;
import com.alttd.essentia.model.EssentiaHome;
import com.alttd.essentia.storage.StorageProvider;
import com.alttd.essentia.user.EssentiaUser;
import org.bukkit.Bukkit;
@@ -47,10 +49,10 @@ public class YamlStorageProvider extends StorageProvider {
setStoredLocation(config, "teleports.back", user.getBackLocation(false));
setStoredLocation(config, "teleports.death", user.getBackLocation(true));
for (Map.Entry<String, Location> entry : user.getHomeData().entrySet()) {
setStoredLocation(config, "home." + entry.getKey(), entry.getValue());
for (Map.Entry<String, Home> entry : user.getHomeData().entrySet()) {
setStoredLocation(config, "home." + entry.getKey(), entry.getValue().location());
}
config.set("allow-teleports", user.allowTeleports());
config.set("allow-teleports", user.getUserSettings().allowTeleports());
config.save(configFile);
}
@@ -90,14 +92,14 @@ public class YamlStorageProvider extends StorageProvider {
return new Location(world, x, y, z, yaw, pitch);
}
public Map<String, Location> getHomeData(YamlConfiguration config) {
public Map<String, Home> getHomeData(YamlConfiguration config) {
ConfigurationSection section = config.getConfigurationSection("home");
if (section == null) {
return null;
}
Map<String, Location> map = new HashMap<>();
Map<String, Home> map = new HashMap<>();
for (String key : section.getValues(false).keySet()) {
map.put(key, getStoredLocation(config, "home." + key));
map.put(key, new EssentiaHome(key, getStoredLocation(config, "home." + key)));
}
return map;