From 9f7da9a57acc266a177f95c8e8452a20c13a4e7d Mon Sep 17 00:00:00 2001 From: MagicBot Date: Mon, 22 May 2023 06:46:30 -0400 Subject: [PATCH] Refactor to remove abstraction --- src/engine/db/handlers/dbMobBaseHandler.java | 140 ++++++++++--------- 1 file changed, 71 insertions(+), 69 deletions(-) diff --git a/src/engine/db/handlers/dbMobBaseHandler.java b/src/engine/db/handlers/dbMobBaseHandler.java index d5abeaec..531c1415 100644 --- a/src/engine/db/handlers/dbMobBaseHandler.java +++ b/src/engine/db/handlers/dbMobBaseHandler.java @@ -14,7 +14,6 @@ import engine.gameManager.DbManager; import engine.objects.MobBase; import engine.objects.MobBaseEffects; import engine.objects.MobBaseStats; -import engine.objects.MobLootBase; import engine.server.MBServerStatics; import org.pmw.tinylog.Logger; @@ -59,14 +58,28 @@ public class dbMobBaseHandler extends dbHandlerBase { public ArrayList GET_ALL_MOBBASES() { - prepareCallable("SELECT * FROM `static_npc_mobbase`;"); - return getObjectList(); + + ArrayList mobbaseList = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_mobbase`;")) { + + ResultSet rs = preparedStatement.executeQuery(); + mobbaseList = getObjectsFromRs(rs, 1000); + + } catch (SQLException e) { + Logger.error(e); + } + return mobbaseList; } public void SET_AI_DEFAULTS() { - prepareCallable("SELECT * FROM `static_ai_defaults`"); - try { - ResultSet rs = executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_ai_defaults`")) { + + ResultSet rs = preparedStatement.executeQuery(); + while (rs.next()) { MBServerStatics.AI_BASE_AGGRO_RANGE = rs.getInt("aggro_range"); MBServerStatics.AI_PATROL_DIVISOR = rs.getInt("patrol_chance"); @@ -75,109 +88,101 @@ public class dbMobBaseHandler extends dbHandlerBase { MBServerStatics.AI_RECALL_RANGE = rs.getInt("recall_range"); MBServerStatics.AI_PET_HEEL_DISTANCE = rs.getInt("pet_heel_distance"); } - rs.close(); } catch (SQLException e) { - Logger.error( e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } - } public boolean UPDATE_AI_DEFAULTS() { - prepareCallable("UPDATE `static_ai_defaults` SET `aggro_range` = ?,`patrol_chance`= ?,`drop_aggro_range`= ?,`cast_chance`= ?,`recall_range`= ? WHERE `ID` = 1"); - setInt(1, MBServerStatics.AI_BASE_AGGRO_RANGE); - setInt(2, MBServerStatics.AI_PATROL_DIVISOR); - setInt(3, MBServerStatics.AI_DROP_AGGRO_RANGE); - setInt(4, MBServerStatics.AI_POWER_DIVISOR); - setInt(5, MBServerStatics.AI_RECALL_RANGE); - return (executeUpdate() > 0); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_ai_defaults` SET `aggro_range` = ?,`patrol_chance`= ?,`drop_aggro_range`= ?,`cast_chance`= ?,`recall_range`= ? WHERE `ID` = 1")) { + + preparedStatement.setInt(1, MBServerStatics.AI_BASE_AGGRO_RANGE); + preparedStatement.setInt(2, MBServerStatics.AI_PATROL_DIVISOR); + preparedStatement.setInt(3, MBServerStatics.AI_DROP_AGGRO_RANGE); + preparedStatement.setInt(4, MBServerStatics.AI_POWER_DIVISOR); + preparedStatement.setInt(5, MBServerStatics.AI_RECALL_RANGE); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } } public HashMap LOAD_STATIC_POWERS(int mobBaseUUID) { + HashMap powersList = new HashMap<>(); - prepareCallable("SELECT * FROM `static_npc_mobbase_powers` WHERE `mobbaseUUID`=?"); - setInt(1, mobBaseUUID); - try { - ResultSet rs = executeQuery(); - while (rs.next()) { + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_mobbase_powers` WHERE `mobbaseUUID`=?")) { + + preparedStatement.setInt(1, mobBaseUUID); + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) powersList.put(rs.getInt("token"), rs.getInt("rank")); - } - rs.close(); + } catch (SQLException e) { - Logger.error( e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } return powersList; - } public ArrayList GET_RUNEBASE_EFFECTS(int runeID) { + ArrayList effectsList = new ArrayList<>(); - prepareCallable("SELECT * FROM `static_npc_mobbase_effects` WHERE `mobbaseUUID` = ?"); - setInt(1, runeID); - try { - ResultSet rs = executeQuery(); - while (rs.next()) { + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_mobbase_effects` WHERE `mobbaseUUID` = ?")) { + preparedStatement.setInt(1, runeID); + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { MobBaseEffects mbs = new MobBaseEffects(rs); effectsList.add(mbs); } - rs.close(); + } catch (SQLException e) { - Logger.error (e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } return effectsList; - } public MobBaseStats LOAD_STATS(int mobBaseUUID) { - MobBaseStats mbs = MobBaseStats.GetGenericStats(); - prepareCallable("SELECT * FROM `static_npc_mobbase_stats` WHERE `mobbaseUUID` = ?"); - setInt(1, mobBaseUUID); - try { - ResultSet rs = executeQuery(); - while (rs.next()) { + MobBaseStats mobBaseStats = MobBaseStats.GetGenericStats(); - mbs = new MobBaseStats(rs); - } + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_mobbase_stats` WHERE `mobbaseUUID` = ?")) { - } catch (SQLException e) { - Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); - } - return mbs; + preparedStatement.setInt(1, mobBaseUUID); + ResultSet rs = preparedStatement.executeQuery(); - } + while (rs.next()) + mobBaseStats = new MobBaseStats(rs); - public boolean RENAME_MOBBASE(int ID, String newName) { - prepareCallable("UPDATE `static_npc_mobbase` SET `name`=? WHERE `ID`=?;"); - setString(1, newName); - setInt(2, ID); - return (executeUpdate() > 0); + } catch (SQLException e) { + Logger.error(e); + } + return mobBaseStats; } public void LOAD_ALL_MOBBASE_SPEEDS(MobBase mobBase) { if (mobBase.getLoadID() == 0) return; - ArrayList mobLootList = new ArrayList<>(); - prepareCallable("SELECT * FROM `static_npc_mobbase_race` WHERE `mobbaseID` = ?"); - setInt(1, mobBase.getLoadID()); - try { - ResultSet rs = executeQuery(); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_mobbase_race` WHERE `mobbaseID` = ?")) { + + preparedStatement.setInt(1, mobBase.getLoadID()); + ResultSet rs = preparedStatement.executeQuery(); - //shrines cached in rs for easy cache on creation. while (rs.next()) { float walk = rs.getFloat("walkStandard"); float walkCombat = rs.getFloat("walkCombat"); @@ -186,11 +191,8 @@ public class dbMobBaseHandler extends dbHandlerBase { mobBase.updateSpeeds(walk, walkCombat, run, runCombat); } - } catch (SQLException e) { - Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); + Logger.error(e); } } }