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;
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.PreparedStatement;
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 {
{
setDescription("");
@@ -67,9 +67,8 @@ public class PmobCommand extends Command {
mob.setRx0(xpos + 50);
mob.setRx1(xpos - 50);
mob.setFh(fh);
try {
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 ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
try (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 ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )")) {
ps.setInt(1, mobId);
ps.setInt(2, 0);
ps.setInt(3, fh);
@@ -84,10 +83,8 @@ public class PmobCommand extends Command {
ps.setInt(12, mobTime);
ps.setInt(13, 0);
ps.executeUpdate();
ps.close();
con.close();
for (Channel ch: player.getWorldServer().getChannels()) {
for (Channel ch : player.getWorldServer().getChannels()) {
MapleMap map = ch.getMapFactory().getMap(mapId);
map.addMonsterSpawn(mob, mobTime, -1);
map.addAllMonsterSpawn(mob, mobTime, -1);

View File

@@ -23,6 +23,15 @@
*/
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.PreparedStatement;
import java.sql.ResultSet;
@@ -30,15 +39,6 @@ import java.sql.SQLException;
import java.util.LinkedList;
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 {
{
setDescription("");
@@ -56,10 +56,8 @@ public class PmobRemoveCommand extends Command {
int ypos = pos.y;
List<Pair<Integer, Pair<Integer, Integer>>> toRemove = new LinkedList<>();
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps;
try (Connection con = DatabaseConnection.getConnection()) {
final PreparedStatement ps;
if (mobId > -1) {
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);
@@ -79,27 +77,25 @@ public class PmobRemoveCommand extends Command {
ps.setInt(7, ypos + 50);
}
ResultSet rs = ps.executeQuery();
while (true) {
rs.beforeFirst();
if (!rs.next()) {
break;
try (ResultSet rs = ps.executeQuery()) {
while (true) {
rs.beforeFirst();
if (!rs.next()) {
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();
con.close();
} catch (SQLException e) {
e.printStackTrace();
player.dropMessage(5, "Failed to remove pmob from the database.");
}
if (!toRemove.isEmpty()) {
for (Channel ch: player.getWorldServer().getChannels()) {
for (Channel ch : player.getWorldServer().getChannels()) {
MapleMap map = ch.getMapFactory().getMap(mapId);
for (Pair<Integer, Pair<Integer, Integer>> r : toRemove) {

View File

@@ -23,21 +23,21 @@
*/
package client.command.commands.gm4;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import client.MapleCharacter;
import client.MapleClient;
import client.command.Command;
import net.server.channel.Channel;
import server.life.MapleLifeFactory;
import server.life.MapleNPC;
import client.command.Command;
import client.MapleCharacter;
import client.MapleClient;
import java.awt.Point;
import server.maps.MapleMap;
import tools.DatabaseConnection;
import tools.MaplePacketCreator;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class PnpcCommand extends Command {
{
setDescription("");
@@ -67,9 +67,8 @@ public class PnpcCommand extends Command {
int fh = player.getMap().getFootholds().findBelow(checkpos).getId();
if (npc != null && !npc.getName().equals("MISSINGNO")) {
try {
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 ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
try (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 ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )")) {
ps.setInt(1, npcId);
ps.setInt(2, 0);
ps.setInt(3, fh);
@@ -84,10 +83,8 @@ public class PnpcCommand extends Command {
ps.setInt(12, -1);
ps.setInt(13, 0);
ps.executeUpdate();
ps.close();
con.close();
for (Channel ch: player.getWorldServer().getChannels()) {
for (Channel ch : player.getWorldServer().getChannels()) {
npc = MapleLifeFactory.getNPC(npcId);
npc.setPosition(checkpos);
npc.setCy(ypos);

View File

@@ -23,22 +23,22 @@
*/
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.MapleClient;
import java.awt.Point;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class PnpcRemoveCommand extends Command {
{
setDescription("");
@@ -56,10 +56,8 @@ public class PnpcRemoveCommand extends Command {
int ypos = pos.y;
List<Pair<Integer, Pair<Integer, Integer>>> toRemove = new LinkedList<>();
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps;
try (Connection con = DatabaseConnection.getConnection()) {
final PreparedStatement ps;
if (npcId > -1) {
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);
@@ -79,20 +77,19 @@ public class PnpcRemoveCommand extends Command {
ps.setInt(7, ypos + 50);
}
ResultSet rs = ps.executeQuery();
while (true) {
rs.beforeFirst();
if (!rs.next()) {
break;
}
try (ResultSet rs = ps.executeQuery()) {
while (true) {
rs.beforeFirst();
if (!rs.next()) {
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();
con.close();
} catch (SQLException e) {
e.printStackTrace();
player.dropMessage(5, "Failed to remove pNPC from the database.");