diff --git a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotManager.java b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotManager.java index fdd39dfb4..aea72eb29 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotManager.java @@ -20,6 +20,8 @@ import mineplex.core.report.data.Report; */ public class SnapshotManager { + public static final int MAX_SNAPSHOTS = 5; + // There aren't any List or Set caching implementations // For an easy work around, we store values as the Key // For the value we just use some dummy object diff --git a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java index 1ee5e0daa..261f081f8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java @@ -6,15 +6,16 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; +import java.util.ArrayList; import java.util.Base64; import java.util.Collection; +import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.logging.Logger; import mineplex.core.common.util.UtilTime; -import mineplex.core.report.data.Report; import mineplex.serverdata.database.DBPool; /** @@ -72,6 +73,7 @@ public class SnapshotRepository private static final String GET_ID_FROM_TOKEN = "SELECT snapshots.id FROM snapshots WHERE snapshots.token = ?;"; private static final String GET_METADATA = "SELECT token, creatorId FROM snapshots WHERE id = ?;"; private static final String SET_TOKEN = "UPDATE snapshots SET token = ? WHERE id = ?;"; + private static final String GET_USER_SNAPSHOTS = "SELECT snapshots.id FROM snapshots WHERE snapshots.creatorId = ?;"; private final String _serverName; private final Logger _logger; @@ -208,6 +210,40 @@ public class SnapshotRepository return future; } + public CompletableFuture> getUserSnapshots(int creatorId) + { + CompletableFuture> future = CompletableFuture.supplyAsync(() -> + { + try (Connection connection = DBPool.getAccount().getConnection()) + { + PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_SNAPSHOTS); + preparedStatement.setInt(1, creatorId); + + ResultSet resultSet = preparedStatement.executeQuery(); + List snapshotIds = new ArrayList<>(); + + while (resultSet.next()) + { + snapshotIds.add(resultSet.getInt("id")); + } + + return snapshotIds; + } + catch (SQLException e) + { + throw new RuntimeException(e); + } + }); + + future.exceptionally(throwable -> + { + _logger.log(Level.SEVERE, "Error getting snapshots for user " + creatorId + "."); + return new ArrayList<>(); + }); + + return future; + } + private void setToken(Connection connection, int snapshotId, String token) throws SQLException { try (PreparedStatement setTokenStatement = connection.prepareStatement(SET_TOKEN)) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/command/ChatSnapCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/command/ChatSnapCommand.java index 2ab7ffc70..1c085c647 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/command/ChatSnapCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/command/ChatSnapCommand.java @@ -33,23 +33,34 @@ public class ChatSnapCommand extends CommandBase { SnapshotManager manager = Plugin.getSnapshotManager(); SnapshotRepository repository = manager.getRepository(); - int accountId = _commandCenter.GetClientManager().getAccountId(player); - Set messages = manager.getMessagesInvolving(accountId); - repository.createSnapshot(accountId).thenAccept(snapshotMetadata -> + Plugin.getSnapshotManager().getRepository().getUserSnapshots(accountId).thenAccept(snapshotIds -> { - String token = snapshotMetadata.getToken().orElseThrow(() -> - new IllegalStateException("Snapshot doesn't have a token.")); + if (snapshotIds.size() < SnapshotManager.MAX_SNAPSHOTS) + { + Set messages = manager.getMessagesInvolving(accountId); - repository.insertMessages(snapshotMetadata.getId(), messages).join(); + repository.createSnapshot(accountId).thenAccept(snapshotMetadata -> + { + String token = snapshotMetadata.getToken().orElseThrow(() -> + new IllegalStateException("Snapshot doesn't have a token.")); - UtilPlayer.message(player, F.main(Plugin.getName(), "Snapshot successfully created.")); + repository.insertMessages(snapshotMetadata.getId(), messages).join(); - new JsonMessage(F.main(Plugin.getName(), "Your snapshot token is: ")) - .extra(F.elem(token)) - .click(ClickEvent.OPEN_URL, SnapshotRepository.getURL(token)) - .sendToPlayer(player); + UtilPlayer.message(player, F.main(Plugin.getName(), "Snapshot successfully created.")); + + new JsonMessage(F.main(Plugin.getName(), "Your snapshot token is: ")) + .extra(F.elem(token)) + .click(ClickEvent.OPEN_URL, SnapshotRepository.getURL(token)) + .sendToPlayer(player); + }); + } + else + { + UtilPlayer.message(player, F.main(Plugin.getName(), + C.cRed + "Cannot create snapshot, you have reached the limit.")); + } }); } else diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportCommand.java index e9f3e2280..89be81d30 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/command/ReportCommand.java @@ -92,7 +92,7 @@ public class ReportCommand extends CommandBase else { UtilPlayer.message(reporter, F.main(Plugin.getName(), - C.cRed + "Cannot create report, you currently have too many open reports.")); + C.cRed + "Cannot create report, you have reached the limit.")); } } else diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java index cacb668d7..94fda9c24 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/data/ReportRepository.java @@ -506,7 +506,7 @@ public class ReportRepository public CompletableFuture> getOpenReports(int reporterId) { - CompletableFuture> future = CompletableFuture.supplyAsync(() -> + return CompletableFuture.supplyAsync(() -> { try (Connection connection = DBPool.getAccount().getConnection()) { @@ -528,8 +528,6 @@ public class ReportRepository throw new RuntimeException(e); } }); - - return future; } private Set getReportReasons(Connection connection, long reportId)