feat(sql): player storage
This commit is contained in:
parent
2d2e315793
commit
72c2c5ed12
3 changed files with 52 additions and 17 deletions
|
@ -8,6 +8,7 @@ import net.artelnatif.nicko.disguise.NickoProfile;
|
|||
import net.artelnatif.nicko.i18n.I18NDict;
|
||||
import net.artelnatif.nicko.storage.Storage;
|
||||
import net.artelnatif.nicko.storage.StorageProvider;
|
||||
import net.artelnatif.nicko.storage.sql.SQLStorageProvider;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Optional;
|
||||
|
@ -16,13 +17,20 @@ import java.util.UUID;
|
|||
public class JSONStorage extends Storage {
|
||||
private final NickoBukkit instance;
|
||||
|
||||
private JSONStorageProvider provider;
|
||||
|
||||
final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
|
||||
final File directory = new File(NickoBukkit.getInstance().getDataFolder() + "/players/");
|
||||
|
||||
public JSONStorage(NickoBukkit instance) { this.instance = instance; }
|
||||
|
||||
@Override
|
||||
public StorageProvider getProvider() { return new JSONStorageProvider(directory); }
|
||||
public StorageProvider getProvider() {
|
||||
if (provider == null) {
|
||||
provider = new JSONStorageProvider(directory);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
|
||||
|
|
|
@ -6,20 +6,29 @@ import net.artelnatif.nicko.disguise.NickoProfile;
|
|||
import net.artelnatif.nicko.i18n.I18NDict;
|
||||
import net.artelnatif.nicko.storage.Storage;
|
||||
|
||||
import java.sql.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SQLStorage extends Storage {
|
||||
private final NickoBukkit instance;
|
||||
|
||||
private SQLStorageProvider provider;
|
||||
|
||||
public SQLStorage(NickoBukkit instance) {
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLStorageProvider getProvider() {
|
||||
return new SQLStorageProvider(instance);
|
||||
if (provider == null) {
|
||||
provider = new SQLStorageProvider(instance);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,21 +36,21 @@ public class SQLStorage extends Storage {
|
|||
final Connection connection = getProvider().getConnection();
|
||||
try {
|
||||
final String sql = """
|
||||
INSERT INTO nicko.DATA
|
||||
(uuid, name, skin, bungeecord)
|
||||
INSERT IGNORE INTO nicko.DATA
|
||||
(`uuid`, `name`, `skin`, `bungeecord`)
|
||||
VALUES
|
||||
(?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE uuid = %s
|
||||
""".formatted(uuid.toString());
|
||||
""";
|
||||
|
||||
final PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setObject(0, uuid);
|
||||
statement.setString(1, profile.getName());
|
||||
statement.setString(2, profile.getSkin());
|
||||
statement.setBoolean(3, profile.isBungeecordTransfer());
|
||||
statement.setObject(1, uuidToBin(uuid));
|
||||
statement.setString(2, profile.getName());
|
||||
statement.setString(3, profile.getSkin());
|
||||
statement.setBoolean(4, profile.isBungeecordTransfer());
|
||||
statement.executeUpdate();
|
||||
return new ActionResult<>();
|
||||
} catch (SQLException e) {
|
||||
instance.getLogger().warning("Unable to store player.");
|
||||
instance.getLogger().warning("Unable to store player. (%s)".formatted(e.getMessage()));
|
||||
return new ActionResult<>(I18NDict.Error.SQL_ERROR);
|
||||
}
|
||||
}
|
||||
|
@ -55,4 +64,18 @@ public class SQLStorage extends Storage {
|
|||
public Optional<NickoProfile> retrieve(UUID uuid) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private byte[] uuidToBin(UUID uuid) {
|
||||
final byte[] uuidBytes = new byte[16];
|
||||
final ByteBuffer buffer = ByteBuffer.wrap(uuidBytes)
|
||||
.order(ByteOrder.BIG_ENDIAN)
|
||||
.putLong(uuid.getMostSignificantBits())
|
||||
.putLong(uuid.getLeastSignificantBits());
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
private UUID binToUUID(byte[] array) {
|
||||
final ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||
return new UUID(buffer.getLong(), buffer.getLong());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,16 @@ package net.artelnatif.nicko.storage.sql;
|
|||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.NickoConfiguration;
|
||||
import net.artelnatif.nicko.storage.StorageProvider;
|
||||
import org.mariadb.jdbc.MariaDbDataSource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SQLStorageProvider implements StorageProvider {
|
||||
private final NickoBukkit instance;
|
||||
private Connection connection;
|
||||
private MariaDbDataSource dataSource;
|
||||
|
||||
private final String schemaName = "nicko";
|
||||
|
||||
|
@ -23,9 +24,11 @@ public class SQLStorageProvider implements StorageProvider {
|
|||
public boolean init() {
|
||||
try {
|
||||
final NickoConfiguration config = instance.getNickoConfig();
|
||||
connection = DriverManager.getConnection("jdbc:mariadb://" + config.getSQLAddress(),
|
||||
config.getSQLUsername(),
|
||||
config.getSQLPassword());
|
||||
dataSource = new MariaDbDataSource();
|
||||
dataSource.setUrl("jdbc:mariadb://" + config.getSQLAddress());
|
||||
dataSource.setUser(config.getSQLUsername());
|
||||
dataSource.setPassword(config.getSQLPassword());
|
||||
connection = dataSource.getConnection();
|
||||
final boolean initialized = connection != null && !connection.isClosed();
|
||||
|
||||
if (!initialized) return false;
|
||||
|
@ -37,6 +40,7 @@ public class SQLStorageProvider implements StorageProvider {
|
|||
createTable();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +61,7 @@ public class SQLStorageProvider implements StorageProvider {
|
|||
|
||||
final String query = """
|
||||
CREATE TABLE IF NOT EXISTS %s.DATA (
|
||||
uuid uuid NOT NULL,
|
||||
uuid binary(16) NOT NULL,
|
||||
name varchar(16) NOT NULL,
|
||||
skin varchar(16) NOT NULL,
|
||||
bungeecord boolean NOT NULL,
|
||||
|
|
Loading…
Reference in a new issue