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,48 +239,48 @@ 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 = ?";
|
||||||
}
|
}
|
||||||
ps.setInt(1, id);
|
|
||||||
ResultSet rs = ps.executeQuery();
|
try (PreparedStatement ps = con.prepareStatement(query)) {
|
||||||
if (rs.next()) {
|
ps.setInt(1, id);
|
||||||
shopId = rs.getInt("shopid");
|
|
||||||
ret = new MapleShop(shopId, rs.getInt("npcid"));
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
rs.close();
|
if (rs.next()) {
|
||||||
ps.close();
|
shopId = rs.getInt("shopid");
|
||||||
} else {
|
ret = new MapleShop(shopId, rs.getInt("npcid"));
|
||||||
rs.close();
|
} else {
|
||||||
ps.close();
|
return null;
|
||||||
con.close();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
ps = con.prepareStatement("SELECT itemid, price, pitch FROM shopitems WHERE shopid = ? ORDER BY position DESC");
|
|
||||||
ps.setInt(1, shopId);
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
List<Integer> recharges = new ArrayList<>(rechargeableItems);
|
|
||||||
while (rs.next()) {
|
|
||||||
if (ItemConstants.isRechargeable(rs.getInt("itemid"))) {
|
|
||||||
MapleShopItem starItem = new MapleShopItem((short) 1, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("pitch"));
|
|
||||||
ret.addItem(starItem);
|
|
||||||
if (rechargeableItems.contains(starItem.getItemId())) {
|
|
||||||
recharges.remove(Integer.valueOf(starItem.getItemId()));
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ret.addItem(new MapleShopItem((short) 1000, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("pitch")));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Integer recharge : recharges) {
|
|
||||||
ret.addItem(new MapleShopItem((short) 1000, recharge.intValue(), 0, 0));
|
try (PreparedStatement ps = con.prepareStatement("SELECT itemid, price, pitch FROM shopitems WHERE shopid = ? ORDER BY position DESC")) {
|
||||||
|
ps.setInt(1, shopId);
|
||||||
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
List<Integer> recharges = new ArrayList<>(rechargeableItems);
|
||||||
|
while (rs.next()) {
|
||||||
|
if (ItemConstants.isRechargeable(rs.getInt("itemid"))) {
|
||||||
|
MapleShopItem starItem = new MapleShopItem((short) 1, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("pitch"));
|
||||||
|
ret.addItem(starItem);
|
||||||
|
if (rechargeableItems.contains(starItem.getItemId())) {
|
||||||
|
recharges.remove(Integer.valueOf(starItem.getItemId()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret.addItem(new MapleShopItem((short) 1000, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("pitch")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (Integer recharge : recharges) {
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
@@ -350,23 +347,23 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
//Server.getInstance().getChannel(world, channel).removeHiredMerchant(ownerId);
|
//Server.getInstance().getChannel(world, channel).removeHiredMerchant(ownerId);
|
||||||
map.broadcastMessage(MaplePacketCreator.removeHiredMerchantBox(getOwnerId()));
|
map.broadcastMessage(MaplePacketCreator.removeHiredMerchantBox(getOwnerId()));
|
||||||
map.removeMapObject(this);
|
map.removeMapObject(this);
|
||||||
|
|
||||||
MapleCharacter owner = Server.getInstance().getWorld(world).getPlayerStorage().getCharacterById(ownerId);
|
MapleCharacter owner = Server.getInstance().getWorld(world).getPlayerStorage().getCharacterById(ownerId);
|
||||||
|
|
||||||
visitorLock.lock();
|
visitorLock.lock();
|
||||||
try {
|
try {
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
removeAllVisitors();
|
removeAllVisitors();
|
||||||
|
|
||||||
if(owner != null && owner.isLoggedinWorld() && this == owner.getHiredMerchant()) {
|
if (owner != null && owner.isLoggedinWorld() && this == owner.getHiredMerchant()) {
|
||||||
closeOwnerMerchant(owner);
|
closeOwnerMerchant(owner);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
visitorLock.unlock();
|
visitorLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
Server.getInstance().getWorld(world).unregisterHiredMerchant(this);
|
Server.getInstance().getWorld(world).unregisterHiredMerchant(this);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
saveItems(true);
|
saveItems(true);
|
||||||
synchronized (items) {
|
synchronized (items) {
|
||||||
@@ -375,22 +372,18 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
MapleCharacter player = Server.getInstance().getWorld(world).getPlayerStorage().getCharacterById(ownerId);
|
|
||||||
if(player != null) {
|
|
||||||
player.setHasMerchant(false);
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS);
|
|
||||||
ps.setInt(1, ownerId);
|
|
||||||
ps.executeUpdate();
|
|
||||||
|
|
||||||
ps.close();
|
MapleCharacter player = Server.getInstance().getWorld(world).getPlayerStorage().getCharacterById(ownerId);
|
||||||
con.close();
|
if (player != null) {
|
||||||
} catch (SQLException ex) {
|
player.setHasMerchant(false);
|
||||||
ex.printStackTrace();
|
} else {
|
||||||
}
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement ps = con.prepareStatement("UPDATE characters SET HasMerchant = 0 WHERE id = ?", PreparedStatement.RETURN_GENERATED_KEYS)) {
|
||||||
|
ps.setInt(1, ownerId);
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
map = null;
|
map = null;
|
||||||
@@ -407,7 +400,7 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
map.removeMapObject(this);
|
map.removeMapObject(this);
|
||||||
map.broadcastMessage(MaplePacketCreator.removeHiredMerchantBox(ownerId));
|
map.broadcastMessage(MaplePacketCreator.removeHiredMerchantBox(ownerId));
|
||||||
c.getChannelServer().removeHiredMerchant(ownerId);
|
c.getChannelServer().removeHiredMerchant(ownerId);
|
||||||
|
|
||||||
this.removeAllVisitors();
|
this.removeAllVisitors();
|
||||||
this.removeOwner(c.getPlayer());
|
this.removeOwner(c.getPlayer());
|
||||||
|
|
||||||
@@ -415,7 +408,7 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
List<MaplePlayerShopItem> copyItems = getItems();
|
List<MaplePlayerShopItem> copyItems = getItems();
|
||||||
if (check(c.getPlayer(), copyItems) && !timeout) {
|
if (check(c.getPlayer(), copyItems) && !timeout) {
|
||||||
for (MaplePlayerShopItem mpsi : copyItems) {
|
for (MaplePlayerShopItem mpsi : copyItems) {
|
||||||
if(mpsi.isExist()) {
|
if (mpsi.isExist()) {
|
||||||
if (mpsi.getItem().getInventoryType().equals(MapleInventoryType.EQUIP)) {
|
if (mpsi.getItem().getInventoryType().equals(MapleInventoryType.EQUIP)) {
|
||||||
MapleInventoryManipulator.addFromDrop(c, mpsi.getItem(), false);
|
MapleInventoryManipulator.addFromDrop(c, mpsi.getItem(), false);
|
||||||
} else {
|
} else {
|
||||||
@@ -434,20 +427,19 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
// thanks Rohenn for noticing a possible dupe scenario on closing shop
|
// thanks Rohenn for noticing a possible dupe scenario on closing shop
|
||||||
MapleCharacter player = c.getWorldServer().getPlayerStorage().getCharacterById(ownerId);
|
MapleCharacter player = c.getWorldServer().getPlayerStorage().getCharacterById(ownerId);
|
||||||
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) {
|
||||||
c.getPlayer().saveCharToDB(false);
|
c.getPlayer().saveCharToDB(false);
|
||||||
}
|
}
|
||||||
@@ -458,7 +450,7 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
Server.getInstance().getWorld(world).unregisterHiredMerchant(this);
|
Server.getInstance().getWorld(world).unregisterHiredMerchant(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -646,10 +638,10 @@ public class MapleHiredMerchant extends AbstractMapleMapObject {
|
|||||||
bundles.add(newBundle);
|
bundles.add(newBundle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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