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