refactor: use try-with-resources for fm shop db operations
This commit is contained in:
@@ -21,12 +21,15 @@
|
|||||||
*/
|
*/
|
||||||
package server;
|
package server;
|
||||||
|
|
||||||
import client.inventory.manipulator.MapleInventoryManipulator;
|
|
||||||
import client.MapleClient;
|
import client.MapleClient;
|
||||||
import client.inventory.Item;
|
import client.inventory.Item;
|
||||||
import client.inventory.MapleInventoryType;
|
import client.inventory.MapleInventoryType;
|
||||||
import client.inventory.MaplePet;
|
import client.inventory.MaplePet;
|
||||||
|
import client.inventory.manipulator.MapleInventoryManipulator;
|
||||||
import constants.inventory.ItemConstants;
|
import constants.inventory.ItemConstants;
|
||||||
|
import tools.DatabaseConnection;
|
||||||
|
import tools.MaplePacketCreator;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -35,8 +38,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import tools.DatabaseConnection;
|
|
||||||
import tools.MaplePacketCreator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -238,30 +239,31 @@ public class MapleShop {
|
|||||||
public static MapleShop createFromDB(int id, boolean isShopId) {
|
public static MapleShop createFromDB(int id, boolean isShopId) {
|
||||||
MapleShop ret = null;
|
MapleShop ret = null;
|
||||||
int shopId;
|
int shopId;
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
final String query;
|
||||||
PreparedStatement ps;
|
|
||||||
if (isShopId) {
|
if (isShopId) {
|
||||||
ps = con.prepareStatement("SELECT * FROM shops WHERE shopid = ?");
|
query = "SELECT * FROM shops WHERE shopid = ?";
|
||||||
} else {
|
} else {
|
||||||
ps = con.prepareStatement("SELECT * FROM shops WHERE npcid = ?");
|
query = "SELECT * FROM shops WHERE npcid = ?";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try (PreparedStatement ps = con.prepareStatement(query)) {
|
||||||
ps.setInt(1, id);
|
ps.setInt(1, id);
|
||||||
ResultSet rs = ps.executeQuery();
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
shopId = rs.getInt("shopid");
|
shopId = rs.getInt("shopid");
|
||||||
ret = new MapleShop(shopId, rs.getInt("npcid"));
|
ret = new MapleShop(shopId, rs.getInt("npcid"));
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
} else {
|
} else {
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ps = con.prepareStatement("SELECT itemid, price, pitch FROM shopitems WHERE shopid = ? ORDER BY position DESC");
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (PreparedStatement ps = con.prepareStatement("SELECT itemid, price, pitch FROM shopitems WHERE shopid = ? ORDER BY position DESC")) {
|
||||||
ps.setInt(1, shopId);
|
ps.setInt(1, shopId);
|
||||||
rs = ps.executeQuery();
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
List<Integer> recharges = new ArrayList<>(rechargeableItems);
|
List<Integer> recharges = new ArrayList<>(rechargeableItems);
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
if (ItemConstants.isRechargeable(rs.getInt("itemid"))) {
|
if (ItemConstants.isRechargeable(rs.getInt("itemid"))) {
|
||||||
@@ -277,9 +279,8 @@ public class MapleShop {
|
|||||||
for (Integer recharge : recharges) {
|
for (Integer recharge : recharges) {
|
||||||
ret.addItem(new MapleShopItem((short) 1000, recharge.intValue(), 0, 0));
|
ret.addItem(new MapleShopItem((short) 1000, recharge.intValue(), 0, 0));
|
||||||
}
|
}
|
||||||
rs.close();
|
}
|
||||||
ps.close();
|
}
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,15 @@ import client.inventory.manipulator.MapleInventoryManipulator;
|
|||||||
import client.inventory.manipulator.MapleKarmaManipulator;
|
import client.inventory.manipulator.MapleKarmaManipulator;
|
||||||
import client.processor.npc.FredrickProcessor;
|
import client.processor.npc.FredrickProcessor;
|
||||||
import config.YamlConfig;
|
import config.YamlConfig;
|
||||||
|
import net.server.Server;
|
||||||
|
import net.server.audit.locks.MonitoredLockType;
|
||||||
|
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||||
|
import server.MapleItemInformationProvider;
|
||||||
|
import server.MapleTrade;
|
||||||
|
import tools.DatabaseConnection;
|
||||||
|
import tools.MaplePacketCreator;
|
||||||
|
import tools.Pair;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -41,14 +50,6 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
|
||||||
import net.server.Server;
|
|
||||||
import server.MapleItemInformationProvider;
|
|
||||||
import tools.DatabaseConnection;
|
|
||||||
import tools.MaplePacketCreator;
|
|
||||||
import tools.Pair;
|
|
||||||
import net.server.audit.locks.MonitoredLockType;
|
|
||||||
import server.MapleTrade;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -294,9 +295,7 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
if (owner != null) {
|
if (owner != null) {
|
||||||
owner.addMerchantMesos(price);
|
owner.addMerchantMesos(price);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
|
|
||||||
long merchantMesos = 0;
|
long merchantMesos = 0;
|
||||||
try (PreparedStatement ps = con.prepareStatement("SELECT MerchantMesos FROM characters WHERE id = ?")) {
|
try (PreparedStatement ps = con.prepareStatement("SELECT MerchantMesos FROM characters WHERE id = ?")) {
|
||||||
ps.setInt(1, ownerId);
|
ps.setInt(1, ownerId);
|
||||||
@@ -313,8 +312,6 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
ps.setInt(2, ownerId);
|
ps.setInt(2, ownerId);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -380,14 +377,10 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
player.setHasMerchant(false);
|
player.setHasMerchant(false);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
Connection con = DatabaseConnection.getConnection();
|
PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||||
PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS);
|
|
||||||
ps.setInt(1, ownerId);
|
ps.setInt(1, ownerId);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -440,12 +433,11 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
player.setHasMerchant(false);
|
player.setHasMerchant(false);
|
||||||
} else {
|
} else {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS)) {
|
PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||||
ps.setInt(1, ownerId);
|
ps.setInt(1, ownerId);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
con.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (YamlConfig.config.server.USE_ENFORCE_MERCHANT_SAVE) {
|
if (YamlConfig.config.server.USE_ENFORCE_MERCHANT_SAVE) {
|
||||||
@@ -647,9 +639,9 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
ItemFactory.MERCHANT.saveItems(itemsWithType, bundles, this.ownerId, con);
|
ItemFactory.MERCHANT.saveItems(itemsWithType, bundles, this.ownerId, con);
|
||||||
con.close();
|
}
|
||||||
|
|
||||||
FredrickProcessor.insertFredrickLog(this.ownerId);
|
FredrickProcessor.insertFredrickLog(this.ownerId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user