9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-28 19:39:14 +00:00

More database stuff

This commit is contained in:
HeroBrineGoat
2022-01-20 19:09:54 -05:00
parent d78bf20ca9
commit 70ee381f48
8 changed files with 85 additions and 70 deletions

View File

@@ -21,6 +21,7 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.BufferedReader;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -49,7 +50,11 @@ public class HMCCosmetics extends JavaPlugin {
this.userManager.startTeleportTask();
this.database = DatabaseFactory.create(this);
try {
this.database = DatabaseFactory.create(this);
} catch (final SQLException exception) {
exception.printStackTrace();
}
this.registerCommands();
this.registerListeners();

View File

@@ -2,14 +2,19 @@ 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.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.cosmetic.CosmeticManager;
import io.github.fisher2911.hmccosmetics.database.dao.ArmorItemDAO;
import io.github.fisher2911.hmccosmetics.database.dao.UserDAO;
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
import io.github.fisher2911.hmccosmetics.user.User;
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,9 +39,9 @@ public abstract class Database {
"))";
protected final HMCCosmetics plugin;
private final DataSourceConnectionSource dataSource;
private final ConnectionSource dataSource;
public Database(final HMCCosmetics plugin, final DataSourceConnectionSource dataSource) {
public Database(final HMCCosmetics plugin, final ConnectionSource dataSource) {
this.plugin = plugin;
this.dataSource = dataSource;
}
@@ -49,8 +54,8 @@ public abstract class Database {
private void createTables() {
try {
TableUtils.createTable(this.dataSource, ArmorItemDAO.class);
TableUtils.createTable(this.dataSource, UserDAO.class);
TableUtils.createTableIfNotExists(this.dataSource, ArmorItemDAO.class);
TableUtils.createTableIfNotExists(this.dataSource, UserDAO.class);
} catch (final SQLException exception) {
exception.printStackTrace();
}
@@ -61,9 +66,11 @@ public abstract class Database {
final UserDAO user = new UserDAO(u.getUuid());
final Dao<UserDAO, UUID> userDao = DaoManager.createDao(this.dataSource, UserDAO.class);
userDao.create(user);
userDao.assignEmptyForeignCollection(user, "armorItems");
userDao.create(user);
for (final ArmorItem armorItem : u.getPlayerArmor().getArmorItems()) {
user.getArmorItems().add(ArmorItemDAO.fromArmorItem(armorItem));
}
userDao.createOrUpdate(user);
} catch (final SQLException exception) {
exception.printStackTrace();
}
@@ -89,52 +96,52 @@ public abstract class Database {
// }
// }
//
// 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 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();

View File

@@ -7,6 +7,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.logging.Logger;
public class DatabaseFactory {
@@ -19,7 +20,7 @@ public class DatabaseFactory {
private static final String IP_PATH = "ip";
private static final String PORT_PATH = "port";
public static Database create(final HMCCosmetics plugin) {
public static Database create(final HMCCosmetics plugin) throws SQLException {
final File file = Path.of(plugin.getDataFolder().getPath(), FILE_NAME).toFile();
if (!file.exists()) {

View File

@@ -39,7 +39,7 @@ public class MySQLDatabase extends Database {
final String password,
final String ip,
final String port) {
super(plugin);
super(plugin, null);
final HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + name);

View File

@@ -1,5 +1,7 @@
package io.github.fisher2911.hmccosmetics.database;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
import io.github.fisher2911.hmccosmetics.user.User;
@@ -15,8 +17,12 @@ public class SQLiteDatabase extends Database {
private Connection conn;
public SQLiteDatabase(final HMCCosmetics plugin) {
super(plugin);
public SQLiteDatabase(final HMCCosmetics plugin) throws SQLException {
super(plugin, new JdbcPooledConnectionSource("jdbc:sqlite:" + Path.of(
plugin.getDataFolder().getPath(),
"database",
"users.db"
).toFile().getPath()));
}
String SAVE_STATEMENT =

View File

@@ -7,6 +7,7 @@ import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
@@ -60,4 +61,8 @@ public class PlayerArmor {
public ArmorItem setItem(final ArmorItem armorItem) {
return this.armorItems.put(armorItem.getType(), armorItem);
}
public Collection<ArmorItem> getArmorItems() {
return this.armorItems.values();
}
}

View File

@@ -174,18 +174,6 @@ public class User {
final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
final PacketContainer teleportPacket = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT);
teleportPacket.getIntegers().write(0, this.armorStandId);
teleportPacket.getDoubles().
write(0, location.getX()).
write(1, location.getY()).
write(2, location.getZ());
teleportPacket.getBytes().
write(0, (byte) (location.getYaw() * 256.0F / 360.0F)).
write(1, (byte) (location.getPitch() * 256.0F / 360.0F));
for (final Player p : Bukkit.getOnlinePlayers()) {
try {
protocolManager.sendServerPacket(p, armorPacket);

View File

@@ -70,6 +70,9 @@ public class UserManager {
this.armorStandIdMap.remove(user.getArmorStandId());
// todo - remove
this.plugin.getDatabase().saveUser(user);
user.removeAllCosmetics();
this.setFakeHelmet(user);
user.despawnAttached();