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("");
@@ -54,12 +54,12 @@ public class PmobCommand extends Command {
int mapId = player.getMapId();
int mobId = Integer.parseInt(params[0]);
int mobTime = (params.length > 1) ? Integer.parseInt(params[1]) : -1;
Point checkpos = player.getMap().getGroundBelow(player.getPosition());
int xpos = checkpos.x;
int ypos = checkpos.y;
int fh = player.getMap().getFootholds().findBelow(checkpos).getId();
MapleMonster mob = MapleLifeFactory.getMonster(mobId);
if (mob != null && !mob.getName().equals("MISSINGNO")) {
mob.setPosition(checkpos);
@@ -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,15 +83,13 @@ 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);
}
player.yellowMessage("Pmob created.");
} catch (SQLException e) {
e.printStackTrace();

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("");
@@ -47,19 +47,17 @@ public class PmobRemoveCommand extends Command {
@Override
public void execute(MapleClient c, String[] params) {
MapleCharacter player = c.getPlayer();
int mapId = player.getMapId();
int mobId = params.length > 0 ? Integer.parseInt(params[0]) : -1;
Point pos = player.getPosition();
int xpos = pos.x;
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);
@@ -78,28 +76,26 @@ public class PmobRemoveCommand extends Command {
ps.setInt(6, ypos - 50);
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) {
@@ -108,7 +104,7 @@ public class PmobRemoveCommand extends Command {
}
}
}
player.yellowMessage("Cleared " + toRemove.size() + " pmob placements.");
}
}

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("");
@@ -50,7 +50,7 @@ public class PnpcCommand extends Command {
player.yellowMessage("Syntax: !pnpc <npcid>");
return;
}
// command suggestion thanks to HighKey21, none, bibiko94 (TAYAMO), asafgb
int mapId = player.getMapId();
int npcId = Integer.parseInt(params[0]);
@@ -58,18 +58,17 @@ public class PnpcCommand extends Command {
player.dropMessage(5, "This map already contains the specified NPC.");
return;
}
MapleNPC npc = MapleLifeFactory.getNPC(npcId);
Point checkpos = player.getMap().getGroundBelow(player.getPosition());
int xpos = checkpos.x;
int ypos = checkpos.y;
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,17 +83,15 @@ 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);
npc.setRx0(xpos + 50);
npc.setRx1(xpos - 50);
npc.setFh(fh);
MapleMap map = ch.getMapFactory().getMap(mapId);
map.addMapObject(npc);
map.broadcastMessage(MaplePacketCreator.spawnNPC(npc));

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.");