refactor: use try-with-resources for db operations in various places

This commit is contained in:
P0nk
2021-04-05 00:19:59 +02:00
parent 4ec4600406
commit 69635f5e6c
10 changed files with 171 additions and 267 deletions

View File

@@ -22,16 +22,17 @@
package scripting.portal;
import client.MapleClient;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import scripting.AbstractPlayerInteraction;
import scripting.map.MapScriptManager;
import server.maps.MaplePortal;
import tools.DatabaseConnection;
import tools.MaplePacketCreator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PortalPlayerInteraction extends AbstractPlayerInteraction {
private MaplePortal portal;
@@ -44,49 +45,31 @@ public class PortalPlayerInteraction extends AbstractPlayerInteraction {
public MaplePortal getPortal() {
return portal;
}
public void runMapScript() {
MapScriptManager msm = MapScriptManager.getInstance();
msm.runMapScript(c, "onUserEnter/" + portal.getScriptName(), false);
}
public boolean hasLevel30Character() {
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?");
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?")) {
ps.setInt(1, getPlayer().getAccountID());
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("level") >= 30) {
ps.close();
rs.close();
return true;
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
if (rs.getInt("level") >= 30) {
return true;
}
}
}
} catch (SQLException sqle) {
sqle.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 ex) {
ex.printStackTrace();
}
}
return getPlayer().getLevel() >= 30;
}
public void blockPortal() {
c.getPlayer().blockPortal(getPortal().getScriptName());
}

View File

@@ -22,6 +22,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting.reactor;
import client.MapleClient;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import server.maps.MapleReactor;
import server.maps.ReactorDropEntry;
import tools.DatabaseConnection;
import tools.FilePrinter;
import javax.script.ScriptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -29,14 +37,6 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import server.maps.MapleReactor;
import server.maps.ReactorDropEntry;
import tools.DatabaseConnection;
import tools.FilePrinter;
/**
* @author Lerk
@@ -83,8 +83,7 @@ public class ReactorScriptManager extends AbstractScriptManager {
List<ReactorDropEntry> ret = drops.get(rid);
if (ret == null) {
ret = new LinkedList<>();
try {
Connection con = DatabaseConnection.getConnection();
try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT itemid, chance, questid FROM reactordrops WHERE reactorid = ? AND chance >= 0")) {
ps.setInt(1, rid);
try (ResultSet rs = ps.executeQuery()) {
@@ -93,8 +92,6 @@ public class ReactorScriptManager extends AbstractScriptManager {
}
}
}
con.close();
} catch (Throwable e) {
FilePrinter.printError(FilePrinter.REACTOR + rid + ".txt", e);
}