mirror of
https://github.com/songoda/EpicFarming.git
synced 2024-09-28 06:37:29 +02:00
Remove deprecated methods, add MySQL support
This commit is contained in:
parent
778b6af275
commit
75dc4ad103
2
pom.xml
2
pom.xml
@ -124,7 +124,7 @@
|
||||
<dependency>
|
||||
<groupId>com.songoda</groupId>
|
||||
<artifactId>SongodaCore</artifactId>
|
||||
<version>2.6.13</version>
|
||||
<version>2.6.14-DEV</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -8,6 +8,7 @@ import com.songoda.core.compatibility.ServerVersion;
|
||||
import com.songoda.core.configuration.Config;
|
||||
import com.songoda.core.database.DataMigrationManager;
|
||||
import com.songoda.core.database.DatabaseConnector;
|
||||
import com.songoda.core.database.MySQLConnector;
|
||||
import com.songoda.core.database.SQLiteConnector;
|
||||
import com.songoda.core.gui.GuiManager;
|
||||
import com.songoda.core.hooks.EconomyManager;
|
||||
@ -136,8 +137,27 @@ public class EpicFarming extends SongodaPlugin {
|
||||
new CommandSettings(this)
|
||||
);
|
||||
|
||||
this.databaseConnector = new SQLiteConnector(this);
|
||||
this.getLogger().info("Data handler connected using SQLite.");
|
||||
// Database stuff.
|
||||
try {
|
||||
if (Settings.MYSQL_ENABLED.getBoolean()) {
|
||||
String hostname = Settings.MYSQL_HOSTNAME.getString();
|
||||
int port = Settings.MYSQL_PORT.getInt();
|
||||
String database = Settings.MYSQL_DATABASE.getString();
|
||||
String username = Settings.MYSQL_USERNAME.getString();
|
||||
String password = Settings.MYSQL_PASSWORD.getString();
|
||||
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, poolSize);
|
||||
this.getLogger().info("Data handler connected using MySQL.");
|
||||
} else {
|
||||
this.databaseConnector = new SQLiteConnector(this);
|
||||
this.getLogger().info("Data handler connected using SQLite.");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
this.getLogger().severe("Fatal error trying to connect to database. Please make sure all your connection settings are correct and try again. Plugin has been disabled.");
|
||||
this.emergencyStop();
|
||||
}
|
||||
|
||||
this.dataManager = new DataManager(this.databaseConnector, this);
|
||||
DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
|
||||
@ -252,7 +272,7 @@ public class EpicFarming extends SongodaPlugin {
|
||||
}
|
||||
|
||||
final boolean finalConverted = converted;
|
||||
dataManager.queueAsync(() -> {
|
||||
dataManager.runAsync(() -> {
|
||||
if (finalConverted) {
|
||||
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)");
|
||||
}
|
||||
@ -261,7 +281,7 @@ public class EpicFarming extends SongodaPlugin {
|
||||
this.farmManager.addFarms(farms.values());
|
||||
this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts));
|
||||
});
|
||||
}, "create");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
@ -30,21 +31,26 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
|
||||
public void createBoost(BoostData boostData) {
|
||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||
String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createBoostedPlayer)) {
|
||||
statement.setString(1, boostData.getPlayer().toString());
|
||||
statement.setInt(2, boostData.getMultiplier());
|
||||
statement.setLong(3, boostData.getEndTime());
|
||||
statement.executeUpdate();
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createBoostedPlayer)) {
|
||||
statement.setString(1, boostData.getPlayer().toString());
|
||||
statement.setInt(2, boostData.getMultiplier());
|
||||
statement.setLong(3, boostData.getEndTime());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public void getBoosts(Consumer<List<BoostData>> callback) {
|
||||
List<BoostData> boosts = new ArrayList<>();
|
||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players";
|
||||
ResultSet result = statement.executeQuery(selectBoostedPlayers);
|
||||
while (result.next()) {
|
||||
@ -53,50 +59,59 @@ public class DataManager extends DataManagerAbstract {
|
||||
long endTime = result.getLong("end_time");
|
||||
boosts.add(new BoostData(multiplier, endTime, player));
|
||||
}
|
||||
}
|
||||
|
||||
this.sync(() -> callback.accept(boosts));
|
||||
}));
|
||||
this.sync(() -> callback.accept(boosts));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void deleteBoost(BoostData boostData) {
|
||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||
String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteBoost)) {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
|
||||
PreparedStatement statement = connection.prepareStatement(deleteBoost);
|
||||
statement.setLong(1, boostData.getEndTime());
|
||||
statement.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public void createFarm(Farm farm) {
|
||||
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
String createFarm = "INSERT INTO " + this.getTablePrefix() + "active_farms (farm_type, level, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createFarm)) {
|
||||
statement.setString(1, farm.getFarmType().name());
|
||||
statement.setInt(2, farm.getLevel().getLevel());
|
||||
statement.setString(3, farm.getPlacedBy().toString());
|
||||
statement.setString(4, farm.getLocation().getWorld().getName());
|
||||
statement.setInt(5, farm.getLocation().getBlockX());
|
||||
statement.setInt(6, farm.getLocation().getBlockY());
|
||||
statement.setInt(7, farm.getLocation().getBlockZ());
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String createFarm = "INSERT INTO " + this.getTablePrefix() + "active_farms (farm_type, level, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createFarm)) {
|
||||
statement.setString(1, farm.getFarmType().name());
|
||||
statement.setInt(2, farm.getLevel().getLevel());
|
||||
statement.setString(3, farm.getPlacedBy().toString());
|
||||
statement.setString(4, farm.getLocation().getWorld().getName());
|
||||
statement.setInt(5, farm.getLocation().getBlockX());
|
||||
statement.setInt(6, farm.getLocation().getBlockY());
|
||||
statement.setInt(7, farm.getLocation().getBlockZ());
|
||||
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
int farmId = this.lastInsertedId(connection, "active_farms");
|
||||
farm.setId(farmId);
|
||||
|
||||
String createItem = "INSERT INTO " + this.getTablePrefix() + "items (farm_id, item) VALUES (?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createItem)) {
|
||||
for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) {
|
||||
statement.setInt(1, farm.getId());
|
||||
statement.setBytes(2, ItemSerializer.serializeItem(item));
|
||||
statement.addBatch();
|
||||
statement.executeUpdate();
|
||||
}
|
||||
statement.executeBatch();
|
||||
|
||||
int farmId = this.lastInsertedId(connection, "active_farms");
|
||||
farm.setId(farmId);
|
||||
|
||||
String createItem = "INSERT INTO " + this.getTablePrefix() + "items (farm_id, item) VALUES (?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createItem)) {
|
||||
for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) {
|
||||
statement.setInt(1, farm.getId());
|
||||
statement.setBytes(2, ItemSerializer.serializeItem(item));
|
||||
statement.addBatch();
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}), "create");
|
||||
});
|
||||
}
|
||||
|
||||
public void createFarms(List<Farm> farms) {
|
||||
@ -106,39 +121,46 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
|
||||
public void updateFarm(Farm farm) {
|
||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||
String updateFarm = "UPDATE " + this.getTablePrefix() + "active_farms SET level = ?, farm_type = ? WHERE id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(updateFarm)) {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String updateFarm = "UPDATE " + this.getTablePrefix() + "active_farms SET level = ?, farm_type = ? WHERE id = ?";
|
||||
PreparedStatement statement = connection.prepareStatement(updateFarm);
|
||||
statement.setInt(1, farm.getLevel().getLevel());
|
||||
statement.setString(2, farm.getFarmType().name());
|
||||
statement.setInt(3, farm.getId());
|
||||
statement.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public void deleteFarm(Farm farm) {
|
||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||
String deleteFarm = "DELETE FROM " + this.getTablePrefix() + "active_farms WHERE id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteFarm)) {
|
||||
statement.setInt(1, farm.getId());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deleteFarm = "DELETE FROM " + this.getTablePrefix() + "active_farms WHERE id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteFarm)) {
|
||||
statement.setInt(1, farm.getId());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE farm_id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
|
||||
statement.setInt(1, farm.getId());
|
||||
statement.executeUpdate();
|
||||
String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE farm_id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
|
||||
statement.setInt(1, farm.getId());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public void updateItemsAsync(Farm farm) {
|
||||
this.async(() -> updateItems(farm));
|
||||
this.runAsync(() -> updateItems(farm));
|
||||
}
|
||||
|
||||
public void updateItems(Farm farm) {
|
||||
this.databaseConnector.connect(connection -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE farm_id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
|
||||
statement.setInt(1, farm.getId());
|
||||
@ -155,62 +177,68 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
farm.setNeedsToBeSaved(false);
|
||||
}
|
||||
|
||||
public void getFarms(Consumer<Map<Integer, Farm>> callback) {
|
||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||
Map<Integer, Farm> farms = new HashMap<>();
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
String selectFarms = "SELECT * FROM " + this.getTablePrefix() + "active_farms";
|
||||
ResultSet result = statement.executeQuery(selectFarms);
|
||||
while (result.next()) {
|
||||
World world = Bukkit.getWorld(result.getString("world"));
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
Map<Integer, Farm> farms = new HashMap<>();
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
String selectFarms = "SELECT * FROM " + this.getTablePrefix() + "active_farms";
|
||||
ResultSet result = statement.executeQuery(selectFarms);
|
||||
while (result.next()) {
|
||||
World world = Bukkit.getWorld(result.getString("world"));
|
||||
|
||||
if (world == null) {
|
||||
continue;
|
||||
}
|
||||
if (world == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int id = result.getInt("id");
|
||||
int level = result.getInt("level");
|
||||
int id = result.getInt("id");
|
||||
int level = result.getInt("level");
|
||||
|
||||
String placedByStr = result.getString("placed_by");
|
||||
UUID placedBy = placedByStr == null ? null : UUID.fromString(result.getString("placed_by"));
|
||||
String placedByStr = result.getString("placed_by");
|
||||
UUID placedBy = placedByStr == null ? null : UUID.fromString(result.getString("placed_by"));
|
||||
|
||||
FarmType farmType = FarmType.valueOf(result.getString("farm_type"));
|
||||
FarmType farmType = FarmType.valueOf(result.getString("farm_type"));
|
||||
|
||||
int x = result.getInt("x");
|
||||
int y = result.getInt("y");
|
||||
int z = result.getInt("z");
|
||||
Location location = new Location(world, x, y, z);
|
||||
int x = result.getInt("x");
|
||||
int y = result.getInt("y");
|
||||
int z = result.getInt("z");
|
||||
Location location = new Location(world, x, y, z);
|
||||
|
||||
Farm farm = new Farm(location, EpicFarming.getInstance().getLevelManager().getLevel(level), placedBy);
|
||||
farm.setId(id);
|
||||
farm.setFarmType(farmType);
|
||||
Farm farm = new Farm(location, EpicFarming.getInstance().getLevelManager().getLevel(level), placedBy);
|
||||
farm.setId(id);
|
||||
farm.setFarmType(farmType);
|
||||
|
||||
farms.put(id, farm);
|
||||
}
|
||||
}
|
||||
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
String selectItems = "SELECT * FROM " + this.getTablePrefix() + "items";
|
||||
ResultSet result = statement.executeQuery(selectItems);
|
||||
while (result.next()) {
|
||||
int id = result.getInt("farm_id");
|
||||
ItemStack item = ItemSerializer.deserializeItem(result.getBytes("item"));
|
||||
|
||||
Farm farm = farms.get(id);
|
||||
if (farm == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (item != null) {
|
||||
farm.addItem(item);
|
||||
farms.put(id, farm);
|
||||
}
|
||||
}
|
||||
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
String selectItems = "SELECT * FROM " + this.getTablePrefix() + "items";
|
||||
ResultSet result = statement.executeQuery(selectItems);
|
||||
while (result.next()) {
|
||||
int id = result.getInt("farm_id");
|
||||
ItemStack item = ItemSerializer.deserializeItem(result.getBytes("item"));
|
||||
|
||||
Farm farm = farms.get(id);
|
||||
if (farm == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (item != null) {
|
||||
farm.addItem(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.sync(() -> callback.accept(farms));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
this.sync(() -> callback.accept(farms));
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +68,15 @@ public class Settings {
|
||||
"The enabled language file.",
|
||||
"More language files (if available) can be found in the plugins data folder.");
|
||||
|
||||
public static final ConfigSetting MYSQL_ENABLED = new ConfigSetting(config, "MySQL.Enabled", false, "Set to 'true' to use MySQL instead of SQLite for data storage.");
|
||||
public static final ConfigSetting MYSQL_HOSTNAME = new ConfigSetting(config, "MySQL.Hostname", "localhost");
|
||||
public static final ConfigSetting MYSQL_PORT = new ConfigSetting(config, "MySQL.Port", 3306);
|
||||
public static final ConfigSetting MYSQL_DATABASE = new ConfigSetting(config, "MySQL.Database", "your-database");
|
||||
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_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.");
|
||||
|
||||
/**
|
||||
* In order to set dynamic economy comment correctly, this needs to be
|
||||
* called after EconomyManager load
|
||||
|
Loading…
Reference in New Issue
Block a user