From 87df6804c949ce69d9f42fd0581ccace2375a689 Mon Sep 17 00:00:00 2001 From: Keir Nellyer Date: Sat, 9 Jul 2016 14:36:30 -0400 Subject: [PATCH] Improve code and error logging --- .../mineplex/core/report/ReportManager.java | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java index 3566d904a..7879d0f78 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java @@ -11,6 +11,7 @@ import mineplex.core.chatsnap.command.PushSnapshotsCommand; import mineplex.core.chatsnap.command.PushSnapshotsHandler; import mineplex.core.command.CommandCenter; import mineplex.core.common.jsonchat.JsonMessage; +import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.portal.Portal; import mineplex.core.report.command.ReportHandlerNotification; @@ -36,7 +37,7 @@ public class ReportManager private static final int ABUSE_BAN_THRESHOLD = 1; - private JavaPlugin _javaPlugin; + private JavaPlugin _plugin; private SnapshotManager _snapshotManager; private CoreClientManager _clientManager; private String _serverName; @@ -45,10 +46,10 @@ public class ReportManager private final ReportRepository _reportRepository; private final ReportProfileRepository _reportProfileRepository; - public ReportManager(JavaPlugin javaPlugin, SnapshotManager snapshotManager, CoreClientManager clientManager, + public ReportManager(JavaPlugin plugin, SnapshotManager snapshotManager, CoreClientManager clientManager, String serverName, int serverWeight) { - _javaPlugin = javaPlugin; + _plugin = plugin; _snapshotManager = snapshotManager; _clientManager = clientManager; _serverName = serverName; @@ -56,11 +57,11 @@ public class ReportManager _reportRepository = new ReportRepository(this); - _reportProfileRepository = new ReportProfileRepository(javaPlugin); + _reportProfileRepository = new ReportProfileRepository(plugin); ServerCommandManager commandManager = ServerCommandManager.getInstance(); ReportNotificationCallback notificationCallback = new ReportNotificationCallback(this); - PushSnapshotsHandler pushHandler = new PushSnapshotsHandler(_snapshotManager, _javaPlugin.getLogger()); + PushSnapshotsHandler pushHandler = new PushSnapshotsHandler(_snapshotManager, _plugin.getLogger()); commandManager.registerCommandType("ReportHandlerNotification", ReportHandlerNotification.class, notificationCallback); commandManager.registerCommandType("PushSnapshotsCommand", PushSnapshotsCommand.class, pushHandler); } @@ -92,7 +93,7 @@ public class ReportManager Report report = _reportRepository.getOngoingReport(suspectId, category) .exceptionally(throwable -> { - _javaPlugin.getLogger().log(Level.SEVERE, "Error fetching ongoing report.", throwable); + _plugin.getLogger().log(Level.SEVERE, "Error fetching ongoing report.", throwable); return Optional.empty(); }).thenCompose(reportIdOptional -> reportIdOptional.isPresent() ? _reportRepository.getReport(reportIdOptional.get()) : CompletableFuture.completedFuture(null)) .join(); @@ -104,15 +105,15 @@ public class ReportManager report.addReportReason(new ReportMessage(reporterId, reason, _serverName, _serverWeight)); - _reportRepository.updateReport(report) + saveReport(report) .exceptionally(throwable -> { - _javaPlugin.getLogger().log(Level.SEVERE, "Error updating report to database.", throwable); + _plugin.getLogger().log(Level.SEVERE, "Error updating report to database.", throwable); return null; }).join(); // join (block) so that report is assigned an id return report; }).exceptionally(throwable -> { - _javaPlugin.getLogger().log(Level.SEVERE, "Error whilst reporting player.", throwable); + _plugin.getLogger().log(Level.SEVERE, "Error whilst reporting player.", throwable); return null; }); } @@ -156,12 +157,14 @@ public class ReportManager pushCommand.publish(); } - _reportRepository.updateReport(report); - + saveReport(report); Portal.transferPlayer(reportHandler.getName(), report.getLatestMessage().getServer()); // Show user details of the report every x seconds - new ReportHandlerMessageTask(this, report).runTaskTimer(_javaPlugin, 20L * 10, 20L * 10); + new ReportHandlerMessageTask(this, report).runTaskTimer(_plugin, 20L * 10, 20L * 10); + + reportHandler.sendMessage( + F.main(getReportPrefix(reportId), C.cAqua + "You are now handling this report.")); } } @@ -190,7 +193,10 @@ public class ReportManager } report.setReportResult(reportResult); - _reportRepository.updateReport(report).thenAccept(reportId -> + + CompletableFuture saveCompletableFuture = saveReport(report); + + saveCompletableFuture.thenAccept(reportId -> { _reportRepository.clearCache(reportId); @@ -208,6 +214,18 @@ public class ReportManager }); } + public CompletableFuture saveReport(Report report) + { + CompletableFuture updateCompletableFuture = _reportRepository.updateReport(report); + + updateCompletableFuture.exceptionally((throwable) -> { + _plugin.getLogger().log(Level.SEVERE, "Error whilst saving report.", throwable); + return null; + }); + + return updateCompletableFuture; + } + /** * Fetches the amount of reports this player has filed which have been marked abusive, * this value is then compared with the abuse threshold to determine whether the player @@ -222,7 +240,7 @@ public class ReportManager return _reportRepository.getResultCount(accountId, ReportResultType.ABUSIVE) .exceptionally(throwable -> { - _javaPlugin.getLogger().log(Level.SEVERE, "Error fetching user report result count.", throwable); + _plugin.getLogger().log(Level.SEVERE, "Error fetching user report result count.", throwable); return 0; }).thenApply(abusiveReportsCount -> abusiveReportsCount < ABUSE_BAN_THRESHOLD); } @@ -278,6 +296,9 @@ public class ReportManager } return false; + }).exceptionally(throwable -> { + _plugin.getLogger().log(Level.SEVERE, "Error fetching reports player is handling.", throwable); + return true; }); } @@ -314,7 +335,7 @@ public class ReportManager if (priority <= 0) { - System.out.println("Closing report due to expiration"); + System.out.println("Closing report due to expiration (#" + report.getId().orElse((long) -1) + ")."); closeReport(report, null, new ReportResult(ReportResultType.EXPIRED, null)); }