Skip to content

Commit

Permalink
Fix: don't use replace if not already in map
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jun 10, 2024
1 parent 992efbf commit a1c9772
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/main/java/xyz/nucleoid/extras/error/ExtrasErrorReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a1c9772

Please sign in to comment.