From e4ef0021e80e76d411d41a9746076d7d0ba333ee Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 21 May 2023 08:36:39 -0400 Subject: [PATCH] Refactor to remove abstraction --- .../handlers/dbBuildingLocationHandler.java | 30 ++++-- .../db/handlers/dbHeightMapHandler.java | 48 ++++----- src/engine/db/handlers/dbItemBaseHandler.java | 100 ++++++++---------- 3 files changed, 88 insertions(+), 90 deletions(-) diff --git a/src/engine/db/handlers/dbBuildingLocationHandler.java b/src/engine/db/handlers/dbBuildingLocationHandler.java index 7a24bfcc..bd89fad0 100644 --- a/src/engine/db/handlers/dbBuildingLocationHandler.java +++ b/src/engine/db/handlers/dbBuildingLocationHandler.java @@ -9,8 +9,14 @@ package engine.db.handlers; +import engine.gameManager.DbManager; import engine.objects.BuildingLocation; +import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; public class dbBuildingLocationHandler extends dbHandlerBase { @@ -21,11 +27,23 @@ public class dbBuildingLocationHandler extends dbHandlerBase { } public ArrayList LOAD_BUILDING_LOCATIONS() { - prepareCallable("select * from static_building_location " + - "where type = 6 or type = 8 " + - "GROUP BY buildingID, slot " + - "ORDER BY buildingID, slot ASC;"); - return getObjectList(); - } + ArrayList buildingLocations = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("select * from static_building_location " + + "where type = 6 or type = 8 " + + "GROUP BY buildingID, slot " + + "ORDER BY buildingID, slot ASC;")) { + + ResultSet rs = preparedStatement.executeQuery(); + buildingLocations = getObjectsFromRs(rs, 20); + + } catch (SQLException e) { + Logger.error(e); + return buildingLocations; + } + + return buildingLocations; + } } diff --git a/src/engine/db/handlers/dbHeightMapHandler.java b/src/engine/db/handlers/dbHeightMapHandler.java index 1ea5d54c..eec0bfbc 100644 --- a/src/engine/db/handlers/dbHeightMapHandler.java +++ b/src/engine/db/handlers/dbHeightMapHandler.java @@ -1,47 +1,43 @@ package engine.db.handlers; import engine.InterestManagement.HeightMap; +import engine.gameManager.DbManager; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class dbHeightMapHandler extends dbHandlerBase { - public dbHeightMapHandler() { + public dbHeightMapHandler() { - } + } - public void LOAD_ALL_HEIGHTMAPS() { + public void LOAD_ALL_HEIGHTMAPS() { - HeightMap thisHeightmap; + HeightMap thisHeightmap; + HeightMap.heightMapsCreated = 0; - int recordsRead = 0; - int worthlessDupes = 0; + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_zone_heightmap INNER JOIN static_zone_size ON static_zone_size.loadNum = static_zone_heightmap.zoneLoadID")) { - HeightMap.heightMapsCreated = 0; + ResultSet rs = preparedStatement.executeQuery(); - prepareCallable("SELECT * FROM static_zone_heightmap INNER JOIN static_zone_size ON static_zone_size.loadNum = static_zone_heightmap.zoneLoadID"); + while (rs.next()) { + thisHeightmap = new HeightMap(rs); - try { - ResultSet rs = executeQuery(); + if (thisHeightmap.getHeightmapImage() == null) { + Logger.info("Imagemap for " + thisHeightmap.getHeightMapID() + " was null"); + continue; + } + } - while (rs.next()) { - - recordsRead++; - thisHeightmap = new HeightMap(rs); - - if (thisHeightmap.getHeightmapImage() == null) { - Logger.info( "Imagemap for " + thisHeightmap.getHeightMapID() + " was null"); - continue; - } - } - } catch (SQLException e) { - Logger.error("LoadAllHeightMaps: " + e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); - } - } + } catch (SQLException e) { + Logger.error(e); + } + } } diff --git a/src/engine/db/handlers/dbItemBaseHandler.java b/src/engine/db/handlers/dbItemBaseHandler.java index 0b6911d6..a2f6b793 100644 --- a/src/engine/db/handlers/dbItemBaseHandler.java +++ b/src/engine/db/handlers/dbItemBaseHandler.java @@ -9,10 +9,13 @@ package engine.db.handlers; +import engine.gameManager.DbManager; import engine.objects.BootySetEntry; import engine.objects.ItemBase; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -26,23 +29,20 @@ public class dbItemBaseHandler extends dbHandlerBase { public void LOAD_BAKEDINSTATS(ItemBase itemBase) { - try { - prepareCallable("SELECT * FROM `static_item_bakedinstat` WHERE `itemID` = ?"); - setInt(1, itemBase.getUUID()); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_item_bakedinstat` WHERE `itemID` = ?")) { - ResultSet rs = executeQuery(); + preparedStatement.setInt(1, itemBase.getUUID()); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { - if (rs.getBoolean("fromUse")) itemBase.getUsedStats().put(rs.getInt("token"), rs.getInt("numTrains")); else itemBase.getBakedInStats().put(rs.getInt("token"), rs.getInt("numTrains")); } } catch (SQLException e) { - Logger.error( e.toString()); - } finally { - closeCallable(); + Logger.error(e); } } @@ -50,27 +50,24 @@ public class dbItemBaseHandler extends dbHandlerBase { ArrayList tempList = new ArrayList<>(); ArrayList tempListOff = new ArrayList<>(); - try { - prepareCallable("SELECT * FROM `static_itembase_animations` WHERE `itemBaseUUID` = ?"); - setInt(1, itemBase.getUUID()); - ResultSet rs = executeQuery(); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_itembase_animations` WHERE `itemBaseUUID` = ?")) { + + preparedStatement.setInt(1, itemBase.getUUID()); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { int animation = rs.getInt("animation"); - boolean rightHand = rs.getBoolean("rightHand"); if (rightHand) tempList.add(animation); else tempListOff.add(animation); - } } catch (SQLException e) { - Logger.error( e.toString()); - } finally { - closeCallable(); + Logger.error(e); } itemBase.setAnimations(tempList); @@ -80,44 +77,37 @@ public class dbItemBaseHandler extends dbHandlerBase { public void LOAD_ALL_ITEMBASES() { ItemBase itemBase; - int recordsRead = 0; - prepareCallable("SELECT * FROM static_itembase"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_itembase")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { - recordsRead++; itemBase = new ItemBase(rs); - - // Add ItemBase to internal cache - ItemBase.addToCache(itemBase); } - Logger.info( "read: " + recordsRead + "cached: " + ItemBase.getUUIDCache().size()); - } catch (SQLException e) { - Logger.error( e.toString()); - } finally { - closeCallable(); + Logger.error(e); } + + Logger.info("read: " + recordsRead + "cached: " + ItemBase.getUUIDCache().size()); } public HashMap> LOAD_RUNES_FOR_NPC_AND_MOBS() { HashMap> runeSets = new HashMap<>(); - int runeSetID; + int runeSetID; int runeBaseID; int recordsRead = 0; - prepareCallable("SELECT * FROM static_npc_runeSet"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_npc_runeSet")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -126,25 +116,23 @@ public class dbItemBaseHandler extends dbHandlerBase { runeSetID = rs.getInt("runeSet"); runeBaseID = rs.getInt("runeBase"); - if (runeSets.get(runeSetID) == null){ + if (runeSets.get(runeSetID) == null) { ArrayList runeList = new ArrayList<>(); runeList.add(runeBaseID); runeSets.put(runeSetID, runeList); - } - else{ - ArrayListruneList = runeSets.get(runeSetID); + } else { + ArrayList runeList = runeSets.get(runeSetID); runeList.add(runeSetID); runeSets.put(runeSetID, runeList); } } - Logger.info("read: " + recordsRead + " cached: " + runeSets.size()); - } catch (SQLException e) { - Logger.error( e.toString()); - } finally { - closeCallable(); + Logger.error(e); + return runeSets; } + + Logger.info("read: " + recordsRead + " cached: " + runeSets.size()); return runeSets; } @@ -152,14 +140,13 @@ public class dbItemBaseHandler extends dbHandlerBase { HashMap> bootySets = new HashMap<>(); BootySetEntry bootySetEntry; - int bootySetID; - + int bootySetID; int recordsRead = 0; - prepareCallable("SELECT * FROM static_npc_bootySet"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_npc_bootySet")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -168,25 +155,22 @@ public class dbItemBaseHandler extends dbHandlerBase { bootySetID = rs.getInt("bootySet"); bootySetEntry = new BootySetEntry(rs); - if (bootySets.get(bootySetID) == null){ + if (bootySets.get(bootySetID) == null) { ArrayList bootyList = new ArrayList<>(); bootyList.add(bootySetEntry); bootySets.put(bootySetID, bootyList); - } - else{ - ArrayListbootyList = bootySets.get(bootySetID); + } else { + ArrayList bootyList = bootySets.get(bootySetID); bootyList.add(bootySetEntry); bootySets.put(bootySetID, bootyList); } } - - Logger.info("read: " + recordsRead + " cached: " + bootySets.size()); - } catch (SQLException e) { - Logger.error( e.toString()); - } finally { - closeCallable(); + Logger.error(e); + return bootySets; } + + Logger.info("read: " + recordsRead + " cached: " + bootySets.size()); return bootySets; } }