refactor: use try-with-resources for client db operations

This commit is contained in:
P0nk
2021-04-04 16:49:01 +02:00
parent 0c267f8eee
commit 7e206d4589

View File

@@ -21,47 +21,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package client; package client;
import java.io.*; import client.inventory.MapleInventoryType;
import java.net.InetAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import config.YamlConfig; import config.YamlConfig;
import constants.game.GameConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine; import jdk.nashorn.api.scripting.NashornScriptEngine;
import tools.*;
import net.server.Server; import net.server.Server;
import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.channel.Channel;
import net.server.coordinator.login.MapleLoginBypassCoordinator;
import net.server.coordinator.session.MapleSessionCoordinator; import net.server.coordinator.session.MapleSessionCoordinator;
import net.server.coordinator.session.MapleSessionCoordinator.AntiMulticlientResult; import net.server.coordinator.session.MapleSessionCoordinator.AntiMulticlientResult;
import net.server.channel.Channel;
import net.server.guild.MapleGuild; import net.server.guild.MapleGuild;
import net.server.guild.MapleGuildCharacter; import net.server.guild.MapleGuildCharacter;
import net.server.world.MapleMessengerCharacter; import net.server.world.*;
import net.server.world.MapleParty;
import net.server.world.MaplePartyCharacter;
import net.server.world.PartyOperation;
import net.server.world.World;
import org.apache.mina.core.session.IoSession; import org.apache.mina.core.session.IoSession;
import client.inventory.MapleInventoryType;
import constants.game.GameConstants;
import scripting.AbstractPlayerInteraction; import scripting.AbstractPlayerInteraction;
import scripting.event.EventInstanceManager; import scripting.event.EventInstanceManager;
import scripting.event.EventManager; import scripting.event.EventManager;
@@ -69,13 +43,23 @@ import scripting.npc.NPCConversationManager;
import scripting.npc.NPCScriptManager; import scripting.npc.NPCScriptManager;
import scripting.quest.QuestActionManager; import scripting.quest.QuestActionManager;
import scripting.quest.QuestScriptManager; import scripting.quest.QuestScriptManager;
import server.life.MapleMonster;
import server.ThreadManager; import server.ThreadManager;
import server.maps.*; import server.life.MapleMonster;
import server.maps.FieldLimit;
import server.maps.MapleMap;
import server.maps.MapleMiniDungeonInfo;
import tools.*;
import net.server.audit.locks.MonitoredLockType; import java.io.IOException;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory; import java.io.UnsupportedEncodingException;
import net.server.coordinator.login.MapleLoginBypassCoordinator; import java.net.InetAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.Date;
import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
public class MapleClient { public class MapleClient {
@@ -193,20 +177,17 @@ public class MapleClient {
} }
private List<CharNameAndId> loadCharactersInternal(int worldId) { private List<CharNameAndId> loadCharactersInternal(int worldId) {
PreparedStatement ps;
List<CharNameAndId> chars = new ArrayList<>(15); List<CharNameAndId> chars = new ArrayList<>(15);
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT id, name FROM characters WHERE accountid = ? AND world = ?")) {
ps = con.prepareStatement("SELECT id, name FROM characters WHERE accountid = ? AND world = ?");
ps.setInt(1, this.getAccID()); ps.setInt(1, this.getAccID());
ps.setInt(2, worldId); ps.setInt(2, worldId);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
chars.add(new CharNameAndId(rs.getString("name"), rs.getInt("id"))); chars.add(new CharNameAndId(rs.getString("name"), rs.getInt("id")));
} }
} }
ps.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -219,9 +200,8 @@ public class MapleClient {
public boolean hasBannedIP() { public boolean hasBannedIP() {
boolean ret = false; boolean ret = false;
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM ipbans WHERE ? LIKE CONCAT(ip, '%')")) {
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM ipbans WHERE ? LIKE CONCAT(ip, '%')")) {
ps.setString(1, session.getRemoteAddress().toString()); ps.setString(1, session.getRemoteAddress().toString());
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
rs.next(); rs.next();
@@ -229,8 +209,6 @@ public class MapleClient {
ret = true; ret = true;
} }
} }
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -241,9 +219,9 @@ public class MapleClient {
if (voteTime != -1) { if (voteTime != -1) {
return voteTime; return voteTime;
} }
try {
Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT date FROM bit_votingrecords WHERE UPPER(account) = UPPER(?)")) { PreparedStatement ps = con.prepareStatement("SELECT date FROM bit_votingrecords WHERE UPPER(account) = UPPER(?)")) {
ps.setString(1, accountName); ps.setString(1, accountName);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) { if (!rs.next()) {
@@ -251,8 +229,6 @@ public class MapleClient {
} }
voteTime = rs.getInt("date"); voteTime = rs.getInt("date");
} }
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
FilePrinter.printError("hasVotedAlready.txt", e); FilePrinter.printError("hasVotedAlready.txt", e);
return -1; return -1;
@@ -277,32 +253,20 @@ public class MapleClient {
} }
boolean ret = false; boolean ret = false;
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection();
Connection con = null; PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM hwidbans WHERE hwid LIKE ?")) {
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT COUNT(*) FROM hwidbans WHERE hwid LIKE ?");
ps.setString(1, hwid); ps.setString(1, hwid);
ResultSet rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (rs != null && rs.next()) { if (rs != null && rs.next()) {
if(rs.getInt(1) > 0) if (rs.getInt(1) > 0) {
ret = true; ret = true;
} }
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(ps != null && !ps.isClosed()) {
ps.close();
} }
if(con != null && !con.isClosed()) {
con.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
return ret; return ret;
} }
@@ -313,7 +277,6 @@ public class MapleClient {
} }
boolean ret = false; boolean ret = false;
int i; int i;
try {
StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM macbans WHERE mac IN ("); StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM macbans WHERE mac IN (");
for (i = 0; i < macs.size(); i++) { for (i = 0; i < macs.size(); i++) {
sql.append("?"); sql.append("?");
@@ -323,12 +286,11 @@ public class MapleClient {
} }
sql.append(")"); sql.append(")");
Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement(sql.toString())) { PreparedStatement ps = con.prepareStatement(sql.toString())) {
i = 0; i = 0;
for (String mac : macs) { for (String mac : macs) {
i++; ps.setString(++i, mac);
ps.setString(i, mac);
} }
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
rs.next(); rs.next();
@@ -336,27 +298,24 @@ public class MapleClient {
ret = true; ret = true;
} }
} }
} finally {
con.close();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return ret; return ret;
} }
private void loadHWIDIfNescessary() throws SQLException { private void loadHWIDIfNescessary() throws SQLException {
if (hwid == null) { if (hwid == null) {
Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
try(PreparedStatement ps = con.prepareStatement("SELECT hwid FROM accounts WHERE id = ?")) { PreparedStatement ps = con.prepareStatement("SELECT hwid FROM accounts WHERE id = ?")) {
ps.setInt(1, accId); ps.setInt(1, accId);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
hwid = rs.getString("hwid"); hwid = rs.getString("hwid");
} }
} }
} finally {
con.close();
} }
} }
} }
@@ -364,8 +323,8 @@ public class MapleClient {
// TODO: Recode to close statements... // TODO: Recode to close statements...
private void loadMacsIfNescessary() throws SQLException { private void loadMacsIfNescessary() throws SQLException {
if (macs.isEmpty()) { if (macs.isEmpty()) {
Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT macs FROM accounts WHERE id = ?")) { PreparedStatement ps = con.prepareStatement("SELECT macs FROM accounts WHERE id = ?")) {
ps.setInt(1, accId); ps.setInt(1, accId);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
@@ -376,50 +335,37 @@ public class MapleClient {
} }
} }
} }
} finally {
con.close();
} }
} }
} }
public void banHWID() { public void banHWID() {
PreparedStatement ps = null;
Connection con = null;
try { try {
loadHWIDIfNescessary(); loadHWIDIfNescessary();
con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
ps = con.prepareStatement("INSERT INTO hwidbans (hwid) VALUES (?)"); PreparedStatement ps = con.prepareStatement("INSERT INTO hwidbans (hwid) VALUES (?)")) {
ps.setString(1, hwid); ps.setString(1, hwid);
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(ps != null && !ps.isClosed()) {
ps.close();
}
if(con != null && !con.isClosed()) {
con.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
public void banMacs() { public void banMacs() {
Connection con = null;
try { try {
loadMacsIfNescessary(); loadMacsIfNescessary();
con = DatabaseConnection.getConnection();
List<String> filtered = new LinkedList<>(); List<String> filtered = new LinkedList<>();
try (PreparedStatement ps = con.prepareStatement("SELECT filter FROM macfilters"); ResultSet rs = ps.executeQuery()) { try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT filter FROM macfilters");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
filtered.add(rs.getString("filter")); filtered.add(rs.getString("filter"));
} }
} }
try (PreparedStatement ps = con.prepareStatement("INSERT INTO macbans (mac, aid) VALUES (?, ?)")) { try (PreparedStatement ps = con.prepareStatement("INSERT INTO macbans (mac, aid) VALUES (?, ?)")) {
for (String mac : macs) { for (String mac : macs) {
boolean matched = false; boolean matched = false;
@@ -436,8 +382,7 @@ public class MapleClient {
} }
} }
} }
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -460,15 +405,11 @@ public class MapleClient {
public void setPin(String pin) { public void setPin(String pin) {
this.pin = pin; this.pin = pin;
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pin = ? WHERE id = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pin = ? WHERE id = ?")) {
ps.setString(1, pin); ps.setString(1, pin);
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
} finally {
con.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -497,15 +438,11 @@ public class MapleClient {
public void setPic(String pic) { public void setPic(String pic) {
this.pic = pic; this.pic = pic;
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pic = ? WHERE id = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pic = ? WHERE id = ?")) {
ps.setString(1, pic); ps.setString(1, pic);
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
} finally {
con.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -542,14 +479,11 @@ public class MapleClient {
return 6; // thanks Survival_Project for finding out an issue with AUTOMATIC_REGISTER here return 6; // thanks Survival_Project for finding out an issue with AUTOMATIC_REGISTER here
} }
Connection con = null; try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = null; PreparedStatement ps = con.prepareStatement("SELECT id, password, gender, banned, pin, pic, characterslots, tos, language FROM accounts WHERE name = ?")) {
ResultSet rs = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT id, password, gender, banned, pin, pic, characterslots, tos, language FROM accounts WHERE name = ?");
ps.setString(1, login); ps.setString(1, login);
rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
accId = -2; accId = -2;
if (rs.next()) { if (rs.next()) {
accId = rs.getInt("id"); accId = rs.getInt("id");
@@ -568,9 +502,6 @@ public class MapleClient {
String passhash = rs.getString("password"); String passhash = rs.getString("password");
byte tos = rs.getByte("tos"); byte tos = rs.getByte("tos");
ps.close();
rs.close();
if (banned) { if (banned) {
return 3; return 3;
} }
@@ -590,23 +521,10 @@ public class MapleClient {
} else { } else {
accId = -3; accId = -3;
} }
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (con != null && !con.isClosed()) {
con.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
if (loginok == 0 || loginok == 4) { if (loginok == 0 || loginok == 4) {
AntiMulticlientResult res = MapleSessionCoordinator.getInstance().attemptLoginSession(session, nibbleHwid, accId, loginok == 4); AntiMulticlientResult res = MapleSessionCoordinator.getInstance().attemptLoginSession(session, nibbleHwid, accId, loginok == 4);
@@ -640,44 +558,31 @@ public class MapleClient {
} }
public Calendar getTempBanCalendarFromDB() { public Calendar getTempBanCalendarFromDB() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
final Calendar lTempban = Calendar.getInstance(); final Calendar lTempban = Calendar.getInstance();
try {
con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `tempban` FROM accounts WHERE id = ?"); PreparedStatement ps = con.prepareStatement("SELECT `tempban` FROM accounts WHERE id = ?")) {
ps.setInt(1, getAccID()); ps.setInt(1, getAccID());
rs = ps.executeQuery();
final Timestamp tempban;
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) { if (!rs.next()) {
return null; return null;
} }
final Timestamp tempban = rs.getTimestamp("tempban"); tempban = rs.getTimestamp("tempban");
if (tempban.toLocalDateTime().equals(DefaultDates.getTempban())) { if (tempban.toLocalDateTime().equals(DefaultDates.getTempban())) {
return null; return null;
} }
}
lTempban.setTimeInMillis(tempban.getTime()); lTempban.setTimeInMillis(tempban.getTime());
tempBanCalendar = lTempban; tempBanCalendar = lTempban;
return lTempban; return lTempban;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} }
return null;//why oh why!?! return null;//why oh why!?!
} }
@@ -716,28 +621,13 @@ public class MapleClient {
this.hwid = hwid.toString(); this.hwid = hwid.toString();
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection();
Connection con = null; PreparedStatement ps = con.prepareStatement("UPDATE accounts SET hwid = ? WHERE id = ?")) {
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("UPDATE accounts SET hwid = ? WHERE id = ?");
ps.setString(1, this.hwid); ps.setString(1, this.hwid);
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if(ps != null && !ps.isClosed()) {
ps.close();
}
if(con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} }
} else { } else {
this.disconnect(false, false); // Invalid HWID... this.disconnect(false, false); // Invalid HWID...
@@ -748,7 +638,6 @@ public class MapleClient {
macs.addAll(Arrays.asList(macData.split(", "))); macs.addAll(Arrays.asList(macData.split(", ")));
StringBuilder newMacData = new StringBuilder(); StringBuilder newMacData = new StringBuilder();
Iterator<String> iter = macs.iterator(); Iterator<String> iter = macs.iterator();
PreparedStatement ps = null;
while (iter.hasNext()) { while (iter.hasNext()) {
String cur = iter.next(); String cur = iter.next();
newMacData.append(cur); newMacData.append(cur);
@@ -756,27 +645,14 @@ public class MapleClient {
newMacData.append(", "); newMacData.append(", ");
} }
} }
Connection con = null;
try { try (Connection con = DatabaseConnection.getConnection();
con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET macs = ? WHERE id = ?")) {
ps = con.prepareStatement("UPDATE accounts SET macs = ? WHERE id = ?");
ps.setString(1, newMacData.toString()); ps.setString(1, newMacData.toString());
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} }
} }
@@ -794,17 +670,14 @@ public class MapleClient {
MapleSessionCoordinator.getInstance().updateOnlineSession(this.getSession()); MapleSessionCoordinator.getInstance().updateOnlineSession(this.getSession());
} }
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = ?, lastlogin = ? WHERE id = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = ?, lastlogin = ? WHERE id = ?")) {
// using sql currenttime here could potentially break the login, thanks Arnah for pointing this out // using sql currenttime here could potentially break the login, thanks Arnah for pointing this out
ps.setInt(1, newstate); ps.setInt(1, newstate);
ps.setTimestamp(2, new java.sql.Timestamp(Server.getInstance().getCurrentTime())); ps.setTimestamp(2, new java.sql.Timestamp(Server.getInstance().getCurrentTime()));
ps.setInt(3, getAccID()); ps.setInt(3, getAccID());
ps.executeUpdate(); ps.executeUpdate();
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -820,23 +693,23 @@ public class MapleClient {
} }
public int getLoginState() { // 0 = LOGIN_NOTLOGGEDIN, 1= LOGIN_SERVER_TRANSITION, 2 = LOGIN_LOGGEDIN public int getLoginState() { // 0 = LOGIN_NOTLOGGEDIN, 1= LOGIN_SERVER_TRANSITION, 2 = LOGIN_LOGGEDIN
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection(); int state;
PreparedStatement ps = con.prepareStatement("SELECT loggedin, lastlogin, birthday FROM accounts WHERE id = ?"); try (PreparedStatement ps = con.prepareStatement("SELECT loggedin, lastlogin, birthday FROM accounts WHERE id = ?")) {
ps.setInt(1, getAccID()); ps.setInt(1, getAccID());
ResultSet rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) { if (!rs.next()) {
rs.close();
ps.close();
throw new RuntimeException("getLoginState - MapleClient AccID: " + getAccID()); throw new RuntimeException("getLoginState - MapleClient AccID: " + getAccID());
} }
birthday = Calendar.getInstance(); birthday = Calendar.getInstance();
try { try {
birthday.setTime(rs.getDate("birthday")); birthday.setTime(rs.getDate("birthday"));
} catch(SQLException e) {} } catch (SQLException e) {
}
int state = rs.getInt("loggedin"); state = rs.getInt("loggedin");
if (state == LOGIN_SERVER_TRANSITION) { if (state == LOGIN_SERVER_TRANSITION) {
if (rs.getTimestamp("lastlogin").getTime() + 30000 < Server.getInstance().getCurrentTime()) { if (rs.getTimestamp("lastlogin").getTime() + 30000 < Server.getInstance().getCurrentTime()) {
int accountId = accId; int accountId = accId;
@@ -845,20 +718,18 @@ public class MapleClient {
this.setAccID(accountId); this.setAccID(accountId);
} }
} }
rs.close(); }
ps.close(); }
if (state == LOGIN_LOGGEDIN) { if (state == LOGIN_LOGGEDIN) {
loggedIn = true; loggedIn = true;
} else if (state == LOGIN_SERVER_TRANSITION) { } else if (state == LOGIN_SERVER_TRANSITION) {
ps = con.prepareStatement("UPDATE accounts SET loggedin = 0 WHERE id = ?"); try (PreparedStatement ps2 = con.prepareStatement("UPDATE accounts SET loggedin = 0 WHERE id = ?")) {
ps.setInt(1, getAccID()); ps2.setInt(1, getAccID());
ps.executeUpdate(); ps2.executeUpdate();
ps.close(); }
} else { } else {
loggedIn = false; loggedIn = false;
} }
con.close();
return state; return state;
} catch (SQLException e) { } catch (SQLException e) {
loggedIn = false; loggedIn = false;
@@ -1196,32 +1067,32 @@ public class MapleClient {
} }
public boolean acceptToS() { public boolean acceptToS() {
boolean disconnectForBeingAFaggot = false;
if (accountName == null) { if (accountName == null) {
return true; return true;
} }
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT `tos` FROM accounts WHERE id = ?");
ps.setInt(1, accId);
ResultSet rs = ps.executeQuery();
boolean disconnect = false;
try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT `tos` FROM accounts WHERE id = ?")) {
ps.setInt(1, accId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
if (rs.getByte("tos") == 1) { if (rs.getByte("tos") == 1) {
disconnectForBeingAFaggot = true; disconnect = true;
} }
} }
ps.close(); }
rs.close(); }
ps = con.prepareStatement("UPDATE accounts SET tos = 1 WHERE id = ?");
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET tos = 1 WHERE id = ?")) {
ps.setInt(1, accId); ps.setInt(1, accId);
ps.executeUpdate(); ps.executeUpdate();
ps.close(); }
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return disconnectForBeingAFaggot; return disconnect;
} }
public void checkChar(int accid) { /// issue with multiple chars from same account login found by shavit, resinate public void checkChar(int accid) { /// issue with multiple chars from same account login found by shavit, resinate
@@ -1242,19 +1113,15 @@ public class MapleClient {
public int getVotePoints() { public int getVotePoints() {
int points = 0; int points = 0;
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT `votepoints` FROM accounts WHERE id = ?")) {
PreparedStatement ps = con.prepareStatement("SELECT `votepoints` FROM accounts WHERE id = ?");
ps.setInt(1, accId); ps.setInt(1, accId);
ResultSet rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
points = rs.getInt("votepoints"); points = rs.getInt("votepoints");
} }
ps.close(); }
rs.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -1278,15 +1145,11 @@ public class MapleClient {
} }
private void saveVotePoints() { private void saveVotePoints() {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET votepoints = ? WHERE id = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET votepoints = ? WHERE id = ?")) {
ps.setInt(1, votePoints); ps.setInt(1, votePoints);
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -1376,17 +1239,12 @@ public class MapleClient {
public synchronized boolean gainCharacterSlot() { public synchronized boolean gainCharacterSlot() {
if (canGainCharacterSlot()) { if (canGainCharacterSlot()) {
Connection con = null; try (Connection con = DatabaseConnection.getConnection();
try { PreparedStatement ps = con.prepareStatement("UPDATE accounts SET characterslots = ? WHERE id = ?")) {
con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET characterslots = ? WHERE id = ?")) {
ps.setInt(1, this.characterSlots += 1); ps.setInt(1, this.characterSlots += 1);
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -1396,34 +1254,18 @@ public class MapleClient {
} }
public final byte getGReason() { public final byte getGReason() {
Connection con = null; try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = null; PreparedStatement ps = con.prepareStatement("SELECT `greason` FROM `accounts` WHERE id = ?")) {
ResultSet rs = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `greason` FROM `accounts` WHERE id = ?");
ps.setInt(1, accId); ps.setInt(1, accId);
rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
return rs.getByte("greason"); return rs.getByte("greason");
} }
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
if (con != null) {
con.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
return 0; return 0;
} }
@@ -1433,16 +1275,12 @@ public class MapleClient {
public void setGender(byte m) { public void setGender(byte m) {
this.gender = m; this.gender = m;
Connection con = null;
try { try (Connection con = DatabaseConnection.getConnection();
con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET gender = ? WHERE id = ?")) {
try (PreparedStatement ps = con.prepareStatement("UPDATE accounts SET gender = ? WHERE id = ?")) {
ps.setByte(1, gender); ps.setByte(1, gender);
ps.setInt(2, accId); ps.setInt(2, accId);
ps.executeUpdate(); ps.executeUpdate();
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }