mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-28 19:39:14 +00:00
Database stuff (incomplete)
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package io.github.fisher2911.hmccosmetics.database;
|
||||
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
import com.j256.ormlite.jdbc.DataSourceConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
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.database.dao.ArmorItemDAO;
|
||||
import io.github.fisher2911.hmccosmetics.database.dao.UserDAO;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -34,87 +34,107 @@ public abstract class Database {
|
||||
"))";
|
||||
|
||||
protected final HMCCosmetics plugin;
|
||||
private final DataSourceConnectionSource dataSource;
|
||||
|
||||
public Database(final HMCCosmetics plugin) {
|
||||
public Database(final HMCCosmetics plugin, final DataSourceConnectionSource dataSource) {
|
||||
this.plugin = plugin;
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public abstract Connection getConnection();
|
||||
|
||||
public void load() {
|
||||
try (final PreparedStatement statement = this.getConnection().prepareStatement(CREATE_TABLE_STATEMENT)) {
|
||||
statement.executeUpdate();
|
||||
this.createTables();
|
||||
}
|
||||
|
||||
private void createTables() {
|
||||
try {
|
||||
TableUtils.createTable(this.dataSource, ArmorItemDAO.class);
|
||||
TableUtils.createTable(this.dataSource, UserDAO.class);
|
||||
} catch (final SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveUser(final User user) {
|
||||
public void saveUser(final User u) {
|
||||
try {
|
||||
final UserDAO user = new UserDAO(u.getUuid());
|
||||
|
||||
try (final PreparedStatement statement = this.getConnection().prepareStatement(this.getSaveStatement())) {
|
||||
final PlayerArmor playerArmor = user.getPlayerArmor();
|
||||
final String hat = playerArmor.getHat().getId();
|
||||
final String backpack = playerArmor.getBackpack().getId();
|
||||
|
||||
statement.setString(1, user.getUuid().toString());
|
||||
statement.setString(2, backpack);
|
||||
statement.setString(3, hat);
|
||||
statement.setString(5, backpack);
|
||||
statement.setString(6, hat);
|
||||
|
||||
statement.executeUpdate();
|
||||
} catch (final SQLException exception) {
|
||||
this.plugin.getLogger().severe("There was in issue saving the player!");
|
||||
final Dao<UserDAO, UUID> userDao = DaoManager.createDao(this.dataSource, UserDAO.class);
|
||||
userDao.create(user);
|
||||
userDao.assignEmptyForeignCollection(user, "armorItems");
|
||||
userDao.create(user);
|
||||
} catch (final SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public User loadUser(final UUID uuid) {
|
||||
final int armorStandId = ARMOR_STAND_ID.getAndDecrement();
|
||||
|
||||
final User blankUser = new User(
|
||||
uuid,
|
||||
PlayerArmor.empty(),
|
||||
armorStandId
|
||||
);
|
||||
|
||||
try (final PreparedStatement statement = this.getConnection().prepareStatement(this.getLoadStatement())) {
|
||||
statement.setString(1, uuid.toString());
|
||||
|
||||
final ResultSet results = statement.executeQuery();
|
||||
|
||||
if (!results.next()) {
|
||||
return blankUser;
|
||||
}
|
||||
|
||||
final String backpackId = results.getString(1);
|
||||
final String hatId = results.getString(2);
|
||||
final int dye = results.getInt(3);
|
||||
|
||||
final CosmeticManager manager = this.plugin.getCosmeticManager();
|
||||
|
||||
ArmorItem backpack = manager.getArmorItem(backpackId);
|
||||
ArmorItem hat = manager.getArmorItem(hatId);
|
||||
|
||||
if (backpack == null) backpack = ArmorItem.empty(ArmorItem.Type.BACKPACK);
|
||||
if (hat == null) hat = ArmorItem.empty(ArmorItem.Type.HAT);
|
||||
|
||||
return new User(
|
||||
uuid,
|
||||
new PlayerArmor(
|
||||
hat,
|
||||
backpack,
|
||||
// todo
|
||||
null
|
||||
),
|
||||
armorStandId
|
||||
);
|
||||
|
||||
} catch (final SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
return blankUser;
|
||||
}
|
||||
}
|
||||
// public void saveUser(final User user) {
|
||||
//
|
||||
// try (final PreparedStatement statement = this.getConnection().prepareStatement(this.getSaveStatement())) {
|
||||
// final PlayerArmor playerArmor = user.getPlayerArmor();
|
||||
// final String hat = playerArmor.getHat().getId();
|
||||
// final String backpack = playerArmor.getBackpack().getId();
|
||||
//
|
||||
// statement.setString(1, user.getUuid().toString());
|
||||
// statement.setString(2, backpack);
|
||||
// statement.setString(3, hat);
|
||||
// statement.setString(5, backpack);
|
||||
// statement.setString(6, hat);
|
||||
//
|
||||
// statement.executeUpdate();
|
||||
// } catch (final SQLException exception) {
|
||||
// this.plugin.getLogger().severe("There was in issue saving the player!");
|
||||
// exception.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public User loadUser(final UUID uuid) {
|
||||
// final int armorStandId = ARMOR_STAND_ID.getAndDecrement();
|
||||
//
|
||||
// final User blankUser = new User(
|
||||
// uuid,
|
||||
// PlayerArmor.empty(),
|
||||
// armorStandId
|
||||
// );
|
||||
//
|
||||
// try (final PreparedStatement statement = this.getConnection().prepareStatement(this.getLoadStatement())) {
|
||||
// statement.setString(1, uuid.toString());
|
||||
//
|
||||
// final ResultSet results = statement.executeQuery();
|
||||
//
|
||||
// if (!results.next()) {
|
||||
// return blankUser;
|
||||
// }
|
||||
//
|
||||
// final String backpackId = results.getString(1);
|
||||
// final String hatId = results.getString(2);
|
||||
// final int dye = results.getInt(3);
|
||||
//
|
||||
// final CosmeticManager manager = this.plugin.getCosmeticManager();
|
||||
//
|
||||
// ArmorItem backpack = manager.getArmorItem(backpackId);
|
||||
// ArmorItem hat = manager.getArmorItem(hatId);
|
||||
//
|
||||
// if (backpack == null) backpack = ArmorItem.empty(ArmorItem.Type.BACKPACK);
|
||||
// if (hat == null) hat = ArmorItem.empty(ArmorItem.Type.HAT);
|
||||
//
|
||||
// return new User(
|
||||
// uuid,
|
||||
// new PlayerArmor(
|
||||
// hat,
|
||||
// backpack,
|
||||
// // todo
|
||||
// null
|
||||
// ),
|
||||
// armorStandId
|
||||
// );
|
||||
//
|
||||
// } catch (final SQLException exception) {
|
||||
// exception.printStackTrace();
|
||||
// return blankUser;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public abstract void close();
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package io.github.fisher2911.hmccosmetics.database.dao;
|
||||
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
|
||||
@DatabaseTable(tableName = "armor_item")
|
||||
public class ArmorItemDAO {
|
||||
|
||||
@DatabaseField(foreign = true, foreignAutoRefresh = true)
|
||||
private UserDAO user;
|
||||
|
||||
@DatabaseField
|
||||
private String id;
|
||||
|
||||
@DatabaseField
|
||||
private String type;
|
||||
|
||||
@DatabaseField(columnName = "color")
|
||||
private int rgbDye;
|
||||
|
||||
public ArmorItemDAO(final String id, final String type, final int rgbDye) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.rgbDye = rgbDye;
|
||||
}
|
||||
|
||||
public static ArmorItemDAO fromArmorItem(final ArmorItem armorItem) {
|
||||
return new ArmorItemDAO(armorItem.getId(), armorItem.getType().toString(), armorItem.getDye());
|
||||
}
|
||||
|
||||
public ArmorItemDAO() {
|
||||
}
|
||||
|
||||
public UserDAO getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(final UserDAO user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getRgbDye() {
|
||||
return rgbDye;
|
||||
}
|
||||
|
||||
public void setRgbDye(final int rgbDye) {
|
||||
this.rgbDye = rgbDye;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package io.github.fisher2911.hmccosmetics.database.dao;
|
||||
|
||||
import com.j256.ormlite.dao.EagerForeignCollection;
|
||||
import com.j256.ormlite.dao.ForeignCollection;
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.field.ForeignCollectionField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@DatabaseTable(tableName = "user")
|
||||
public class UserDAO {
|
||||
|
||||
@DatabaseField(id = true)
|
||||
private UUID uuid;
|
||||
|
||||
@ForeignCollectionField(eager = true)
|
||||
private ForeignCollection<ArmorItemDAO> armorItems;
|
||||
|
||||
public UserDAO() {
|
||||
}
|
||||
|
||||
public UserDAO(final UUID uuid, final ForeignCollection<ArmorItemDAO> armorItems) {
|
||||
this.uuid = uuid;
|
||||
this.armorItems = armorItems;
|
||||
}
|
||||
|
||||
public void setUuid(final UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public UserDAO(final UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public ForeignCollection<ArmorItemDAO> getArmorItems() {
|
||||
return armorItems;
|
||||
}
|
||||
|
||||
public void setArmorItems(final ForeignCollection<ArmorItemDAO> armorItems) {
|
||||
this.armorItems = armorItems;
|
||||
}
|
||||
}
|
||||
@@ -201,6 +201,10 @@ public class ArmorItem extends GuiItem {
|
||||
return dyeable;
|
||||
}
|
||||
|
||||
public int getDye() {
|
||||
return dye;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
return this.color(super.getItemStack());
|
||||
|
||||
Reference in New Issue
Block a user