diff --git a/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorage.java b/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorage.java index 2d82ece..177df31 100644 --- a/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorage.java +++ b/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorage.java @@ -1,13 +1,16 @@ package net.artelnatif.nicko.storage.sql; +import com.google.common.io.ByteStreams; import net.artelnatif.nicko.Nicko; +import net.artelnatif.nicko.bukkit.i18n.I18NDict; import net.artelnatif.nicko.disguise.ActionResult; import net.artelnatif.nicko.disguise.NickoProfile; -import net.artelnatif.nicko.bukkit.i18n.I18NDict; import net.artelnatif.nicko.storage.Storage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -40,7 +43,7 @@ public class SQLStorage extends Storage { final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `bungeecord`) VALUES (?, ?, ?, ?)"; final PreparedStatement statement = connection.prepareStatement(sql); - statement.setObject(1, uuidToBin(uuid)); + statement.setBinaryStream(1, uuidToBin(uuid)); statement.setString(2, profile.getName()); statement.setString(3, profile.getSkin()); statement.setBoolean(4, profile.isBungeecordTransfer()); @@ -62,17 +65,20 @@ public class SQLStorage extends Storage { return Optional.empty(); } - private byte[] uuidToBin(UUID uuid) { - final byte[] uuidBytes = new byte[16]; - final ByteBuffer buffer = ByteBuffer.wrap(uuidBytes) - .order(ByteOrder.BIG_ENDIAN) + private ByteArrayInputStream uuidToBin(UUID uuid) { + byte[] bytes = new byte[16]; + ByteBuffer.wrap(bytes) .putLong(uuid.getMostSignificantBits()) .putLong(uuid.getLeastSignificantBits()); - return buffer.array(); + return new ByteArrayInputStream(bytes); } - private UUID binToUUID(byte[] array) { - final ByteBuffer buffer = ByteBuffer.wrap(array); - return new UUID(buffer.getLong(), buffer.getLong()); + private UUID binToUUID(InputStream stream) { + final ByteBuffer buffer = ByteBuffer.allocate(16); + try { + buffer.put(ByteStreams.toByteArray(stream)); + buffer.flip(); + return new UUID(buffer.getLong(), buffer.getLong()); + } catch (IOException ignored) { return null; } } } diff --git a/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorageProvider.java b/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorageProvider.java index 060c37d..314b55b 100644 --- a/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorageProvider.java +++ b/core/src/main/java/net/artelnatif/nicko/storage/sql/SQLStorageProvider.java @@ -12,7 +12,6 @@ import java.sql.SQLException; public class SQLStorageProvider implements StorageProvider { private final Nicko nicko; private Connection connection; - private MariaDbDataSource dataSource; private final String schemaName = "nicko"; @@ -24,7 +23,7 @@ public class SQLStorageProvider implements StorageProvider { public boolean init() { try { final Configuration config = nicko.getConfig(); - dataSource = new MariaDbDataSource(); + final MariaDbDataSource dataSource = new MariaDbDataSource(); dataSource.setUrl("jdbc:mariadb://" + config.getAddress()); dataSource.setUser(config.getUsername()); dataSource.setPassword(config.getPassword()); @@ -33,10 +32,7 @@ public class SQLStorageProvider implements StorageProvider { if (!initialized) return false; - nicko.getLogger().info("Creating SQL database..."); createDatabase(); - - nicko.getLogger().info("Creating SQL table..."); createTable(); return true; } catch (SQLException e) { @@ -56,36 +52,26 @@ public class SQLStorageProvider implements StorageProvider { } } - private void createTable() { + private void createTable() throws SQLException { final Connection connection = getConnection(); String query = "CREATE TABLE IF NOT EXISTS %s.DATA (uuid binary(16) NOT NULL,name varchar(16) NOT NULL,skin varchar(16) NOT NULL,bungeecord boolean NOT NULL,PRIMARY KEY (UUID))"; query = query.replace("%s", schemaName); - try { - final PreparedStatement statement = connection.prepareStatement(query); - statement.executeUpdate(); - statement.close(); - } catch (SQLException e) { - // TODO: 12/10/22 Handle error - throw new RuntimeException(e); - } + final PreparedStatement statement = connection.prepareStatement(query); + statement.executeUpdate(); + statement.close(); } - private void createDatabase() { + private void createDatabase() throws SQLException { final Connection connection = getConnection(); String query = "CREATE DATABASE IF NOT EXISTS %s"; query = query.replace("%s", schemaName); - try { - final PreparedStatement statement = connection.prepareStatement(query); - statement.executeUpdate(); - statement.close(); - } catch (SQLException e) { - // TODO: 12/10/22 Handle error - throw new RuntimeException(e); - } + final PreparedStatement statement = connection.prepareStatement(query); + statement.executeUpdate(); + statement.close(); } public Connection getConnection() {