refactor: use try-with-resources for Duey db operations
This commit is contained in:
@@ -34,16 +34,6 @@ import client.inventory.manipulator.MapleInventoryManipulator;
|
||||
import client.inventory.manipulator.MapleKarmaManipulator;
|
||||
import config.YamlConfig;
|
||||
import constants.inventory.ItemConstants;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import net.server.channel.Channel;
|
||||
import server.DueyPackage;
|
||||
import server.MapleItemInformationProvider;
|
||||
@@ -53,8 +43,13 @@ import tools.FilePrinter;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.Pair;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author RonanLana - synchronization of Duey modules
|
||||
*/
|
||||
public class DueyProcessor {
|
||||
@@ -84,7 +79,7 @@ public class DueyProcessor {
|
||||
TOCLIENT_RECV_PACKAGE_MSG(0x1B);
|
||||
final byte code;
|
||||
|
||||
private Actions(int code) {
|
||||
Actions(int code) {
|
||||
this.code = (byte) code;
|
||||
}
|
||||
|
||||
@@ -94,69 +89,40 @@ public class DueyProcessor {
|
||||
}
|
||||
|
||||
private static Pair<Integer, Integer> getAccountCharacterIdFromCNAME(String name) {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT id,accountid FROM characters WHERE name = ?");
|
||||
Pair<Integer, Integer> ids = null;
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT id,accountid FROM characters WHERE name = ?")) {
|
||||
ps.setString(1, name);
|
||||
|
||||
Pair<Integer, Integer> id_ = null;
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
id_ = new Pair<>(rs.getInt("accountid"), rs.getInt("id"));
|
||||
ids = new Pair<>(rs.getInt("accountid"), rs.getInt("id"));
|
||||
}
|
||||
}
|
||||
ps.close();
|
||||
con.close();
|
||||
return id_;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
private static void showDueyNotification(MapleClient c, MapleCharacter player) {
|
||||
Connection con = null;
|
||||
PreparedStatement ps = null;
|
||||
PreparedStatement pss = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
ps = con.prepareStatement("SELECT SenderName, Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC");
|
||||
ps.setInt(1, player.getId());
|
||||
rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
try {
|
||||
Connection con2 = DatabaseConnection.getConnection();
|
||||
pss = con2.prepareStatement("UPDATE dueypackages SET Checked = 0 where ReceiverId = ?");
|
||||
pss.setInt(1, player.getId());
|
||||
pss.executeUpdate();
|
||||
pss.close();
|
||||
con2.close();
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT SenderName, Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC")) {
|
||||
|
||||
c.announce(MaplePacketCreator.sendDueyParcelReceived(rs.getString("SenderName"), rs.getInt("Type") == 1));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
ps.setInt(1, player.getId());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
try (PreparedStatement ps2 = con.prepareStatement("UPDATE dueypackages SET Checked = 0 where ReceiverId = ?")) {
|
||||
ps2.setInt(1, player.getId());
|
||||
ps2.executeUpdate();
|
||||
|
||||
c.announce(MaplePacketCreator.sendDueyParcelReceived(rs.getString("SenderName"), rs.getInt("Type") == 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
}
|
||||
if (pss != null) {
|
||||
pss.close();
|
||||
}
|
||||
if (ps != null) {
|
||||
ps.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,17 +131,12 @@ public class DueyProcessor {
|
||||
}
|
||||
|
||||
private static void removePackageFromDB(int packageId) {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps = con.prepareStatement("DELETE FROM dueypackages WHERE PackageId = ?");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("DELETE FROM dueypackages WHERE PackageId = ?")) {
|
||||
ps.setInt(1, packageId);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
deletePackageFromInventoryDB(con, packageId);
|
||||
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -208,21 +169,21 @@ public class DueyProcessor {
|
||||
|
||||
private static List<DueyPackage> loadPackages(MapleCharacter chr) {
|
||||
List<DueyPackage> packages = new LinkedList<>();
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages dp WHERE ReceiverId = ?")) {
|
||||
ps.setInt(1, chr.getId());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
DueyPackage dueypack = getPackageFromDB(rs);
|
||||
if (dueypack == null) continue;
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages dp WHERE ReceiverId = ?")) {
|
||||
ps.setInt(1, chr.getId());
|
||||
|
||||
packages.add(dueypack);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
DueyPackage dueypack = getPackageFromDB(rs);
|
||||
if (dueypack == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
packages.add(dueypack);
|
||||
}
|
||||
}
|
||||
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -231,50 +192,32 @@ public class DueyProcessor {
|
||||
}
|
||||
|
||||
private static int createPackage(int mesos, String message, String sender, int toCid, boolean quick) {
|
||||
try {
|
||||
Connection con = null;
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO `dueypackages` (ReceiverId, SenderName, Mesos, TimeStamp, Message, Type, Checked) VALUES (?, ?, ?, ?, ?, ?, 1)", Statement.RETURN_GENERATED_KEYS)) {
|
||||
ps.setInt(1, toCid);
|
||||
ps.setString(2, sender);
|
||||
ps.setInt(3, mesos);
|
||||
ps.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
|
||||
ps.setString(5, message);
|
||||
ps.setInt(6, quick ? 1 : 0);
|
||||
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
ps = con.prepareStatement("INSERT INTO `dueypackages` (ReceiverId, SenderName, Mesos, TimeStamp, Message, Type, Checked) VALUES (?, ?, ?, ?, ?, ?, 1)", Statement.RETURN_GENERATED_KEYS);
|
||||
ps.setInt(1, toCid);
|
||||
ps.setString(2, sender);
|
||||
ps.setInt(3, mesos);
|
||||
ps.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
|
||||
ps.setString(5, message);
|
||||
ps.setInt(6, quick ? 1 : 0);
|
||||
int updateRows = ps.executeUpdate();
|
||||
if (updateRows < 1) {
|
||||
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Error trying to create package [mesos: " + mesos + ", " + sender + ", quick: " + quick + ", to CharacterId: " + toCid + "]");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int updateRows = ps.executeUpdate();
|
||||
if (updateRows < 1) {
|
||||
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Error trying to create package [mesos: " + mesos + ", " + sender + ", quick: " + quick + ", to CharacterId: " + toCid + "]");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int packageId;
|
||||
rs = ps.getGeneratedKeys();
|
||||
final int packageId;
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
packageId = rs.getInt(1);
|
||||
} else {
|
||||
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Failed inserting package [mesos: " + mesos + ", " + sender + ", quick: " + quick + ", to CharacterId: " + toCid + "]");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return packageId;
|
||||
} finally {
|
||||
if (rs != null && !rs.isClosed()) {
|
||||
rs.close();
|
||||
}
|
||||
|
||||
if (ps != null && !ps.isClosed()) {
|
||||
ps.close();
|
||||
}
|
||||
|
||||
if (con != null && !con.isClosed()) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
return packageId;
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
@@ -283,18 +226,15 @@ public class DueyProcessor {
|
||||
}
|
||||
|
||||
private static boolean insertPackageItem(int packageId, Item item) {
|
||||
try {
|
||||
Pair<Item, MapleInventoryType> dueyItem = new Pair<>(item, MapleInventoryType.getByType(item.getItemType()));
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
Pair<Item, MapleInventoryType> dueyItem = new Pair<>(item, MapleInventoryType.getByType(item.getItemType()));
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
ItemFactory.DUEY.saveItems(Collections.singletonList(dueyItem), packageId, con);
|
||||
con.close();
|
||||
|
||||
return true;
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int addPackageItemFromInventory(int packageId, MapleClient c, byte invTypeId, short itemPos, short amount) {
|
||||
@@ -407,9 +347,9 @@ public class DueyProcessor {
|
||||
int channel = c.getWorldServer().find(recipient);
|
||||
if (channel > -1) {
|
||||
Channel rcserv = c.getWorldServer().getChannel(channel);
|
||||
if(rcserv != null) {
|
||||
if (rcserv != null) {
|
||||
MapleCharacter rChr = rcserv.getPlayerStorage().getCharacterByName(recipient);
|
||||
if(rChr != null) {
|
||||
if (rChr != null) {
|
||||
rClient = rChr.getClient();
|
||||
}
|
||||
}
|
||||
@@ -440,9 +380,8 @@ public class DueyProcessor {
|
||||
try {
|
||||
try {
|
||||
DueyPackage dp = null;
|
||||
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages dp WHERE PackageId = ?")) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages dp WHERE PackageId = ?")) {
|
||||
ps.setInt(1, packageId);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
@@ -451,7 +390,6 @@ public class DueyProcessor {
|
||||
}
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
|
||||
if (dp == null) {
|
||||
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_UNKNOWN_ERROR.getCode()));
|
||||
@@ -473,7 +411,7 @@ public class DueyProcessor {
|
||||
|
||||
if (!MapleInventoryManipulator.checkSpace(c, dpItem.getItemId(), dpItem.getQuantity(), dpItem.getOwner())) {
|
||||
int itemid = dpItem.getItemId();
|
||||
if(MapleItemInformationProvider.getInstance().isPickupRestricted(itemid) && c.getPlayer().getInventory(ItemConstants.getInventoryType(itemid)).findById(itemid) != null) {
|
||||
if (MapleItemInformationProvider.getInstance().isPickupRestricted(itemid) && c.getPlayer().getInventory(ItemConstants.getInventoryType(itemid)).findById(itemid) != null) {
|
||||
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_RECEIVER_WITH_UNIQUE.getCode()));
|
||||
} else {
|
||||
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_NO_FREE_SLOTS.getCode()));
|
||||
@@ -501,7 +439,7 @@ public class DueyProcessor {
|
||||
if (c.tryacquireClient()) {
|
||||
try {
|
||||
long timeNow = System.currentTimeMillis();
|
||||
if(timeNow - c.getPlayer().getNpcCooldown() < YamlConfig.config.server.BLOCK_NPC_RACE_CONDT) {
|
||||
if (timeNow - c.getPlayer().getNpcCooldown() < YamlConfig.config.server.BLOCK_NPC_RACE_CONDT) {
|
||||
c.announce(MaplePacketCreator.enableActions());
|
||||
return;
|
||||
}
|
||||
@@ -526,34 +464,30 @@ public class DueyProcessor {
|
||||
}
|
||||
|
||||
public static void runDueyExpireSchedule() {
|
||||
try {
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.add(Calendar.DATE, -30);
|
||||
|
||||
Timestamp ts = new Timestamp(c.getTime().getTime());
|
||||
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT `PackageId` FROM dueypackages WHERE `TimeStamp` < ?");
|
||||
ps.setTimestamp(1, ts);
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.add(Calendar.DATE, -30);
|
||||
final Timestamp ts = new Timestamp(c.getTime().getTime());
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()){
|
||||
List<Integer> toRemove = new LinkedList<>();
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
toRemove.add(rs.getInt("PackageId"));
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT `PackageId` FROM dueypackages WHERE `TimeStamp` < ?")) {
|
||||
ps.setTimestamp(1, ts);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
toRemove.add(rs.getInt("PackageId"));
|
||||
}
|
||||
}
|
||||
}
|
||||
ps.close();
|
||||
|
||||
for (Integer pid : toRemove) {
|
||||
removePackageFromDB(pid);
|
||||
}
|
||||
|
||||
ps = con.prepareStatement("DELETE FROM dueypackages WHERE `TimeStamp` < ?");
|
||||
ps.setTimestamp(1, ts);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
con.close();
|
||||
try (PreparedStatement ps = con.prepareStatement("DELETE FROM dueypackages WHERE `TimeStamp` < ?")) {
|
||||
ps.setTimestamp(1, ts);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -21,60 +21,41 @@
|
||||
*/
|
||||
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 java.util.Map.Entry;
|
||||
|
||||
import client.*;
|
||||
import client.inventory.*;
|
||||
import client.keybind.MapleKeyBinding;
|
||||
import config.YamlConfig;
|
||||
import constants.game.GameConstants;
|
||||
import constants.game.ScriptableNPCConstants;
|
||||
import net.AbstractMaplePacketHandler;
|
||||
import net.server.PlayerBuffValueHolder;
|
||||
import net.server.Server;
|
||||
import net.server.channel.Channel;
|
||||
import net.server.channel.CharacterIdChannelPair;
|
||||
import net.server.coordinator.session.MapleSessionCoordinator;
|
||||
import net.server.coordinator.world.MapleEventRecallCoordinator;
|
||||
import net.server.guild.MapleAlliance;
|
||||
import net.server.guild.MapleGuild;
|
||||
import net.server.world.MaplePartyCharacter;
|
||||
import net.server.world.PartyOperation;
|
||||
import net.server.world.World;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import scripting.event.EventInstanceManager;
|
||||
import server.life.MobSkill;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.FilePrinter;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.Pair;
|
||||
import tools.data.input.SeekableLittleEndianAccessor;
|
||||
import client.BuddyList;
|
||||
import client.BuddylistEntry;
|
||||
import client.CharacterNameAndId;
|
||||
import client.MapleCharacter;
|
||||
import client.MapleClient;
|
||||
import client.MapleDisease;
|
||||
import client.MapleFamily;
|
||||
import client.MapleFamilyEntry;
|
||||
import client.keybind.MapleKeyBinding;
|
||||
import client.MapleMount;
|
||||
import client.SkillFactory;
|
||||
import client.inventory.Equip;
|
||||
import client.inventory.Item;
|
||||
import client.inventory.MapleInventory;
|
||||
import client.inventory.MapleInventoryType;
|
||||
import client.inventory.MaplePet;
|
||||
import constants.game.GameConstants;
|
||||
import constants.game.ScriptableNPCConstants;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import net.server.coordinator.world.MapleEventRecallCoordinator;
|
||||
import net.server.coordinator.session.MapleSessionCoordinator;
|
||||
import org.apache.mina.core.session.IoSession;
|
||||
import server.life.MobSkill;
|
||||
import scripting.event.EventInstanceManager;
|
||||
import tools.packets.Wedding;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public final class PlayerLoggedinHandler extends AbstractMaplePacketHandler {
|
||||
|
||||
private static Set<Integer> attemptingLoginAccounts = new HashSet<>();
|
||||
@@ -443,48 +424,22 @@ public final class PlayerLoggedinHandler extends AbstractMaplePacketHandler {
|
||||
}
|
||||
|
||||
private static void showDueyNotification(MapleClient c, MapleCharacter player) {
|
||||
Connection con = null;
|
||||
PreparedStatement ps = null;
|
||||
PreparedStatement pss = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
ps = con.prepareStatement("SELECT Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC")) {
|
||||
ps.setInt(1, player.getId());
|
||||
rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
try {
|
||||
Connection con2 = DatabaseConnection.getConnection();
|
||||
pss = con2.prepareStatement("UPDATE dueypackages SET Checked = 0 WHERE ReceiverId = ?");
|
||||
pss.setInt(1, player.getId());
|
||||
pss.executeUpdate();
|
||||
pss.close();
|
||||
con2.close();
|
||||
|
||||
c.announce(MaplePacketCreator.sendDueyParcelNotification(rs.getInt("Type") == 1));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
try (PreparedStatement ps2 = con.prepareStatement("UPDATE dueypackages SET Checked = 0 WHERE ReceiverId = ?")){
|
||||
ps2.setInt(1, player.getId());
|
||||
ps2.executeUpdate();
|
||||
|
||||
c.announce(MaplePacketCreator.sendDueyParcelNotification(rs.getInt("Type") == 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
}
|
||||
if (pss != null) {
|
||||
pss.close();
|
||||
}
|
||||
if (ps != null) {
|
||||
ps.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user