refactor: use try-with-resources for buddy list db operations
This commit is contained in:
@@ -61,18 +61,19 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
||||
}
|
||||
|
||||
private CharacterIdNameBuddyCapacity getCharacterIdAndNameFromDatabase(String name) throws SQLException {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
CharacterIdNameBuddyCapacity ret;
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT id, name, buddyCapacity FROM characters WHERE name LIKE ?")) {
|
||||
CharacterIdNameBuddyCapacity ret = null;
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT id, name, buddyCapacity FROM characters WHERE name LIKE ?")) {
|
||||
ps.setString(1, name);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
ret = null;
|
||||
if (rs.next()) {
|
||||
ret = new CharacterIdNameBuddyCapacity(rs.getInt("id"), rs.getString("name"), rs.getInt("buddyCapacity"));
|
||||
}
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -110,27 +111,30 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
||||
if (channel != -1) {
|
||||
buddyAddResult = world.requestBuddyAdd(addName, c.getChannel(), player.getId(), player.getName());
|
||||
} else {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as buddyCount FROM buddies WHERE characterid = ? AND pending = 0");
|
||||
ps.setInt(1, charWithId.getId());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (!rs.next()) {
|
||||
throw new RuntimeException("Result set expected");
|
||||
} else if (rs.getInt("buddyCount") >= charWithId.getBuddyCapacity()) {
|
||||
buddyAddResult = BuddyAddResult.BUDDYLIST_FULL;
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as buddyCount FROM buddies WHERE characterid = ? AND pending = 0")) {
|
||||
ps.setInt(1, charWithId.getId());
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) {
|
||||
throw new RuntimeException("Result set expected");
|
||||
} else if (rs.getInt("buddyCount") >= charWithId.getBuddyCapacity()) {
|
||||
buddyAddResult = BuddyAddResult.BUDDYLIST_FULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT pending FROM buddies WHERE characterid = ? AND buddyid = ?")) {
|
||||
ps.setInt(1, charWithId.getId());
|
||||
ps.setInt(2, player.getId());
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
buddyAddResult = BuddyAddResult.ALREADY_ON_LIST;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
ps = con.prepareStatement("SELECT pending FROM buddies WHERE characterid = ? AND buddyid = ?");
|
||||
ps.setInt(1, charWithId.getId());
|
||||
ps.setInt(2, player.getId());
|
||||
rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
buddyAddResult = BuddyAddResult.ALREADY_ON_LIST;
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
con.close();
|
||||
}
|
||||
if (buddyAddResult == BuddyAddResult.BUDDYLIST_FULL) {
|
||||
c.announce(MaplePacketCreator.serverNotice(1, "\"" + addName + "\"'s Buddylist is full"));
|
||||
@@ -142,13 +146,12 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
||||
displayChannel = channel;
|
||||
notifyRemoteChannel(c, channel, otherCid, ADDED);
|
||||
} else if (buddyAddResult != BuddyAddResult.ALREADY_ON_LIST && channel == -1) {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`) VALUES (?, ?, 1)")) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`) VALUES (?, ?, 1)")) {
|
||||
ps.setInt(1, charWithId.getId());
|
||||
ps.setInt(2, player.getId());
|
||||
ps.executeUpdate();
|
||||
}
|
||||
con.close();
|
||||
}
|
||||
buddylist.put(new BuddylistEntry(charWithId.getName(), group, otherCid, displayChannel, true));
|
||||
c.announce(MaplePacketCreator.updateBuddylist(buddylist.getBuddies()));
|
||||
@@ -171,16 +174,16 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
||||
String otherName = null;
|
||||
MapleCharacter otherChar = c.getChannelServer().getPlayerStorage().getCharacterById(otherCid);
|
||||
if (otherChar == null) {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT name FROM characters WHERE id = ?")) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT name FROM characters WHERE id = ?")) {
|
||||
ps.setInt(1, otherCid);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
otherName = rs.getString("name");
|
||||
}
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
} else {
|
||||
otherName = otherChar.getName();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user