Merge branch 'development'

This commit is contained in:
Christian Koop 2022-09-05 22:43:57 +02:00
commit 22c5a44134
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
4 changed files with 164 additions and 107 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.songoda</groupId>
<artifactId>EpicFarming</artifactId>
<version>3.2.2</version>
<version>3.3.0</version>
<build>
<defaultGoal>clean install</defaultGoal>
@ -124,7 +124,7 @@
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>2.6.13</version>
<version>2.6.16</version>
<scope>compile</scope>
</dependency>

View File

@ -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)
);
// 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");
});
});
}

View File

@ -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,7 +31,8 @@ public class DataManager extends DataManagerAbstract {
}
public void createBoost(BoostData boostData) {
this.async(() -> this.databaseConnector.connect(connection -> {
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());
@ -38,13 +40,17 @@ public class DataManager extends DataManagerAbstract {
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,24 +59,30 @@ public class DataManager extends DataManagerAbstract {
long endTime = result.getLong("end_time");
boosts.add(new BoostData(multiplier, endTime, player));
}
}
this.sync(() -> callback.accept(boosts));
}));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void deleteBoost(BoostData boostData) {
this.async(() -> this.databaseConnector.connect(connection -> {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteBoost)) {
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 -> {
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());
@ -96,7 +108,10 @@ public class DataManager extends DataManagerAbstract {
}
statement.executeBatch();
}
}), "create");
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void createFarms(List<Farm> farms) {
@ -106,19 +121,23 @@ public class DataManager extends DataManagerAbstract {
}
public void updateFarm(Farm farm) {
this.async(() -> this.databaseConnector.connect(connection -> {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateFarm = "UPDATE " + this.getTablePrefix() + "active_farms SET level = ?, farm_type = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateFarm)) {
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 -> {
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());
@ -130,15 +149,18 @@ public class DataManager extends DataManagerAbstract {
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,12 +177,15 @@ 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 -> {
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";
@ -211,6 +236,9 @@ public class DataManager extends DataManagerAbstract {
}
}
this.sync(() -> callback.accept(farms));
}));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
}

View File

@ -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