Remove deprecated methods, fix MySQL

This commit is contained in:
ceze88 2022-08-14 11:53:38 +02:00
parent 4ed9b5e1c5
commit 2b0f1b4b23
4 changed files with 174 additions and 123 deletions

View File

@ -119,7 +119,7 @@
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>2.6.13</version>
<version>2.6.14-DEV</version>
<scope>compile</scope>
</dependency>

View File

@ -106,8 +106,9 @@ public class UltimateModeration extends SongodaPlugin {
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);
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);
@ -150,7 +151,7 @@ public class UltimateModeration extends SongodaPlugin {
@Override
public void onDataLoad() {
getDataManager().queueAsync(() -> {
getDataManager().runAsync(() -> {
// Load data from DB
this.dataManager.getTemplates((templates) -> {
for (Template template : templates) {
@ -169,7 +170,7 @@ public class UltimateModeration extends SongodaPlugin {
for (Ticket ticket : tickets.values())
this.ticketManager.addTicket(ticket);
});
}, "create");
});
}
@Override

View File

@ -13,6 +13,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
@ -30,36 +31,43 @@ public class DataManager extends DataManagerAbstract {
}
public void createTemplate(Template template) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
String createTemplate = "INSERT INTO " + this.getTablePrefix() + "templates (punishment_type, duration, reason, name, creator) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createTemplate)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createTemplate = "INSERT INTO " + this.getTablePrefix() + "templates (punishment_type, duration, reason, name, creator) VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createTemplate);
statement.setString(1, template.getPunishmentType().name());
statement.setLong(2, template.getDuration());
statement.setString(3, template.getReason());
statement.setString(4, template.getName());
statement.setString(5, template.getCreator().toString());
statement.executeUpdate();
}
int templateId = this.lastInsertedId(connection, "templates");
template.setId(templateId);
}), "create");
int templateId = this.lastInsertedId(connection, "templates");
template.setId(templateId);
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void deleteTemplate(Template template) {
this.async(() -> this.databaseConnector.connect(connection -> {
String deleteTemplate = "DELETE FROM " + this.getTablePrefix() + "templates WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteTemplate)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deleteTemplate = "DELETE FROM " + this.getTablePrefix() + "templates WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deleteTemplate);
statement.setLong(1, template.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}));
});
}
public void getTemplates(Consumer<List<Template>> callback) {
List<Template> templates = 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 selectTemplates = "SELECT * FROM " + this.getTablePrefix() + "templates";
ResultSet result = statement.executeQuery(selectTemplates);
while (result.next()) {
@ -73,16 +81,19 @@ public class DataManager extends DataManagerAbstract {
template.setId(id);
templates.add(template);
}
}
this.sync(() -> callback.accept(templates));
}));
this.sync(() -> callback.accept(templates));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void createAppliedPunishment(AppliedPunishment punishment) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
String createPunishment = "INSERT INTO " + this.getTablePrefix() + "punishments (type, duration, reason, victim, punisher, expiration) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createPunishment)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createPunishment = "INSERT INTO " + this.getTablePrefix() + "punishments (type, duration, reason, victim, punisher, expiration) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createPunishment);
statement.setString(1, punishment.getPunishmentType().name());
statement.setLong(2, punishment.getDuration());
statement.setString(3, punishment.getReason());
@ -90,27 +101,33 @@ public class DataManager extends DataManagerAbstract {
statement.setString(5, punishment.getPunisher().toString());
statement.setLong(6, punishment.getExpiration());
statement.executeUpdate();
}
int punishmentId = this.lastInsertedId(connection, "punishments");
punishment.setId(punishmentId);
}), "create");
int punishmentId = this.lastInsertedId(connection, "punishments");
punishment.setId(punishmentId);
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void deleteAppliedPunishment(AppliedPunishment punishment) {
this.async(() -> this.databaseConnector.connect(connection -> {
String deletePunishment = "DELETE FROM " + this.getTablePrefix() + "punishments WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deletePunishment)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deletePunishment = "DELETE FROM " + this.getTablePrefix() + "punishments WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deletePunishment);
statement.setLong(1, punishment.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}));
});
}
public void updateAppliedPunishment(AppliedPunishment punishment) {
this.async(() -> this.databaseConnector.connect(connection -> {
String updatePunishment = "UPDATE " + this.getTablePrefix() + "punishments set type = ?, duration = ?, reason = ?, victim = ?, punisher = ?, expiration = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updatePunishment)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updatePunishment = "UPDATE " + this.getTablePrefix() + "punishments set type = ?, duration = ?, reason = ?, victim = ?, punisher = ?, expiration = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updatePunishment);
statement.setString(1, punishment.getPunishmentType().name());
statement.setLong(2, punishment.getDuration());
statement.setString(3, punishment.getReason());
@ -119,14 +136,17 @@ public class DataManager extends DataManagerAbstract {
statement.setLong(6, punishment.getExpiration());
statement.setLong(7, punishment.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}));
});
}
public void getAppliedPunishments(Consumer<List<AppliedPunishment>> callback) {
List<AppliedPunishment> appliedPunishments = 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 selectPunishments = "SELECT * FROM " + this.getTablePrefix() + "punishments";
ResultSet result = statement.executeQuery(selectPunishments);
while (result.next()) {
@ -139,42 +159,51 @@ public class DataManager extends DataManagerAbstract {
long expiration = result.getLong("expiration");
appliedPunishments.add(new AppliedPunishment(punishmentType, duration, reason, victim, punisher, expiration, id));
}
}
this.sync(() -> callback.accept(appliedPunishments));
}));
this.sync(() -> callback.accept(appliedPunishments));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void createNote(PunishmentNote note) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
String createNote = "INSERT INTO " + this.getTablePrefix() + "notes (note, author, subject, creation) VALUES (?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createNote)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createNote = "INSERT INTO " + this.getTablePrefix() + "notes (note, author, subject, creation) VALUES (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createNote);
statement.setString(1, note.getNote());
statement.setString(2, note.getAuthor().toString());
statement.setString(3, note.getSubject().toString());
statement.setLong(4, note.getCreationDate());
statement.executeUpdate();
}
int noteId = this.lastInsertedId(connection, "notes");
note.setId(noteId);
}), "create");
int noteId = this.lastInsertedId(connection, "notes");
note.setId(noteId);
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void deleteNote(PunishmentNote note) {
this.async(() -> this.databaseConnector.connect(connection -> {
String deleteNote = "DELETE FROM " + this.getTablePrefix() + "notes WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteNote)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deleteNote = "DELETE FROM " + this.getTablePrefix() + "notes WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(deleteNote);
statement.setLong(1, note.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}));
});
}
public void getNotes(Consumer<List<PunishmentNote>> callback) {
List<PunishmentNote> notes = 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 getNotes = "SELECT * FROM " + this.getTablePrefix() + "notes";
ResultSet result = statement.executeQuery(getNotes);
while (result.next()) {
@ -185,16 +214,19 @@ public class DataManager extends DataManagerAbstract {
long creation = result.getLong("creation");
notes.add(new PunishmentNote(id, note, author, subject, creation));
}
}
this.sync(() -> callback.accept(notes));
}));
this.sync(() -> callback.accept(notes));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void createTicket(Ticket ticket) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
String createTicket = "INSERT INTO " + this.getTablePrefix() + "tickets (victim, subject, type, status, world, x, y, z, pitch, yaw) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createTicket)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createTicket = "INSERT INTO " + this.getTablePrefix() + "tickets (victim, subject, type, status, world, x, y, z, pitch, yaw) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createTicket);
statement.setString(1, ticket.getVictim().toString());
statement.setString(2, ticket.getSubject());
statement.setString(3, ticket.getType());
@ -209,36 +241,44 @@ public class DataManager extends DataManagerAbstract {
statement.setFloat(9, location.getPitch());
statement.setFloat(10, location.getYaw());
statement.executeUpdate();
for (TicketResponse response : ticket.getResponses()) {
createTicketResponse(response);
}
int ticketId = this.lastInsertedId(connection, "tickets");
ticket.setId(ticketId);
} catch (Exception ex) {
ex.printStackTrace();
}
for (TicketResponse response : ticket.getResponses())
createTicketResponse(response);
int ticketId = this.lastInsertedId(connection, "tickets");
ticket.setId(ticketId);
}), "create");
});
}
public void deleteTicket(Ticket ticket) {
this.async(() -> this.databaseConnector.connect(connection -> {
String deleteTicket = "DELETE FROM " + this.getTablePrefix() + "tickets WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteTicket)) {
statement.setLong(1, ticket.getId());
statement.executeUpdate();
}
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String deleteTicket = "DELETE FROM " + this.getTablePrefix() + "tickets WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteTicket)) {
statement.setLong(1, ticket.getId());
statement.executeUpdate();
}
String deleteTicketResponses = "DELETE FROM " + this.getTablePrefix() + "ticket_responses WHERE ticket_id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteTicketResponses)) {
statement.setLong(1, ticket.getId());
statement.executeUpdate();
String deleteTicketResponses = "DELETE FROM " + this.getTablePrefix() + "ticket_responses WHERE ticket_id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteTicketResponses)) {
statement.setLong(1, ticket.getId());
statement.executeUpdate();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}));
});
}
public void updateTicket(Ticket ticket) {
this.async(() -> this.databaseConnector.connect(connection -> {
String updateTicket = "UPDATE " + this.getTablePrefix() + "tickets SET victim = ?, subject = ?, type = ?, status = ?, world = ?, x = ?, y = ?, z = ?, pitch = ?, yaw = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateTicket)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateTicket = "UPDATE " + this.getTablePrefix() + "tickets SET victim = ?, subject = ?, type = ?, status = ?, world = ?, x = ?, y = ?, z = ?, pitch = ?, yaw = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(updateTicket);
statement.setString(1, ticket.getVictim().toString());
statement.setString(2, ticket.getSubject());
statement.setString(3, ticket.getType());
@ -254,73 +294,82 @@ public class DataManager extends DataManagerAbstract {
statement.setFloat(10, location.getYaw());
statement.setInt(11, ticket.getId());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}));
});
}
public void getTickets(Consumer<Map<Integer, Ticket>> callback) {
Map<Integer, Ticket> tickets = new TreeMap<>();
this.async(() -> this.databaseConnector.connect(connection -> {
try (Statement statement = connection.createStatement()) {
String selectTickets = "SELECT * FROM " + this.getTablePrefix() + "tickets";
ResultSet result = statement.executeQuery(selectTickets);
while (result.next()) {
int id = result.getInt("id");
UUID victim = UUID.fromString(result.getString("victim"));
String subject = result.getString("subject");
String type = result.getString("type");
TicketStatus status = TicketStatus.valueOf(result.getString("status"));
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
try (Statement statement = connection.createStatement()) {
String selectTickets = "SELECT * FROM " + this.getTablePrefix() + "tickets";
ResultSet result = statement.executeQuery(selectTickets);
while (result.next()) {
int id = result.getInt("id");
UUID victim = UUID.fromString(result.getString("victim"));
String subject = result.getString("subject");
String type = result.getString("type");
TicketStatus status = TicketStatus.valueOf(result.getString("status"));
String world = result.getString("world");
double x = result.getDouble("x");
double y = result.getDouble("y");
double z = result.getDouble("z");
float pitch = result.getFloat("pitch");
float yaw = result.getFloat("yaw");
String world = result.getString("world");
double x = result.getDouble("x");
double y = result.getDouble("y");
double z = result.getDouble("z");
float pitch = result.getFloat("pitch");
float yaw = result.getFloat("yaw");
Location location = Bukkit.getWorld(world) == null ? null : new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
Location location = Bukkit.getWorld(world) == null ? null : new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
Ticket ticket = new Ticket(id, victim, subject, type, status, location);
ticket.setId(id)
;
tickets.put(id, ticket);
Ticket ticket = new Ticket(id, victim, subject, type, status, location);
ticket.setId(id)
;
tickets.put(id, ticket);
}
}
}
try (Statement statement = connection.createStatement()) {
String selectTickets = "SELECT * FROM " + this.getTablePrefix() + "ticket_responses";
ResultSet result = statement.executeQuery(selectTickets);
while (result.next()) {
int id = result.getInt("ticket_id");
try (Statement statement = connection.createStatement()) {
String selectTickets = "SELECT * FROM " + this.getTablePrefix() + "ticket_responses";
ResultSet result = statement.executeQuery(selectTickets);
while (result.next()) {
int id = result.getInt("ticket_id");
Ticket ticket = tickets.get(id);
if (ticket == null) continue;
Ticket ticket = tickets.get(id);
if (ticket == null) continue;
UUID author = UUID.fromString(result.getString("author"));
String message = result.getString("message");
long postedDate = result.getLong("posted_date");
UUID author = UUID.fromString(result.getString("author"));
String message = result.getString("message");
long postedDate = result.getLong("posted_date");
TicketResponse ticketResponse = new TicketResponse(author, message, postedDate);
ticketResponse.setTicketId(id);
TicketResponse ticketResponse = new TicketResponse(author, message, postedDate);
ticketResponse.setTicketId(id);
ticket.addResponse(ticketResponse);
ticket.addResponse(ticketResponse);
}
}
}
this.sync(() -> callback.accept(tickets));
}));
this.sync(() -> callback.accept(tickets));
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
public void createTicketResponse(TicketResponse ticketResponse) {
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
String createTicketResponse = "INSERT INTO " + this.getTablePrefix() + "ticket_responses (ticket_id, author, message, posted_date) VALUES (?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createTicketResponse)) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createTicketResponse = "INSERT INTO " + this.getTablePrefix() + "ticket_responses (ticket_id, author, message, posted_date) VALUES (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(createTicketResponse);
statement.setInt(1, ticketResponse.getTicketId());
statement.setString(2, ticketResponse.getAuthor().toString());
statement.setString(3, ticketResponse.getMessage());
statement.setLong(4, ticketResponse.getPostedDate());
statement.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}), "create");
});
}
}

View File

@ -64,6 +64,7 @@ public class Settings {
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.");
public static void setupConfig() {
config.load();