forked from unimatrix/web
Deliver paid kits in the active MySQL inventory transaction
This commit is contained in:
parent
d5edec1907
commit
976ce1c2fc
1 changed files with 54 additions and 9 deletions
|
|
@ -48,16 +48,21 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
|
|||
|
||||
@Override public void onDisable() { inflight.clear(); notices.clear(); }
|
||||
|
||||
private static boolean mysql(Connection connection) throws SQLException { return connection.getMetaData().getDatabaseProductName().equalsIgnoreCase("MySQL"); }
|
||||
private static void identity(PreparedStatement statement, int index, UUID value, boolean mysql) throws SQLException {
|
||||
if (mysql) statement.setString(index, value.toString()); else statement.setObject(index, value);
|
||||
}
|
||||
|
||||
private Connection connect() throws Exception {
|
||||
String url = core.getConfig().getString("database.jdbc-url", "");
|
||||
if (!url.startsWith("jdbc:postgresql://")) throw new SQLException("Private shared inventory database required");
|
||||
if (!url.startsWith("jdbc:postgresql://") && !url.startsWith("jdbc:mysql://")) throw new SQLException("Private shared inventory database required");
|
||||
Properties settings = new Properties();
|
||||
settings.setProperty("user", core.getConfig().getString("database.username", ""));
|
||||
settings.setProperty("password", core.getConfig().getString("database.password", ""));
|
||||
settings.setProperty("connectTimeout", "3"); settings.setProperty("socketTimeout", "5");
|
||||
settings.setProperty("ApplicationName", "DuneKits");
|
||||
// The running DuneCore already contains its matching PostgreSQL driver.
|
||||
Driver driver = (Driver) Class.forName("org.postgresql.Driver", true, core.getClass().getClassLoader())
|
||||
Driver driver = (Driver) Class.forName(url.startsWith("jdbc:mysql://") ? "com.mysql.cj.jdbc.Driver" : "org.postgresql.Driver", true, core.getClass().getClassLoader())
|
||||
.getDeclaredConstructor().newInstance();
|
||||
Connection connection = driver.connect(url, settings);
|
||||
if (connection == null) throw new SQLException("Private inventory connection unavailable");
|
||||
|
|
@ -70,9 +75,9 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
|
|||
getServer().getScheduler().runTaskAsynchronously(this, () -> {
|
||||
List<UUID> pending = new ArrayList<>();
|
||||
try (Connection connection = connect(); PreparedStatement q = connection.prepareStatement(
|
||||
"SELECT id FROM dune_kit_deliveries WHERE buyer_uuid=? AND delivered_at IS NULL ORDER BY created_at,id LIMIT 5")) {
|
||||
q.setObject(1, buyer);
|
||||
try (ResultSet rows = q.executeQuery()) { while (rows.next()) pending.add((UUID) rows.getObject(1)); }
|
||||
KitDeliverySQL.pending(mysql(connection)))) {
|
||||
identity(q, 1, buyer, mysql(connection));
|
||||
try (ResultSet rows = q.executeQuery()) { while (rows.next()) pending.add(UUID.fromString(rows.getString(1))); }
|
||||
} catch (Exception ex) {
|
||||
// Never log credentials, snapshot contents or private item codes.
|
||||
getLogger().fine("Pending kit read unavailable: " + ex.getClass().getSimpleName());
|
||||
|
|
@ -122,8 +127,8 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
|
|||
try {
|
||||
Consumer<Connection> operation = connection -> {
|
||||
try (PreparedStatement q = connection.prepareStatement(
|
||||
"SELECT snapshot_json,snapshot_hash FROM dune_kit_deliveries WHERE id=? AND buyer_uuid=? AND delivered_at IS NULL FOR UPDATE")) {
|
||||
q.setObject(1, id); q.setObject(2, player.getUniqueId());
|
||||
KitDeliverySQL.lock(mysql(connection)))) {
|
||||
identity(q, 1, id, mysql(connection)); identity(q, 2, player.getUniqueId(), mysql(connection));
|
||||
String raw, hash;
|
||||
try (ResultSet rows = q.executeQuery()) {
|
||||
if (!rows.next()) return;
|
||||
|
|
@ -136,8 +141,8 @@ public final class DuneKits extends JavaPlugin implements CommandExecutor, org.b
|
|||
if (!player.getInventory().addItem(items.toArray(ItemStack[]::new)).isEmpty())
|
||||
throw new NoRoom();
|
||||
try (PreparedStatement update = connection.prepareStatement(
|
||||
"UPDATE dune_kit_deliveries SET delivered_at=(EXTRACT(EPOCH FROM clock_timestamp())*1000)::bigint WHERE id=? AND buyer_uuid=? AND delivered_at IS NULL")) {
|
||||
update.setObject(1, id); update.setObject(2, player.getUniqueId());
|
||||
KitDeliverySQL.acknowledge(mysql(connection)))) {
|
||||
identity(update, 1, id, mysql(connection)); identity(update, 2, player.getUniqueId(), mysql(connection));
|
||||
if (update.executeUpdate() != 1) throw new SQLException("Delivery changed");
|
||||
}
|
||||
delivered[0] = true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff