From f2d28fd7af69930432e3c76e55b3d96097c89ea9 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Thu, 14 Mar 2024 15:22:38 -0400 Subject: [PATCH] Refactored out duplicate db interface. --- src/engine/db/handlers/dbPowerHandler.java | 31 +- src/engine/objects/AbstractGameObject.java | 16 - src/engine/objects/LevelDefault.java | 71 - src/engine/objects/PowerGrant.java | 28 +- src/engine/objects/PowerReq.java | 19 +- .../objects/PreparedStatementShared.java | 1261 ----------------- src/engine/powers/ActionsBase.java | 21 +- src/engine/powers/EffectsBase.java | 23 +- src/engine/powers/FailCondition.java | 118 -- src/engine/powers/PowerPrereq.java | 17 +- .../poweractions/AbstractPowerAction.java | 45 +- src/engine/server/login/LoginServer.java | 6 - src/engine/server/world/WorldServer.java | 6 - 13 files changed, 86 insertions(+), 1576 deletions(-) delete mode 100644 src/engine/objects/LevelDefault.java delete mode 100644 src/engine/objects/PreparedStatementShared.java delete mode 100644 src/engine/powers/FailCondition.java diff --git a/src/engine/db/handlers/dbPowerHandler.java b/src/engine/db/handlers/dbPowerHandler.java index ae3da531..e9714afe 100644 --- a/src/engine/db/handlers/dbPowerHandler.java +++ b/src/engine/db/handlers/dbPowerHandler.java @@ -13,10 +13,11 @@ import engine.Enum; import engine.gameManager.DbManager; import engine.gameManager.PowersManager; import engine.objects.Mob; -import engine.objects.PreparedStatementShared; import engine.powers.EffectsBase; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashSet; @@ -28,16 +29,17 @@ public class dbPowerHandler extends dbHandlerBase { } public static void addAllSourceTypes() { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM static_power_sourcetype"); - ResultSet rs = ps.executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_sourcetype")) { + + ResultSet rs = preparedStatement.executeQuery(); String IDString, source; + while (rs.next()) { IDString = rs.getString("IDString"); int token = DbManager.hasher.SBStringHash(IDString); - source = rs.getString("source").replace("-", "").trim(); Enum.EffectSourceType effectSourceType = Enum.EffectSourceType.GetEffectSourceType(source); @@ -46,19 +48,18 @@ public class dbPowerHandler extends dbHandlerBase { EffectsBase.effectSourceTypeMap.get(token).add(effectSourceType); } - rs.close(); } catch (Exception e) { Logger.error(e); - } finally { - ps.release(); } } public static void addAllAnimationOverrides() { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM static_power_animation_override"); - ResultSet rs = ps.executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_animation_override")) { + + ResultSet rs = preparedStatement.executeQuery(); + String IDString; int animation; while (rs.next()) { @@ -72,12 +73,10 @@ public class dbPowerHandler extends dbHandlerBase { PowersManager.AnimationOverrides.put(IDString, animation); } - rs.close(); } catch (Exception e) { Logger.error(e); - } finally { - ps.release(); } + } } diff --git a/src/engine/objects/AbstractGameObject.java b/src/engine/objects/AbstractGameObject.java index e0b5f206..ad321d57 100644 --- a/src/engine/objects/AbstractGameObject.java +++ b/src/engine/objects/AbstractGameObject.java @@ -84,22 +84,6 @@ public abstract class AbstractGameObject { return GameObjectType.values()[ordinal]; } - /** - * Generates a {@link PreparedStatementShared} based on the specified query. - *

- * If {@link AbstractGameObject} Database functions will properly release - * the PreparedStatementShared upon completion. If these functions are not - * used, then {@link PreparedStatementShared#release release()} must be - * called when finished with this object. - * - * @param sql The SQL string used to generate the PreparedStatementShared - * @return {@link PreparedStatementShared} - * @throws {@link SQLException} - **/ - protected static PreparedStatementShared prepareStatement(String sql) throws SQLException { - return new PreparedStatementShared(sql); - } - public static AbstractGameObject getFromTypeAndID(long compositeID) { int objectTypeID = extractTypeOrdinal(compositeID); int tableID = extractTableID(objectTypeID, compositeID); diff --git a/src/engine/objects/LevelDefault.java b/src/engine/objects/LevelDefault.java deleted file mode 100644 index 60492d7e..00000000 --- a/src/engine/objects/LevelDefault.java +++ /dev/null @@ -1,71 +0,0 @@ -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - -package engine.objects; - -import engine.server.MBServerStatics; -import org.pmw.tinylog.Logger; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.concurrent.ConcurrentHashMap; - -public class LevelDefault { - - public static ConcurrentHashMap defaults = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); - public final int level; - public final float health; - public final float mana; - public final float stamina; - public final float atr; - public final float def; - public final float minDamage; - public final float maxDamage; - public final int goldMin; - public final int goldMax; - - /** - * ResultSet Constructor - */ - public LevelDefault(ResultSet rs) throws SQLException { - super(); - this.level = rs.getInt("level"); - this.health = rs.getFloat("health"); - this.mana = (float) rs.getInt("mana"); - this.stamina = (float) rs.getInt("stamina"); - this.atr = (float) rs.getInt("atr"); - this.def = (float) rs.getInt("def"); - this.minDamage = (float) rs.getInt("minDamage"); - this.maxDamage = (float) rs.getInt("maxDamage"); - this.goldMin = rs.getInt("goldMin"); - this.goldMax = rs.getInt("goldMax"); - } - - public static LevelDefault getLevelDefault(byte level) { - LevelDefault ret = null; - if (LevelDefault.defaults.containsKey(level)) - return LevelDefault.defaults.get(level); - - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM `static_npc_level_defaults` WHERE level = ?;"); - ps.setInt(1, (int) level); - ResultSet rs = ps.executeQuery(); - if (rs.next()) { - ret = new LevelDefault(rs); - LevelDefault.defaults.put(level, ret); - } - } catch (SQLException e) { - Logger.error("SQL Error number: " + e.getErrorCode() + ' ' + e.getMessage()); - } finally { - ps.release(); - } - return ret; - } -} \ No newline at end of file diff --git a/src/engine/objects/PowerGrant.java b/src/engine/objects/PowerGrant.java index cffa21b9..04c08552 100644 --- a/src/engine/objects/PowerGrant.java +++ b/src/engine/objects/PowerGrant.java @@ -9,9 +9,12 @@ package engine.objects; +import engine.gameManager.DbManager; import engine.server.MBServerStatics; 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; @@ -86,18 +89,26 @@ public class PowerGrant extends AbstractGameObject { } public static void fillGrantedPowers() { + PowerGrant.grantedPowers = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); - PreparedStatementShared ps = null; - try { - ps = prepareStatement("SELECT * FROM static_power_powergrant"); - ResultSet rs = ps.executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_powergrant")) { + + + ResultSet rs = preparedStatement.executeQuery(); + if (PowerGrant.grantedPowers.size() > 0) { rs.close(); return; } + while (rs.next()) { + int token = rs.getInt("powerToken"); - PowerGrant pg = null; + + PowerGrant pg; + if (PowerGrant.grantedPowers.containsKey(token)) { pg = PowerGrant.grantedPowers.get(token); pg.addRuneGrant(rs.getInt("runeID"), rs.getShort("grantAmount")); @@ -109,16 +120,9 @@ public class PowerGrant extends AbstractGameObject { rs.close(); } catch (SQLException e) { Logger.error("SQL Error number: " + e.getErrorCode(), e); - } finally { - ps.release(); } } - public ConcurrentHashMap getRuneGrants() { - return this.runeGrants; - } - - /* * Database */ diff --git a/src/engine/objects/PowerReq.java b/src/engine/objects/PowerReq.java index bc1e7c2b..6c0a4481 100644 --- a/src/engine/objects/PowerReq.java +++ b/src/engine/objects/PowerReq.java @@ -9,11 +9,14 @@ package engine.objects; +import engine.gameManager.DbManager; import engine.gameManager.PowersManager; import engine.powers.PowersBase; import engine.server.MBServerStatics; 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; @@ -87,15 +90,18 @@ public class PowerReq extends AbstractGameObject implements Comparable } public static ConcurrentHashMap> fillRunePowers() { + PowerReq.runePowers = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); - PreparedStatementShared ps = null; - try { - ps = prepareStatement("SELECT * FROM static_power_powerrequirement"); - ResultSet rs = ps.executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_powerrequirement")) { + + ResultSet rs = preparedStatement.executeQuery(); + if (PowerReq.runePowers.size() > 0) { - rs.close(); return PowerReq.runePowers; } + while (rs.next()) { ArrayList runePR = null; int runeID = rs.getInt("runeID"); @@ -130,9 +136,8 @@ public class PowerReq extends AbstractGameObject implements Comparable } } catch (SQLException e) { Logger.error("SQL Error number: " + e.getErrorCode(), e); - } finally { - ps.release(); } + return PowerReq.runePowers; } diff --git a/src/engine/objects/PreparedStatementShared.java b/src/engine/objects/PreparedStatementShared.java deleted file mode 100644 index de3aae0c..00000000 --- a/src/engine/objects/PreparedStatementShared.java +++ /dev/null @@ -1,1261 +0,0 @@ -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - -package engine.objects; - -import engine.gameManager.DbManager; -import engine.job.JobScheduler; -import engine.jobs.BasicScheduledJob; -import engine.server.MBServerStatics; -import org.pmw.tinylog.Logger; - -import java.io.InputStream; -import java.io.Reader; -import java.math.BigDecimal; -import java.net.URL; -import java.sql.*; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.concurrent.ConcurrentHashMap; - -/** - * A thread-safe sharing implementation of {@link PreparedStatement}. - *

- * All of the methods from the PreparedStatement interface simply check to see - * that the PreparedStatement is active, and call the corresponding method on - * that PreparedStatement. - * - * @author Burfo - * @see PreparedStatement - **/ -public class PreparedStatementShared implements PreparedStatement { - private static final ConcurrentHashMap> statementList = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); - private static final ArrayList statementListDelegated = new ArrayList<>(); - private static final String ExceptionMessage = "PreparedStatementShared object " + "was accessed after being released."; - private static boolean debuggingIsOn; - - private PreparedStatement ps = null; - private int sqlHash; - private String sql; - private long delegatedTime; - - //debugging variables - private StackTraceElement[] stackTrace; - private DebugParam[] variables; - private String filteredSql; - - /** - * Generates a new PreparedStatementShared based on the specified sql. - * - * @param sql Query string to generate the PreparedStatement - * @throws SQLException - **/ - public PreparedStatementShared(String sql) throws SQLException { - this.sqlHash = sql.hashCode(); - this.sql = sql; - this.delegatedTime = System.currentTimeMillis(); - this.ps = getFromPool(sql, sqlHash); - if (this.ps == null) { - this.ps = createNew(sql, sqlHash); - } - - - if (debuggingIsOn) { - //see if there are any '?' in the statement that are not bind variables - //and filter them out. - boolean isString = false; - char[] sqlString = this.sql.toCharArray(); - for (int i = 0; i < sqlString.length; i++) { - if (sqlString[i] == '\'') - isString = !isString; - //substitute the ? with an unprintable character if is in a string - if (sqlString[i] == '?' && isString) - sqlString[i] = '\u0007'; - } - this.filteredSql = new String(sqlString); - - //find out how many variables are present in statement. - int count = 0; - int index = -1; - while ((index = filteredSql.indexOf('?', index + 1)) != -1) { - count++; - } - - //create variables array with size equal to count of variables - this.variables = new DebugParam[count]; - - this.stackTrace = Thread.currentThread().getStackTrace(); - - } else { - this.stackTrace = null; - this.variables = null; - this.filteredSql = null; - } - - synchronized (statementListDelegated) { - statementListDelegated.add(this); - } - - } - - private static PreparedStatement getFromPool(String sql, int sqlHash) throws SQLException { - PreparedStatement ps = null; - - if (statementList.containsKey(sqlHash)) { - LinkedList list = statementList.get(sqlHash); - if (list == null) { // Shouldn't happen b/c no keys are ever removed - throw new AssertionError("list cannot be null."); - } - boolean success = false; - synchronized (list) { - do { - ps = list.pollFirst(); - if (ps == null) { - break; - } - if (ps.isClosed()) { // should rarely happen - Logger.warn("A closed PreparedStatement was removed " - + "from AbstractGameObject statementList. " + "SQL: " + sql); - } else { - success = true; - } - } while (!success); - } - - if (ps != null) { - if (MBServerStatics.DB_DEBUGGING_ON_BY_DEFAULT) { - Logger.info("Found cached PreparedStatement for SQL hash: " + sqlHash - + " SQL String: " + sql); - } - } - } - return ps; - } - - private static PreparedStatement createNew(String sql, int sqlHash) throws SQLException { - statementList.putIfAbsent(sqlHash, new LinkedList<>()); - return DbManager.prepareStatement(sql); - } - - public static void submitPreparedStatementsCleaningJob() { - JobScheduler.getInstance().scheduleJob(new BasicScheduledJob("cleanUnreleasedStatements", PreparedStatementShared.class), 1000 * 60 * 2); // 2 - // minutes - } - - public static void cleanUnreleasedStatements() { - long now = System.currentTimeMillis(); - long timeLimit = 120000; // 2 minutes - - synchronized (statementListDelegated) { - Iterator iterator = statementListDelegated.iterator(); - while (iterator.hasNext()) { - PreparedStatementShared pss = iterator.next(); - if ((pss.delegatedTime + timeLimit) >= now) { - continue; - } - iterator.remove(); - - Logger.warn("Forcefully released after being held for > 2 minutes." + " SQL STRING: \"" - + pss.sql + "\" METHOD: " + pss.getTraceInfo()); - } - } - - submitPreparedStatementsCleaningJob(); // resubmit - } - - public static void enableDebugging() { - debuggingIsOn = true; - Logger.info("Database debugging has been enabled."); - } - - public static void disableDebugging() { - debuggingIsOn = false; - Logger.info("Database debugging has been disabled."); - } - - @Override - public boolean isCloseOnCompletion() { - return true; - } - - @Override - public void closeOnCompletion() { - Logger.warn("Prepared Statement Closed"); - } - - /** - * Releases the use of a PreparedStatementShared that was generated by - * {@link AbstractGameObject#prepareStatement}, making it available for use - * by another query. - *

- * Do not utilize or modify the object after calling this method. - *

- * Example: - * - *

-     * @code
-     * PreparedStatementShared ps = prepareStatement(...);
-     * ps.executeUpdate();
-     * ps.release();
-     * ps = null;}
-     * 
- **/ - public void release() { - if (this.ps == null) { - return; - } // nothing to release - if (statementListDelegated.contains(this)) { - synchronized (statementListDelegated) { - statementListDelegated.remove(this); - } - try { - if (this.ps.isClosed()) { - return; - } - this.ps.clearParameters(); - this.variables = null; - } catch (SQLException ignore) { - } - - // add back to pool - LinkedList list = statementList.get(this.sqlHash); - if (list == null) { - return; - } - synchronized (list) { - list.add(this.ps); - } - } - // clear values from this object so caller cannot use it after it has - // been released - this.ps = null; - this.sqlHash = 0; - this.sql = ""; - this.delegatedTime = 0; - this.stackTrace = null; - } - - /** - * Determines if the object is in a usable state. - * - * @return True if the object is in a useable state. - **/ - public boolean isUsable() { - if (ps == null) { - return false; - } - try { - if (ps.isClosed()) { - return false; - } - } catch (SQLException e) { - return false; - } - return true; - } - - private String getTraceInfo() { - if (stackTrace == null) { - return ""; - } - - if (stackTrace.length > 3) { - return stackTrace[3].getClassName() + '.' + stackTrace[3].getMethodName(); - } else if (stackTrace.length == 0) { - return ""; - } else { - return stackTrace[stackTrace.length - 1].getClassName() + '.' + stackTrace[stackTrace.length - 1].getMethodName(); - } - } - - @Override - public boolean equals(Object obj) { - if (ps == null || obj == null) { - return false; - } - - if (obj instanceof PreparedStatementShared) { - return this.ps.equals(((PreparedStatementShared) obj).ps); - } - - if (obj instanceof PreparedStatement) { - return this.ps.equals(obj); - } - - return false; - } - - @Override - public String toString() { - if (!debuggingIsOn || variables == null) { - return "SQL: " + this.sql + " (enable DB debugging for more data)"; - } - - String out; - - out = "SQL: [" + this.sql + "] "; - out += "VARIABLES[count=" + variables.length + "]: "; - - for (int i = 0; i < variables.length; i++) { - out += "[" + (i + 1) + "]: "; - DebugParam dp = variables[i]; - if (dp == null || !dp.isValueAssigned()) { - out += "{MISSING} "; - continue; - } - Object dpObj = dp.getDebugObject(); - out += dpObj.toString() + ' '; - } - return out; - } - - @Override - public void addBatch() throws SQLException { - if (this.ps == null) { - throw new SQLException(); - } - this.ps.addBatch(); - - } - - private void saveObject(int parameterIndex, Object obj) throws SQLException { - if (!debuggingIsOn || this.variables == null) { - return; - } - - if (parameterIndex > variables.length) { - throw new SQLException("Parameter index of " + parameterIndex + - " exceeds actual parameter count of " + this.variables.length); - } - - this.variables[parameterIndex - 1] = new DebugParam(obj); - } - - private void logExceptionAndRethrow(SQLException e) throws SQLException { - Logger.error("SQL operation failed: (" + - e.getMessage() + ") " + this.toString(), e); - throw e; - } - - @Override - public void clearParameters() throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - - this.ps.clearParameters(); - for (int i = 0; i < this.variables.length; i++) { - this.variables[i] = null; - } - - } - - @Override - public boolean execute() throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - - if (debuggingIsOn || MBServerStatics.ENABLE_EXECUTION_TIME_WARNING) { - long startTime = System.currentTimeMillis(); - boolean rs = false; - try { - rs = this.ps.execute(); - } catch (SQLException e) { - logExceptionAndRethrow(e); - } - if ((startTime + MBServerStatics.DB_EXECUTION_WARNING_TIME_MS) < System.currentTimeMillis()) - Logger.warn("The following statement took " + (System.currentTimeMillis() - startTime) - + " millis to execute: " + this.sql); - return rs; - } - - return this.ps.execute(); - } - - @Override - public ResultSet executeQuery() throws SQLException, SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - - if (debuggingIsOn || MBServerStatics.ENABLE_QUERY_TIME_WARNING) { - long startTime = System.currentTimeMillis(); - ResultSet rs = null; - try { - rs = this.ps.executeQuery(); - } catch (SQLException e) { - logExceptionAndRethrow(e); - } - if ((startTime + MBServerStatics.DB_QUERY_WARNING_TIME_MS) < System.currentTimeMillis()) - Logger.warn("The following query took " + (System.currentTimeMillis() - startTime) - + " millis to execute: " + this.sql); - return rs; - } - - return this.ps.executeQuery(); - } - - @Override - public int executeUpdate() throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - - if (debuggingIsOn || MBServerStatics.ENABLE_UPDATE_TIME_WARNING) { - long startTime = System.currentTimeMillis(); - int rs = 0; - try { - rs = this.ps.executeUpdate(); - } catch (SQLException e) { - logExceptionAndRethrow(e); - } - if ((startTime + MBServerStatics.DB_UPDATE_WARNING_TIME_MS) < System.currentTimeMillis()) - Logger.warn("The following update took " + (System.currentTimeMillis() - startTime) - + " millis to execute: " + this.sql); - return rs; - } - - return this.ps.executeUpdate(); - } - - @Override - public ResultSetMetaData getMetaData() throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - return this.ps.getMetaData(); - } - - @Override - public ParameterMetaData getParameterMetaData() throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - return this.ps.getParameterMetaData(); - } - - @Override - public void setArray(int parameterIndex, Array x) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - this.saveObject(parameterIndex, x); - this.ps.setArray(parameterIndex, x); - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (x == null ? "NULL" : "")); - this.ps.setAsciiStream(parameterIndex, x); - } - - @Override - public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (x == null ? "NULL" : "")); - this.ps.setBinaryStream(parameterIndex, x); - } - - @Override - public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (x == null ? "NULL" : "")); - this.ps.setBlob(parameterIndex, x); - } - - @Override - public void setBlob(int parameterIndex, InputStream x, long length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (x == null ? "NULL" : "")); - this.ps.setCharacterStream(parameterIndex, reader); - } - - @Override - public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (reader == null ? "NULL" : "")); - this.ps.setClob(parameterIndex, reader); - } - - @Override - public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (reader == null ? "NULL" : "")); - this.ps.setNCharacterStream(parameterIndex, value); - } - - @Override - public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (value == null ? "NULL" : "")); - this.ps.setNClob(parameterIndex, value); - } - - @Override - public void setNClob(int parameterIndex, Reader reader) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (reader == null ? "NULL" : "")); - this.ps.setNClob(parameterIndex, reader); - } - - @Override - public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - saveObject(parameterIndex, (reader == null ? "NULL" : " iface) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - return this.ps.isWrapperFor(iface); - } - - @Override - public T unwrap(Class iface) throws SQLException { - if (this.ps == null) { - throw new SQLException(ExceptionMessage); - } - return this.ps.unwrap(iface); - } - - private class DebugParam { - private Object debugObject; - private boolean valueAssigned; - - public DebugParam(Object debugObject) { - this.debugObject = debugObject; - valueAssigned = true; - } - - public Object getDebugObject() { - return debugObject; - } - - public boolean isValueAssigned() { - return valueAssigned; - } - } - -} diff --git a/src/engine/powers/ActionsBase.java b/src/engine/powers/ActionsBase.java index bf3596c7..6e36031f 100644 --- a/src/engine/powers/ActionsBase.java +++ b/src/engine/powers/ActionsBase.java @@ -12,11 +12,17 @@ package engine.powers; import engine.Enum.ModType; import engine.Enum.SourceType; import engine.Enum.StackType; +import engine.gameManager.DbManager; import engine.gameManager.PowersManager; -import engine.objects.*; +import engine.objects.AbstractCharacter; +import engine.objects.AbstractWorldObject; +import engine.objects.PlayerBonuses; +import engine.objects.Runegate; import engine.powers.poweractions.AbstractPowerAction; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; @@ -133,13 +139,15 @@ public class ActionsBase { // } public static void getActionsBase(HashMap powers, HashMap apa) { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM static_power_action"); - ResultSet rs = ps.executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_action")) { + + ResultSet rs = preparedStatement.executeQuery(); String IDString; ActionsBase toAdd; PowersBase pb; + while (rs.next()) { IDString = rs.getString("powerID"); pb = powers.get(IDString); @@ -151,11 +159,10 @@ public class ActionsBase { rs.close(); } catch (Exception e) { Logger.error(e.toString()); - } finally { - ps.release(); } int gateID = 5000; + for (String IDString : Runegate.GetAllOpenGateIDStrings()) { gateID++; ActionsBase openGateActionBase = new ActionsBase(gateID, "OPENGATE", 5, 9999, 0, 0, true, "IgnoreStack", 0, true, false, false, PowersManager.getPowerActionByIDString("OPENGATE")); diff --git a/src/engine/powers/EffectsBase.java b/src/engine/powers/EffectsBase.java index 20150e90..ed6d80e0 100644 --- a/src/engine/powers/EffectsBase.java +++ b/src/engine/powers/EffectsBase.java @@ -29,6 +29,8 @@ import engine.powers.effectmodifiers.AbstractEffectModifier; import engine.server.MBServerStatics; 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; @@ -169,21 +171,21 @@ public class EffectsBase { } public static void getFailConditions(HashMap effects) { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM static_power_failcondition WHERE powerOrEffect = 'Effect';"); - ResultSet rs = ps.executeQuery(); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_failcondition WHERE powerOrEffect = 'Effect';")) { + + ResultSet rs = preparedStatement.executeQuery(); + PowerFailCondition failCondition = null; - Object value; while (rs.next()) { String fail = rs.getString("type"); String IDString = rs.getString("IDString"); - int token = DbManager.hasher.SBStringHash(IDString); failCondition = PowerFailCondition.valueOf(fail); + if (failCondition == null) { Logger.error("Couldn't Find FailCondition " + fail + " for " + IDString); continue; @@ -200,11 +202,10 @@ public class EffectsBase { case TakeDamage: - // dont go any further. - if (eb == null) { + + if (eb == null) break; - } eb.cancelOnTakeDamage = true; @@ -217,7 +218,6 @@ public class EffectsBase { String damageType2 = rs.getString("damageType2"); String damageType3 = rs.getString("damageType3"); - if (damageType1.isEmpty() && damageType2.isEmpty() && damageType3.isEmpty()) break; @@ -274,10 +274,7 @@ public class EffectsBase { rs.close(); } catch (Exception e) { Logger.error(e); - } finally { - ps.release(); } - } private static Enum.SourceType getDamageType(String name) { diff --git a/src/engine/powers/FailCondition.java b/src/engine/powers/FailCondition.java deleted file mode 100644 index faeece1e..00000000 --- a/src/engine/powers/FailCondition.java +++ /dev/null @@ -1,118 +0,0 @@ -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - -package engine.powers; - -import engine.objects.PreparedStatementShared; -import org.pmw.tinylog.Logger; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; - - -public class FailCondition { - - private String IDString; - private Boolean forPower; - private String type; - private float amount; - private float ramp; - private boolean rampAdd; - - // private String damageType1; - // private String damageType2; - // private String damageType3; - - /** - * No Table ID Constructor - */ - public FailCondition() { - - } - - /** - * ResultSet Constructor - */ - public FailCondition(ResultSet rs) throws SQLException { - - this.IDString = rs.getString("IDString"); - this.forPower = (rs.getString("powerOrEffect").equals("Power")) ? true : false; - this.type = rs.getString("type"); - this.amount = rs.getFloat("amount"); - this.ramp = rs.getFloat("ramp"); - this.rampAdd = (rs.getInt("useAddFormula") == 1) ? true : false; - // this.damageType1 = rs.getString("damageType1"); - // this.damageType2 = rs.getString("damageType2"); - // this.damageType3 = rs.getString("damageType3"); - } - - public static ArrayList getAllFailConditions() { - PreparedStatementShared ps = null; - ArrayList out = new ArrayList<>(); - try { - ps = new PreparedStatementShared("SELECT * FROM failconditions"); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - FailCondition toAdd = new FailCondition(rs); - out.add(toAdd); - } - rs.close(); - } catch (Exception e) { - Logger.error(e); - - } finally { - ps.release(); - } - return out; - } - - public String getIDString() { - return this.IDString; - } - - public String getType() { - return this.type; - } - - public boolean forPower() { - return this.forPower; - } - - public float getAmount() { - return this.amount; - } - - public float getRamp() { - return this.ramp; - } - - public float getAmountForTrains(float trains) { - if (this.rampAdd) - return this.amount + (this.ramp * trains); - else - return this.amount * (1 + (this.ramp * trains)); - } - - public boolean useRampAdd() { - return this.rampAdd; - } - - // public String getDamageType1() { - // return this.damageType1; - // } - - // public String getDamageType2() { - // return this.damageType2; - // } - - // public String getDamageType3() { - // return this.damageType3; - // } -} diff --git a/src/engine/powers/PowerPrereq.java b/src/engine/powers/PowerPrereq.java index 086185a5..2085c047 100644 --- a/src/engine/powers/PowerPrereq.java +++ b/src/engine/powers/PowerPrereq.java @@ -9,9 +9,11 @@ package engine.powers; -import engine.objects.PreparedStatementShared; +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; import java.util.HashMap; @@ -60,14 +62,17 @@ public class PowerPrereq { } public static void getAllPowerPrereqs(HashMap powers) { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM static_power_powercastprereq"); - ResultSet rs = ps.executeQuery(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_powercastprereq\"")) { + + ResultSet rs = preparedStatement.executeQuery(); + int type; String IDString; PowerPrereq toAdd; PowersBase pb; + while (rs.next()) { IDString = rs.getString("IDString"); pb = powers.get(IDString); @@ -85,8 +90,6 @@ public class PowerPrereq { rs.close(); } catch (Exception e) { Logger.error(e.toString()); - } finally { - ps.release(); } } diff --git a/src/engine/powers/poweractions/AbstractPowerAction.java b/src/engine/powers/poweractions/AbstractPowerAction.java index 5f8f2131..26197daa 100644 --- a/src/engine/powers/poweractions/AbstractPowerAction.java +++ b/src/engine/powers/poweractions/AbstractPowerAction.java @@ -15,12 +15,13 @@ import engine.math.Vector3fImmutable; import engine.objects.AbstractCharacter; import engine.objects.AbstractWorldObject; import engine.objects.Item; -import engine.objects.PreparedStatementShared; import engine.powers.ActionsBase; import engine.powers.EffectsBase; import engine.powers.PowersBase; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; @@ -64,11 +65,14 @@ public abstract class AbstractPowerAction { } public static void getAllPowerActions(HashMap powerActions, HashMap powerActionsByID, HashMap effects) { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM static_power_poweraction"); - ResultSet rs = ps.executeQuery(); + + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_poweraction")) { + + ResultSet rs = preparedStatement.executeQuery(); String IDString, type; + while (rs.next()) { AbstractPowerAction apa; type = rs.getString("type"); @@ -185,45 +189,14 @@ public abstract class AbstractPowerAction { rs.close(); } catch (Exception e) { Logger.error(e.toString()); - } finally { - ps.release(); } - //Add OpenGatePowerAction AbstractPowerAction openGateAction = new OpenGatePowerAction(5000, "OPENGATE", "OpenGate", false, 0); powerActions.put("OPENGATE", openGateAction); powerActionsByID.put(openGateAction.UUID, openGateAction); } - public static void loadValidItemFlags(HashMap powerActions) { - PreparedStatementShared ps = null; - try { - ps = new PreparedStatementShared("SELECT * FROM `static_power_effect_allowed_item`"); - ResultSet rs = ps.executeQuery(); - String IDS; - long flags; - while (rs.next()) { - AbstractPowerAction apa; - flags = rs.getLong("flags"); - IDS = rs.getString("IDString"); - if (powerActions.containsKey(IDS)) { - apa = powerActions.get(IDS); - apa.validItemFlags = flags; - } else { - Logger.error("Unable to find PowerAction " + IDS); - continue; - } - } - rs.close(); - } catch (Exception e) { - Logger.error(e.toString()); - } finally { - ps.release(); - } - - } - public void startAction(AbstractCharacter source, AbstractWorldObject awo, Vector3fImmutable targetLoc, int numTrains, ActionsBase ab, PowersBase pb) { this._startAction(source, awo, targetLoc, numTrains, ab, pb); } diff --git a/src/engine/server/login/LoginServer.java b/src/engine/server/login/LoginServer.java index 4b675e7a..7b487426 100644 --- a/src/engine/server/login/LoginServer.java +++ b/src/engine/server/login/LoginServer.java @@ -311,12 +311,6 @@ public class LoginServer { return false; } - PreparedStatementShared.submitPreparedStatementsCleaningJob(); - - if (MBServerStatics.DB_DEBUGGING_ON_BY_DEFAULT) { - PreparedStatementShared.enableDebugging(); - } - return true; } diff --git a/src/engine/server/world/WorldServer.java b/src/engine/server/world/WorldServer.java index af80feae..25a178a9 100644 --- a/src/engine/server/world/WorldServer.java +++ b/src/engine/server/world/WorldServer.java @@ -534,12 +534,6 @@ public class WorldServer { return false; } - PreparedStatementShared.submitPreparedStatementsCleaningJob(); - - if (MBServerStatics.DB_DEBUGGING_ON_BY_DEFAULT) { - PreparedStatementShared.enableDebugging(); - } - return true; }