refactor: use try-with-resources for cash shop db operations

This commit is contained in:
P0nk
2021-04-04 14:54:54 +02:00
parent 6639188c5a
commit 8ddfe05dd6

View File

@@ -21,36 +21,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server;
import java.io.File;
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.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.locks.Lock;
import client.inventory.*;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import net.server.Server;
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.Pair;
import client.inventory.Equip;
import client.inventory.Item;
import client.inventory.ItemFactory;
import client.inventory.MapleInventoryType;
import client.inventory.MaplePet;
import constants.inventory.ItemConstants;
import java.util.Collections;
import net.server.audit.locks.MonitoredLockType;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.locks.Lock;
/*
* @author Flav
@@ -188,31 +179,21 @@ public class CashShop {
}
}
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT * FROM specialcashitems");
rs = ps.executeQuery();
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM specialcashitems");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
specialcashitems.add(new SpecialCashItem(rs.getInt("sn"), rs.getInt("modifier"), rs.getByte("info")));
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null && !rs.isClosed()) rs.close();
if (ps != null && !ps.isClosed()) ps.close();
if (con != null && !con.isClosed()) con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static CashItem getRandomCashItem() {
if(randomitemsns.isEmpty()) return null;
if (randomitemsns.isEmpty()) {
return null;
}
int rnd = (int) (Math.random() * randomitemsns.size());
return items.get(randomitemsns.get(rnd));
@@ -242,26 +223,14 @@ public class CashShop {
public static void reloadSpecialCashItems() {//Yay?
specialcashitems.clear();
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT * FROM specialcashitems");
rs = ps.executeQuery();
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM specialcashitems");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
specialcashitems.add(new SpecialCashItem(rs.getInt("sn"), rs.getInt("modifier"), rs.getByte("info")));
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null && !rs.isClosed()) rs.close();
if (ps != null && !ps.isClosed()) ps.close();
if (con != null && !con.isClosed()) con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
@@ -290,42 +259,32 @@ public class CashShop {
factory = ItemFactory.CASH_OVERALL;
}
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("SELECT `nxCredit`, `maplePoint`, `nxPrepaid` FROM `accounts` WHERE `id` = ?");
try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT `nxCredit`, `maplePoint`, `nxPrepaid` FROM `accounts` WHERE `id` = ?")) {
ps.setInt(1, accountId);
rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
this.nxCredit = rs.getInt("nxCredit");
this.maplePoint = rs.getInt("maplePoint");
this.nxPrepaid = rs.getInt("nxPrepaid");
}
rs.close();
ps.close();
}
}
for (Pair<Item, MapleInventoryType> item : factory.loadItems(accountId, false)) {
inventory.add(item.getLeft());
}
ps = con.prepareStatement("SELECT `sn` FROM `wishlists` WHERE `charid` = ?");
try (PreparedStatement ps = con.prepareStatement("SELECT `sn` FROM `wishlists` WHERE `charid` = ?")) {
ps.setInt(1, characterId);
rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
wishList.add(rs.getInt("sn"));
}
rs.close();
ps.close();
con.close();
} finally {
if (ps != null && !ps.isClosed()) ps.close();
if (rs != null && !rs.isClosed()) rs.close();
if (con != null && !con.isClosed()) con.close();
}
}
}
}
@@ -358,7 +317,9 @@ public class CashShop {
public void gainCash(int type, CashItem buyItem, int world) {
gainCash(type, -buyItem.getPrice());
if(!YamlConfig.config.server.USE_ENFORCE_ITEM_SUGGESTION) Server.getInstance().getWorld(world).addCashItemBought(buyItem.getSN());
if (!YamlConfig.config.server.USE_ENFORCE_ITEM_SUGGESTION) {
Server.getInstance().getWorld(world).addCashItemBought(buyItem.getSN());
}
}
public boolean isOpened() {
@@ -432,40 +393,28 @@ public class CashShop {
}
public void gift(int recipient, String from, String message, int sn, int ringid) {
PreparedStatement ps = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("INSERT INTO `gifts` VALUES (DEFAULT, ?, ?, ?, ?, ?)");
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO `gifts` VALUES (DEFAULT, ?, ?, ?, ?, ?)")) {
ps.setInt(1, recipient);
ps.setString(2, from);
ps.setString(3, message);
ps.setInt(4, sn);
ps.setInt(5, ringid);
ps.executeUpdate();
con.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
if (ps != null && !ps.isClosed()) ps.close();
if (con != null && !con.isClosed()) con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public List<Pair<Item, String>> loadGifts() {
List<Pair<Item, String>> gifts = new ArrayList<>();
Connection con = null;
try {
con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM `gifts` WHERE `to` = ?");
try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM `gifts` WHERE `to` = ?")) {
ps.setInt(1, characterId);
ResultSet rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
notes++;
CashItem cItem = CashItemFactory.getItem(rs.getInt("sn"));
@@ -475,9 +424,10 @@ public class CashShop {
if (item.getInventoryType().equals(MapleInventoryType.EQUIP)) {
equip = (Equip) item;
equip.setRingId(rs.getInt("ringid"));
gifts.add(new Pair<Item, String>(equip, rs.getString("message")));
} else
gifts.add(new Pair<>(equip, rs.getString("message")));
} else {
gifts.add(new Pair<>(item, rs.getString("message")));
}
if (CashItemFactory.isPackage(cItem.getItemId())) { //Packages never contains a ring
for (Item packageItem : CashItemFactory.getPackage(cItem.getItemId())) {
@@ -488,14 +438,13 @@ public class CashShop {
addToInventory(equip == null ? item : equip);
}
}
}
}
rs.close();
ps.close();
ps = con.prepareStatement("DELETE FROM `gifts` WHERE `to` = ?");
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `gifts` WHERE `to` = ?")) {
ps.setInt(1, characterId);
ps.executeUpdate();
ps.close();
con.close();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
@@ -512,13 +461,14 @@ public class CashShop {
}
public void save(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `nxCredit` = ?, `maplePoint` = ?, `nxPrepaid` = ? WHERE `id` = ?");
try (PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `nxCredit` = ?, `maplePoint` = ?, `nxPrepaid` = ? WHERE `id` = ?")) {
ps.setInt(1, nxCredit);
ps.setInt(2, maplePoint);
ps.setInt(3, nxPrepaid);
ps.setInt(4, accountId);
ps.executeUpdate();
ps.close();
}
List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<>();
List<Item> inv = getInventory();
@@ -527,19 +477,21 @@ public class CashShop {
}
factory.saveItems(itemsWithType, accountId, con);
ps = con.prepareStatement("DELETE FROM `wishlists` WHERE `charid` = ?");
ps.setInt(1, characterId);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("INSERT INTO `wishlists` VALUES (DEFAULT, ?, ?)");
ps.setInt(1, characterId);
for (int sn : wishList) {
ps.setInt(2, sn);
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `wishlists` WHERE `charid` = ?")) {
ps.setInt(1, characterId);
ps.executeUpdate();
}
ps.close();
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `wishlists` VALUES (DEFAULT, ?, ?)")) {
ps.setInt(1, characterId);
for (int sn : wishList) {
// TODO: batch insert
ps.setInt(2, sn);
ps.executeUpdate();
}
}
}
private Item getCashShopItemByItemid(int itemid) {