UltimateStacker/src/main/java/com/songoda/ultimatestacker/database/DataManager.java

423 lines
19 KiB
Java
Raw Normal View History

2019-08-02 15:59:10 +02:00
package com.songoda.ultimatestacker.database;
2020-08-25 01:01:11 +02:00
import com.songoda.core.compatibility.CompatibleMaterial;
2019-09-03 22:38:00 +02:00
import com.songoda.core.database.DataManagerAbstract;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.DatabaseType;
import com.songoda.ultimatestacker.settings.Settings;
2020-08-25 01:01:11 +02:00
import com.songoda.ultimatestacker.stackable.block.BlockStack;
import com.songoda.ultimatestacker.stackable.entity.ColdEntityStack;
import com.songoda.ultimatestacker.stackable.entity.EntityStack;
import com.songoda.ultimatestacker.stackable.entity.StackedEntity;
import com.songoda.ultimatestacker.stackable.spawner.SpawnerStack;
2019-09-03 22:38:00 +02:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
2019-09-03 22:38:00 +02:00
import org.bukkit.plugin.Plugin;
2022-08-07 16:39:49 +02:00
import org.checkerframework.common.returnsreceiver.qual.This;
2019-08-02 15:59:10 +02:00
2022-08-07 16:39:49 +02:00
import java.sql.Connection;
2020-08-25 01:01:11 +02:00
import java.sql.PreparedStatement;
import java.sql.ResultSet;
2022-08-07 16:39:49 +02:00
import java.sql.SQLException;
2020-08-25 01:01:11 +02:00
import java.sql.Statement;
import java.util.ArrayList;
2020-09-01 20:44:39 +02:00
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
2020-08-25 01:01:11 +02:00
import java.util.function.Consumer;
2019-09-03 22:38:00 +02:00
public class DataManager extends DataManagerAbstract {
2019-08-02 15:59:10 +02:00
public DataManager(DatabaseConnector databaseConnector, Plugin plugin) {
2019-09-03 22:38:00 +02:00
super(databaseConnector, plugin);
2019-08-02 15:59:10 +02:00
}
public void bulkUpdateSpawners(Collection<SpawnerStack> spawnerStacks) {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2019-08-03 06:23:36 +02:00
String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?";
2022-08-07 16:39:49 +02:00
PreparedStatement statement = connection.prepareStatement(updateSpawner);
for (SpawnerStack spawnerStack : spawnerStacks) {
statement.setInt(1, spawnerStack.getAmount());
statement.setInt(2, spawnerStack.getId());
statement.addBatch();
2019-08-02 15:59:10 +02:00
}
2022-08-07 16:39:49 +02:00
statement.executeBatch();
2022-08-09 20:01:00 +02:00
} catch (Exception ex) {
ex.printStackTrace();
}
2019-08-02 15:59:10 +02:00
}
public void updateSpawner(SpawnerStack spawnerStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateSpawner);
2019-08-02 15:59:10 +02:00
statement.setInt(1, spawnerStack.getAmount());
2019-08-02 20:51:15 +02:00
statement.setInt(2, spawnerStack.getId());
2019-08-02 15:59:10 +02:00
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (SQLException exception) {
exception.printStackTrace();
2019-08-02 15:59:10 +02:00
}
2022-08-07 16:39:49 +02:00
});
2019-08-02 15:59:10 +02:00
}
public void createSpawner(SpawnerStack spawnerStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
String createSpawner = "INSERT INTO " + getSyntax("OR REPLACE ", DatabaseType.SQLITE) + this.getTablePrefix() + "spawners (amount, world, x, y, z) VALUES (?, ?, ?, ?, ?)";
2022-08-07 16:39:49 +02:00
PreparedStatement statement = connection.prepareStatement(createSpawner);
2019-08-02 15:59:10 +02:00
statement.setInt(1, spawnerStack.getAmount());
statement.setString(2, spawnerStack.getWorld().getName());
statement.setInt(3, spawnerStack.getX());
statement.setInt(4, spawnerStack.getY());
statement.setInt(5, spawnerStack.getZ());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
int spawnerId = this.lastInsertedId(connection, "spawners");
this.sync(() -> spawnerStack.setId(spawnerId));
} catch (Exception ex) {
ex.printStackTrace();
2019-08-02 15:59:10 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void updateBlock(BlockStack blockStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String updateBlock = "UPDATE " + this.getTablePrefix() + "blocks SET amount = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateBlock);
if (blockStack.getAmount() == 0) return;
2020-08-25 01:01:11 +02:00
statement.setInt(1, blockStack.getAmount());
statement.setInt(2, blockStack.getId());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void createBlock(BlockStack blockStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
String createSpawner = "INSERT INTO " + getSyntax("OR REPLACE ", DatabaseType.SQLITE) + this.getTablePrefix() + "blocks (amount, material, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
2022-08-07 16:39:49 +02:00
PreparedStatement statement = connection.prepareStatement(createSpawner);
2020-08-25 01:01:11 +02:00
statement.setInt(1, blockStack.getAmount());
statement.setString(2, blockStack.getMaterial().name());
statement.setString(3, blockStack.getWorld().getName());
statement.setInt(4, blockStack.getX());
statement.setInt(5, blockStack.getY());
statement.setInt(6, blockStack.getZ());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
int blockId = this.lastInsertedId(connection, "blocks");
this.sync(() -> blockStack.setId(blockId));
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void createHostEntity(ColdEntityStack stack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
String createSerializedEntity = "INSERT INTO " + getSyntax("OR REPLACE ", DatabaseType.SQLITE) + this.getTablePrefix() + "host_entities (uuid, create_duplicates) VALUES (?, ?)";
2022-08-07 16:39:49 +02:00
PreparedStatement statement = connection.prepareStatement(createSerializedEntity);
if (stack == null || stack.getHostUniqueId() == null) return;
2020-08-25 01:01:11 +02:00
statement.setString(1, stack.getHostUniqueId().toString());
statement.setInt(2, stack.getCreateDuplicates());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
int stackId = this.lastInsertedId(connection, "host_entities");
this.sync(() -> stack.setId(stackId));
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void createStackedEntity(EntityStack hostStack, StackedEntity stackedEntity) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String createSerializedEntity = "INSERT INTO " + getSyntax("OR REPLACE ", DatabaseType.SQLITE) + this.getTablePrefix() + "stacked_entities (uuid, host, serialized_entity) VALUES (?, ?, ?) "
+ (Settings.MYSQL_ENABLED.getBoolean() ? "ON DUPLICATE KEY UPDATE host = ?, serialized_entity = ?" : "ON CONFLICT(uuid) DO UPDATE SET host = ?, serialized_entity = ?");
2022-08-07 16:39:49 +02:00
PreparedStatement statement = connection.prepareStatement(createSerializedEntity);
2020-08-31 15:42:48 +02:00
if (hostStack.getHostUniqueId() == null) return;
2020-08-25 01:01:11 +02:00
statement.setString(1, stackedEntity.getUniqueId().toString());
statement.setInt(2, hostStack.getId());
statement.setBytes(3, stackedEntity.getSerializedEntity());
statement.setInt(4, hostStack.getId());
statement.setBytes(5, stackedEntity.getSerializedEntity());
2020-08-25 01:01:11 +02:00
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void createStackedEntities(ColdEntityStack hostStack, List<StackedEntity> stackedEntities) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String createSerializedEntity = "REPLACE INTO " + this.getTablePrefix() + "stacked_entities (uuid, host, serialized_entity) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createSerializedEntity);
2020-08-31 15:42:48 +02:00
if (hostStack.getHostUniqueId() == null) return;
2020-08-25 01:01:11 +02:00
for (StackedEntity entity : stackedEntities) {
statement.setString(1, entity.getUniqueId().toString());
statement.setInt(2, hostStack.getId());
statement.setBytes(3, entity.getSerializedEntity());
statement.addBatch();
}
statement.executeBatch();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void updateHost(ColdEntityStack hostStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String updateHost = "UPDATE " + this.getTablePrefix() + "host_entities SET uuid = ?, create_duplicates = ?, updated_at = current_timestamp WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateHost);
2020-08-31 15:42:48 +02:00
if (hostStack.getHostUniqueId() == null) return;
2020-08-25 01:01:11 +02:00
statement.setString(1, hostStack.getHostUniqueId().toString());
statement.setInt(2, hostStack.getCreateDuplicates());
statement.setInt(3, hostStack.getId());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void deleteHost(ColdEntityStack stack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String deleteHost = "DELETE FROM " + this.getTablePrefix() + "host_entities WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteHost)) {
statement.setInt(1, stack.getId());
statement.executeUpdate();
}
2020-08-25 01:01:11 +02:00
2022-08-07 16:39:49 +02:00
String deleteStackedEntities = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE host = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteStackedEntities)) {
statement.setInt(1, stack.getId());
statement.executeUpdate();
}
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void deleteStackedEntity(UUID uuid) {
2021-10-17 02:22:38 +02:00
if (uuid == null)
return;
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String deleteStackedEntity = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE uuid = ?";
PreparedStatement statement = connection.prepareStatement(deleteStackedEntity);
2020-08-25 01:01:11 +02:00
statement.setString(1, uuid.toString());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void deleteStackedEntities(List<StackedEntity> entities) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String deleteStackedEntities = "DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE uuid = ?";
PreparedStatement statement = connection.prepareStatement(deleteStackedEntities);
2020-08-25 01:01:11 +02:00
for (StackedEntity entity : entities) {
if (entity == null) continue;
statement.setString(1, entity.getUniqueId().toString());
statement.addBatch();
}
statement.executeBatch();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2019-08-02 15:59:10 +02:00
}
public void deleteSpawner(SpawnerStack spawnerStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String deleteSpawner = "DELETE FROM " + this.getTablePrefix() + "spawners WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deleteSpawner);
2019-08-02 20:51:15 +02:00
statement.setInt(1, spawnerStack.getId());
2019-08-02 15:59:10 +02:00
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2019-08-02 15:59:10 +02:00
}
2022-08-07 16:39:49 +02:00
});
2019-08-02 15:59:10 +02:00
}
2020-08-25 01:01:11 +02:00
public void deleteBlock(BlockStack blockStack) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
String deleteBlock = "DELETE FROM " + this.getTablePrefix() + "blocks WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deleteBlock);
2020-08-25 01:01:11 +02:00
statement.setInt(1, blockStack.getId());
statement.executeUpdate();
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
public void getEntities(Consumer<Map<Integer, ColdEntityStack>> callback) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
2022-08-09 20:01:00 +02:00
try (Connection connection = this.databaseConnector.getConnection()) {
2022-08-07 16:39:49 +02:00
Map<Integer, ColdEntityStack> entities = new HashMap<>();
boolean mysql = Settings.MYSQL_ENABLED.getBoolean();
int databasePurge = Settings.DATABASE_PURGE.getInt();
String whereStatement = mysql ? "WHERE updated_at < NOW() - INTERVAL " + databasePurge + " DAY" : "WHERE updated_at <= date('now','-" + databasePurge + " day')";
String selectOldEntities = "SELECT * FROM " + this.getTablePrefix() + "host_entities " + whereStatement;
try (Statement statement = connection.createStatement()) {
List<String> toDelete = new ArrayList<>();
ResultSet result = statement.executeQuery(selectOldEntities);
while (result.next()) {
int hostId = result.getInt("id");
toDelete.add(String.valueOf(hostId));
}
if (!toDelete.isEmpty()) {
statement.execute("DELETE FROM " + this.getTablePrefix() + "host_entities " + whereStatement);
statement.execute("DELETE FROM " + this.getTablePrefix() + "stacked_entities WHERE host IN (" + String.join(", ", toDelete) + ")");
}
} catch (Exception e) {
e.printStackTrace();
}
2021-10-17 02:22:38 +02:00
2022-08-07 16:39:49 +02:00
String selectEntities = "SELECT * FROM " + this.getTablePrefix() + "host_entities";
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(selectEntities);
while (result.next()) {
int hostId = result.getInt("id");
2022-08-07 16:39:49 +02:00
UUID host = UUID.fromString(result.getString("uuid"));
2020-08-25 01:01:11 +02:00
2022-08-07 16:39:49 +02:00
int createDuplicates = result.getInt("create_duplicates");
2020-08-25 01:01:11 +02:00
2022-08-07 16:39:49 +02:00
ColdEntityStack stack = new ColdEntityStack(host, hostId);
stack.createDuplicates(createDuplicates);
entities.put(hostId, stack);
}
} catch (Exception e) {
e.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
String selectStackedEntities = "SELECT * FROM " + this.getTablePrefix() + "stacked_entities";
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(selectStackedEntities);
while (result.next()) {
UUID uuid = UUID.fromString(result.getString("uuid"));
int hostId = result.getInt("host");
byte[] serializedEntity = result.getBytes("serialized_entity");
ColdEntityStack stack = entities.get(hostId);
if (stack == null) continue;
stack.addEntityToStackSilently(new StackedEntity(uuid, serializedEntity));
}
} catch (Exception e) {
e.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
this.sync(() -> callback.accept(entities));
} catch (Exception ex) {
ex.printStackTrace();
}
});
2020-08-25 01:01:11 +02:00
}
2019-08-02 15:59:10 +02:00
public void getSpawners(Consumer<Map<Location, SpawnerStack>> callback) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()){
String selectSpawners = "SELECT * FROM " + this.getTablePrefix() + "spawners";
2019-08-02 15:59:10 +02:00
2022-08-07 16:39:49 +02:00
Map<Location, SpawnerStack> spawners = new HashMap<>();
2019-08-02 15:59:10 +02:00
2022-08-07 16:39:49 +02:00
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(selectSpawners);
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
2022-08-07 16:39:49 +02:00
if (world == null)
continue;
2022-08-07 16:39:49 +02:00
int spawnerId = result.getInt("id");
2019-08-02 20:51:15 +02:00
2022-08-07 16:39:49 +02:00
int amount = result.getInt("amount");
2019-08-02 15:59:10 +02:00
2022-08-07 16:39:49 +02:00
int x = result.getInt("x");
int y = result.getInt("y");
int z = result.getInt("z");
Location location = new Location(world, x, y, z);
2019-08-02 15:59:10 +02:00
2022-08-07 16:39:49 +02:00
SpawnerStack spawnerStack = new SpawnerStack(location, amount);
spawnerStack.setId(spawnerId);
spawners.put(location, spawnerStack);
}
} catch (Exception e) {
e.printStackTrace();
2019-08-02 15:59:10 +02:00
}
2022-08-07 16:39:49 +02:00
this.sync(() -> callback.accept(spawners));
} catch (Exception ex) {
ex.printStackTrace();
}
});
2019-08-02 15:59:10 +02:00
}
2020-08-25 01:01:11 +02:00
public void getBlocks(Consumer<Map<Location, BlockStack>> callback) {
2022-08-07 16:39:49 +02:00
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String selectBlocks = "SELECT * FROM " + this.getTablePrefix() + "blocks";
Map<Location, BlockStack> blocks = new HashMap<>();
2020-08-25 01:01:11 +02:00
2022-08-07 16:39:49 +02:00
Statement statement = connection.createStatement();
2020-08-25 01:01:11 +02:00
ResultSet result = statement.executeQuery(selectBlocks);
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
if (world == null)
continue;
int blockId = result.getInt("id");
CompatibleMaterial material = CompatibleMaterial.getMaterial(result.getString("material"));
int amount = result.getInt("amount");
int x = result.getInt("x");
int y = result.getInt("y");
int z = result.getInt("z");
Location location = new Location(world, x, y, z);
BlockStack blockStack = new BlockStack(material, location, amount);
blockStack.setId(blockId);
blocks.put(location, blockStack);
2022-08-07 16:39:49 +02:00
this.sync(() -> callback.accept(blocks));
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
} catch (Exception ex) {
ex.printStackTrace();
2020-08-25 01:01:11 +02:00
}
2022-08-07 16:39:49 +02:00
});
2020-08-25 01:01:11 +02:00
}
2019-08-02 15:59:10 +02:00
}