refactor: use try-with-resources for cash id db operations
This commit is contained in:
@@ -19,65 +19,50 @@
|
||||
*/
|
||||
package client.inventory.manipulator;
|
||||
|
||||
import tools.DatabaseConnection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import tools.DatabaseConnection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author RonanLana
|
||||
*/
|
||||
public class MapleCashidGenerator {
|
||||
|
||||
|
||||
private final static Set<Integer> existentCashids = new HashSet<>(10000);
|
||||
private static Integer runningCashid = 0;
|
||||
|
||||
|
||||
private static void loadExistentCashIdsFromQuery(Connection con, String query) throws SQLException {
|
||||
PreparedStatement ps = con.prepareStatement(query);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt(1);
|
||||
if (!rs.wasNull()) {
|
||||
existentCashids.add(id);
|
||||
try (PreparedStatement ps = con.prepareStatement(query);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt(1);
|
||||
if (!rs.wasNull()) {
|
||||
existentCashids.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
}
|
||||
|
||||
|
||||
public static synchronized void loadExistentCashIdsFromDb() {
|
||||
Connection con = null;
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
loadExistentCashIdsFromQuery(con, "SELECT id FROM rings");
|
||||
loadExistentCashIdsFromQuery(con, "SELECT petid FROM pets");
|
||||
|
||||
con.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (con != null && !con.isClosed()) {
|
||||
con.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
runningCashid = 0;
|
||||
do {
|
||||
runningCashid++; // hopefully the id will never surpass the allotted amount for pets/rings?
|
||||
} while (existentCashids.contains(runningCashid));
|
||||
}
|
||||
|
||||
|
||||
private static void getNextAvailableCashId() {
|
||||
runningCashid++;
|
||||
if (runningCashid >= 777000000) {
|
||||
@@ -85,23 +70,23 @@ public class MapleCashidGenerator {
|
||||
loadExistentCashIdsFromDb();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static synchronized int generateCashId() {
|
||||
while (true) {
|
||||
if (!existentCashids.contains(runningCashid)) {
|
||||
int ret = runningCashid;
|
||||
getNextAvailableCashId();
|
||||
|
||||
|
||||
// existentCashids.add(ret)... no need to do this since the wrap over already refetches already used cashids from the DB
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
getNextAvailableCashId();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static synchronized void freeCashId(int cashId) {
|
||||
existentCashids.remove(cashId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user