Fix MySQL

This commit is contained in:
ceze88 2022-08-07 16:39:49 +02:00
parent cbfa58bf17
commit d533e86ac3
3 changed files with 219 additions and 166 deletions

View File

@ -109,7 +109,6 @@ public class UltimateStacker extends SongodaPlugin {
public void onPluginEnable() { public void onPluginEnable() {
// Run Songoda Updater // Run Songoda Updater
SongodaCore.registerPlugin(this, 16, CompatibleMaterial.IRON_INGOT); SongodaCore.registerPlugin(this, 16, CompatibleMaterial.IRON_INGOT);
// Setup Config // Setup Config
Settings.setupConfig(); Settings.setupConfig();
this.setLocale(Settings.LANGUGE_MODE.getString(), false); this.setLocale(Settings.LANGUGE_MODE.getString(), false);
@ -209,8 +208,9 @@ public class UltimateStacker extends SongodaPlugin {
String username = Settings.MYSQL_USERNAME.getString(); String username = Settings.MYSQL_USERNAME.getString();
String password = Settings.MYSQL_PASSWORD.getString(); String password = Settings.MYSQL_PASSWORD.getString();
boolean useSSL = Settings.MYSQL_USE_SSL.getBoolean(); boolean useSSL = Settings.MYSQL_USE_SSL.getBoolean();
int poolSize = Settings.MYSQL_POOL_SIZE.getInt();
this.databaseConnector = new MySQLConnector(this, hostname, port, database, username, password, useSSL); this.databaseConnector = new MySQLConnector(this, hostname, port, database, username, password, useSSL, poolSize);
this.getLogger().info("Data handler connected using MySQL."); this.getLogger().info("Data handler connected using MySQL.");
} else { } else {
this.databaseConnector = new SQLiteConnector(this); this.databaseConnector = new SQLiteConnector(this);

View File

@ -13,9 +13,12 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.checkerframework.common.returnsreceiver.qual.This;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -34,7 +37,7 @@ public class DataManager extends DataManagerAbstract {
public void bulkUpdateSpawners(Collection<SpawnerStack> spawnerStacks) { public void bulkUpdateSpawners(Collection<SpawnerStack> spawnerStacks) {
this.databaseConnector.connect(connection -> { this.databaseConnector.connect(connection -> {
String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?"; String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateSpawner)) { PreparedStatement statement = connection.prepareStatement(updateSpawner);
for (SpawnerStack spawnerStack : spawnerStacks) { for (SpawnerStack spawnerStack : spawnerStacks) {
statement.setInt(1, spawnerStack.getAmount()); statement.setInt(1, spawnerStack.getAmount());
statement.setInt(2, spawnerStack.getId()); statement.setInt(2, spawnerStack.getId());
@ -42,26 +45,30 @@ public class DataManager extends DataManagerAbstract {
} }
statement.executeBatch(); statement.executeBatch();
}
}); });
} }
public void updateSpawner(SpawnerStack spawnerStack) { public void updateSpawner(SpawnerStack spawnerStack) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try(Connection connection = this.databaseConnector.getConnection()) {
String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?"; String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateSpawner)) {
PreparedStatement statement = connection.prepareStatement(updateSpawner);
statement.setInt(1, spawnerStack.getAmount()); statement.setInt(1, spawnerStack.getAmount());
statement.setInt(2, spawnerStack.getId()); statement.setInt(2, spawnerStack.getId());
statement.executeUpdate(); statement.executeUpdate();
} catch (SQLException exception) {
exception.printStackTrace();
} }
})); });
} }
public void createSpawner(SpawnerStack spawnerStack) { public void createSpawner(SpawnerStack spawnerStack) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try(Connection connection = this.databaseConnector.getConnection()){
String createSpawner = "INSERT INTO " + this.getTablePrefix() + "spawners (amount, world, x, y, z) VALUES (?, ?, ?, ?, ?)"; String createSpawner = "INSERT INTO " + this.getTablePrefix() + "spawners (amount, world, x, y, z) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createSpawner)) { PreparedStatement statement = connection.prepareStatement(createSpawner);
statement.setInt(1, spawnerStack.getAmount()); statement.setInt(1, spawnerStack.getAmount());
statement.setString(2, spawnerStack.getWorld().getName()); statement.setString(2, spawnerStack.getWorld().getName());
@ -69,29 +76,34 @@ public class DataManager extends DataManagerAbstract {
statement.setInt(4, spawnerStack.getY()); statement.setInt(4, spawnerStack.getY());
statement.setInt(5, spawnerStack.getZ()); statement.setInt(5, spawnerStack.getZ());
statement.executeUpdate(); statement.executeUpdate();
}
int spawnerId = this.lastInsertedId(connection, "spawners"); int spawnerId = this.lastInsertedId(connection, "spawners");
this.sync(() -> spawnerStack.setId(spawnerId)); this.sync(() -> spawnerStack.setId(spawnerId));
}), "create"); } catch (Exception ex) {
ex.printStackTrace();
}
});
} }
public void updateBlock(BlockStack blockStack) { public void updateBlock(BlockStack blockStack) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
if (blockStack.getAmount() == 0) return; try(Connection connection = this.databaseConnector.getConnection()) {
String updateBlock = "UPDATE " + this.getTablePrefix() + "blocks SET amount = ? WHERE id = ?"; String updateBlock = "UPDATE " + this.getTablePrefix() + "blocks SET amount = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateBlock)) { PreparedStatement statement = connection.prepareStatement(updateBlock);
if (blockStack.getAmount() == 0) return;
statement.setInt(1, blockStack.getAmount()); statement.setInt(1, blockStack.getAmount());
statement.setInt(2, blockStack.getId()); statement.setInt(2, blockStack.getId());
statement.executeUpdate(); statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} }
})); });
} }
public void createBlock(BlockStack blockStack) { public void createBlock(BlockStack blockStack) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String createSpawner = "INSERT INTO " + this.getTablePrefix() + "blocks (amount, material, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)"; String createSpawner = "INSERT INTO " + this.getTablePrefix() + "blocks (amount, material, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createSpawner)) { PreparedStatement statement = connection.prepareStatement(createSpawner);
statement.setInt(1, blockStack.getAmount()); statement.setInt(1, blockStack.getAmount());
statement.setString(2, blockStack.getMaterial().name()); statement.setString(2, blockStack.getMaterial().name());
@ -100,43 +112,52 @@ public class DataManager extends DataManagerAbstract {
statement.setInt(5, blockStack.getY()); statement.setInt(5, blockStack.getY());
statement.setInt(6, blockStack.getZ()); statement.setInt(6, blockStack.getZ());
statement.executeUpdate(); statement.executeUpdate();
}
int blockId = this.lastInsertedId(connection, "blocks"); int blockId = this.lastInsertedId(connection, "blocks");
this.sync(() -> blockStack.setId(blockId)); this.sync(() -> blockStack.setId(blockId));
}), "create"); } catch (Exception ex) {
ex.printStackTrace();
}
});
} }
public void createHostEntity(ColdEntityStack stack) { public void createHostEntity(ColdEntityStack stack) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String createSerializedEntity = "INSERT INTO " + this.getTablePrefix() + "host_entities (uuid, create_duplicates) VALUES (?, ?)"; String createSerializedEntity = "INSERT INTO " + this.getTablePrefix() + "host_entities (uuid, create_duplicates) VALUES (?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createSerializedEntity)) { PreparedStatement statement = connection.prepareStatement(createSerializedEntity);
if (stack == null || stack.getHostUniqueId() == null) return; if (stack == null || stack.getHostUniqueId() == null) return;
statement.setString(1, stack.getHostUniqueId().toString()); statement.setString(1, stack.getHostUniqueId().toString());
statement.setInt(2, stack.getCreateDuplicates()); statement.setInt(2, stack.getCreateDuplicates());
statement.executeUpdate(); statement.executeUpdate();
}
int stackId = this.lastInsertedId(connection, "host_entities"); int stackId = this.lastInsertedId(connection, "host_entities");
this.sync(() -> stack.setId(stackId)); this.sync(() -> stack.setId(stackId));
}), "create"); } catch (Exception ex) {
ex.printStackTrace();
}
});
} }
public void createStackedEntity(EntityStack hostStack, StackedEntity stackedEntity) { public void createStackedEntity(EntityStack hostStack, StackedEntity stackedEntity) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String createSerializedEntity = "INSERT INTO " + this.getTablePrefix() + "stacked_entities (uuid, host, serialized_entity) VALUES (?, ?, ?)"; String createSerializedEntity = "INSERT INTO " + this.getTablePrefix() + "stacked_entities (uuid, host, serialized_entity) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createSerializedEntity)) { PreparedStatement statement = connection.prepareStatement(createSerializedEntity);
if (hostStack.getHostUniqueId() == null) return; if (hostStack.getHostUniqueId() == null) return;
statement.setString(1, stackedEntity.getUniqueId().toString()); statement.setString(1, stackedEntity.getUniqueId().toString());
statement.setInt(2, hostStack.getId()); statement.setInt(2, hostStack.getId());
statement.setBytes(3, stackedEntity.getSerializedEntity()); statement.setBytes(3, stackedEntity.getSerializedEntity());
statement.executeUpdate(); statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} }
}), "create"); });
} }
public void createStackedEntities(ColdEntityStack hostStack, List<StackedEntity> stackedEntities) { public void createStackedEntities(ColdEntityStack hostStack, List<StackedEntity> stackedEntities) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String createSerializedEntity = "REPLACE INTO " + this.getTablePrefix() + "stacked_entities (uuid, host, serialized_entity) VALUES (?, ?, ?)"; String createSerializedEntity = "REPLACE INTO " + this.getTablePrefix() + "stacked_entities (uuid, host, serialized_entity) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createSerializedEntity)) { PreparedStatement statement = connection.prepareStatement(createSerializedEntity);
if (hostStack.getHostUniqueId() == null) return; if (hostStack.getHostUniqueId() == null) return;
for (StackedEntity entity : stackedEntities) { for (StackedEntity entity : stackedEntities) {
statement.setString(1, entity.getUniqueId().toString()); statement.setString(1, entity.getUniqueId().toString());
@ -145,25 +166,31 @@ public class DataManager extends DataManagerAbstract {
statement.addBatch(); statement.addBatch();
} }
statement.executeBatch(); statement.executeBatch();
} catch (Exception ex) {
ex.printStackTrace();
} }
}), "create"); });
} }
public void updateHost(ColdEntityStack hostStack) { public void updateHost(ColdEntityStack hostStack) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String updateHost = "UPDATE " + this.getTablePrefix() + "host_entities SET uuid = ?, create_duplicates = ?, updated_at = current_timestamp WHERE id = ?"; String updateHost = "UPDATE " + this.getTablePrefix() + "host_entities SET uuid = ?, create_duplicates = ?, updated_at = current_timestamp WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateHost)) { PreparedStatement statement = connection.prepareStatement(updateHost);
if (hostStack.getHostUniqueId() == null) return; if (hostStack.getHostUniqueId() == null) return;
statement.setString(1, hostStack.getHostUniqueId().toString()); statement.setString(1, hostStack.getHostUniqueId().toString());
statement.setInt(2, hostStack.getCreateDuplicates()); statement.setInt(2, hostStack.getCreateDuplicates());
statement.setInt(3, hostStack.getId()); statement.setInt(3, hostStack.getId());
statement.executeUpdate(); statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} }
})); });
} }
public void deleteHost(ColdEntityStack stack) { public void deleteHost(ColdEntityStack stack) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String deleteHost = "DELETE FROM " + this.getTablePrefix() + "host_entities WHERE id = ?"; String deleteHost = "DELETE FROM " + this.getTablePrefix() + "host_entities WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteHost)) { try (PreparedStatement statement = connection.prepareStatement(deleteHost)) {
statement.setInt(1, stack.getId()); statement.setInt(1, stack.getId());
@ -175,58 +202,74 @@ public class DataManager extends DataManagerAbstract {
statement.setInt(1, stack.getId()); statement.setInt(1, stack.getId());
statement.executeUpdate(); statement.executeUpdate();
} }
})); } catch (Exception ex) {
ex.printStackTrace();
}
});
} }
public void deleteStackedEntity(UUID uuid) { public void deleteStackedEntity(UUID uuid) {
if (uuid == null) if (uuid == null)
return; return;
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String deleteStackedEntity = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE uuid = ?"; String deleteStackedEntity = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE uuid = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteStackedEntity)) { PreparedStatement statement = connection.prepareStatement(deleteStackedEntity);
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.executeUpdate(); statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} }
})); });
} }
public void deleteStackedEntities(List<StackedEntity> entities) { public void deleteStackedEntities(List<StackedEntity> entities) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String deleteStackedEntities = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE uuid = ?"; String deleteStackedEntities = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE uuid = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteStackedEntities)) { PreparedStatement statement = connection.prepareStatement(deleteStackedEntities);
for (StackedEntity entity : entities) { for (StackedEntity entity : entities) {
if (entity == null) continue; if (entity == null) continue;
statement.setString(1, entity.getUniqueId().toString()); statement.setString(1, entity.getUniqueId().toString());
statement.addBatch(); statement.addBatch();
} }
statement.executeBatch(); statement.executeBatch();
} catch (Exception ex) {
ex.printStackTrace();
} }
})); });
} }
public void deleteSpawner(SpawnerStack spawnerStack) { public void deleteSpawner(SpawnerStack spawnerStack) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String deleteSpawner = "DELETE FROM " + this.getTablePrefix() + "spawners WHERE id = ?"; String deleteSpawner = "DELETE FROM " + this.getTablePrefix() + "spawners WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteSpawner)) { PreparedStatement statement = connection.prepareStatement(deleteSpawner);
statement.setInt(1, spawnerStack.getId()); statement.setInt(1, spawnerStack.getId());
statement.executeUpdate(); statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} }
})); });
} }
public void deleteBlock(BlockStack blockStack) { public void deleteBlock(BlockStack blockStack) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String deleteBlock = "DELETE FROM " + this.getTablePrefix() + "blocks WHERE id = ?"; String deleteBlock = "DELETE FROM " + this.getTablePrefix() + "blocks WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteBlock)) { PreparedStatement statement = connection.prepareStatement(deleteBlock);
statement.setInt(1, blockStack.getId()); statement.setInt(1, blockStack.getId());
statement.executeUpdate(); statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} }
})); });
} }
public void getEntities(Consumer<Map<Integer, ColdEntityStack>> callback) { public void getEntities(Consumer<Map<Integer, ColdEntityStack>> callback) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
Map<Integer, ColdEntityStack> entities = new HashMap<>(); Map<Integer, ColdEntityStack> entities = new HashMap<>();
@ -288,11 +331,15 @@ public class DataManager extends DataManagerAbstract {
} }
this.sync(() -> callback.accept(entities)); this.sync(() -> callback.accept(entities));
})); } catch (Exception ex) {
ex.printStackTrace();
}
});
} }
public void getSpawners(Consumer<Map<Location, SpawnerStack>> callback) { public void getSpawners(Consumer<Map<Location, SpawnerStack>> callback) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String selectSpawners = "SELECT * FROM " + this.getTablePrefix() + "spawners"; String selectSpawners = "SELECT * FROM " + this.getTablePrefix() + "spawners";
Map<Location, SpawnerStack> spawners = new HashMap<>(); Map<Location, SpawnerStack> spawners = new HashMap<>();
@ -323,16 +370,21 @@ public class DataManager extends DataManagerAbstract {
} }
this.sync(() -> callback.accept(spawners)); this.sync(() -> callback.accept(spawners));
})); } catch (Exception ex) {
ex.printStackTrace();
}
});
} }
public void getBlocks(Consumer<Map<Location, BlockStack>> callback) { public void getBlocks(Consumer<Map<Location, BlockStack>> callback) {
this.async(() -> this.databaseConnector.connect(connection -> { this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String selectBlocks = "SELECT * FROM " + this.getTablePrefix() + "blocks"; String selectBlocks = "SELECT * FROM " + this.getTablePrefix() + "blocks";
Map<Location, BlockStack> blocks = new HashMap<>(); Map<Location, BlockStack> blocks = new HashMap<>();
try (Statement statement = connection.createStatement()) { Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(selectBlocks); ResultSet result = statement.executeQuery(selectBlocks);
while (result.next()) { while (result.next()) {
World world = Bukkit.getWorld(result.getString("world")); World world = Bukkit.getWorld(result.getString("world"));
@ -354,12 +406,12 @@ public class DataManager extends DataManagerAbstract {
BlockStack blockStack = new BlockStack(material, location, amount); BlockStack blockStack = new BlockStack(material, location, amount);
blockStack.setId(blockId); blockStack.setId(blockId);
blocks.put(location, blockStack); blocks.put(location, blockStack);
}
} catch (Exception e) {
e.printStackTrace();
}
this.sync(() -> callback.accept(blocks)); this.sync(() -> callback.accept(blocks));
})); }
} catch (Exception ex) {
ex.printStackTrace();
}
});
} }
} }

View File

@ -283,6 +283,7 @@ public class Settings {
public static final ConfigSetting MYSQL_USERNAME = new ConfigSetting(config, "MySQL.Username", "user"); public static final ConfigSetting MYSQL_USERNAME = new ConfigSetting(config, "MySQL.Username", "user");
public static final ConfigSetting MYSQL_PASSWORD = new ConfigSetting(config, "MySQL.Password", "pass"); public static final ConfigSetting MYSQL_PASSWORD = new ConfigSetting(config, "MySQL.Password", "pass");
public static final ConfigSetting MYSQL_USE_SSL = new ConfigSetting(config, "MySQL.Use SSL", false); public static final ConfigSetting MYSQL_USE_SSL = new ConfigSetting(config, "MySQL.Use SSL", false);
public static final ConfigSetting MYSQL_POOL_SIZE = new ConfigSetting(config, "MySQL.Pool Size", 3, "Determines the number of connections the pool is using. Increase this value if you are getting timeout errors when more players online.");
public static void setupConfig() { public static void setupConfig() {
config.load(); config.load();