refactor: use try-with-resources for perma npc/mob db operations

This commit is contained in:
P0nk
2021-04-04 23:34:35 +02:00
parent 59014b86bf
commit 6b6558af01
4 changed files with 83 additions and 96 deletions

View File

@@ -23,20 +23,20 @@
*/ */
package client.command.commands.gm4; package client.command.commands.gm4;
import client.MapleCharacter;
import client.MapleClient;
import client.command.Command;
import net.server.channel.Channel;
import server.life.MapleLifeFactory;
import server.life.MapleMonster;
import server.maps.MapleMap;
import tools.DatabaseConnection;
import java.awt.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import net.server.channel.Channel;
import server.life.MapleLifeFactory;
import server.maps.MapleMap;
import client.command.Command;
import client.MapleCharacter;
import client.MapleClient;
import java.awt.Point;
import server.life.MapleMonster;
import tools.DatabaseConnection;
public class PmobCommand extends Command { public class PmobCommand extends Command {
{ {
setDescription(""); setDescription("");
@@ -67,9 +67,8 @@ public class PmobCommand extends Command {
mob.setRx0(xpos + 50); mob.setRx0(xpos + 50);
mob.setRx1(xpos - 50); mob.setRx1(xpos - 50);
mob.setFh(fh); mob.setFh(fh);
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO plife ( life, f, fh, cy, rx0, rx1, type, x, y, world, map, mobtime, hide ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )")) {
PreparedStatement ps = con.prepareStatement("INSERT INTO plife ( life, f, fh, cy, rx0, rx1, type, x, y, world, map, mobtime, hide ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
ps.setInt(1, mobId); ps.setInt(1, mobId);
ps.setInt(2, 0); ps.setInt(2, 0);
ps.setInt(3, fh); ps.setInt(3, fh);
@@ -84,10 +83,8 @@ public class PmobCommand extends Command {
ps.setInt(12, mobTime); ps.setInt(12, mobTime);
ps.setInt(13, 0); ps.setInt(13, 0);
ps.executeUpdate(); ps.executeUpdate();
ps.close();
con.close();
for (Channel ch: player.getWorldServer().getChannels()) { for (Channel ch : player.getWorldServer().getChannels()) {
MapleMap map = ch.getMapFactory().getMap(mapId); MapleMap map = ch.getMapFactory().getMap(mapId);
map.addMonsterSpawn(mob, mobTime, -1); map.addMonsterSpawn(mob, mobTime, -1);
map.addAllMonsterSpawn(mob, mobTime, -1); map.addAllMonsterSpawn(mob, mobTime, -1);

View File

@@ -23,6 +23,15 @@
*/ */
package client.command.commands.gm4; package client.command.commands.gm4;
import client.MapleCharacter;
import client.MapleClient;
import client.command.Command;
import net.server.channel.Channel;
import server.maps.MapleMap;
import tools.DatabaseConnection;
import tools.Pair;
import java.awt.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@@ -30,15 +39,6 @@ import java.sql.SQLException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import net.server.channel.Channel;
import server.maps.MapleMap;
import client.command.Command;
import client.MapleCharacter;
import client.MapleClient;
import java.awt.Point;
import tools.DatabaseConnection;
import tools.Pair;
public class PmobRemoveCommand extends Command { public class PmobRemoveCommand extends Command {
{ {
setDescription(""); setDescription("");
@@ -56,10 +56,8 @@ public class PmobRemoveCommand extends Command {
int ypos = pos.y; int ypos = pos.y;
List<Pair<Integer, Pair<Integer, Integer>>> toRemove = new LinkedList<>(); List<Pair<Integer, Pair<Integer, Integer>>> toRemove = new LinkedList<>();
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection(); final PreparedStatement ps;
PreparedStatement ps;
if (mobId > -1) { if (mobId > -1) {
String select = "SELECT * FROM plife WHERE world = ? AND map = ? AND type LIKE ? AND life = ?"; String select = "SELECT * FROM plife WHERE world = ? AND map = ? AND type LIKE ? AND life = ?";
ps = con.prepareStatement(select, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ps = con.prepareStatement(select, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
@@ -79,27 +77,25 @@ public class PmobRemoveCommand extends Command {
ps.setInt(7, ypos + 50); ps.setInt(7, ypos + 50);
} }
ResultSet rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
while (true) { while (true) {
rs.beforeFirst(); rs.beforeFirst();
if (!rs.next()) { if (!rs.next()) {
break; break;
}
toRemove.add(new Pair<>(rs.getInt("life"), new Pair<>(rs.getInt("x"), rs.getInt("y"))));
rs.deleteRow();
} }
toRemove.add(new Pair<>(rs.getInt("life"), new Pair<>(rs.getInt("x"), rs.getInt("y"))));
rs.deleteRow();
} }
rs.close();
ps.close(); ps.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
player.dropMessage(5, "Failed to remove pmob from the database."); player.dropMessage(5, "Failed to remove pmob from the database.");
} }
if (!toRemove.isEmpty()) { if (!toRemove.isEmpty()) {
for (Channel ch: player.getWorldServer().getChannels()) { for (Channel ch : player.getWorldServer().getChannels()) {
MapleMap map = ch.getMapFactory().getMap(mapId); MapleMap map = ch.getMapFactory().getMap(mapId);
for (Pair<Integer, Pair<Integer, Integer>> r : toRemove) { for (Pair<Integer, Pair<Integer, Integer>> r : toRemove) {

View File

@@ -23,21 +23,21 @@
*/ */
package client.command.commands.gm4; package client.command.commands.gm4;
import java.sql.Connection; import client.MapleCharacter;
import java.sql.PreparedStatement; import client.MapleClient;
import java.sql.SQLException; import client.command.Command;
import net.server.channel.Channel; import net.server.channel.Channel;
import server.life.MapleLifeFactory; import server.life.MapleLifeFactory;
import server.life.MapleNPC; import server.life.MapleNPC;
import client.command.Command;
import client.MapleCharacter;
import client.MapleClient;
import java.awt.Point;
import server.maps.MapleMap; import server.maps.MapleMap;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class PnpcCommand extends Command { public class PnpcCommand extends Command {
{ {
setDescription(""); setDescription("");
@@ -67,9 +67,8 @@ public class PnpcCommand extends Command {
int fh = player.getMap().getFootholds().findBelow(checkpos).getId(); int fh = player.getMap().getFootholds().findBelow(checkpos).getId();
if (npc != null && !npc.getName().equals("MISSINGNO")) { if (npc != null && !npc.getName().equals("MISSINGNO")) {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO plife ( life, f, fh, cy, rx0, rx1, type, x, y, world, map, mobtime, hide ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )")) {
PreparedStatement ps = con.prepareStatement("INSERT INTO plife ( life, f, fh, cy, rx0, rx1, type, x, y, world, map, mobtime, hide ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
ps.setInt(1, npcId); ps.setInt(1, npcId);
ps.setInt(2, 0); ps.setInt(2, 0);
ps.setInt(3, fh); ps.setInt(3, fh);
@@ -84,10 +83,8 @@ public class PnpcCommand extends Command {
ps.setInt(12, -1); ps.setInt(12, -1);
ps.setInt(13, 0); ps.setInt(13, 0);
ps.executeUpdate(); ps.executeUpdate();
ps.close();
con.close();
for (Channel ch: player.getWorldServer().getChannels()) { for (Channel ch : player.getWorldServer().getChannels()) {
npc = MapleLifeFactory.getNPC(npcId); npc = MapleLifeFactory.getNPC(npcId);
npc.setPosition(checkpos); npc.setPosition(checkpos);
npc.setCy(ypos); npc.setCy(ypos);

View File

@@ -23,22 +23,22 @@
*/ */
package client.command.commands.gm4; package client.command.commands.gm4;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import net.server.channel.Channel;
import client.command.Command;
import client.MapleCharacter; import client.MapleCharacter;
import client.MapleClient; import client.MapleClient;
import java.awt.Point; import client.command.Command;
import java.sql.ResultSet; import net.server.channel.Channel;
import java.util.LinkedList;
import java.util.List;
import server.maps.MapleMap; import server.maps.MapleMap;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.Pair; import tools.Pair;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class PnpcRemoveCommand extends Command { public class PnpcRemoveCommand extends Command {
{ {
setDescription(""); setDescription("");
@@ -56,10 +56,8 @@ public class PnpcRemoveCommand extends Command {
int ypos = pos.y; int ypos = pos.y;
List<Pair<Integer, Pair<Integer, Integer>>> toRemove = new LinkedList<>(); List<Pair<Integer, Pair<Integer, Integer>>> toRemove = new LinkedList<>();
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection(); final PreparedStatement ps;
PreparedStatement ps;
if (npcId > -1) { if (npcId > -1) {
String select = "SELECT * FROM plife WHERE world = ? AND map = ? AND type LIKE ? AND life = ?"; String select = "SELECT * FROM plife WHERE world = ? AND map = ? AND type LIKE ? AND life = ?";
ps = con.prepareStatement(select, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ps = con.prepareStatement(select, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
@@ -79,20 +77,19 @@ public class PnpcRemoveCommand extends Command {
ps.setInt(7, ypos + 50); ps.setInt(7, ypos + 50);
} }
ResultSet rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
while (true) { while (true) {
rs.beforeFirst(); rs.beforeFirst();
if (!rs.next()) { if (!rs.next()) {
break; break;
} }
toRemove.add(new Pair<>(rs.getInt("life"), new Pair<>(rs.getInt("x"), rs.getInt("y")))); toRemove.add(new Pair<>(rs.getInt("life"), new Pair<>(rs.getInt("x"), rs.getInt("y"))));
rs.deleteRow(); rs.deleteRow();
}
} }
rs.close();
ps.close(); ps.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
player.dropMessage(5, "Failed to remove pNPC from the database."); player.dropMessage(5, "Failed to remove pNPC from the database.");