From a1c97720c0bb712726bafdbb75d60fd9ddf29f54 Mon Sep 17 00:00:00 2001 From: Gegy Date: Mon, 10 Jun 2024 09:17:04 +0200 Subject: [PATCH] Fix: don't use replace if not already in map --- .../extras/error/ExtrasErrorReporter.java | 25 ++++++++++--------- .../extras/mixin/debug/EntityMixin.java | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/xyz/nucleoid/extras/error/ExtrasErrorReporter.java b/src/main/java/xyz/nucleoid/extras/error/ExtrasErrorReporter.java index 0cebfa5..ed420a1 100644 --- a/src/main/java/xyz/nucleoid/extras/error/ExtrasErrorReporter.java +++ b/src/main/java/xyz/nucleoid/extras/error/ExtrasErrorReporter.java @@ -64,20 +64,9 @@ public static void onServerCrash(CrashReport report) { } public static void reportCustom(CustomErrorType type, Throwable throwable) { - var now = Instant.now(); - var lastReport = LAST_REPORT.get(type); - - if (lastReport != null) { - var timeSinceLastReport = Duration.between(lastReport, now); - if (timeSinceLastReport.compareTo(type.reportInterval) < 0) { - return; - } - } - - if (!LAST_REPORT.replace(type, lastReport, now)) { + if (!updateLastReportTime(type, Instant.now())) { return; } - var webhook = openWebhook(); if (webhook != null) { var message = new DiscordWebhook.Message("An error has occurred!"); @@ -91,6 +80,18 @@ public static void reportCustom(CustomErrorType type, Throwable throwable) { } } + private static boolean updateLastReportTime(CustomErrorType type, Instant now) { + var lastReport = LAST_REPORT.putIfAbsent(type, now); + if (lastReport != null) { + var timeSinceLastReport = Duration.between(lastReport, now); + if (timeSinceLastReport.compareTo(type.reportInterval) < 0) { + return false; + } + return LAST_REPORT.replace(type, lastReport, now); + } + return true; + } + @Nullable private static DiscordWebhook openWebhook() { var errorReporting = NucleoidExtrasConfig.get().errorReporting(); diff --git a/src/main/java/xyz/nucleoid/extras/mixin/debug/EntityMixin.java b/src/main/java/xyz/nucleoid/extras/mixin/debug/EntityMixin.java index 4840b42..6d60034 100644 --- a/src/main/java/xyz/nucleoid/extras/mixin/debug/EntityMixin.java +++ b/src/main/java/xyz/nucleoid/extras/mixin/debug/EntityMixin.java @@ -14,7 +14,7 @@ public class EntityMixin { @Unique private static final double MAX_REASONABLE_VELOCITY = MathHelper.square(100.0); - @ModifyVariable(method = "setVelocity(Lnet/minecraft/util/math/Vec3d;)V", at = @At("HEAD")) + @ModifyVariable(method = "setVelocity(Lnet/minecraft/util/math/Vec3d;)V", at = @At("HEAD"), argsOnly = true) private Vec3d clampVelocity(Vec3d velocity) { double lengthSq = velocity.lengthSquared(); if (lengthSq > MAX_REASONABLE_VELOCITY) {