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

@@ -24,8 +24,8 @@
package client.command.commands.gm1; package client.command.commands.gm1;
import client.MapleCharacter; import client.MapleCharacter;
import client.command.Command;
import client.MapleClient; import client.MapleClient;
import client.command.Command;
import server.MapleItemInformationProvider; import server.MapleItemInformationProvider;
import server.life.MapleMonsterInformationProvider; import server.life.MapleMonsterInformationProvider;
import tools.DatabaseConnection; import tools.DatabaseConnection;
@@ -48,31 +48,29 @@ public class WhoDropsCommand extends Command {
player.dropMessage(5, "Please do @whodrops <item name>"); player.dropMessage(5, "Please do @whodrops <item name>");
return; return;
} }
if (c.tryacquireClient()) { if (c.tryacquireClient()) {
try { try {
String searchString = player.getLastCommandMessage(); String searchString = player.getLastCommandMessage();
String output = ""; String output = "";
Iterator<Pair<Integer, String>> listIterator = MapleItemInformationProvider.getInstance().getItemDataByName(searchString).iterator(); Iterator<Pair<Integer, String>> listIterator = MapleItemInformationProvider.getInstance().getItemDataByName(searchString).iterator();
if(listIterator.hasNext()) { if (listIterator.hasNext()) {
int count = 1; int count = 1;
while(listIterator.hasNext() && count <= 3) { while (listIterator.hasNext() && count <= 3) {
Pair<Integer, String> data = listIterator.next(); Pair<Integer, String> data = listIterator.next();
output += "#b" + data.getRight() + "#k is dropped by:\r\n"; output += "#b" + data.getRight() + "#k is dropped by:\r\n";
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT dropperid FROM drop_data WHERE itemid = ? LIMIT 50")) {
PreparedStatement ps = con.prepareStatement("SELECT dropperid FROM drop_data WHERE itemid = ? LIMIT 50");
ps.setInt(1, data.getLeft()); ps.setInt(1, data.getLeft());
ResultSet rs = ps.executeQuery();
while(rs.next()) { try (ResultSet rs = ps.executeQuery()) {
String resultName = MapleMonsterInformationProvider.getInstance().getMobNameFromId(rs.getInt("dropperid")); while (rs.next()) {
if (resultName != null) { String resultName = MapleMonsterInformationProvider.getInstance().getMobNameFromId(rs.getInt("dropperid"));
output += resultName + ", "; if (resultName != null) {
output += resultName + ", ";
}
} }
} }
rs.close();
ps.close();
con.close();
} catch (Exception e) { } catch (Exception e) {
player.dropMessage(6, "There was a problem retrieving the required data. Please try again."); player.dropMessage(6, "There was a problem retrieving the required data. Please try again.");
e.printStackTrace(); e.printStackTrace();
@@ -85,7 +83,7 @@ public class WhoDropsCommand extends Command {
player.dropMessage(5, "The item you searched for doesn't exist."); player.dropMessage(5, "The item you searched for doesn't exist.");
return; return;
} }
c.getAbstractPlayerInteraction().npcTalk(9010000, output); c.getAbstractPlayerInteraction().npcTalk(9010000, output);
} finally { } finally {
c.releaseClient(); c.releaseClient();

View File

@@ -19,14 +19,13 @@
*/ */
package net.server.coordinator.session; package net.server.coordinator.session;
import net.server.coordinator.login.LoginStorage;
import client.MapleCharacter; import client.MapleCharacter;
import client.MapleClient; import client.MapleClient;
import config.YamlConfig; import config.YamlConfig;
import net.server.Server; import net.server.Server;
import net.server.audit.locks.MonitoredLockType; import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory; import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.coordinator.login.LoginStorage;
import org.apache.mina.core.session.IoSession; import org.apache.mina.core.session.IoSession;
import tools.DatabaseConnection; import tools.DatabaseConnection;
@@ -34,16 +33,8 @@ 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.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@@ -148,10 +139,9 @@ public class MapleSessionCoordinator {
} }
private static boolean associateHwidAccountIfAbsent(String remoteHwid, int accountId) { private static boolean associateHwidAccountIfAbsent(String remoteHwid, int accountId) {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
int hwidCount = 0; int hwidCount = 0;
try (PreparedStatement ps = con.prepareStatement("SELECT SQL_CACHE hwid FROM hwidaccounts WHERE accountid = ?")) { try (PreparedStatement ps = con.prepareStatement("SELECT SQL_CACHE hwid FROM hwidaccounts WHERE accountid = ?")) {
ps.setInt(1, accountId); ps.setInt(1, accountId);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
@@ -160,7 +150,7 @@ public class MapleSessionCoordinator {
if (rsHwid.contentEquals(remoteHwid)) { if (rsHwid.contentEquals(remoteHwid)) {
return false; return false;
} }
hwidCount++; hwidCount++;
} }
} }
@@ -169,8 +159,6 @@ public class MapleSessionCoordinator {
registerAccessAccount(con, remoteHwid, accountId); registerAccessAccount(con, remoteHwid, accountId);
return true; return true;
} }
} finally {
con.close();
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
@@ -180,12 +168,12 @@ public class MapleSessionCoordinator {
} }
private static boolean attemptAccessAccount(String nibbleHwid, int accountId, boolean routineCheck) { private static boolean attemptAccessAccount(String nibbleHwid, int accountId, boolean routineCheck) {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
int hwidCount = 0; int hwidCount = 0;
try (PreparedStatement ps = con.prepareStatement("SELECT SQL_CACHE * FROM hwidaccounts WHERE accountid = ?")) { try (PreparedStatement ps = con.prepareStatement("SELECT SQL_CACHE * FROM hwidaccounts WHERE accountid = ?")) {
ps.setInt(1, accountId); ps.setInt(1, accountId);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
String rsHwid = rs.getString("hwid"); String rsHwid = rs.getString("hwid");
@@ -207,8 +195,6 @@ public class MapleSessionCoordinator {
if (hwidCount < YamlConfig.config.server.MAX_ALLOWED_ACCOUNT_HWID) { if (hwidCount < YamlConfig.config.server.MAX_ALLOWED_ACCOUNT_HWID) {
return true; return true;
} }
} finally {
con.close();
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
@@ -563,17 +549,13 @@ public class MapleSessionCoordinator {
} }
public void runUpdateHwidHistory() { public void runUpdateHwidHistory() {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("DELETE FROM hwidaccounts WHERE expiresat < CURRENT_TIMESTAMP")) {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM hwidaccounts WHERE expiresat < CURRENT_TIMESTAMP")) { ps.executeUpdate();
ps.execute();
} finally {
con.close();
}
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
long timeNow = Server.getInstance().getCurrentTime(); long timeNow = Server.getInstance().getCurrentTime();
List<String> toRemove = new LinkedList<>(); List<String> toRemove = new LinkedList<>();
for (Entry<String, Long> cht : cachedHostTimeout.entrySet()) { for (Entry<String, Long> cht : cachedHostTimeout.entrySet()) {
@@ -581,7 +563,7 @@ public class MapleSessionCoordinator {
toRemove.add(cht.getKey()); toRemove.add(cht.getKey());
} }
} }
if (!toRemove.isEmpty()) { if (!toRemove.isEmpty()) {
for (String s : toRemove) { for (String s : toRemove) {
cachedHostHwids.remove(s); cachedHostHwids.remove(s);

View File

@@ -68,8 +68,6 @@ public class MapleGuild {
ResultSet rs = ps.executeQuery()) { ResultSet rs = ps.executeQuery()) {
if (!rs.next()) { if (!rs.next()) {
id = -1; id = -1;
ps.close();
rs.close();
return; return;
} }
id = guildid; id = guildid;
@@ -514,17 +512,13 @@ public class MapleGuild {
if (mgc.isOnline()) { if (mgc.isOnline()) {
Server.getInstance().getWorld(mgc.getWorld()).setGuildAndRank(cid, 0, 5); Server.getInstance().getWorld(mgc.getWorld()).setGuildAndRank(cid, 0, 5);
} else { } else {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO notes (`to`, `from`, `message`, `timestamp`) VALUES (?, ?, ?, ?)")) {
try (PreparedStatement ps = con.prepareStatement("INSERT INTO notes (`to`, `from`, `message`, `timestamp`) VALUES (?, ?, ?, ?)")) { ps.setString(1, mgc.getName());
ps.setString(1, mgc.getName()); ps.setString(2, initiator.getName());
ps.setString(2, initiator.getName()); ps.setString(3, "You have been expelled from the guild.");
ps.setString(3, "You have been expelled from the guild."); ps.setLong(4, System.currentTimeMillis());
ps.setLong(4, System.currentTimeMillis()); ps.executeUpdate();
ps.executeUpdate();
}
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("expelMember - MapleGuild " + e); System.out.println("expelMember - MapleGuild " + e);

View File

@@ -22,16 +22,17 @@
package scripting.portal; package scripting.portal;
import client.MapleClient; import client.MapleClient;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import scripting.AbstractPlayerInteraction; import scripting.AbstractPlayerInteraction;
import scripting.map.MapScriptManager; import scripting.map.MapScriptManager;
import server.maps.MaplePortal; import server.maps.MaplePortal;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PortalPlayerInteraction extends AbstractPlayerInteraction { public class PortalPlayerInteraction extends AbstractPlayerInteraction {
private MaplePortal portal; private MaplePortal portal;
@@ -44,49 +45,31 @@ public class PortalPlayerInteraction extends AbstractPlayerInteraction {
public MaplePortal getPortal() { public MaplePortal getPortal() {
return portal; return portal;
} }
public void runMapScript() { public void runMapScript() {
MapScriptManager msm = MapScriptManager.getInstance(); MapScriptManager msm = MapScriptManager.getInstance();
msm.runMapScript(c, "onUserEnter/" + portal.getScriptName(), false); msm.runMapScript(c, "onUserEnter/" + portal.getScriptName(), false);
} }
public boolean hasLevel30Character() { public boolean hasLevel30Character() {
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection();
ResultSet rs = null; PreparedStatement ps = con.prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?")) {
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?");
ps.setInt(1, getPlayer().getAccountID()); ps.setInt(1, getPlayer().getAccountID());
rs = ps.executeQuery();
while (rs.next()) { try (ResultSet rs = ps.executeQuery()) {
if (rs.getInt("level") >= 30) { while (rs.next()) {
ps.close(); if (rs.getInt("level") >= 30) {
rs.close(); return true;
return true; }
} }
} }
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); 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; return getPlayer().getLevel() >= 30;
} }
public void blockPortal() { public void blockPortal() {
c.getPlayer().blockPortal(getPortal().getScriptName()); 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; package scripting.reactor;
import client.MapleClient; 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.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@@ -29,14 +37,6 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; 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 * @author Lerk
@@ -83,8 +83,7 @@ public class ReactorScriptManager extends AbstractScriptManager {
List<ReactorDropEntry> ret = drops.get(rid); List<ReactorDropEntry> ret = drops.get(rid);
if (ret == null) { if (ret == null) {
ret = new LinkedList<>(); ret = new LinkedList<>();
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT itemid, chance, questid FROM reactordrops WHERE reactorid = ? AND chance >= 0")) { try (PreparedStatement ps = con.prepareStatement("SELECT itemid, chance, questid FROM reactordrops WHERE reactorid = ? AND chance >= 0")) {
ps.setInt(1, rid); ps.setInt(1, rid);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
@@ -93,8 +92,6 @@ public class ReactorScriptManager extends AbstractScriptManager {
} }
} }
} }
con.close();
} catch (Throwable e) { } catch (Throwable e) {
FilePrinter.printError(FilePrinter.REACTOR + rid + ".txt", e); FilePrinter.printError(FilePrinter.REACTOR + rid + ".txt", e);
} }

View File

@@ -19,23 +19,24 @@
*/ */
package server; package server;
import client.MapleClient;
import client.MapleCharacter; import client.MapleCharacter;
import client.MapleClient;
import client.inventory.Item; import client.inventory.Item;
import client.inventory.ItemFactory; import client.inventory.ItemFactory;
import client.inventory.MapleInventory; import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType; import client.inventory.MapleInventoryType;
import client.inventory.manipulator.MapleInventoryManipulator; import client.inventory.manipulator.MapleInventoryManipulator;
import scripting.event.EventInstanceManager; import scripting.event.EventInstanceManager;
import scripting.event.EventManager;
import tools.DatabaseConnection;
import tools.Pair;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import scripting.event.EventManager;
import tools.DatabaseConnection;
import tools.Pair;
/** /**
* *
@@ -118,10 +119,8 @@ public class MapleMarriage extends EventInstanceManager {
public static boolean claimGiftItems(MapleClient c, MapleCharacter chr) { public static boolean claimGiftItems(MapleClient c, MapleCharacter chr) {
List<Item> gifts = loadGiftItemsFromDb(c, chr.getId()); List<Item> gifts = loadGiftItemsFromDb(c, chr.getId());
if (MapleInventory.checkSpot(chr, gifts)) { if (MapleInventory.checkSpot(chr, gifts)) {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection(); ItemFactory.MARRIAGE_GIFTS.saveItems(new LinkedList<>(), chr.getId(), con);
ItemFactory.MARRIAGE_GIFTS.saveItems(new LinkedList<Pair<Item, MapleInventoryType>>(), chr.getId(), con);
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }
@@ -159,11 +158,9 @@ public class MapleMarriage extends EventInstanceManager {
for (Item it : giftItems) { for (Item it : giftItems) {
items.add(new Pair<>(it, it.getInventoryType())); items.add(new Pair<>(it, it.getInventoryType()));
} }
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
ItemFactory.MARRIAGE_GIFTS.saveItems(items, cid, con); ItemFactory.MARRIAGE_GIFTS.saveItems(items, cid, con);
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }

View File

@@ -19,22 +19,6 @@
*/ */
package server; package server;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import client.MapleCharacter; import client.MapleCharacter;
import provider.MapleData; import provider.MapleData;
import provider.MapleDataProvider; import provider.MapleDataProvider;
@@ -42,6 +26,16 @@ import provider.MapleDataProviderFactory;
import provider.MapleDataTool; import provider.MapleDataTool;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* *
* @author RonanLana * @author RonanLana
@@ -165,25 +159,18 @@ public class MapleSkillbookInformationProvider {
} }
private static void fetchSkillbooksFromReactors() { private static void fetchSkillbooksFromReactors() {
Connection con = null; try (Connection con = DatabaseConnection.getConnection();
try { PreparedStatement ps = con.prepareStatement("SELECT itemid FROM reactordrops WHERE itemid >= ? AND itemid < ?;")) {
con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT itemid FROM reactordrops WHERE itemid >= ? AND itemid < ?;");
ps.setInt(1, skillbookMinItemid); ps.setInt(1, skillbookMinItemid);
ps.setInt(2, skillbookMaxItemid); ps.setInt(2, skillbookMaxItemid);
ResultSet rs = ps.executeQuery();
if (rs.isBeforeFirst()) { try (ResultSet rs = ps.executeQuery()) {
while(rs.next()) { if (rs.isBeforeFirst()) {
foundSkillbooks.put(rs.getInt("itemid"), SkillBookEntry.REACTOR); while (rs.next()) {
foundSkillbooks.put(rs.getInt("itemid"), SkillBookEntry.REACTOR);
}
} }
} }
rs.close();
ps.close();
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }

View File

@@ -23,29 +23,24 @@ import client.inventory.Item;
import client.inventory.ItemFactory; import client.inventory.ItemFactory;
import client.inventory.MapleInventoryType; import client.inventory.MapleInventoryType;
import constants.game.GameConstants; import constants.game.GameConstants;
import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import tools.DatabaseConnection;
import tools.FilePrinter;
import tools.MaplePacketCreator;
import tools.Pair;
import java.io.File; import java.io.File;
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.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import tools.DatabaseConnection;
import tools.MaplePacketCreator;
import tools.Pair;
import net.server.audit.locks.MonitoredLockType;
import tools.FilePrinter;
/** /**
* *
@@ -70,39 +65,34 @@ public class MapleStorage {
} }
private static MapleStorage create(int id, int world) throws SQLException { private static MapleStorage create(int id, int world) throws SQLException {
Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("INSERT INTO storages (accountid, world, slots, meso) VALUES (?, ?, 4, 0)")) { PreparedStatement ps = con.prepareStatement("INSERT INTO storages (accountid, world, slots, meso) VALUES (?, ?, 4, 0)")) {
ps.setInt(1, id); ps.setInt(1, id);
ps.setInt(2, world); ps.setInt(2, world);
ps.executeUpdate(); ps.executeUpdate();
} }
con.close();
return loadOrCreateFromDB(id, world); return loadOrCreateFromDB(id, world);
} }
public static MapleStorage loadOrCreateFromDB(int id, int world) { public static MapleStorage loadOrCreateFromDB(int id, int world) {
try { MapleStorage ret;
MapleStorage ret; try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT storageid, slots, meso FROM storages WHERE accountid = ? AND world = ?")) {
PreparedStatement ps = con.prepareStatement("SELECT storageid, slots, meso FROM storages WHERE accountid = ? AND world = ?");
ps.setInt(1, id); ps.setInt(1, id);
ps.setInt(2, world); ps.setInt(2, world);
ResultSet rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
ret = new MapleStorage(rs.getInt("storageid"), (byte) rs.getInt("slots"), rs.getInt("meso")); ret = new MapleStorage(rs.getInt("storageid"), (byte) rs.getInt("slots"), rs.getInt("meso"));
for (Pair<Item, MapleInventoryType> item : ItemFactory.STORAGE.loadItems(ret.id, false)) { for (Pair<Item, MapleInventoryType> item : ItemFactory.STORAGE.loadItems(ret.id, false)) {
ret.items.add(item.getLeft()); ret.items.add(item.getLeft());
}
} else {
ret = create(id, world);
} }
} else {
ret = create(id, world);
} }
rs.close();
ps.close();
con.close();
return ret; return ret;
} catch (SQLException ex) { // exceptions leading to deploy null storages found thanks to Jefe } catch (SQLException ex) { // exceptions leading to deploy null storages found thanks to Jefe
FilePrinter.printError(FilePrinter.STORAGE, ex, "SQL error occurred when trying to load storage for accountid " + id + ", world " + GameConstants.WORLD_NAMES[world]); FilePrinter.printError(FilePrinter.STORAGE, ex, "SQL error occurred when trying to load storage for accountid " + id + ", world " + GameConstants.WORLD_NAMES[world]);

View File

@@ -19,19 +19,15 @@
*/ */
package server.expeditions; package server.expeditions;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import config.YamlConfig; import config.YamlConfig;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.Pair; import tools.Pair;
import java.sql.*;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
/** /**
* *
* @author Conrad * @author Conrad
@@ -123,18 +119,14 @@ public class MapleExpeditionBossLog {
private static void resetBossLogTable(boolean week, Calendar c) { private static void resetBossLogTable(boolean week, Calendar c) {
List<Pair<Timestamp, BossLogEntry>> resetTimestamps = BossLogEntry.getBossLogResetTimestamps(c, week); List<Pair<Timestamp, BossLogEntry>> resetTimestamps = BossLogEntry.getBossLogResetTimestamps(c, week);
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
for (Pair<Timestamp, BossLogEntry> p : resetTimestamps) { for (Pair<Timestamp, BossLogEntry> p : resetTimestamps) {
PreparedStatement ps = con.prepareStatement("DELETE FROM " + getBossLogTable(week) + " WHERE attempttime <= ? AND bosstype LIKE ?"); try (PreparedStatement ps = con.prepareStatement("DELETE FROM " + getBossLogTable(week) + " WHERE attempttime <= ? AND bosstype LIKE ?")) {
ps.setTimestamp(1, p.getLeft()); ps.setTimestamp(1, p.getLeft());
ps.setString(2, p.getRight().name()); ps.setString(2, p.getRight().name());
ps.executeUpdate(); ps.executeUpdate();
ps.close(); }
} }
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -146,21 +138,18 @@ public class MapleExpeditionBossLog {
private static int countPlayerEntries(int cid, BossLogEntry boss) { private static int countPlayerEntries(int cid, BossLogEntry boss) {
int ret_count = 0; int ret_count = 0;
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM " + getBossLogTable(boss.week) + " WHERE characterid = ? AND bosstype LIKE ?")) {
PreparedStatement ps;
ps = con.prepareStatement("SELECT COUNT(*) FROM " + getBossLogTable(boss.week) + " WHERE characterid = ? AND bosstype LIKE ?");
ps.setInt(1, cid); ps.setInt(1, cid);
ps.setString(2, boss.name()); ps.setString(2, boss.name());
ResultSet rs = ps.executeQuery();
if (rs.next()) { try (ResultSet rs = ps.executeQuery()) {
ret_count = rs.getInt(1); if (rs.next()) {
} else { ret_count = rs.getInt(1);
ret_count = -1; } else {
ret_count = -1;
}
} }
rs.close();
ps.close();
con.close();
return ret_count; return ret_count;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -169,14 +158,11 @@ public class MapleExpeditionBossLog {
} }
private static void insertPlayerEntry(int cid, BossLogEntry boss) { private static void insertPlayerEntry(int cid, BossLogEntry boss) {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO " + getBossLogTable(boss.week) + " (characterid, bosstype) VALUES (?,?)")) {
PreparedStatement ps = con.prepareStatement("INSERT INTO " + getBossLogTable(boss.week) + " (characterid, bosstype) VALUES (?,?)");
ps.setInt(1, cid); ps.setInt(1, cid);
ps.setString(2, boss.name()); ps.setString(2, boss.name());
ps.executeUpdate(); ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -21,8 +21,17 @@
*/ */
package server.maps; package server.maps;
import java.awt.Point; import provider.MapleData;
import java.awt.Rectangle; import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import scripting.event.EventInstanceManager;
import server.life.*;
import server.partyquest.GuardianSpawnPoint;
import tools.DatabaseConnection;
import tools.StringUtil;
import java.awt.*;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@@ -31,19 +40,6 @@ import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import server.life.AbstractLoadedMapleLife;
import server.life.MapleLifeFactory;
import server.life.MapleMonster;
import server.life.MaplePlayerNPC;
import server.life.MaplePlayerNPCFactory;
import scripting.event.EventInstanceManager;
import server.partyquest.GuardianSpawnPoint;
import tools.DatabaseConnection;
import tools.StringUtil;
public class MapleMapFactory { public class MapleMapFactory {
@@ -84,33 +80,29 @@ public class MapleMapFactory {
} }
private static void loadLifeFromDb(MapleMap map) { private static void loadLifeFromDb(MapleMap map) {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT * FROM plife WHERE map = ? and world = ?")) {
PreparedStatement ps = con.prepareStatement("SELECT * FROM plife WHERE map = ? and world = ?");
ps.setInt(1, map.getId()); ps.setInt(1, map.getId());
ps.setInt(2, map.getWorld()); ps.setInt(2, map.getWorld());
ResultSet rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
int id = rs.getInt("life"); int id = rs.getInt("life");
String type = rs.getString("type"); String type = rs.getString("type");
int cy = rs.getInt("cy"); int cy = rs.getInt("cy");
int f = rs.getInt("f"); int f = rs.getInt("f");
int fh = rs.getInt("fh"); int fh = rs.getInt("fh");
int rx0 = rs.getInt("rx0"); int rx0 = rs.getInt("rx0");
int rx1 = rs.getInt("rx1"); int rx1 = rs.getInt("rx1");
int x = rs.getInt("x"); int x = rs.getInt("x");
int y = rs.getInt("y"); int y = rs.getInt("y");
int hide = rs.getInt("hide"); int hide = rs.getInt("hide");
int mobTime = rs.getInt("mobtime"); int mobTime = rs.getInt("mobtime");
int team = rs.getInt("team"); int team = rs.getInt("team");
loadLifeRaw(map, id, type, cy, f, fh, rx0, rx1, x, y, hide, mobTime, team); loadLifeRaw(map, id, type, cy, f, fh, rx0, rx1, x, y, hide, mobTime, team);
}
} }
rs.close();
ps.close();
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }
@@ -243,18 +235,16 @@ public class MapleMapFactory {
map.setSeats(seats); map.setSeats(seats);
} }
if (event == null) { if (event == null) {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs WHERE map = ? AND world = ?")) {
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs WHERE map = ? AND world = ?")) { ps.setInt(1, mapid);
ps.setInt(1, mapid); ps.setInt(2, world);
ps.setInt(2, world);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
map.addPlayerNPCMapObject(new MaplePlayerNPC(rs)); map.addPlayerNPCMapObject(new MaplePlayerNPC(rs));
}
} }
} }
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }