refactor: use try-with-resources for db operations in various handlers
This commit is contained in:
@@ -21,11 +21,6 @@
|
||||
*/
|
||||
package net.server.handlers.login;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import client.MapleClient;
|
||||
import client.MapleFamily;
|
||||
import net.AbstractMaplePacketHandler;
|
||||
@@ -35,55 +30,65 @@ import tools.FilePrinter;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.data.input.SeekableLittleEndianAccessor;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public final class DeleteCharHandler extends AbstractMaplePacketHandler {
|
||||
|
||||
@Override
|
||||
public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
|
||||
String pic = slea.readMapleAsciiString();
|
||||
int cid = slea.readInt();
|
||||
if (c.checkPic(pic)) {
|
||||
//check for family, guild leader, pending marriage, world transfer
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT `world`, `guildid`, `guildrank`, `familyId` FROM characters WHERE id = ?");
|
||||
PreparedStatement ps2 = con.prepareStatement("SELECT COUNT(*) as rowcount FROM worldtransfers WHERE `characterid` = ? AND completionTime IS NULL")) {
|
||||
ps.setInt(1, cid);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if(!rs.next()) throw new SQLException("Character record does not exist.");
|
||||
int world = rs.getInt("world");
|
||||
int guildId = rs.getInt("guildid");
|
||||
int guildRank = rs.getInt("guildrank");
|
||||
int familyId = rs.getInt("familyId");
|
||||
if(guildId != 0 && guildRank <= 1) {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x16));
|
||||
return;
|
||||
} else if(familyId != -1) {
|
||||
MapleFamily family = Server.getInstance().getWorld(world).getFamily(familyId);
|
||||
if(family != null && family.getTotalMembers() > 1) {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1D));
|
||||
return;
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
ps2.setInt(1, cid);
|
||||
rs = ps2.executeQuery();
|
||||
rs.next();
|
||||
if(rs.getInt("rowcount") > 0) {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1A));
|
||||
return;
|
||||
}
|
||||
} catch(SQLException e) {
|
||||
e.printStackTrace();
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09));
|
||||
return;
|
||||
}
|
||||
if(c.deleteCharacter(cid, c.getAccID())) {
|
||||
FilePrinter.print(FilePrinter.DELETED_CHAR + c.getAccountName() + ".txt", c.getAccountName() + " deleted CID: " + cid);
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0));
|
||||
} else {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09));
|
||||
}
|
||||
} else {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x14));
|
||||
}
|
||||
}
|
||||
String pic = slea.readMapleAsciiString();
|
||||
int cid = slea.readInt();
|
||||
if (c.checkPic(pic)) {
|
||||
//check for family, guild leader, pending marriage, world transfer
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT `world`, `guildid`, `guildrank`, `familyId` FROM characters WHERE id = ?");
|
||||
PreparedStatement ps2 = con.prepareStatement("SELECT COUNT(*) as rowcount FROM worldtransfers WHERE `characterid` = ? AND completionTime IS NULL")) {
|
||||
ps.setInt(1, cid);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) {
|
||||
throw new SQLException("Character record does not exist.");
|
||||
}
|
||||
int world = rs.getInt("world");
|
||||
int guildId = rs.getInt("guildid");
|
||||
int guildRank = rs.getInt("guildrank");
|
||||
int familyId = rs.getInt("familyId");
|
||||
if (guildId != 0 && guildRank <= 1) {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x16));
|
||||
return;
|
||||
} else if (familyId != -1) {
|
||||
MapleFamily family = Server.getInstance().getWorld(world).getFamily(familyId);
|
||||
if (family != null && family.getTotalMembers() > 1) {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1D));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ps2.setInt(1, cid);
|
||||
try (ResultSet rs = ps2.executeQuery()) {
|
||||
rs.next();
|
||||
if (rs.getInt("rowcount") > 0) {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1A));
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09));
|
||||
return;
|
||||
}
|
||||
if (c.deleteCharacter(cid, c.getAccID())) {
|
||||
FilePrinter.print(FilePrinter.DELETED_CHAR + c.getAccountName() + ".txt", c.getAccountName() + " deleted CID: " + cid);
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0));
|
||||
} else {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09));
|
||||
}
|
||||
} else {
|
||||
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x14));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user