refactor: use try-with-resources for db operations in various handlers
This commit is contained in:
@@ -21,19 +21,12 @@
|
||||
*/
|
||||
package net.server.channel.handlers;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import config.YamlConfig;
|
||||
import client.MapleCharacter;
|
||||
import client.MapleClient;
|
||||
import client.inventory.Equip;
|
||||
import client.inventory.Item;
|
||||
import client.processor.action.BuybackProcessor;
|
||||
import config.YamlConfig;
|
||||
import net.AbstractMaplePacketHandler;
|
||||
import net.server.Server;
|
||||
import server.MTSItemInfo;
|
||||
@@ -43,6 +36,13 @@ import tools.DatabaseConnection;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.data.input.SeekableLittleEndianAccessor;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public final class EnterMTSHandler extends AbstractMaplePacketHandler {
|
||||
@Override
|
||||
@@ -120,15 +120,75 @@ public final class EnterMTSHandler extends AbstractMaplePacketHandler {
|
||||
c.announce(MaplePacketCreator.showMTSCash(c.getPlayer()));
|
||||
List<MTSItemInfo> items = new ArrayList<>();
|
||||
int pages = 0;
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE tab = 1 AND transfer = 0 ORDER BY id DESC LIMIT 16, 16");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
try (Connection con = DatabaseConnection.getConnection();) {
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE tab = 1 AND transfer = 0 ORDER BY id DESC LIMIT 16, 16");
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("type") != 1) {
|
||||
Item i = new Item(rs.getInt("itemid"), (short) 0, (short) rs.getInt("quantity"));
|
||||
i.setOwner(rs.getString("owner"));
|
||||
items.add(new MTSItemInfo(i, rs.getInt("price") + 100 + (int) (rs.getInt("price") * 0.1), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
} else {
|
||||
Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
|
||||
equip.setOwner(rs.getString("owner"));
|
||||
equip.setQuantity((short) 1);
|
||||
equip.setAcc((short) rs.getInt("acc"));
|
||||
equip.setAvoid((short) rs.getInt("avoid"));
|
||||
equip.setDex((short) rs.getInt("dex"));
|
||||
equip.setHands((short) rs.getInt("hands"));
|
||||
equip.setHp((short) rs.getInt("hp"));
|
||||
equip.setInt((short) rs.getInt("int"));
|
||||
equip.setJump((short) rs.getInt("jump"));
|
||||
equip.setVicious((short) rs.getInt("vicious"));
|
||||
equip.setFlag((short) rs.getInt("flag"));
|
||||
equip.setLuk((short) rs.getInt("luk"));
|
||||
equip.setMatk((short) rs.getInt("matk"));
|
||||
equip.setMdef((short) rs.getInt("mdef"));
|
||||
equip.setMp((short) rs.getInt("mp"));
|
||||
equip.setSpeed((short) rs.getInt("speed"));
|
||||
equip.setStr((short) rs.getInt("str"));
|
||||
equip.setWatk((short) rs.getInt("watk"));
|
||||
equip.setWdef((short) rs.getInt("wdef"));
|
||||
equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
|
||||
equip.setLevel((byte) rs.getInt("level"));
|
||||
equip.setItemLevel(rs.getByte("itemlevel"));
|
||||
equip.setItemExp(rs.getInt("itemexp"));
|
||||
equip.setRingId(rs.getInt("ringid"));
|
||||
equip.setExpiration(rs.getLong("expiration"));
|
||||
equip.setGiftFrom(rs.getString("giftFrom"));
|
||||
|
||||
items.add(new MTSItemInfo((Item) equip, rs.getInt("price") + 100 + (int) (rs.getInt("price") * 0.1), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM mts_items");
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
pages = (int) Math.ceil(rs.getInt(1) / 16);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
c.announce(MaplePacketCreator.sendMTS(items, 1, 0, 0, pages));
|
||||
c.announce(MaplePacketCreator.transferInventory(getTransfer(chr.getId())));
|
||||
c.announce(MaplePacketCreator.notYetSoldInv(getNotYetSold(chr.getId())));
|
||||
}
|
||||
}
|
||||
|
||||
private List<MTSItemInfo> getNotYetSold(int cid) {
|
||||
List<MTSItemInfo> items = new ArrayList<>();
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE seller = ? AND transfer = 0 ORDER BY id DESC")) {
|
||||
ps.setInt(1, cid);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("type") != 1) {
|
||||
Item i = new Item(rs.getInt("itemid"), (short) 0, (short) rs.getInt("quantity"));
|
||||
i.setOwner(rs.getString("owner"));
|
||||
items.add(new MTSItemInfo(i, rs.getInt("price") + 100 + (int) (rs.getInt("price") * 0.1), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
items.add(new MTSItemInfo((Item) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
} else {
|
||||
Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
|
||||
equip.setOwner(rs.getString("owner"));
|
||||
@@ -141,7 +201,6 @@ public final class EnterMTSHandler extends AbstractMaplePacketHandler {
|
||||
equip.setInt((short) rs.getInt("int"));
|
||||
equip.setJump((short) rs.getInt("jump"));
|
||||
equip.setVicious((short) rs.getInt("vicious"));
|
||||
equip.setFlag((short) rs.getInt("flag"));
|
||||
equip.setLuk((short) rs.getInt("luk"));
|
||||
equip.setMatk((short) rs.getInt("matk"));
|
||||
equip.setMdef((short) rs.getInt("mdef"));
|
||||
@@ -155,78 +214,13 @@ public final class EnterMTSHandler extends AbstractMaplePacketHandler {
|
||||
equip.setItemLevel(rs.getByte("itemlevel"));
|
||||
equip.setItemExp(rs.getInt("itemexp"));
|
||||
equip.setRingId(rs.getInt("ringid"));
|
||||
equip.setFlag((short) rs.getInt("flag"));
|
||||
equip.setExpiration(rs.getLong("expiration"));
|
||||
equip.setGiftFrom(rs.getString("giftFrom"));
|
||||
|
||||
items.add(new MTSItemInfo((Item) equip, rs.getInt("price") + 100 + (int) (rs.getInt("price") * 0.1), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
ps = con.prepareStatement("SELECT COUNT(*) FROM mts_items");
|
||||
rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
pages = (int) Math.ceil(rs.getInt(1) / 16);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
c.announce(MaplePacketCreator.sendMTS(items, 1, 0, 0, pages));
|
||||
c.announce(MaplePacketCreator.transferInventory(getTransfer(chr.getId())));
|
||||
c.announce(MaplePacketCreator.notYetSoldInv(getNotYetSold(chr.getId())));
|
||||
}
|
||||
}
|
||||
|
||||
private List<MTSItemInfo> getNotYetSold(int cid) {
|
||||
List<MTSItemInfo> items = new ArrayList<>();
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE seller = ? AND transfer = 0 ORDER BY id DESC")) {
|
||||
ps.setInt(1, cid);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("type") != 1) {
|
||||
Item i = new Item(rs.getInt("itemid"), (short) 0, (short) rs.getInt("quantity"));
|
||||
i.setOwner(rs.getString("owner"));
|
||||
items.add(new MTSItemInfo((Item) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
} else {
|
||||
Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
|
||||
equip.setOwner(rs.getString("owner"));
|
||||
equip.setQuantity((short) 1);
|
||||
equip.setAcc((short) rs.getInt("acc"));
|
||||
equip.setAvoid((short) rs.getInt("avoid"));
|
||||
equip.setDex((short) rs.getInt("dex"));
|
||||
equip.setHands((short) rs.getInt("hands"));
|
||||
equip.setHp((short) rs.getInt("hp"));
|
||||
equip.setInt((short) rs.getInt("int"));
|
||||
equip.setJump((short) rs.getInt("jump"));
|
||||
equip.setVicious((short) rs.getInt("vicious"));
|
||||
equip.setLuk((short) rs.getInt("luk"));
|
||||
equip.setMatk((short) rs.getInt("matk"));
|
||||
equip.setMdef((short) rs.getInt("mdef"));
|
||||
equip.setMp((short) rs.getInt("mp"));
|
||||
equip.setSpeed((short) rs.getInt("speed"));
|
||||
equip.setStr((short) rs.getInt("str"));
|
||||
equip.setWatk((short) rs.getInt("watk"));
|
||||
equip.setWdef((short) rs.getInt("wdef"));
|
||||
equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
|
||||
equip.setLevel((byte) rs.getInt("level"));
|
||||
equip.setItemLevel(rs.getByte("itemlevel"));
|
||||
equip.setItemExp(rs.getInt("itemexp"));
|
||||
equip.setRingId(rs.getInt("ringid"));
|
||||
equip.setFlag((short) rs.getInt("flag"));
|
||||
equip.setExpiration(rs.getLong("expiration"));
|
||||
equip.setGiftFrom(rs.getString("giftFrom"));
|
||||
items.add(new MTSItemInfo((Item) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
}
|
||||
items.add(new MTSItemInfo((Item) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
}
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -235,50 +229,48 @@ public final class EnterMTSHandler extends AbstractMaplePacketHandler {
|
||||
|
||||
private List<MTSItemInfo> getTransfer(int cid) {
|
||||
List<MTSItemInfo> items = new ArrayList<>();
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE transfer = 1 AND seller = ? ORDER BY id DESC")) {
|
||||
ps.setInt(1, cid);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("type") != 1) {
|
||||
Item i = new Item(rs.getInt("itemid"), (short) 0, (short) rs.getInt("quantity"));
|
||||
i.setOwner(rs.getString("owner"));
|
||||
items.add(new MTSItemInfo((Item) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
} else {
|
||||
Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
|
||||
equip.setOwner(rs.getString("owner"));
|
||||
equip.setQuantity((short) 1);
|
||||
equip.setAcc((short) rs.getInt("acc"));
|
||||
equip.setAvoid((short) rs.getInt("avoid"));
|
||||
equip.setDex((short) rs.getInt("dex"));
|
||||
equip.setHands((short) rs.getInt("hands"));
|
||||
equip.setHp((short) rs.getInt("hp"));
|
||||
equip.setInt((short) rs.getInt("int"));
|
||||
equip.setJump((short) rs.getInt("jump"));
|
||||
equip.setVicious((short) rs.getInt("vicious"));
|
||||
equip.setLuk((short) rs.getInt("luk"));
|
||||
equip.setMatk((short) rs.getInt("matk"));
|
||||
equip.setMdef((short) rs.getInt("mdef"));
|
||||
equip.setMp((short) rs.getInt("mp"));
|
||||
equip.setSpeed((short) rs.getInt("speed"));
|
||||
equip.setStr((short) rs.getInt("str"));
|
||||
equip.setWatk((short) rs.getInt("watk"));
|
||||
equip.setWdef((short) rs.getInt("wdef"));
|
||||
equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
|
||||
equip.setLevel((byte) rs.getInt("level"));
|
||||
equip.setItemLevel(rs.getByte("itemlevel"));
|
||||
equip.setItemExp(rs.getInt("itemexp"));
|
||||
equip.setRingId(rs.getInt("ringid"));
|
||||
equip.setFlag((short) rs.getInt("flag"));
|
||||
equip.setExpiration(rs.getLong("expiration"));
|
||||
equip.setGiftFrom(rs.getString("giftFrom"));
|
||||
items.add(new MTSItemInfo((Item) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
}
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE transfer = 1 AND seller = ? ORDER BY id DESC")) {
|
||||
ps.setInt(1, cid);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
if (rs.getInt("type") != 1) {
|
||||
Item i = new Item(rs.getInt("itemid"), (short) 0, (short) rs.getInt("quantity"));
|
||||
i.setOwner(rs.getString("owner"));
|
||||
items.add(new MTSItemInfo((Item) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
} else {
|
||||
Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
|
||||
equip.setOwner(rs.getString("owner"));
|
||||
equip.setQuantity((short) 1);
|
||||
equip.setAcc((short) rs.getInt("acc"));
|
||||
equip.setAvoid((short) rs.getInt("avoid"));
|
||||
equip.setDex((short) rs.getInt("dex"));
|
||||
equip.setHands((short) rs.getInt("hands"));
|
||||
equip.setHp((short) rs.getInt("hp"));
|
||||
equip.setInt((short) rs.getInt("int"));
|
||||
equip.setJump((short) rs.getInt("jump"));
|
||||
equip.setVicious((short) rs.getInt("vicious"));
|
||||
equip.setLuk((short) rs.getInt("luk"));
|
||||
equip.setMatk((short) rs.getInt("matk"));
|
||||
equip.setMdef((short) rs.getInt("mdef"));
|
||||
equip.setMp((short) rs.getInt("mp"));
|
||||
equip.setSpeed((short) rs.getInt("speed"));
|
||||
equip.setStr((short) rs.getInt("str"));
|
||||
equip.setWatk((short) rs.getInt("watk"));
|
||||
equip.setWdef((short) rs.getInt("wdef"));
|
||||
equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
|
||||
equip.setLevel((byte) rs.getInt("level"));
|
||||
equip.setItemLevel(rs.getByte("itemlevel"));
|
||||
equip.setItemExp(rs.getInt("itemexp"));
|
||||
equip.setRingId(rs.getInt("ringid"));
|
||||
equip.setFlag((short) rs.getInt("flag"));
|
||||
equip.setExpiration(rs.getLong("expiration"));
|
||||
equip.setGiftFrom(rs.getString("giftFrom"));
|
||||
items.add(new MTSItemInfo((Item) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
|
||||
}
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user