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,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));