mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-28 19:39:14 +00:00
Added database converter
This commit is contained in:
@@ -61,10 +61,9 @@ public class Database {
|
||||
|
||||
public void load() {
|
||||
new DatabaseConverter(this.plugin, this).convert();
|
||||
this.createTables();
|
||||
}
|
||||
|
||||
private void createTables() {
|
||||
protected void createTables() {
|
||||
try {
|
||||
TableUtils.createTableIfNotExists(this.dataSource, ArmorItemDAO.class);
|
||||
TableUtils.createTableIfNotExists(this.dataSource, UserDAO.class);
|
||||
|
||||
@@ -1,19 +1,29 @@
|
||||
package io.github.fisher2911.hmccosmetics.database;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.cosmetic.CosmeticManager;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import io.th0rgal.oraxen.shaded.curl.CUrl;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DatabaseConverter {
|
||||
|
||||
private final HMCCosmetics plugin;
|
||||
private final Database database;
|
||||
private static final int CURRENT_VERSION = 2;
|
||||
|
||||
public DatabaseConverter(final HMCCosmetics plugin, final Database database) {
|
||||
this.database = database;
|
||||
@@ -35,31 +45,70 @@ public class DatabaseConverter {
|
||||
this.plugin.saveResource("database" + File.separator + FILE_NAME, true);
|
||||
}
|
||||
|
||||
final int version = YamlConfiguration.loadConfiguration(file).getInt("version");
|
||||
final YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
final int version = config.getInt("version");
|
||||
|
||||
this.convert(version);
|
||||
final Set<User> users = this.convert(version);
|
||||
|
||||
try {
|
||||
config.set("version", CURRENT_VERSION);
|
||||
config.save(file);
|
||||
} catch (final IOException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
this.database.createTables();
|
||||
|
||||
for (final User user : users) {
|
||||
this.database.saveUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
private void convert(final int version) {
|
||||
|
||||
private Set<User> convert(final int version) {
|
||||
return switch (version) {
|
||||
case 1 -> this.convertVersionOne();
|
||||
default -> new HashSet<>();
|
||||
};
|
||||
}
|
||||
|
||||
private void convertVersionOne() {
|
||||
// switch (this.database.getDatabaseType()) {
|
||||
//
|
||||
// }
|
||||
private Set<User> convertVersionOne() {
|
||||
final String query = "SELECT * from user";
|
||||
|
||||
final Set<User> users = new HashSet<>();
|
||||
final CosmeticManager cosmeticManager = this.plugin.getCosmeticManager();
|
||||
|
||||
try (final PreparedStatement statement = this.database.getDataSource().getReadOnlyConnection("user").
|
||||
getUnderlyingConnection().prepareStatement(query)) {
|
||||
getUnderlyingConnection().prepareStatement(query);
|
||||
final PreparedStatement dropStatement = this.database.getDataSource().getReadWriteConnection("user").
|
||||
getUnderlyingConnection().prepareStatement("DROP TABLE user")) {
|
||||
final ResultSet results = statement.executeQuery();
|
||||
while (results.next()) {
|
||||
System.out.println(results.getObject(1, UUID.class));
|
||||
System.out.println(results.getString(2));
|
||||
System.out.println(results.getString(3));
|
||||
System.out.println(results.getInt(4));
|
||||
final PlayerArmor playerArmor = PlayerArmor.empty();
|
||||
final User user = new User
|
||||
(UUID.fromString(results.getString(1)),
|
||||
playerArmor,
|
||||
this.database.ARMOR_STAND_ID.getAndDecrement()
|
||||
);
|
||||
final String backpackId = results.getString(2);
|
||||
final String hatId = results.getString(3);
|
||||
final int hatDye = results.getInt(4);
|
||||
|
||||
final ArmorItem backpack = cosmeticManager.getArmorItem(backpackId);
|
||||
final ArmorItem hat = cosmeticManager.getArmorItem(hatId);
|
||||
if (backpack != null) playerArmor.setItem(backpack);
|
||||
if (hat != null) {
|
||||
hat.setDye(hatDye);
|
||||
playerArmor.setItem(hat);
|
||||
}
|
||||
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
dropStatement.executeUpdate();
|
||||
|
||||
} catch (final SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user