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

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

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

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"))));
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 pNPC from the database."); player.dropMessage(5, "Failed to remove pNPC from the database.");