mirror of
https://github.com/songoda/EpicFurnaces.git
synced 2025-02-20 05:21:22 +01:00
Remove deprecated methods, add MySQL support
This commit is contained in:
parent
c93cdb5a33
commit
9b4333a643
2
pom.xml
2
pom.xml
@ -125,7 +125,7 @@
|
||||
<dependency>
|
||||
<groupId>com.songoda</groupId>
|
||||
<artifactId>SongodaCore</artifactId>
|
||||
<version>2.6.13</version>
|
||||
<version>2.6.14-DEV</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -7,6 +7,7 @@ import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
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;
|
||||
@ -141,8 +142,26 @@ public class EpicFurnaces extends SongodaPlugin {
|
||||
this.blacklistHandler = new BlacklistHandler();
|
||||
|
||||
// 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,
|
||||
@ -218,7 +237,7 @@ public class EpicFurnaces extends SongodaPlugin {
|
||||
}
|
||||
|
||||
final boolean finalConverted = converted;
|
||||
dataManager.queueAsync(() -> {
|
||||
dataManager.runAsync(() -> {
|
||||
if (finalConverted) {
|
||||
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)");
|
||||
}
|
||||
@ -227,7 +246,7 @@ public class EpicFurnaces extends SongodaPlugin {
|
||||
this.furnaceManager.addFurnaces(furnaces.values());
|
||||
this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts));
|
||||
});
|
||||
}, "create");
|
||||
});
|
||||
});
|
||||
|
||||
setupRecipies();
|
||||
|
@ -12,6 +12,7 @@ import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
@ -63,22 +64,26 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
|
||||
public void createBoost(BoostData boostData) {
|
||||
this.runAsync(() -> 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)) {
|
||||
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) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
List<BoostData> boosts = new ArrayList<>();
|
||||
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
Statement statement = connection.createStatement();
|
||||
String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players";
|
||||
ResultSet result = statement.executeQuery(selectBoostedPlayers);
|
||||
while (result.next()) {
|
||||
@ -87,21 +92,26 @@ 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.runAsync(() -> 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 createFurnaces(List<Furnace> furnaces) {
|
||||
@ -111,7 +121,8 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
|
||||
public void createFurnace(Furnace furnace) {
|
||||
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String createFurnace = "INSERT INTO " + this.getTablePrefix() + "active_furnaces (level, uses, nickname, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createFurnace)) {
|
||||
statement.setInt(1, furnace.getLevel().getLevel());
|
||||
@ -151,13 +162,17 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
}), "create");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void updateFurnaces(Collection<Furnace> furnaces) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String updateFurnace = "UPDATE " + this.getTablePrefix() + "active_furnaces SET level =?, nickname =?, uses =? WHERE id =?;";
|
||||
try (PreparedStatement statement = connection.prepareStatement(updateFurnace)) {
|
||||
PreparedStatement statement = connection.prepareStatement(updateFurnace);
|
||||
for (Furnace furnace : furnaces) {
|
||||
statement.setInt(1, furnace.getLevel().getLevel());
|
||||
statement.setString(2, furnace.getNickname());
|
||||
@ -168,14 +183,17 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
|
||||
statement.executeBatch();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public void deleteFurnace(Furnace furnace) {
|
||||
dequeueFurnaceForUpdate(furnace);
|
||||
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deleteFurnace = "DELETE FROM " + this.getTablePrefix() + "active_furnaces WHERE id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteFurnace)) {
|
||||
statement.setInt(1, furnace.getId());
|
||||
@ -193,35 +211,45 @@ public class DataManager extends DataManagerAbstract {
|
||||
statement.setInt(1, furnace.getId());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void createAccessPlayer(Furnace furnace, UUID uuid) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
|
||||
try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) {
|
||||
PreparedStatement statement = connection.prepareStatement(createAccessPlayer);
|
||||
statement.setInt(1, furnace.getId());
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
// These will be used in the future when the access list gets revamped.
|
||||
// Probably by me since I already have a custom version in my server.
|
||||
public void deleteAccessPlayer(Furnace furnace, UUID uuid) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deleteAccessPlayer = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ? AND uuid = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteAccessPlayer)) {
|
||||
PreparedStatement statement = connection.prepareStatement(deleteAccessPlayer);
|
||||
statement.setInt(1, furnace.getId());
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public void updateAccessPlayers(Furnace furnace) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deletePlayers = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deletePlayers)) {
|
||||
statement.setInt(1, furnace.getId());
|
||||
@ -237,11 +265,15 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
}));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void updateLevelupItems(Furnace furnace, CompatibleMaterial material, int amount) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
String deleteLevelupItem = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ? AND item = ?";
|
||||
try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItem)) {
|
||||
statement.setInt(1, furnace.getId());
|
||||
@ -256,11 +288,15 @@ public class DataManager extends DataManagerAbstract {
|
||||
statement.setInt(3, amount);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void getFurnaces(Consumer<Map<Integer, Furnace>> callback) {
|
||||
this.runAsync(() -> this.databaseConnector.connect(connection -> {
|
||||
this.runAsync(() -> {
|
||||
try (Connection connection = this.databaseConnector.getConnection()) {
|
||||
Map<Integer, Furnace> furnaces = new HashMap<>();
|
||||
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
@ -332,6 +368,9 @@ public class DataManager extends DataManagerAbstract {
|
||||
}
|
||||
}
|
||||
this.sync(() -> callback.accept(furnaces));
|
||||
}));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -88,6 +88,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