From 57456871909f26da79081da7a047ca391aff26c4 Mon Sep 17 00:00:00 2001 From: Heliosares Date: Wed, 16 Aug 2023 21:58:59 -0400 Subject: [PATCH] Better BusyException handling --- pom.xml | 2 +- .../auxprotect/api/AuxProtectAPI.java | 4 ++- .../auxprotect/bungee/APBListener.java | 3 ++ .../auxprotect/core/Parameters.java | 6 ++-- .../auxprotect/core/commands/InvCommand.java | 5 ++- .../core/commands/LookupCommand.java | 17 ++++++---- .../core/commands/PurgeCommand.java | 3 +- .../auxprotect/core/commands/XrayCommand.java | 3 ++ .../auxprotect/database/BlobManager.java | 3 +- .../auxprotect/database/ConnectionPool.java | 14 ++++---- .../auxprotect/database/DatabaseRunnable.java | 3 +- .../auxprotect/database/DbEntry.java | 23 +++++++------ .../auxprotect/database/InvDiffManager.java | 8 ++--- .../auxprotect/database/LookupManager.java | 4 ++- .../auxprotect/database/MigrationManager.java | 24 ++++++------- .../auxprotect/database/PosEntry.java | 2 +- .../auxprotect/database/Results.java | 9 ++--- .../auxprotect/database/SQLManager.java | 34 +++++++++---------- .../auxprotect/database/SQLUserManager.java | 19 ++++++----- .../auxprotect/database/SingleItemEntry.java | 3 +- .../auxprotect/database/XrayResults.java | 3 +- .../auxprotect/exceptions/BusyException.java | 2 +- .../auxprotect/spigot/AuxProtectSpigot.java | 2 +- .../heliosares/auxprotect/spigot/Metrics.java | 3 +- .../auxprotect/spigot/VeinManager.java | 3 +- .../spigot/listeners/PlayerListener.java | 6 ++-- .../auxprotect/towny/TownyEntry.java | 5 +-- .../auxprotect/towny/TownyListener.java | 11 +++--- .../auxprotect/towny/TownyManager.java | 9 ++--- .../auxprotect/utils/PlaybackSolver.java | 5 +-- .../auxprotect/utils/RetentionSolver.java | 3 +- .../auxprotect/utils/SQLConsumer.java | 4 ++- .../auxprotect/utils/SQLFunction.java | 4 ++- .../auxprotect/utils/XraySolver.java | 3 +- 34 files changed, 144 insertions(+), 108 deletions(-) diff --git a/pom.xml b/pom.xml index b607d7d..7bde3d3 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 AuxProtect AuxProtect - 1.2.7 + 1.2.8-pre1 AuxProtect ${project.artifactId}-${project.version} diff --git a/src/main/java/dev/heliosares/auxprotect/api/AuxProtectAPI.java b/src/main/java/dev/heliosares/auxprotect/api/AuxProtectAPI.java index a47568a..dc2619d 100644 --- a/src/main/java/dev/heliosares/auxprotect/api/AuxProtectAPI.java +++ b/src/main/java/dev/heliosares/auxprotect/api/AuxProtectAPI.java @@ -7,6 +7,7 @@ import dev.heliosares.auxprotect.database.EntryAction; import dev.heliosares.auxprotect.database.LookupManager; import dev.heliosares.auxprotect.database.SQLManager; import dev.heliosares.auxprotect.exceptions.AlreadyExistsException; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import dev.heliosares.auxprotect.utils.SQLConsumer; @@ -105,9 +106,10 @@ public final class AuxProtectAPI { * database. * @throws NullPointerException if the plugin, key, or ntext is null or zero length. * @throws IllegalArgumentException if the plugin, key, or ntext is null or zero length. + * @throws BusyException if the database is busy */ - public synchronized EntryAction createAction(@Nonnull String plugin, @Nonnull String key, @Nonnull String ntext, @Nullable String ptext) throws AlreadyExistsException, SQLException { + public synchronized EntryAction createAction(@Nonnull String plugin, @Nonnull String key, @Nonnull String ntext, @Nullable String ptext) throws AlreadyExistsException, SQLException, BusyException { return getSQLManager().createAction(plugin, key, ntext, ptext); } } diff --git a/src/main/java/dev/heliosares/auxprotect/bungee/APBListener.java b/src/main/java/dev/heliosares/auxprotect/bungee/APBListener.java index 2e79b1e..9358d10 100644 --- a/src/main/java/dev/heliosares/auxprotect/bungee/APBListener.java +++ b/src/main/java/dev/heliosares/auxprotect/bungee/APBListener.java @@ -2,6 +2,7 @@ package dev.heliosares.auxprotect.bungee; import dev.heliosares.auxprotect.database.DbEntry; import dev.heliosares.auxprotect.database.EntryAction; +import dev.heliosares.auxprotect.exceptions.BusyException; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.LoginEvent; @@ -45,6 +46,8 @@ public class APBListener implements Listener { try { plugin.getSqlManager().getUserManager().updateUsernameAndIP(e.getConnection().getUniqueId(), e.getConnection().getName(), ip); + } catch (BusyException ex) { + plugin.warning("Database Busy: Unable to update username/ip for " + e.getConnection().getName() + ", this may cause issues with lookups but will resolve when they relog and the database is not busy."); } catch (SQLException ex) { plugin.print(ex); } diff --git a/src/main/java/dev/heliosares/auxprotect/core/Parameters.java b/src/main/java/dev/heliosares/auxprotect/core/Parameters.java index 88b92f1..bfce4dd 100644 --- a/src/main/java/dev/heliosares/auxprotect/core/Parameters.java +++ b/src/main/java/dev/heliosares/auxprotect/core/Parameters.java @@ -566,8 +566,10 @@ public class Parameters implements Cloneable { int uid; try { uid = plugin.getSqlManager().getUserManager().getUIDFromUUID("$" + uuid.toString(), false); - } catch (SQLException e) { + } catch (BusyException e) { throw new LookupException(L.DATABASE_BUSY); + } catch (SQLException e) { + throw new LookupException(L.ERROR); } if (uid > 0) { @@ -928,7 +930,7 @@ public class Parameters implements Cloneable { } @Deprecated - public boolean matches(DbEntry entry) throws SQLException { + public boolean matches(DbEntry entry) throws SQLException, BusyException { if (!uids.isEmpty()) { boolean contains = false; for (String user : uids) { diff --git a/src/main/java/dev/heliosares/auxprotect/core/commands/InvCommand.java b/src/main/java/dev/heliosares/auxprotect/core/commands/InvCommand.java index 2f471d1..a198462 100644 --- a/src/main/java/dev/heliosares/auxprotect/core/commands/InvCommand.java +++ b/src/main/java/dev/heliosares/auxprotect/core/commands/InvCommand.java @@ -72,6 +72,9 @@ public class InvCommand extends Command { player.sendMessage(L.COMMAND__LOOKUP__LOOKING.translate()); try { update(plugin, player, when); + } catch (BusyException e) { + player.sendMessage(L.DATABASE_BUSY.translate()); + return; } catch (Exception e) { plugin.print(e); player.sendMessage(L.ERROR.translate()); @@ -184,7 +187,7 @@ public class InvCommand extends Command { return null; } - private static void update(IAuxProtect plugin, Player staff, long time) throws SQLException { + private static void update(IAuxProtect plugin, Player staff, long time) throws SQLException, BusyException { plugin.getSqlManager().execute( "UPDATE " + Table.AUXPROTECT_INVENTORY + " SET data=" + plugin.getSqlManager().concat("ifnull(data,'')", "?") + " WHERE time=? AND action_id=?", 30000L, LocalDateTime.now().format(XrayCommand.ratedByDateFormatter) + ": Recovered by " + staff.getName() + "; ", diff --git a/src/main/java/dev/heliosares/auxprotect/core/commands/LookupCommand.java b/src/main/java/dev/heliosares/auxprotect/core/commands/LookupCommand.java index 8137117..a384002 100644 --- a/src/main/java/dev/heliosares/auxprotect/core/commands/LookupCommand.java +++ b/src/main/java/dev/heliosares/auxprotect/core/commands/LookupCommand.java @@ -4,6 +4,7 @@ import dev.heliosares.auxprotect.adapters.sender.SenderAdapter; import dev.heliosares.auxprotect.core.*; import dev.heliosares.auxprotect.core.Parameters.Flag; import dev.heliosares.auxprotect.database.*; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.exceptions.LookupException; import dev.heliosares.auxprotect.exceptions.ParseException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; @@ -25,7 +26,7 @@ public class LookupCommand extends Command { super(plugin, "lookup", APPermission.LOOKUP, true, "l"); } - public static List onTabCompleteStatic(IAuxProtect plugin, SenderAdapter sender, String label, String[] args) { + public static List onTabCompleteStatic(IAuxProtect plugin, SenderAdapter sender, String[] args) { List possible = new ArrayList<>(); String currentArg = args[args.length - 1]; boolean lookup = args[0].equalsIgnoreCase("lookup") || args[0].equalsIgnoreCase("l"); @@ -265,12 +266,12 @@ public class LookupCommand extends Command { rs.removeIf(entry -> { try { return entry.getUserUUID().startsWith("$") && !entry.getUserUUID().equals("$" + sender.getUniqueId()); - } catch (SQLException ignored) { + } catch (SQLException | BusyException ignored) { return true; } }); } - if (rs == null || rs.size() == 0) { + if (rs == null || rs.isEmpty()) { sender.sendLang(Language.L.COMMAND__LOOKUP__NORESULTS); return; } @@ -365,13 +366,13 @@ public class LookupCommand extends Command { if (pickupcount > 0 && dropcount > 0) { msg += "&fNet: &9" + (pickupcount - dropcount); } - if (msg.length() > 0) { + if (!msg.isEmpty()) { sender.sendMessageRaw(msg); } return; } else if (params_.hasFlag(Flag.PLAYTIME)) { Set users = params_.getUsers(); - if (users.size() == 0) { + if (users.isEmpty()) { sender.sendLang(Language.L.COMMAND__LOOKUP__PLAYTIME__NOUSER); return; } @@ -410,7 +411,7 @@ public class LookupCommand extends Command { } } else if (params_.hasFlag(Flag.MONEY) && sender.getPlatform() == PlatformType.SPIGOT) { Set users = params_.getUsers(); - if (users.size() == 0) { + if (users.isEmpty()) { sender.sendLang(Language.L.COMMAND__LOOKUP__PLAYTIME__NOUSER); return; } @@ -472,6 +473,8 @@ public class LookupCommand extends Command { } } catch (LookupException | ParseException e) { sender.sendMessageRaw(e.getMessage()); + } catch (BusyException e) { + sender.sendLang(Language.L.DATABASE_BUSY); } catch (Exception e) { sender.sendLang(Language.L.ERROR); plugin.warning("Error during lookup:"); @@ -486,6 +489,6 @@ public class LookupCommand extends Command { @Override public List onTabComplete(SenderAdapter sender, String label, String[] args) { - return onTabCompleteStatic(plugin, sender, label, args); + return onTabCompleteStatic(plugin, sender, args); } } diff --git a/src/main/java/dev/heliosares/auxprotect/core/commands/PurgeCommand.java b/src/main/java/dev/heliosares/auxprotect/core/commands/PurgeCommand.java index aec8151..478ae6b 100644 --- a/src/main/java/dev/heliosares/auxprotect/core/commands/PurgeCommand.java +++ b/src/main/java/dev/heliosares/auxprotect/core/commands/PurgeCommand.java @@ -6,6 +6,7 @@ import dev.heliosares.auxprotect.core.Command; import dev.heliosares.auxprotect.core.IAuxProtect; import dev.heliosares.auxprotect.core.Language; import dev.heliosares.auxprotect.database.Table; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.utils.TimeUtil; import java.sql.SQLException; @@ -70,7 +71,7 @@ public class PurgeCommand extends Command { if (!plugin.getSqlManager().isMySQL()) { plugin.getSqlManager().execute(plugin.getSqlManager()::vacuum, 30000L); } - } catch (SQLException e) { + } catch (SQLException | BusyException e) { plugin.print(e); sender.sendLang(Language.L.COMMAND__PURGE__ERROR); return; diff --git a/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java b/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java index 3a68337..5952c08 100644 --- a/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java +++ b/src/main/java/dev/heliosares/auxprotect/core/commands/XrayCommand.java @@ -201,6 +201,9 @@ public class XrayCommand extends Command { entry.setRating((short) 0, player.getName() + " BULK"); plugin.getSqlManager().updateXrayEntry(entry); it.remove(); + } catch (BusyException e) { + sender.sendLang(Language.L.DATABASE_BUSY); + return; } catch (SQLException e) { sender.sendLang(Language.L.ERROR); return; diff --git a/src/main/java/dev/heliosares/auxprotect/database/BlobManager.java b/src/main/java/dev/heliosares/auxprotect/database/BlobManager.java index a025955..802a26b 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/BlobManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/BlobManager.java @@ -1,6 +1,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.core.IAuxProtect; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.utils.InvSerialization; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; @@ -92,7 +93,7 @@ public class BlobManager { } @SuppressWarnings("deprecation") - public byte[] getBlob(DbEntry entry) throws SQLException { + public byte[] getBlob(DbEntry entry) throws SQLException, BusyException { if (entry.getBlobID() <= 0) {// TODO does this break skipV6? return null; } diff --git a/src/main/java/dev/heliosares/auxprotect/database/ConnectionPool.java b/src/main/java/dev/heliosares/auxprotect/database/ConnectionPool.java index 92580a5..2e58086 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/ConnectionPool.java +++ b/src/main/java/dev/heliosares/auxprotect/database/ConnectionPool.java @@ -115,7 +115,7 @@ public class ConnectionPool { return true; } - public void init(SQLConsumer initializationTask) throws SQLException { + public void init(SQLConsumer initializationTask) throws SQLException, BusyException { connection = newConnectionSupplier.get(); if (ready) throw new IllegalStateException("Already initialized"); initializationTask.accept(connection); @@ -184,7 +184,7 @@ public class ConnectionPool { /** * Same as {@link ConnectionPool#executeReturn(SQLFunction, long, Class)} but as a void */ - public void execute(SQLConsumer task, long wait) throws SQLException { + public void execute(SQLConsumer task, long wait) throws SQLException, BusyException { executeReturn(connection -> { task.accept(connection); return null; @@ -201,7 +201,7 @@ public class ConnectionPool { * @throws BusyException If the wait time is exceeded * @throws SQLException For SQLException thrown by the task */ - public T executeReturn(SQLFunction task, long wait, Class type) throws SQLException { + public T executeReturn(SQLFunction task, long wait, Class type) throws SQLException, BusyException { try { return executeReturnException(task::apply, wait, type); } catch (SQLException | BusyException e) { @@ -261,7 +261,7 @@ public class ConnectionPool { /** * @see PreparedStatement#execute() */ - public void execute(String stmt, long wait, Object... args) throws SQLException { + public void execute(String stmt, long wait, Object... args) throws SQLException, BusyException { execute(connection -> execute(stmt, connection, args), wait); } @@ -279,7 +279,7 @@ public class ConnectionPool { /** * @see PreparedStatement#executeUpdate() */ - public int executeReturnRows(String stmt, Object... args) throws SQLException { + public int executeReturnRows(String stmt, Object... args) throws SQLException, BusyException { debugSQLStatement(stmt, args); return executeReturn(connection -> { try (PreparedStatement pstmt = connection.prepareStatement(stmt)) { @@ -289,7 +289,7 @@ public class ConnectionPool { }, 30000L, Integer.class); } - public int executeReturnGenerated(String stmt, Object... args) throws SQLException { + public int executeReturnGenerated(String stmt, Object... args) throws SQLException, BusyException { debugSQLStatement(stmt, args); return executeReturn(connection -> { try (PreparedStatement pstmt = connection.prepareStatement(stmt, Statement.RETURN_GENERATED_KEYS)) { @@ -305,7 +305,7 @@ public class ConnectionPool { }, 30000L, Integer.class); } - public ResultMap executeGetMap(String stmt, Object... args) throws SQLException { + public ResultMap executeGetMap(String stmt, Object... args) throws SQLException, BusyException { debugSQLStatement(stmt, args); return executeReturn(connection -> { try (PreparedStatement statement = connection.prepareStatement(stmt)) { diff --git a/src/main/java/dev/heliosares/auxprotect/database/DatabaseRunnable.java b/src/main/java/dev/heliosares/auxprotect/database/DatabaseRunnable.java index deee6e1..f4faace 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/DatabaseRunnable.java +++ b/src/main/java/dev/heliosares/auxprotect/database/DatabaseRunnable.java @@ -1,6 +1,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.core.IAuxProtect; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.listeners.JobsListener.JobsEntry; import javax.annotation.Nonnull; @@ -128,7 +129,7 @@ public class DatabaseRunnable implements Runnable { try { if (!next.getUserUUID().equals(entry.getUserUUID())) continue; if (!next.getTargetUUID().equals(entry.getTargetUUID())) continue; - } catch (SQLException ignored) { + } catch (SQLException | BusyException ignored) { //Unlikely / N/A continue; } diff --git a/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java b/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java index dc64608..3b5d925 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/DbEntry.java @@ -1,5 +1,6 @@ package dev.heliosares.auxprotect.database; +import dev.heliosares.auxprotect.exceptions.BusyException; import org.bukkit.Location; import javax.annotation.Nullable; @@ -109,14 +110,14 @@ public class DbEntry { this.data = data; } - public int getUid() throws SQLException { + public int getUid() throws SQLException, BusyException { if (uid > 0) { return uid; } return uid = SQLManager.getInstance().getUserManager().getUIDFromUUID(getUserUUID(), true, true); } - public int getTargetId() throws SQLException { + public int getTargetId() throws SQLException, BusyException { if (action.getTable().hasStringTarget()) { return -1; } @@ -126,11 +127,11 @@ public class DbEntry { return target_id = SQLManager.getInstance().getUserManager().getUIDFromUUID(getTargetUUID(), true, true); } - public String getUser() throws SQLException { + public String getUser() throws SQLException, BusyException { return getUser(true); } - public String getUser(boolean resolve) throws SQLException { + public String getUser(boolean resolve) throws SQLException, BusyException { if (user != null || !resolve) return user; if (!getUserUUID().startsWith("$") || getUserUUID().length() != 37) { @@ -143,11 +144,11 @@ public class DbEntry { return user; } - public String getTarget() throws SQLException { + public String getTarget() throws SQLException, BusyException { return getTarget(true); } - public String getTarget(boolean resolve) throws SQLException { + public String getTarget(boolean resolve) throws SQLException, BusyException { if (target != null || !resolve) return target; if (action.getTable().hasStringTarget() || !getTargetUUID().startsWith("$") || getTargetUUID().length() != 37) { @@ -160,7 +161,7 @@ public class DbEntry { return target; } - public String getTargetUUID() throws SQLException { + public String getTargetUUID() throws SQLException, BusyException { if (targetLabel != null) { return targetLabel; } @@ -175,7 +176,7 @@ public class DbEntry { return targetLabel; } - public String getUserUUID() throws SQLException { + public String getUserUUID() throws SQLException, BusyException { if (userLabel != null) { return userLabel; } @@ -205,7 +206,7 @@ public class DbEntry { return Math.pow(getX() - entry.getX(), 2) + Math.pow(getY() - entry.getY(), 2) + Math.pow(getZ() - entry.getZ(), 2); } - public byte[] getBlob() throws SQLException { + public byte[] getBlob() throws SQLException, BusyException { if (blob == null) blob = SQLManager.getInstance().getBlob(this); return blob; } @@ -232,10 +233,10 @@ public class DbEntry { try { out = String.format("%s %s(%d) %s ", getUser(), getAction().getText(getState()), getAction().getId(getState()), getTarget()); - } catch (SQLException e) { + } catch (SQLException | BusyException e) { out = "ERROR "; } - if (getData() != null && getData().length() > 0) { + if (getData() != null && !getData().isEmpty()) { String data = getData(); if (data.length() > 64) { data = data.substring(0, 64) + "..."; diff --git a/src/main/java/dev/heliosares/auxprotect/database/InvDiffManager.java b/src/main/java/dev/heliosares/auxprotect/database/InvDiffManager.java index 809aa68..1c3d45d 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/InvDiffManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/InvDiffManager.java @@ -1,6 +1,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.core.IAuxProtect; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.utils.InvSerialization; import dev.heliosares.auxprotect.utils.InvSerialization.PlayerInventoryRecord; import org.bukkit.inventory.ItemStack; @@ -92,14 +93,13 @@ public class InvDiffManager extends BlobManager { String stmt = "INSERT INTO " + Table.AUXPROTECT_INVDIFF + " (time, uid, slot, qty, blobid, damage) VALUES (?,?,?,?,?,?)"; sql.execute(stmt, connection, time, sql.getUserManager().getUIDFromUUID("$" + diff.uuid(), false), diff.slot(), diff.qty() >= 0 ? diff.qty() : null, blobid >= 0 ? blobid : null, damage); - } catch (SQLException e) { + } catch (SQLException | BusyException e) { plugin.print(e); } } } - public DiffInventoryRecord getContentsAt(int uid, final long time) throws SQLException, IOException, ClassNotFoundException { - + public DiffInventoryRecord getContentsAt(int uid, final long time) throws SQLException, IOException, ClassNotFoundException, BusyException { long after = 0; try { @@ -178,7 +178,7 @@ public class InvDiffManager extends BlobManager { } return new DiffInventoryRecord(basetime, numdiff, listToPlayerInv(output, inv.exp())); }, 3000L, DiffInventoryRecord.class); - } catch (SQLException | IOException | ClassNotFoundException e) { + } catch (SQLException | IOException | ClassNotFoundException | BusyException e) { throw e; } catch (Exception e) { plugin.print(e); diff --git a/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java b/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java index a6b5ce6..60767e9 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/LookupManager.java @@ -43,8 +43,10 @@ public class LookupManager { if (param.hasFlag(Parameters.Flag.PLAYBACK) || param.hasFlag(Parameters.Flag.INCREMENTAL_POS)) { try { sql.getMultipleBlobs(out.toArray(new DbEntry[0])); + } catch (BusyException e) { + throw new LookupException(Language.L.DATABASE_BUSY); } catch (SQLException e) { - plugin.print(e); + throw new LookupException(Language.L.ERROR); } } diff --git a/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java b/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java index b66c0c2..7b5ffd5 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/MigrationManager.java @@ -2,6 +2,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.core.IAuxProtect; import dev.heliosares.auxprotect.core.PlatformType; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.utils.InvSerialization; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; @@ -114,12 +115,8 @@ public class MigrationManager { } } } - if (output.size() > 0) { - putRaw(table, output); - } - if (commands.size() > 0) { - putRaw(Table.AUXPROTECT_COMMANDS, commands); - } + if (!output.isEmpty()) putRaw(table, output); + if (!commands.isEmpty()) putRaw(Table.AUXPROTECT_COMMANDS, commands); } })); @@ -165,9 +162,8 @@ public class MigrationManager { } } } - if (output.size() > 0) { - putRaw(Table.AUXPROTECT_POSITION, output); - } + if (!output.isEmpty()) putRaw(Table.AUXPROTECT_POSITION, output); + plugin.info("Deleting old entries."); sql.execute("DELETE FROM " + Table.AUXPROTECT_SPAM + " WHERE action_id = 256;", connection); } @@ -256,7 +252,7 @@ public class MigrationManager { continue; } if (size + entry.getValue().length > 16777215 || subBlobs.size() >= 1000) { - if (subBlobs.size() == 0) { + if (subBlobs.isEmpty()) { plugin.warning("Blob too big. Skipping. " + entry.getKey() + "e"); continue; } @@ -274,7 +270,7 @@ public class MigrationManager { } StringBuilder where = new StringBuilder(); - if (blobs.size() > 0) { + if (!blobs.isEmpty()) { where.append(" WHERE time IN ("); for (Long time : blobs.keySet()) { where.append(time).append(","); @@ -483,7 +479,7 @@ public class MigrationManager { return isMigrating; } - void preTables() throws SQLException { + void preTables() throws SQLException, BusyException { sql.execute("CREATE TABLE IF NOT EXISTS " + Table.AUXPROTECT_VERSION + " (time BIGINT,version INTEGER);", connection); String stmt = "SELECT * FROM " + Table.AUXPROTECT_VERSION; @@ -537,7 +533,7 @@ public class MigrationManager { } } - void postTables() throws SQLException { + void postTables() throws SQLException, BusyException { for (int i = sql.getVersion() + 1; i <= TARGET_DB_VERSION; i++) { MigrationAction action = migrationActions.get(i); if (action.necessary && action.postTableAction != null) { @@ -668,7 +664,7 @@ public class MigrationManager { @FunctionalInterface interface MigrateRunnable { - void run() throws SQLException; + void run() throws SQLException, BusyException; } private record MigrationAction(boolean necessary, @Nullable MigrateRunnable preTableAction, diff --git a/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java b/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java index aad9b38..37207df 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/PosEntry.java @@ -56,7 +56,7 @@ public class PosEntry extends DbEntry { } /** - * Retrieves the fractional values from the increment byte generated in {@link PosEncoder#getFractionalByte(double, double, double)} + * Retrieves the fractional values from the increment byte generated in {@link #getFractionalByte(double, double, double)} * * @return An array of doubles of length 3, containing the x, y, and z fractions respectively. */ diff --git a/src/main/java/dev/heliosares/auxprotect/database/Results.java b/src/main/java/dev/heliosares/auxprotect/database/Results.java index db263c4..a07568b 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/Results.java +++ b/src/main/java/dev/heliosares/auxprotect/database/Results.java @@ -6,6 +6,7 @@ import dev.heliosares.auxprotect.core.IAuxProtect; import dev.heliosares.auxprotect.core.Language; import dev.heliosares.auxprotect.core.Parameters; import dev.heliosares.auxprotect.core.Parameters.Flag; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import dev.heliosares.auxprotect.spigot.VeinManager; import dev.heliosares.auxprotect.utils.InvSerialization; @@ -52,7 +53,7 @@ public class Results { } @SuppressWarnings("deprecation") - public static void sendEntry(IAuxProtect plugin, SenderAdapter player, DbEntry entry, int index, boolean time, boolean coords) throws SQLException { + public static void sendEntry(IAuxProtect plugin, SenderAdapter player, DbEntry entry, int index, boolean time, boolean coords) throws SQLException, BusyException { String commandPrefix = "/" + plugin.getCommandPrefix(); ComponentBuilder message = new ComponentBuilder(); @@ -202,11 +203,11 @@ public class Results { player.sendMessageRaw(headerColor + line + " " + Language.L.RESULTS__HEADER + "&7 " + line); } - public void showPage(int page) throws SQLException { + public void showPage(int page) throws SQLException, BusyException { showPage(page, perPage); } - public void showPage(int page, int perPage_) throws SQLException { + public void showPage(int page, int perPage_) throws SQLException, BusyException { int lastpage = getNumPages(perPage_); if (page > lastpage || page < 1) { player.sendLang(Language.L.COMMAND__LOOKUP__NOPAGE); @@ -223,7 +224,7 @@ public class Results { sendArrowKeys(page); } - public void sendEntry(DbEntry entry, int index) throws SQLException { + public void sendEntry(DbEntry entry, int index) throws SQLException, BusyException { sendEntry(plugin, player, entry, index, true, !params.hasFlag(Flag.HIDE_COORDS)); } diff --git a/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java b/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java index 971c206..ddbc24a 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/SQLManager.java @@ -21,8 +21,6 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; -import java.util.function.BinaryOperator; -import java.util.stream.Stream; public class SQLManager extends ConnectionPool { public static final int MAX_LOOKUP_SIZE = 500000; @@ -112,7 +110,7 @@ public class SQLManager extends ConnectionPool { return migrationmanager.getOriginalVersion(); } - public void connect() throws SQLException { + public void connect() throws SQLException, BusyException { plugin.info("Connecting to database..."); try { @@ -204,7 +202,7 @@ public class SQLManager extends ConnectionPool { return backup.getAbsolutePath(); } - private void init(Connection connection) throws SQLException { + private void init(Connection connection) throws SQLException, BusyException { this.migrationmanager = new MigrationManager(this, connection, plugin); migrationmanager.preTables(); @@ -332,6 +330,7 @@ public class SQLManager extends ConnectionPool { // Log and Clear Cache plugin.debug("Purged " + count + " UIDs"); this.usermanager.clearCache(); + return count; } @@ -361,7 +360,7 @@ public class SQLManager extends ConnectionPool { } - protected void put(Connection connection, Table table) throws SQLException { + protected void put(Connection connection, Table table) throws SQLException, BusyException { long start = System.nanoTime(); int count; List entries = new ArrayList<>(); @@ -469,7 +468,7 @@ public class SQLManager extends ConnectionPool { + (Math.round(elapsed / count * 10.0) / 10.0) + "ms each)", 3); } - public int purge(Table table, long time) throws SQLException { + public int purge(Table table, long time) throws SQLException, BusyException { if (!isConnected) return 0; if (time < Table.MIN_PURGE_INTERVAL) { @@ -507,7 +506,7 @@ public class SQLManager extends ConnectionPool { return count; } - public void updateXrayEntry(XrayEntry entry) throws SQLException { + public void updateXrayEntry(XrayEntry entry) throws SQLException, BusyException { if (!isConnected) return; String stmt = "UPDATE " + entry.getAction().getTable().toString(); @@ -531,11 +530,11 @@ public class SQLManager extends ConnectionPool { } try { - execute("INSERT INTO " + Table.AUXPROTECT_WORLDS + " (name, wid) VALUES (?,?)", 30000L, world, nextWid); + execute("INSERT INTO " + Table.AUXPROTECT_WORLDS + " (name, wid) VALUES (?,?)", 300000L, world, nextWid); worlds.put(world, nextWid); rowcount++; return nextWid++; - } catch (SQLException e) { + } catch (SQLException | BusyException e) { plugin.print(e); } return -1; @@ -568,8 +567,8 @@ public class SQLManager extends ConnectionPool { * @see dev.heliosares.auxprotect.api.AuxProtectAPI#createAction(String, String, String, String) */ @SuppressWarnings("unused") - public synchronized EntryAction createAction(@Nonnull String plugin, @Nonnull String key, @Nonnull String ntext, @Nullable String ptext) throws AlreadyExistsException, SQLException { - if (plugin.length() == 0 || key.isEmpty() || ntext.isEmpty()) { + public synchronized EntryAction createAction(@Nonnull String plugin, @Nonnull String key, @Nonnull String ntext, @Nullable String ptext) throws AlreadyExistsException, SQLException, BusyException { + if (plugin.isEmpty() || key.isEmpty() || ntext.isEmpty()) { throw new IllegalArgumentException("Arguments cannot be empty."); } EntryAction preexisting = EntryAction.getAction(key); @@ -602,18 +601,18 @@ public class SQLManager extends ConnectionPool { } try { total += count(table); - } catch (SQLException ignored) { + } catch (SQLException | BusyException ignored) { } } plugin.debug("Counted all tables. " + total + " rows."); rowcount = total; } - public int count(Table table) throws SQLException { + public int count(Table table) throws SQLException, BusyException { return executeReturn(connection -> count(connection, table.toString()), 30000L, Integer.class); } - public byte[] getBlob(DbEntry entry) throws SQLException { + public byte[] getBlob(DbEntry entry) throws SQLException, BusyException { if (entry.getAction().getTable().hasBlob()) return executeReturn(connection -> { try (PreparedStatement pstmt = connection.prepareStatement("SELECT ablob FROM " + entry.getAction().getTable() + " WHERE time=" + entry.getTime() + " LIMIT 1")) { @@ -630,7 +629,7 @@ public class SQLManager extends ConnectionPool { return null; } - public void getMultipleBlobs(DbEntry... entries) throws SQLException { + public void getMultipleBlobs(DbEntry... entries) throws SQLException, BusyException { execute(connection -> { Table table = null; StringBuilder stmt = new StringBuilder("SELECT time,ablob FROM %s WHERE time IN ("); @@ -676,6 +675,7 @@ public class SQLManager extends ConnectionPool { try { put(connection, t); } catch (BusyException ignored) { + // Only thrown by UID lookups. Shouldn't happen because this thread already has the lock } catch (SQLException e) { plugin.print(e); } @@ -692,7 +692,7 @@ public class SQLManager extends ConnectionPool { } //TODO implement - public void setLast(LastKeys key, long value) throws SQLException { + public void setLast(LastKeys key, long value) throws SQLException, BusyException { key.value = value; execute(connection -> setLast(key, value, connection), 30000L); } @@ -702,7 +702,7 @@ public class SQLManager extends ConnectionPool { execute("UPDATE " + Table.AUXPROTECT_LASTS + " SET value=? WHERE name=?", connection, value, key.id); } - public long getLast(LastKeys key) throws SQLException { + public long getLast(LastKeys key) throws SQLException, BusyException { if (key.value != null) return key.value; return executeReturn(connection -> getLast(key, connection), 30000L, Long.class); } diff --git a/src/main/java/dev/heliosares/auxprotect/database/SQLUserManager.java b/src/main/java/dev/heliosares/auxprotect/database/SQLUserManager.java index cc42984..0571334 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/SQLUserManager.java +++ b/src/main/java/dev/heliosares/auxprotect/database/SQLUserManager.java @@ -1,6 +1,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.core.IAuxProtect; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.utils.BidiMapCache; import java.sql.*; @@ -18,7 +19,7 @@ public class SQLUserManager { this.sql = sql; } - public void updateUsernameAndIP(UUID uuid, String name, String ip) throws SQLException { + public void updateUsernameAndIP(UUID uuid, String name, String ip) throws SQLException, BusyException { final int uid = this.getUIDFromUUID("$" + uuid, true, true); if (uid <= 0) { return; @@ -63,7 +64,7 @@ public class SQLUserManager { }, 300000L); } - public String getUsernameFromUID(int uid, boolean wait) throws SQLException { + public String getUsernameFromUID(int uid, boolean wait) throws SQLException, BusyException { if (uid < 0) { return null; } @@ -101,7 +102,7 @@ public class SQLUserManager { }, wait ? 300000L : 3000L, String.class); } - public HashMap getUsernamesFromUID(int uid, boolean wait) throws SQLException { + public HashMap getUsernamesFromUID(int uid, boolean wait) throws SQLException, BusyException { HashMap out = new HashMap<>(); String stmt = "SELECT * FROM " + Table.AUXPROTECT_LONGTERM + " WHERE action_id=? AND uid=?;"; plugin.debug(stmt, 3); @@ -123,7 +124,7 @@ public class SQLUserManager { return out; } - public int getUIDFromUsername(String username, boolean wait) throws SQLException { + public int getUIDFromUsername(String username, boolean wait) throws SQLException, BusyException { if (username == null) { return -1; } @@ -156,11 +157,11 @@ public class SQLUserManager { }, wait ? 300000L : 3000L, Integer.class); } - public int getUIDFromUUID(String uuid, boolean wait) throws SQLException { + public int getUIDFromUUID(String uuid, boolean wait) throws SQLException, BusyException { return getUIDFromUUID(uuid, false, wait); } - public int getUIDFromUUID(String uuid, boolean insert, boolean wait) throws SQLException { + public int getUIDFromUUID(String uuid, boolean insert, boolean wait) throws SQLException, BusyException { if (uuid == null || uuid.equalsIgnoreCase("#null")) { return -1; } @@ -204,7 +205,7 @@ public class SQLUserManager { return -1; } - public String getUUIDFromUID(int uid, boolean wait) throws SQLException { + public String getUUIDFromUID(int uid, boolean wait) throws SQLException, BusyException { if (uid < 0) { return "#null"; } @@ -234,7 +235,7 @@ public class SQLUserManager { return Collections.unmodifiableCollection(usernames.values()); } - public byte[] getPendingInventory(int uid) throws SQLException { + public byte[] getPendingInventory(int uid) throws SQLException, BusyException { if (uid <= 0) { return null; } @@ -252,7 +253,7 @@ public class SQLUserManager { }, 3000L, byte[].class); } - public void setPendingInventory(int uid, byte[] blob) throws SQLException { + public void setPendingInventory(int uid, byte[] blob) throws SQLException, BusyException { if (uid <= 0) { throw new IllegalArgumentException(); } diff --git a/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java b/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java index 01db2dd..d0e638e 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/database/SingleItemEntry.java @@ -1,6 +1,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.api.AuxProtectAPI; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.utils.InvSerialization; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; @@ -49,7 +50,7 @@ public class SingleItemEntry extends DbEntry { this.damage = damage; } - public ItemStack getItem() throws SQLException { + public ItemStack getItem() throws SQLException, BusyException { if (item != null) { return item; } diff --git a/src/main/java/dev/heliosares/auxprotect/database/XrayResults.java b/src/main/java/dev/heliosares/auxprotect/database/XrayResults.java index a6a6b79..e32603a 100644 --- a/src/main/java/dev/heliosares/auxprotect/database/XrayResults.java +++ b/src/main/java/dev/heliosares/auxprotect/database/XrayResults.java @@ -2,6 +2,7 @@ package dev.heliosares.auxprotect.database; import dev.heliosares.auxprotect.adapters.sender.SenderAdapter; import dev.heliosares.auxprotect.core.APPermission; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import dev.heliosares.auxprotect.spigot.VeinManager; import net.md_5.bungee.api.ChatColor; @@ -18,7 +19,7 @@ public class XrayResults { sender.sendMessageRaw("&f------ &9AuxProtect Xray Check Results&7 ------"); // TODO lang } - public static void sendEntry(AuxProtectSpigot plugin, SenderAdapter sender, XrayEntry en, boolean auto) throws SQLException { + public static void sendEntry(AuxProtectSpigot plugin, SenderAdapter sender, XrayEntry en, boolean auto) throws SQLException, BusyException { sendHeader(sender); Results.sendEntry(plugin, sender, en, -1, true, true); diff --git a/src/main/java/dev/heliosares/auxprotect/exceptions/BusyException.java b/src/main/java/dev/heliosares/auxprotect/exceptions/BusyException.java index bbc09ee..86c58ad 100644 --- a/src/main/java/dev/heliosares/auxprotect/exceptions/BusyException.java +++ b/src/main/java/dev/heliosares/auxprotect/exceptions/BusyException.java @@ -1,6 +1,6 @@ package dev.heliosares.auxprotect.exceptions; -public class BusyException extends IllegalStateException { +public class BusyException extends Exception { public BusyException(StackTraceElement[] stack, long threadID) { super("Database busy, currently held by " + (threadID < 0 ? "none" : ("Thread #" + threadID))); } diff --git a/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java b/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java index 648f9ba..c119923 100644 --- a/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java +++ b/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java @@ -209,7 +209,7 @@ public class AuxProtectSpigot extends JavaPlugin implements IAuxProtect { long lastloaded = 0; try { lastloaded = sqlManager.getLast(SQLManager.LastKeys.TELEMETRY); - } catch (SQLException ignored) { + } catch (SQLException | BusyException ignored) { } long delay = 15 * 20; if (System.currentTimeMillis() - lastloaded > 1000 * 60 * 60) { diff --git a/src/main/java/dev/heliosares/auxprotect/spigot/Metrics.java b/src/main/java/dev/heliosares/auxprotect/spigot/Metrics.java index 06854ae..095b503 100644 --- a/src/main/java/dev/heliosares/auxprotect/spigot/Metrics.java +++ b/src/main/java/dev/heliosares/auxprotect/spigot/Metrics.java @@ -17,6 +17,7 @@ package dev.heliosares.auxprotect.spigot; import dev.heliosares.auxprotect.api.AuxProtectAPI; import dev.heliosares.auxprotect.database.SQLManager; +import dev.heliosares.auxprotect.exceptions.BusyException; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -307,7 +308,7 @@ public class Metrics { } try { AuxProtectAPI.getInstance().getSqlManager().setLast(SQLManager.LastKeys.TELEMETRY, System.currentTimeMillis()); - } catch (SQLException ignored) { + } catch (SQLException | BusyException ignored) { } }); } diff --git a/src/main/java/dev/heliosares/auxprotect/spigot/VeinManager.java b/src/main/java/dev/heliosares/auxprotect/spigot/VeinManager.java index a849993..e3036fd 100644 --- a/src/main/java/dev/heliosares/auxprotect/spigot/VeinManager.java +++ b/src/main/java/dev/heliosares/auxprotect/spigot/VeinManager.java @@ -4,6 +4,7 @@ import dev.heliosares.auxprotect.api.AuxProtectAPI; import dev.heliosares.auxprotect.database.DbEntry; import dev.heliosares.auxprotect.database.SQLManager; import dev.heliosares.auxprotect.database.XrayEntry; +import dev.heliosares.auxprotect.exceptions.BusyException; import net.md_5.bungee.api.ChatColor; import java.sql.SQLException; @@ -64,7 +65,7 @@ public class VeinManager { if (o1.getUser(false) == null) SQLManager.getInstance().execute(c -> o1.getUser(), 3000L); if (o2.getUser(false) == null) SQLManager.getInstance().execute(c -> o2.getUser(), 3000L); return o1.getUser().compareTo(o2.getUser()); - } catch (SQLException e) { + } catch (SQLException | BusyException e) { AuxProtectAPI.getInstance().print(e); return 0; } diff --git a/src/main/java/dev/heliosares/auxprotect/spigot/listeners/PlayerListener.java b/src/main/java/dev/heliosares/auxprotect/spigot/listeners/PlayerListener.java index 1dd1fe8..bee12ca 100644 --- a/src/main/java/dev/heliosares/auxprotect/spigot/listeners/PlayerListener.java +++ b/src/main/java/dev/heliosares/auxprotect/spigot/listeners/PlayerListener.java @@ -5,6 +5,7 @@ import dev.heliosares.auxprotect.core.APPlayer; import dev.heliosares.auxprotect.database.DbEntry; import dev.heliosares.auxprotect.database.EntryAction; import dev.heliosares.auxprotect.database.SingleItemEntry; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import dev.heliosares.auxprotect.utils.InvSerialization; import dev.heliosares.auxprotect.utils.PlaybackSolver; @@ -24,7 +25,6 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityPlaceEvent; import org.bukkit.event.entity.EntityToggleGlideEvent; import org.bukkit.event.entity.PlayerLeashEntityEvent; @@ -188,6 +188,8 @@ public class PlayerListener implements Listener { try { plugin.getSqlManager().getUserManager().updateUsernameAndIP(e.getPlayer().getUniqueId(), e.getPlayer().getName(), ip); + } catch (BusyException ex) { + plugin.warning("Database Busy: Unable to update username/ip for " + e.getPlayer().getName()+", this may cause issues with lookups but will resolve when they relog and the database is not busy."); } catch (SQLException ex) { plugin.print(ex); } @@ -206,7 +208,7 @@ public class PlayerListener implements Listener { .getUIDFromUUID("$" + e.getPlayer().getUniqueId(), false)) == null) { return; } - } catch (SQLException e1) { + } catch (SQLException | BusyException e1) { return; } e.getPlayer().sendMessage(ChatColor.COLOR_CHAR + "aYou have an inventory waiting to be claimed!"); diff --git a/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java b/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java index 8d653ab..39904c3 100644 --- a/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java +++ b/src/main/java/dev/heliosares/auxprotect/towny/TownyEntry.java @@ -3,6 +3,7 @@ package dev.heliosares.auxprotect.towny; import dev.heliosares.auxprotect.database.DbEntry; import dev.heliosares.auxprotect.database.EntryAction; import dev.heliosares.auxprotect.database.SQLManager; +import dev.heliosares.auxprotect.exceptions.BusyException; import org.bukkit.Location; import java.sql.SQLException; @@ -24,7 +25,7 @@ public class TownyEntry extends DbEntry { } @Override - public String getUser() throws SQLException { + public String getUser() throws SQLException, BusyException { if (user != null) { return user; } @@ -39,7 +40,7 @@ public class TownyEntry extends DbEntry { } @Override - public String getTarget() throws SQLException { + public String getTarget() throws SQLException, BusyException { if (target != null) { return target; } diff --git a/src/main/java/dev/heliosares/auxprotect/towny/TownyListener.java b/src/main/java/dev/heliosares/auxprotect/towny/TownyListener.java index fc94446..bf0f8c7 100644 --- a/src/main/java/dev/heliosares/auxprotect/towny/TownyListener.java +++ b/src/main/java/dev/heliosares/auxprotect/towny/TownyListener.java @@ -8,6 +8,7 @@ import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.exceptions.TownyException; import com.palmergames.bukkit.towny.object.*; import dev.heliosares.auxprotect.database.EntryAction; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -48,18 +49,18 @@ public class TownyListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void on(NewDayEvent e) { e.getFallenTowns().forEach((t) -> { - handledeleted(t, false); + handleDeleted(t, false); }); e.getFallenNations().forEach((n) -> { - handledeleted(n, true); + handleDeleted(n, true); }); } - private void handledeleted(String name, boolean nation) { + private void handleDeleted(String name, boolean nation) { int uid; try { uid = plugin.getSqlManager().getTownyManager().getIDFromName(name, true); - } catch (SQLException e) { + } catch (SQLException | BusyException e) { plugin.print(e); return; } @@ -71,7 +72,7 @@ public class TownyListener implements Listener { String uuid = null; try { uuid = plugin.getSqlManager().getUserManager().getUUIDFromUID(uid, true); - } catch (SQLException ignored) { + } catch (SQLException | BusyException ignored) { //Unlikely } if (uuid == null) { diff --git a/src/main/java/dev/heliosares/auxprotect/towny/TownyManager.java b/src/main/java/dev/heliosares/auxprotect/towny/TownyManager.java index fe44a05..fd68a97 100644 --- a/src/main/java/dev/heliosares/auxprotect/towny/TownyManager.java +++ b/src/main/java/dev/heliosares/auxprotect/towny/TownyManager.java @@ -5,6 +5,7 @@ import com.palmergames.bukkit.towny.object.Government; import dev.heliosares.auxprotect.database.EntryAction; import dev.heliosares.auxprotect.database.SQLManager; import dev.heliosares.auxprotect.database.Table; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import dev.heliosares.auxprotect.utils.BidiMapCache; @@ -36,7 +37,7 @@ public class TownyManager { TownyUniverse.getInstance().getNations().forEach((nation) -> sql.getTownyManager().updateName(nation, false)); } - public String getNameFromID(int uid, boolean wait) throws SQLException { + public String getNameFromID(int uid, boolean wait) throws SQLException, BusyException { if (uid < 0) { return null; } @@ -69,7 +70,7 @@ public class TownyManager { }, wait ? 30000L : 1000L, String.class); } - public int getIDFromName(String name, boolean wait) throws SQLException { + public int getIDFromName(String name, boolean wait) throws SQLException, BusyException { if (name == null) { return -1; } @@ -109,7 +110,7 @@ public class TownyManager { int uid = -1; try { uid = sql.getUserManager().getUIDFromUUID("$t" + uuid, true, true); - } catch (SQLException e) { + } catch (SQLException | BusyException e) { plugin.print(e); } if (uid <= 0) { @@ -121,7 +122,7 @@ public class TownyManager { String newestusername; try { newestusername = getNameFromID(uid, true); - } catch (SQLException e) { + } catch (SQLException | BusyException e) { plugin.print(e); return; } diff --git a/src/main/java/dev/heliosares/auxprotect/utils/PlaybackSolver.java b/src/main/java/dev/heliosares/auxprotect/utils/PlaybackSolver.java index 91180f8..b3333cd 100644 --- a/src/main/java/dev/heliosares/auxprotect/utils/PlaybackSolver.java +++ b/src/main/java/dev/heliosares/auxprotect/utils/PlaybackSolver.java @@ -9,6 +9,7 @@ import dev.heliosares.auxprotect.core.PlatformType; import dev.heliosares.auxprotect.database.DbEntry; import dev.heliosares.auxprotect.database.DbEntryBukkit; import dev.heliosares.auxprotect.database.SQLManager; +import dev.heliosares.auxprotect.exceptions.BusyException; import dev.heliosares.auxprotect.exceptions.LookupException; import dev.heliosares.auxprotect.spigot.AuxProtectSpigot; import net.md_5.bungee.api.ChatColor; @@ -41,7 +42,7 @@ public class PlaybackSolver extends BukkitRunnable { private final Set modified = new HashSet<>(); - public PlaybackSolver(IAuxProtect plugin, SenderAdapter sender, List entries, long startTime, @Nullable List blockActions) throws SQLException, LookupException { + public PlaybackSolver(IAuxProtect plugin, SenderAdapter sender, List entries, long startTime, @Nullable List blockActions) throws SQLException, LookupException, BusyException { if (plugin.getPlatform() != PlatformType.SPIGOT) throw new UnsupportedOperationException(); this.audience = (Player) sender.getSender(); this.realReferenceTime = System.currentTimeMillis(); @@ -130,7 +131,7 @@ public class PlaybackSolver extends BukkitRunnable { } - public static List getLocations(IAuxProtect plugin, List entries, long startTime) throws SQLException { + public static List getLocations(IAuxProtect plugin, List entries, long startTime) throws SQLException, BusyException { if (plugin.getPlatform() != PlatformType.SPIGOT) throw new UnsupportedOperationException(); Map lastEntries = new HashMap<>(); entries.sort(Comparator.comparingLong(DbEntry::getTime)); diff --git a/src/main/java/dev/heliosares/auxprotect/utils/RetentionSolver.java b/src/main/java/dev/heliosares/auxprotect/utils/RetentionSolver.java index a857335..e924209 100644 --- a/src/main/java/dev/heliosares/auxprotect/utils/RetentionSolver.java +++ b/src/main/java/dev/heliosares/auxprotect/utils/RetentionSolver.java @@ -3,6 +3,7 @@ package dev.heliosares.auxprotect.utils; import dev.heliosares.auxprotect.adapters.sender.SenderAdapter; import dev.heliosares.auxprotect.core.IAuxProtect; import dev.heliosares.auxprotect.database.DbEntry; +import dev.heliosares.auxprotect.exceptions.BusyException; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Statistic; @@ -14,7 +15,7 @@ import java.util.Map.Entry; public class RetentionSolver { public static void showRetention(IAuxProtect plugin, SenderAdapter sender, List entries, - long startTime, long endTime) throws SQLException { + long startTime, long endTime) throws SQLException, BusyException { long start = System.currentTimeMillis(); Set lookupUids = new HashSet<>(); for (DbEntry entry : entries) { diff --git a/src/main/java/dev/heliosares/auxprotect/utils/SQLConsumer.java b/src/main/java/dev/heliosares/auxprotect/utils/SQLConsumer.java index 845a881..a3bf61b 100644 --- a/src/main/java/dev/heliosares/auxprotect/utils/SQLConsumer.java +++ b/src/main/java/dev/heliosares/auxprotect/utils/SQLConsumer.java @@ -1,9 +1,11 @@ package dev.heliosares.auxprotect.utils; +import dev.heliosares.auxprotect.exceptions.BusyException; + import java.sql.Connection; import java.sql.SQLException; @FunctionalInterface public interface SQLConsumer { - void accept(Connection connection) throws SQLException; + void accept(Connection connection) throws SQLException, BusyException; } diff --git a/src/main/java/dev/heliosares/auxprotect/utils/SQLFunction.java b/src/main/java/dev/heliosares/auxprotect/utils/SQLFunction.java index 712cf6d..3f03418 100644 --- a/src/main/java/dev/heliosares/auxprotect/utils/SQLFunction.java +++ b/src/main/java/dev/heliosares/auxprotect/utils/SQLFunction.java @@ -1,9 +1,11 @@ package dev.heliosares.auxprotect.utils; +import dev.heliosares.auxprotect.exceptions.BusyException; + import java.sql.Connection; import java.sql.SQLException; @FunctionalInterface public interface SQLFunction { - T apply(Connection connection) throws SQLException; + T apply(Connection connection) throws SQLException, BusyException; } diff --git a/src/main/java/dev/heliosares/auxprotect/utils/XraySolver.java b/src/main/java/dev/heliosares/auxprotect/utils/XraySolver.java index 59a4c9c..396c1b1 100644 --- a/src/main/java/dev/heliosares/auxprotect/utils/XraySolver.java +++ b/src/main/java/dev/heliosares/auxprotect/utils/XraySolver.java @@ -3,6 +3,7 @@ package dev.heliosares.auxprotect.utils; import dev.heliosares.auxprotect.database.DbEntry; import dev.heliosares.auxprotect.database.EntryAction; import dev.heliosares.auxprotect.database.XrayEntry; +import dev.heliosares.auxprotect.exceptions.BusyException; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -18,7 +19,7 @@ import java.util.List; public class XraySolver { - public static BaseComponent[] solve(List entries) throws SQLException { + public static BaseComponent[] solve(List entries) throws SQLException, BusyException { ComponentBuilder message = new ComponentBuilder().append("", FormatRetention.NONE); HashMap> hash = new HashMap<>(); for (int i = entries.size() - 1; i >= 0; i--) {