refactor: use try-with-resources for db operations in various handlers

This commit is contained in:
P0nk
2021-04-05 00:11:42 +02:00
parent 429f82e4fe
commit e13c313839
5 changed files with 222 additions and 229 deletions

View File

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

View File

@@ -21,16 +21,16 @@
*/ */
package net.server.channel.handlers; package net.server.channel.handlers;
import java.sql.PreparedStatement; import client.MapleClient;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.AbstractMaplePacketHandler; import net.AbstractMaplePacketHandler;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import tools.data.input.SeekableLittleEndianAccessor; import tools.data.input.SeekableLittleEndianAccessor;
import client.MapleClient;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public final class NoteActionHandler extends AbstractMaplePacketHandler { public final class NoteActionHandler extends AbstractMaplePacketHandler {
@Override @Override
@@ -40,11 +40,12 @@ public final class NoteActionHandler extends AbstractMaplePacketHandler {
String charname = slea.readMapleAsciiString(); String charname = slea.readMapleAsciiString();
String message = slea.readMapleAsciiString(); String message = slea.readMapleAsciiString();
try { try {
if (c.getPlayer().getCashShop().isOpened()) if (c.getPlayer().getCashShop().isOpened()) {
c.announce(MaplePacketCreator.showCashInventory(c)); c.announce(MaplePacketCreator.showCashInventory(c));
}
c.getPlayer().sendNote(charname, message, (byte) 1);
c.getPlayer().getCashShop().decreaseNotes(); c.getPlayer().sendNote(charname, message, (byte) 1);
c.getPlayer().getCashShop().decreaseNotes();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -56,21 +57,22 @@ public final class NoteActionHandler extends AbstractMaplePacketHandler {
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
int id = slea.readInt(); int id = slea.readInt();
slea.readByte(); //Fame, but we read it from the database :) slea.readByte(); //Fame, but we read it from the database :)
PreparedStatement ps;
try {
Connection con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `fame` FROM notes WHERE id=? AND deleted=0");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next())
fame += rs.getInt("fame");
rs.close();
ps = con.prepareStatement("UPDATE notes SET `deleted` = 1 WHERE id = ?"); try (Connection con = DatabaseConnection.getConnection()) {
ps.setInt(1, id); try (PreparedStatement ps = con.prepareStatement("SELECT `fame` FROM notes WHERE id=? AND deleted=0")) {
ps.executeUpdate(); ps.setInt(1, id);
ps.close(); try (ResultSet rs = ps.executeQuery()) {
con.close(); if (rs.next()) {
fame += rs.getInt("fame");
}
}
}
try (PreparedStatement ps = con.prepareStatement("UPDATE notes SET `deleted` = 1 WHERE id = ?")) {
ps.setInt(1, id);
ps.executeUpdate();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -21,19 +21,19 @@
*/ */
package net.server.channel.handlers; package net.server.channel.handlers;
import java.sql.Connection; import client.MapleCharacter;
import java.sql.PreparedStatement; import client.MapleClient;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import net.AbstractMaplePacketHandler; import net.AbstractMaplePacketHandler;
import net.server.Server; import net.server.Server;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import tools.data.input.SeekableLittleEndianAccessor; import tools.data.input.SeekableLittleEndianAccessor;
import client.MapleCharacter;
import client.MapleClient; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
/* /*
* *
@@ -84,10 +84,8 @@ public final class ReportHandler extends AbstractMaplePacketHandler {
public void addReport(int reporterid, int victimid, int reason, String description, String chatlog) { public void addReport(int reporterid, int victimid, int reason, String description, String chatlog) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp currentTimestamp = new java.sql.Timestamp(calendar.getTime().getTime()); Timestamp currentTimestamp = new java.sql.Timestamp(calendar.getTime().getTime());
Connection con; try (Connection con = DatabaseConnection.getConnection();
try { PreparedStatement ps = con.prepareStatement("INSERT INTO reports (`reporttime`, `reporterid`, `victimid`, `reason`, `chatlog`, `description`) VALUES (?, ?, ?, ?, ?, ?)")) {
con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO reports (`reporttime`, `reporterid`, `victimid`, `reason`, `chatlog`, `description`) VALUES (?, ?, ?, ?, ?, ?)");
ps.setString(1, currentTimestamp.toGMTString().toString()); ps.setString(1, currentTimestamp.toGMTString().toString());
ps.setInt(2, reporterid); ps.setInt(2, reporterid);
ps.setInt(3, victimid); ps.setInt(3, victimid);
@@ -96,8 +94,6 @@ public final class ReportHandler extends AbstractMaplePacketHandler {
ps.setString(6, description); ps.setString(6, description);
ps.addBatch(); ps.addBatch();
ps.executeBatch(); ps.executeBatch();
ps.close();
con.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@@ -21,10 +21,9 @@
*/ */
package net.server.channel.handlers; package net.server.channel.handlers;
import java.sql.PreparedStatement; import client.MapleCharacter;
import java.sql.ResultSet; import client.MapleClient;
import java.sql.SQLException; import client.autoban.AutobanFactory;
import config.YamlConfig; import config.YamlConfig;
import net.AbstractMaplePacketHandler; import net.AbstractMaplePacketHandler;
import net.server.world.World; import net.server.world.World;
@@ -33,10 +32,11 @@ import tools.FilePrinter;
import tools.LogHelper; import tools.LogHelper;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import tools.data.input.SeekableLittleEndianAccessor; import tools.data.input.SeekableLittleEndianAccessor;
import client.MapleCharacter;
import client.MapleClient;
import client.autoban.AutobanFactory;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/** /**
* *
@@ -101,20 +101,18 @@ public final class WhisperHandler extends AbstractMaplePacketHandler {
c.announce(MaplePacketCreator.getFindReply(victim.getName(), victim.getMap().getId(), 1)); c.announce(MaplePacketCreator.getFindReply(victim.getName(), victim.getMap().getId(), 1));
} }
} else if (c.getPlayer().isGM()) { // not found } else if (c.getPlayer().isGM()) { // not found
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT gm FROM characters WHERE name = ?")) {
PreparedStatement ps = con.prepareStatement("SELECT gm FROM characters WHERE name = ?");
ps.setString(1, recipient); ps.setString(1, recipient);
ResultSet rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
if (rs.getInt("gm") >= c.getPlayer().gmLevel()) { if (rs.getInt("gm") >= c.getPlayer().gmLevel()) {
c.announce(MaplePacketCreator.getWhisperReply(recipient, (byte) 0)); c.announce(MaplePacketCreator.getWhisperReply(recipient, (byte) 0));
return; return;
}
} }
} }
rs.close();
ps.close();
con.close();
byte channel = (byte) (c.getWorldServer().find(recipient) - 1); byte channel = (byte) (c.getWorldServer().find(recipient) - 1);
if (channel > -1) { if (channel > -1) {
c.announce(MaplePacketCreator.getFindReply(recipient, channel, 3)); c.announce(MaplePacketCreator.getFindReply(recipient, channel, 3));

View File

@@ -21,11 +21,6 @@
*/ */
package net.server.handlers.login; package net.server.handlers.login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import client.MapleClient; import client.MapleClient;
import client.MapleFamily; import client.MapleFamily;
import net.AbstractMaplePacketHandler; import net.AbstractMaplePacketHandler;
@@ -35,55 +30,65 @@ import tools.FilePrinter;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import tools.data.input.SeekableLittleEndianAccessor; import tools.data.input.SeekableLittleEndianAccessor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public final class DeleteCharHandler extends AbstractMaplePacketHandler { public final class DeleteCharHandler extends AbstractMaplePacketHandler {
@Override @Override
public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) { public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
String pic = slea.readMapleAsciiString(); String pic = slea.readMapleAsciiString();
int cid = slea.readInt(); int cid = slea.readInt();
if (c.checkPic(pic)) { if (c.checkPic(pic)) {
//check for family, guild leader, pending marriage, world transfer //check for family, guild leader, pending marriage, world transfer
try (Connection con = DatabaseConnection.getConnection(); try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT `world`, `guildid`, `guildrank`, `familyId` FROM characters WHERE id = ?"); PreparedStatement ps = con.prepareStatement("SELECT `world`, `guildid`, `guildrank`, `familyId` FROM characters WHERE id = ?");
PreparedStatement ps2 = con.prepareStatement("SELECT COUNT(*) as rowcount FROM worldtransfers WHERE `characterid` = ? AND completionTime IS NULL")) { PreparedStatement ps2 = con.prepareStatement("SELECT COUNT(*) as rowcount FROM worldtransfers WHERE `characterid` = ? AND completionTime IS NULL")) {
ps.setInt(1, cid); ps.setInt(1, cid);
ResultSet rs = ps.executeQuery();
if(!rs.next()) throw new SQLException("Character record does not exist."); try (ResultSet rs = ps.executeQuery()) {
int world = rs.getInt("world"); if (!rs.next()) {
int guildId = rs.getInt("guildid"); throw new SQLException("Character record does not exist.");
int guildRank = rs.getInt("guildrank"); }
int familyId = rs.getInt("familyId"); int world = rs.getInt("world");
if(guildId != 0 && guildRank <= 1) { int guildId = rs.getInt("guildid");
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x16)); int guildRank = rs.getInt("guildrank");
return; int familyId = rs.getInt("familyId");
} else if(familyId != -1) { if (guildId != 0 && guildRank <= 1) {
MapleFamily family = Server.getInstance().getWorld(world).getFamily(familyId); c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x16));
if(family != null && family.getTotalMembers() > 1) { return;
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1D)); } else if (familyId != -1) {
return; MapleFamily family = Server.getInstance().getWorld(world).getFamily(familyId);
} if (family != null && family.getTotalMembers() > 1) {
} c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1D));
rs.close(); return;
ps2.setInt(1, cid); }
rs = ps2.executeQuery(); }
rs.next(); }
if(rs.getInt("rowcount") > 0) {
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1A)); ps2.setInt(1, cid);
return; try (ResultSet rs = ps2.executeQuery()) {
} rs.next();
} catch(SQLException e) { if (rs.getInt("rowcount") > 0) {
e.printStackTrace(); c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x1A));
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09)); return;
return; }
} }
if(c.deleteCharacter(cid, c.getAccID())) { } catch (SQLException e) {
FilePrinter.print(FilePrinter.DELETED_CHAR + c.getAccountName() + ".txt", c.getAccountName() + " deleted CID: " + cid); e.printStackTrace();
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0)); c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09));
} else { return;
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09)); }
} if (c.deleteCharacter(cid, c.getAccID())) {
} else { FilePrinter.print(FilePrinter.DELETED_CHAR + c.getAccountName() + ".txt", c.getAccountName() + " deleted CID: " + cid);
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x14)); c.announce(MaplePacketCreator.deleteCharResponse(cid, 0));
} } else {
} c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x09));
}
} else {
c.announce(MaplePacketCreator.deleteCharResponse(cid, 0x14));
}
}
} }